| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.HashMap;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.naming.NamingException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.ParseException;
- /**
- *
- * @author Deben
- */
- @WebServlet(urlPatterns = {"/APS"})
- public class APS extends HttpServlet {
-
- ErrorCode code = new ErrorCode();
- Function function = new Function();
- int ErrCode;
- int ldapError;
- String req;
- String reqBody;
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("application/json;charset=UTF-8");
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- outputResult(response, 900, null, null);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
-
- try {
-
- reqBody = function.getBody(request.getReader());
- HashMap json = function.defragJSON(reqBody);
- LDAP ldap = new LDAP((String) json.get("username"), (String) json.get("password"));
- String requestId = (String) json.get("requestId");
- req = requestId;
-
- System.out.println("============= "+req+" ============= << APS");
- System.out.print(json);
- System.out.println("********************************************************");
-
- switch((String) json.get("function")) {
- case "authenticate":
- System.out.println("##### APS >> AUTHENTICATE");
- if((ldapError = ldap.userCheck()) == 0) {
- String[] keys = {"serviceType",
- "serviceNumber",
- "apiKey",
- "deviceId",
- "mail"};
- HashMap info = ldap.getUserInfo(keys);
- if(info.get("serviceType") != null) {
- info.put("serverURL", function.getURL("SAS"));
- info.put("appURL", function.getURL("APP"));
- info.put("storeURL", function.getURL("STORE"));
- outputResult(response, 0, requestId, info);
- } else {
- outputResult(response, 0, requestId, null);
- }
- }
- else {
- outputResult(response, ldapError, requestId, null);
- }
- break;
- case "sendmail":
- System.out.println("##### APS >> SENDMAIL");
- if((ldapError = ldap.userCheck()) == 0) {
- String[] keys = {"mail"};
- MAIL mail = new MAIL();
- HashMap info = ldap.getUserInfo(keys);
- /*if(!info.get("mail").equals(json.get("email"))) {
- outputResult(response, 51, requestId, null);
- return;
- }*/
- if(!mail.check((String) info.get("mail"))) {
- outputResult(response, 51, requestId, null);
- return;
- }
-
- if(mail.send((String) info.get("mail"), (String) json.get("subject"), (String) json.get("text"))) {
- outputResult(response, 0, requestId, null);
- }
- else {
- outputResult(response, 50, requestId, null);
- }
- }
- else {
- outputResult(response, ldapError, requestId, null);
- }
- break;
- case "register":
- System.out.println("##### APS >> REGISTER");
- if((ldapError = ldap.userCheck()) == 0) {
- String[] keys = {"serviceType",
- "serviceNumber",
- "notificationId",
- "deviceId"};
- boolean state;
- for(String key : keys) {
- state = ldap.writeInfo(key, (String) json.get(key));
- if(!state) {
- outputResult(response, 82, requestId, null);
- return;
- }
- }
- outputResult(response, 0, requestId, null);
- }
- else {
- outputResult(response, ldapError, requestId, null);
- }
- break;
- }
- } catch (ParseException | NamingException ex) {
- Logger.getLogger(APS.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- private void outputResult(HttpServletResponse response, int errorCode, String requestId, HashMap extra) throws IOException {
- JSONObject array = new JSONObject();
-
- array.put("result", errorCode);
- array.put("resultText", code.getCodeText(errorCode));
- array.put("requestId", requestId);
-
- //extra.forEach((k, v) -> System.out.println("key: "+k+" value:"+v));
- if(extra != null) {
- JSONObject userinfo = new JSONObject();
- extra.forEach((k, v) -> userinfo.put(k, v));
- array.put("userInfo", userinfo);
- }
- System.out.println("============= "+req+" ============= >> APS");
- System.out.print(array);
- System.out.println("********************************************************");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println(array);
- }
- }
- }
|