Advertisement
Guest User

Untitled

a guest
May 16th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package rest.controller;
  2.  
  3. import java.io.InputStream;
  4. import java.util.List;
  5.  
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpSession;
  8. import javax.ws.rs.Consumes;
  9. import javax.ws.rs.DELETE;
  10. import javax.ws.rs.GET;
  11. import javax.ws.rs.POST;
  12. import javax.ws.rs.PUT;
  13. import javax.ws.rs.Path;
  14. import javax.ws.rs.PathParam;
  15. import javax.ws.rs.Produces;
  16. import javax.ws.rs.QueryParam;
  17. import javax.ws.rs.core.Context;
  18. import javax.ws.rs.core.MediaType;
  19. import javax.ws.rs.core.Response;
  20. import javax.ws.rs.core.Response.Status;
  21.  
  22. import com.sun.jersey.core.header.FormDataContentDisposition;
  23. import com.sun.jersey.multipart.FormDataParam;
  24.  
  25. import rest.dao.UserDAO;
  26. import rest.model.User;
  27.  
  28. @Path("/users")
  29. public class UserService {
  30.  
  31.     @GET
  32.     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  33.     public List<User> getUsers() {
  34.         return UserDAO.getAllUsers();
  35.     }
  36.  
  37.     // Controle da resposta (status code, mensagem)
  38.     @GET
  39.     @Path("/{id}")
  40.     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  41.     public Response getUser(@PathParam("id") int id) {
  42.         return Response.status(Status.OK).entity(UserDAO.getUser(id)).build();
  43.     }
  44.  
  45.     @GET
  46.     @Path("/search")
  47.     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  48.     public User getUserByName(@QueryParam("username") String username) {
  49.         return UserDAO.getUserByUsername(username);
  50.     }
  51.  
  52.     @POST
  53.     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  54.     @Consumes(MediaType.MULTIPART_FORM_DATA)
  55.     public Response addUser(@FormDataParam("image") InputStream uploadedInputStream,
  56.             @FormDataParam("username") String username, @FormDataParam("password") String password) {
  57.                
  58.         if(username == null || password == null || username.equals("null") || password.equals("null")) {
  59.             return Response.status(400).build();
  60.         }
  61.        
  62.         return Response.status(Status.OK).entity(UserDAO.addUser(username, password, uploadedInputStream)).build();
  63.     }
  64.  
  65.     @PUT
  66.     @Path("/{id}")
  67.     @Consumes(MediaType.MULTIPART_FORM_DATA)
  68.     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  69.     public User updateUser(@PathParam("id") int id, @FormDataParam("image") InputStream uploadedInputStream,
  70.             @FormDataParam("image") FormDataContentDisposition contentDispositionHeader,
  71.             @FormDataParam("username") String username, @FormDataParam("password") String password) {
  72.        
  73.         if(contentDispositionHeader.getFileName() == null) {
  74.             return UserDAO.updateUser(id, username, password, null);   
  75.         } else {
  76.             return UserDAO.updateUser(id, username, password, uploadedInputStream);
  77.         }
  78.     }
  79.  
  80.     @DELETE
  81.     @Path("/{id}")
  82.     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  83.     public void deleteUser(@PathParam("id") int id) {
  84.         UserDAO.deleteUser(id);
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement