package com.example.beer.services;

import java.net.HttpURLConnection;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.example.beer.dao.UserDAO;
import com.example.beer.model.User;
import com.example.beer.model.WebUser;

@Stateless
@Path("user")
public class UserManager {

	@Inject
	private UserDAO userDAO;

	@Inject
	private UserContext userContext;

	@POST
	@Path("login")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response login(WebUser user) {
		User foundUser = userDAO.validateCredentials(user.getUsername(),
				user.getPassword());

		if (foundUser != null) {
			userContext.setCurrentUser(foundUser);
			return Response.status(HttpURLConnection.HTTP_OK).build();
		}

		return Response.status(HttpURLConnection.HTTP_UNAUTHORIZED).build();
	}

	@POST
	@Path("logout")
	public Response logout() {
		userContext.setCurrentUser(null);
		return Response.status(HttpURLConnection.HTTP_OK).build();
	}

	@POST
	@Path("register")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response registerNewUser(User user) {
		User currentUser = userContext.getCurrentUser();
		User foundUser = userDAO.getUserByName(user.getName());

		if (currentUser.isAdmin()) {
			if (foundUser == null) {
				userDAO.addUser(user);
				return Response.status(HttpURLConnection.HTTP_OK).build();
			} else {
				return Response.status(HttpURLConnection.HTTP_CONFLICT).build();
			}
		}

		return Response.status(HttpURLConnection.HTTP_FORBIDDEN).build();
	}
}

@@ -150,16 +150,40 @@
 nsumes;%0A
+import javax.ws.rs.GET;%0A
 import j
@@ -224,16 +224,75 @@
 s.Path;%0A
+import javax.ws.rs.PathParam;%0Aimport javax.ws.rs.Produces;%0A
 import j
@@ -390,16 +390,99 @@
 serDAO;%0A
+import com.example.beer.dto.UserDTO;%0Aimport com.example.beer.dto.UserDTOContainer;%0A
 import c
@@ -1772,10 +1772,906 @@
 d();%0A%09%7D%0A
+%09%0A%09@GET%0A%09@Produces(MediaType.APPLICATION_JSON)%0A%09public Response getAllUsers() %7B%0A%09%09User currentUser = userContext.getCurrentUser();%0A%09%09if (currentUser == null %7C%7C !currentUser.isAdmin()) %7B%0A%09%09%09return Response.status(HttpURLConnection.HTTP_FORBIDDEN).build();%0A%09%09%7D%0A%0A%09%09UserDTOContainer users = new UserDTOContainer(userDAO.getAllUsers());%0A%09%09return Response.ok(users).build();%0A%09%7D%0A%09%0A%09@GET%0A%09@Path(%22%7Busername%7D%22)%0A%09@Produces(MediaType.APPLICATION_JSON)%0A%09public Response getUserByName(@PathParam(%22username%22) String username) %7B%0A%09%09User currentUser = userContext.getCurrentUser();%0A%09%09if (currentUser == null %7C%7C !currentUser.isAdmin()) %7B%0A%09%09%09return Response.status(HttpURLConnection.HTTP_FORBIDDEN).build();%0A%09%09%7D%0A%0A%09%09User user = userDAO.getUserByName(username);%0A%09%09%0A%09%09if (user == null) %7B%0A%09%09%09return Response.status(HttpURLConnection.HTTP_NOT_FOUND).build();%0A%09%09%7D%0A%09%09%0A%09%09%0A%09%09return Response.ok(new UserDTO(user)).build();%0A%09%7D%0A
 %7D%0A

Add get user and get all users to REST API