Advertisement
Guest User

Untitled

a guest
May 16th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package es.uma.informatica.sii.agendaee.ws;
  8.  
  9. import es.uma.informatica.sii.agendaee.entidades.Contacto;
  10. import es.uma.informatica.sii.agendaee.entidades.Usuario;
  11. import es.uma.informatica.sii.agendaee.negocio.AgendaException;
  12. import es.uma.informatica.sii.agendaee.negocio.ContraseniaInvalidaException;
  13. import es.uma.informatica.sii.agendaee.negocio.CuentaInactivaException;
  14. import es.uma.informatica.sii.agendaee.negocio.CuentaInexistenteException;
  15. import es.uma.informatica.sii.agendaee.negocio.Negocio;
  16. import java.net.URI;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import javax.ejb.EJB;
  20. import javax.ws.rs.core.Context;
  21. import javax.ws.rs.core.UriInfo;
  22. import javax.ws.rs.Produces;
  23. import javax.ws.rs.GET;
  24. import javax.ws.rs.Path;
  25. import javax.enterprise.context.RequestScoped;
  26. import javax.ws.rs.Consumes;
  27. import javax.ws.rs.DELETE;
  28. import javax.ws.rs.HeaderParam;
  29. import javax.ws.rs.POST;
  30. import javax.ws.rs.PUT;
  31. import javax.ws.rs.PathParam;
  32. import javax.ws.rs.core.MediaType;
  33. import javax.ws.rs.core.Response;
  34. import javax.ws.rs.core.UriBuilder;
  35.  
  36. /**
  37. * REST Web Service
  38. *
  39. * @author francis
  40. */
  41. @Path("agenda")
  42. @RequestScoped
  43. public class NegocioWS {
  44.  
  45. @Context
  46. private UriInfo context;
  47. @EJB
  48. private Negocio neg;
  49.  
  50. /**
  51. * Creates a new instance of NegocioWS
  52. */
  53. public NegocioWS() {
  54. }
  55.  
  56. @GET
  57. @Path("contactos")
  58. @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  59. public Response getContactos(@HeaderParam("User-auth") String auth)
  60. {
  61. Usuario u = leeAutorizacion(auth);
  62.  
  63. if (u == null) {
  64. return Response.status(Response.Status.BAD_REQUEST).build();
  65. }
  66. // else
  67. try {
  68. Usuario user = neg.refrescarUsuario(u);
  69. return Response.ok(user).build();
  70. } catch (AgendaException e) {
  71. return Response.status(Response.Status.UNAUTHORIZED).build();
  72. }
  73. }
  74.  
  75. @POST
  76. @Path("contactos")
  77. @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})
  78. public Response postContacto(@HeaderParam("User-auth") String auth, Contacto c)
  79. {
  80. try {
  81. Usuario user = leeAutorizacion(auth);
  82. neg.refrescarUsuario(user);
  83. neg.insertar(c);
  84. URI uri = context.getAbsolutePathBuilder().path(c.getId().toString()).build();
  85. return Response.created(uri).build();
  86. } catch (AgendaException ex) {
  87. return Response.status(Response.Status.UNAUTHORIZED).build();
  88. }
  89. }
  90.  
  91.  
  92. @GET
  93. @Path("contacto/{id: [0-9]+}")
  94. @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  95. public Response getContacto(@HeaderParam("User-auth") String auth, @PathParam("id") Long id) {
  96. try {
  97. Usuario user = leeAutorizacion(auth);
  98. neg.refrescarUsuario(user);
  99. Contacto contacto = neg.obtenerContacto(user, id);
  100. return Response.ok(contacto).build();
  101. } catch (AgendaException ex) {
  102. return Response.status(Response.Status.UNAUTHORIZED).build();
  103. }
  104. }
  105.  
  106. @DELETE
  107. @Path("contacto/{id: [0-9]+}")
  108. public Response deleteContacto(@HeaderParam("User-auth") String auth, @PathParam("id") Long id) {
  109. try {
  110. Usuario user = leeAutorizacion(auth);
  111. neg.refrescarUsuario(user);
  112. Contacto contacto = neg.obtenerContacto(user, id);
  113. neg.eliminarContacto(contacto);
  114. return Response.ok("Succesfully deleted contact " + id).build();
  115. } catch (AgendaException ex) {
  116. return Response.status(Response.Status.UNAUTHORIZED).build();
  117. }
  118. }
  119.  
  120. @PUT
  121. @Path("contacto/{id: [0-9]+}")
  122. @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})
  123. public Response modifyContacto(@HeaderParam("User-auth") String auth, Contacto c) {
  124. try {
  125. Usuario user = leeAutorizacion(auth);
  126. neg.refrescarUsuario(user);
  127. neg.modificar(c);
  128. return Response.ok(c).build();
  129. } catch (AgendaException ex) {
  130. return Response.status(Response.Status.UNAUTHORIZED).build();
  131. }
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138. private Usuario leeAutorizacion(String auth)
  139. {
  140. if (auth == null)
  141. {
  142. return null;
  143. }
  144. // else
  145.  
  146. String [] sp = auth.split(":");
  147.  
  148. if (sp.length < 2)
  149. {
  150. return null;
  151. }
  152.  
  153. String id = sp[0];
  154. String pass = sp[1];
  155.  
  156. Usuario u = new Usuario();
  157. u.setCuenta(id);
  158. u.setContrasenia(pass);
  159.  
  160. return u;
  161. }
  162.  
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement