Guest User

Untitled

a guest
Feb 3rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. package myproject.model;
  2.  
  3. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. import javax.xml.bind.annotation.XmlElement;
  6. import javax.xml.bind.annotation.XmlRootElement;
  7. import javax.xml.bind.annotation.XmlTransient;
  8.  
  9. @XmlRootElement
  10. @XmlAccessorType(XmlAccessType.FIELD)
  11. public class UserModel {
  12.  
  13. @XmlElement(name = "id")
  14. private long id = -1;
  15.  
  16. @XmlElement(name = "strUsername")
  17. private String username = null;
  18.  
  19. @XmlTransient
  20. private String encryptedPassword = null;
  21.  
  22. @XmlElement(name = "Consultant", nillable = true)
  23. private Consultant consultant = null;
  24.  
  25. public UserModel(long id, String username, String encPw, Consultant co) {
  26. this.id = id;
  27. this.username = username;
  28. this.encryptedPassword = encPw;
  29. this.consultant = co;
  30. }
  31.  
  32. public static UserModel fromUser(User u) {
  33. return new UserModel(u.getId(), u.getName(), u.getEncryptedPassword(), u.getConsultant());
  34. }
  35.  
  36. public User toUser() {
  37. return User.fromUserModel(this);
  38. }
  39.  
  40. public long getId() {
  41. return id;
  42. }
  43.  
  44. public String getUsername() {
  45. return username;
  46. }
  47.  
  48. public String getEncryptedPassword() {
  49. return encryptedPassword;
  50. }
  51.  
  52. public Consultant getConsultant() {
  53. return consultant;
  54. }
  55.  
  56. public String toString() {
  57. return "id=" + id + ";username=" + username + ";encPw=" + encryptedPassword;
  58. }
  59. }
  60.  
  61. package myproject.rest;
  62.  
  63. import java.sql.Connection;
  64. import java.util.ArrayList;
  65. import java.util.Iterator;
  66. import java.util.List;
  67.  
  68. import javax.ws.rs.GET;
  69. import javax.ws.rs.Path;
  70. import javax.ws.rs.Produces;
  71. import javax.ws.rs.core.GenericEntity;
  72. import javax.ws.rs.core.MediaType;
  73. import javax.ws.rs.core.Response;
  74.  
  75. import org.apache.log4j.Logger;
  76. import org.json.JSONObject;
  77.  
  78. import myproject.User;
  79. import myproject.UserModel;
  80. import myproject.DBUtil;
  81. import myproject.SecurityUtil;
  82.  
  83. @Path("/app/user")
  84. public class HandlerUser extends RestEndpoint {
  85. private static Logger log = Logger.getLogger(HandlerUser.class);
  86.  
  87. @GET
  88. @Produces(MediaType.APPLICATION_JSON)
  89. public Response userGet() {
  90. Connection con = null;
  91.  
  92. try {
  93. con = DBUtil.getDBConnection();
  94.  
  95. //SecurityUtil.checkRight(request);
  96.  
  97. log.info("Collect all Users");
  98.  
  99. List<UserModel> usermodels = new ArrayList<UserModel>();
  100.  
  101. Iterator<User> it = User.getAll(con).iterator();
  102.  
  103. while(it.hasNext()) {
  104. usermodels.add(it.next().toUserModel());
  105. }
  106.  
  107. GenericEntity<List<UserModel>> entity = new GenericEntity<List<UserModel>>(usermodels) {};
  108.  
  109. return Response.ok().entity(entity).build();
  110. }
  111. catch(Exception e) {
  112. log.error("An error occured: " + e.toString());
  113.  
  114. return Response.status(401).entity(e.toString()).build();
  115. }
  116. finally {
  117. DBUtil.close(null, null, con);
  118. }
  119. }
  120. }
Add Comment
Please, Sign In to add comment