APS.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Feel free to copy/use it for your own project.
  3. * Keep in mind that it took me several days/weeks, beers and asperines to make this.
  4. * So be nice, and give me some credit, I won't bite and it won't hurt you.
  5. */
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.HashMap;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.naming.NamingException;
  12. import javax.servlet.ServletException;
  13. import javax.servlet.annotation.WebServlet;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import org.json.simple.JSONObject;
  18. import org.json.simple.parser.ParseException;
  19. /**
  20. *
  21. * @author Deben Oldert
  22. */
  23. @WebServlet(urlPatterns = {"/APS"})
  24. public class APS extends HttpServlet {
  25. ErrorCode code = new ErrorCode();
  26. Function function = new Function();
  27. int ErrCode;
  28. int ldapError;
  29. String req;
  30. String reqBody;
  31. /**
  32. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  33. * methods.
  34. *
  35. * @param request servlet request
  36. * @param response servlet response
  37. * @throws ServletException if a servlet-specific error occurs
  38. * @throws IOException if an I/O error occurs
  39. */
  40. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  41. throws ServletException, IOException {
  42. response.setContentType("application/json;charset=UTF-8");
  43. }
  44. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  45. /**
  46. * Handles the HTTP <code>GET</code> method.
  47. *
  48. * @param request servlet request
  49. * @param response servlet response
  50. * @throws ServletException if a servlet-specific error occurs
  51. * @throws IOException if an I/O error occurs
  52. */
  53. @Override
  54. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  55. throws ServletException, IOException {
  56. processRequest(request, response);
  57. outputResult(response, 900, null, null);
  58. }
  59. /**
  60. * Handles the HTTP <code>POST</code> method.
  61. *
  62. * @param request servlet request
  63. * @param response servlet response
  64. * @throws ServletException if a servlet-specific error occurs
  65. * @throws IOException if an I/O error occurs
  66. */
  67. @Override
  68. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  69. throws ServletException, IOException {
  70. processRequest(request, response);
  71. try {
  72. reqBody = function.getBody(request.getReader());
  73. HashMap json = function.defragJSON(reqBody);
  74. LDAP ldap = new LDAP((String) json.get("username"), (String) json.get("password"));
  75. String requestId = (String) json.get("requestId");
  76. req = requestId;
  77. System.out.println("============= "+req+" ============= << APS");
  78. System.out.print(json);
  79. System.out.println("********************************************************");
  80. switch((String) json.get("function")) {
  81. case "authenticate":
  82. System.out.println("##### APS >> AUTHENTICATE");
  83. if((ldapError = ldap.userCheck()) == 0) {
  84. String[] keys = {"serviceType",
  85. "serviceNumber",
  86. "apiKey",
  87. "deviceId",
  88. "mail"};
  89. HashMap info = ldap.getUserInfo(keys);
  90. if(info.get("serviceType") != null) {
  91. info.put("serverURL", function.getURL("SAS"));
  92. info.put("appURL", function.getURL("APP"));
  93. info.put("storeURL", function.getURL("STORE"));
  94. outputResult(response, 0, requestId, info);
  95. } else {
  96. outputResult(response, 0, requestId, null);
  97. }
  98. }
  99. else {
  100. outputResult(response, ldapError, requestId, null);
  101. }
  102. break;
  103. case "sendmail":
  104. System.out.println("##### APS >> SENDMAIL");
  105. if((ldapError = ldap.userCheck()) == 0) {
  106. String[] keys = {"mail"};
  107. MAIL mail = new MAIL();
  108. HashMap info = ldap.getUserInfo(keys);
  109. /*if(!info.get("mail").equals(json.get("email"))) {
  110. outputResult(response, 51, requestId, null);
  111. return;
  112. }*/
  113. if(!mail.check((String) info.get("mail"))) {
  114. outputResult(response, 51, requestId, null);
  115. return;
  116. }
  117. if(mail.send((String) info.get("mail"), (String) json.get("subject"), (String) json.get("text"))) {
  118. outputResult(response, 0, requestId, null);
  119. }
  120. else {
  121. outputResult(response, 50, requestId, null);
  122. }
  123. }
  124. else {
  125. outputResult(response, ldapError, requestId, null);
  126. }
  127. break;
  128. case "register":
  129. System.out.println("##### APS >> REGISTER");
  130. if((ldapError = ldap.userCheck()) == 0) {
  131. String[] keys = {"serviceType",
  132. "serviceNumber",
  133. "notificationId",
  134. "apiKey",
  135. "deviceId"};
  136. boolean state;
  137. for(String key : keys) {
  138. if(!json.containsKey(key)) {
  139. outputResult(response, 560, requestId, null);
  140. return;
  141. }
  142. state = ldap.writeInfo(key, (String) json.get(key));
  143. if(!state) {
  144. outputResult(response, 82, requestId, null);
  145. return;
  146. }
  147. }
  148. outputResult(response, 0, requestId, null);
  149. }
  150. else {
  151. outputResult(response, ldapError, requestId, null);
  152. }
  153. break;
  154. }
  155. } catch (ParseException | NamingException ex) {
  156. Logger.getLogger(APS.class.getName()).log(Level.SEVERE, null, ex);
  157. }
  158. }
  159. private void outputResult(HttpServletResponse response, int errorCode, String requestId, HashMap extra) throws IOException {
  160. JSONObject array = new JSONObject();
  161. array.put("result", errorCode);
  162. array.put("resultText", code.getCodeText(errorCode));
  163. array.put("requestId", requestId);
  164. //extra.forEach((k, v) -> System.out.println("key: "+k+" value:"+v));
  165. if(extra != null) {
  166. JSONObject userinfo = new JSONObject();
  167. extra.forEach((k, v) -> userinfo.put(k, v));
  168. array.put("userInfo", userinfo);
  169. }
  170. System.out.println("============= "+req+" ============= >> APS");
  171. System.out.print(array);
  172. System.out.println("********************************************************");
  173. try (PrintWriter out = response.getWriter()) {
  174. /* TODO output your page here. You may use following sample code. */
  175. out.println(array);
  176. }
  177. }
  178. }