SAS.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.HashMap;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.naming.NamingException;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.annotation.WebServlet;
  16. import javax.servlet.http.HttpServlet;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import org.json.simple.JSONObject;
  20. import org.json.simple.parser.ParseException;
  21. /**
  22. *
  23. * @author Deben
  24. */
  25. @WebServlet(urlPatterns = {"/SAS"})
  26. public class SAS extends HttpServlet {
  27. Function function = new Function();
  28. ErrorCode code = new ErrorCode();
  29. int ldapError;
  30. SQL sql;
  31. ResultSet result;
  32. final Object T3 = new Object();
  33. final Object T2 = new Object();
  34. final Object T1 = new Object();
  35. String req;
  36. String reqBody;
  37. int i;
  38. int timeout = 60;
  39. boolean finished = false;
  40. public SAS() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
  41. this.sql = (new SQL());
  42. }
  43. /**
  44. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  45. * methods.
  46. *
  47. * @param request servlet request
  48. * @param response servlet response
  49. * @throws ServletException if a servlet-specific error occurs
  50. * @throws IOException if an I/O error occurs
  51. * @throws java.sql.SQLException
  52. * @throws java.lang.InstantiationException
  53. * @throws java.lang.IllegalAccessException
  54. * @throws java.lang.ClassNotFoundException
  55. */
  56. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  57. throws ServletException, IOException, SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
  58. response.setContentType("application/json;charset=UTF-8");
  59. }
  60. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  61. /**
  62. * Handles the HTTP <code>GET</code> method.
  63. *
  64. * @param request servlet request
  65. * @param response servlet response
  66. * @throws ServletException if a servlet-specific error occurs
  67. * @throws IOException if an I/O error occurs
  68. */
  69. @Override
  70. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  71. throws ServletException, IOException {
  72. response.setContentType("application/json;charset=UTF-8");
  73. JSONObject json = new JSONObject();
  74. json.put("result", 900);
  75. json.put("resultText", code.getCodeText(900));
  76. try (PrintWriter out = response.getWriter()) {
  77. out.println(json);
  78. }
  79. }
  80. @Override
  81. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  82. throws ServletException, IOException {
  83. response.setContentType("application/json;charset=UTF-8");
  84. try {
  85. reqBody = function.getBody(request.getReader());
  86. HashMap json = function.defragJSON(reqBody);
  87. LDAP ldap = new LDAP((String) json.get("username"), (String) json.get("password"));
  88. String requestId = (String) json.get("requestId");
  89. String answer;
  90. req = requestId;
  91. System.out.println("============= "+req+" ============= << SAS");
  92. System.out.print(json);
  93. System.out.println("********************************************************");
  94. switch((String) json.get("function")) {
  95. case "authenticate":
  96. sql.threadUpdate(requestId, "birth", null);
  97. System.out.println("##### SAS >> AUTHENTICATE");
  98. String tmpBody;
  99. JSONObject tmpJSON = new JSONObject();
  100. tmpJSON.put("function", "authenticate");
  101. tmpJSON.put("requestId", requestId);
  102. tmpJSON.put("username", (String) json.get("username"));
  103. tmpJSON.put("password", (String) json.get("password"));
  104. tmpBody = tmpJSON.toJSONString();
  105. HashMap jsonAnswer = function.defragJSON(function.makeRequest("POST", function.getURL("APS"), tmpBody));
  106. //System.out.print(jsonAnswer);
  107. if(jsonAnswer.get("result").equals("0")) {
  108. if(jsonAnswer.containsKey("serviceType")) {
  109. System.out.println("SENDING PUSH NOTIFICATION");
  110. tmpJSON = new JSONObject();
  111. tmpJSON.put("function", "notify");
  112. tmpJSON.put("requestId", requestId);
  113. tmpJSON.put("serviceType", (String) jsonAnswer.get("serviceType"));
  114. tmpJSON.put("serviceNumber", (String) jsonAnswer.get("serviceNumber"));
  115. tmpJSON.put("apiKey", (String) jsonAnswer.get("apiKey"));
  116. tmpJSON.put("deviceId", (String) jsonAnswer.get("deviceId"));
  117. tmpBody = tmpJSON.toJSONString();
  118. System.out.println("%%%%%%%%%%%%%%%");
  119. System.out.println((String) jsonAnswer.get("serviceType"));
  120. //function.defragJSON(function.makeRequest("POST", function.getURL((String) jsonAnswer.get("serviceType")), tmpBody));
  121. sql.threadUpdate(requestId, "request", null);
  122. for(i = 1; i<=timeout && !finished; i++) {
  123. result = sql.stmt.executeQuery("SELECT `state`, `data` FROM `thread` WHERE `threadId`='"+requestId+"'");
  124. //System.out.println("GOT RESULT");
  125. if(result.first()) {
  126. //System.out.println("GOT VALID RESULT state: "+result.getString("state")+" || data: "+result.getString("data"));
  127. if(result.getString("state").equals("repley")) {
  128. //System.out.println("GOT REPLEY");
  129. if(result.getString("data") == null || result.getString("data").equals("") || result.getString("data").equals("null")) {
  130. outputResult(response, 601, requestId, null, true);
  131. finished = true;
  132. } else {
  133. outputResult(response, Integer.parseInt((String) jsonAnswer.get("result")), requestId, function.defragJSON(result.getString("data")), true);
  134. finished = true;
  135. break;
  136. }
  137. }
  138. else {
  139. System.out.println("WAITING "+i+" SECONDS");
  140. Thread.sleep(1000);
  141. }
  142. }
  143. else {
  144. //System.out.println("GOT INVALID RESULT");
  145. outputResult(response, 602, requestId, null, true);
  146. finished = true;
  147. break;
  148. }
  149. result = null;
  150. }
  151. System.out.println("WAITED "+i+" SECONDS");
  152. if(i > timeout) {
  153. outputResult(response, 950, requestId, null, true);
  154. }
  155. } else {
  156. System.out.println("SENDING EMAIL");
  157. tmpJSON = new JSONObject();
  158. tmpJSON.put("function", "sendmail");
  159. tmpJSON.put("requestId", requestId);
  160. tmpJSON.put("username", (String) json.get("username"));
  161. tmpJSON.put("password", (String) json.get("password"));
  162. tmpJSON.put("subject", "Enrollment for TFA");
  163. tmpJSON.put("text", "Dear Employee,<br><br>"
  164. + "In order to login to our VPN server you need to download our app to verify your connection request.<br><br>"
  165. + "For Android:<br><ol>"
  166. + "<li>Download and install our app at: "+function.getURL("STORE")+"</li>"
  167. + "<li>Start it: "+function.getURL("APP")+"</li></ol>"
  168. + "We currently don't support iPhone. Sorry for the inconvience.<br><br>"
  169. + "Regards,<br>"
  170. + "The IT Security department");
  171. tmpBody = tmpJSON.toJSONString();
  172. jsonAnswer = function.defragJSON(function.makeRequest("POST", function.getURL("APS"), tmpBody));
  173. if(jsonAnswer.get("result").equals("0")) {
  174. String state;
  175. String data;
  176. for(i=1; i<=timeout && !finished; i++) {
  177. result = sql.stmt.executeQuery("SELECT state,data FROM thread WHERE threadId='"+requestId+"'");
  178. if(result.first()) {
  179. state = result.getString("state");
  180. data = result.getString("data");
  181. if(state.equals("repley")) {
  182. answer = function.makeRequest("POST", function.getURL("APS"), data);
  183. outputResult(response, 0, requestId, null, true);
  184. finished = true;
  185. } else {
  186. System.out.println("WAITING "+i+" SECONDS");
  187. Thread.sleep(1000);
  188. }
  189. } else {
  190. outputResult(response, 602, requestId, null, true);
  191. finished = true;
  192. }
  193. }
  194. System.out.println("WAITED "+i+" SECONDS");
  195. if(i > timeout) {
  196. outputResult(response, 950, requestId, null, true);
  197. }
  198. } else {
  199. outputResult(response, (int) jsonAnswer.get("result"), requestId, null, true);
  200. }
  201. }
  202. } else {
  203. outputResult(response, 1, requestId, null, true);
  204. }
  205. break;
  206. case "register":
  207. System.out.println("##### SAS >> REGISTER");
  208. if((ldapError = ldap.userCheck()) == 0) {
  209. sql.threadUpdate(requestId, "repley", reqBody);
  210. outputResult(response, 0, requestId, null, false);
  211. /*result = sql.stmt.executeQuery("SELECT state,data FROM thread WHERE threadId='"+requestId+"'");
  212. if(result.first()) {
  213. int resultCode = result.getInt("result");
  214. outputResult(response, resultCode, requestId, null, false);
  215. }
  216. else {
  217. outputResult(response, 602, requestId, null, true);
  218. }*/
  219. } else {
  220. outputResult(response, ldapError, requestId, null, true);
  221. }
  222. break;
  223. case "confirm":
  224. System.out.println("##### SAS >> CONFIRM");
  225. //System.out.println("Body: "+reqBody);
  226. sql.threadUpdate(requestId, "repley", reqBody);
  227. //sql.stmt.execute("UPDATE thread set `state`='repley',`data`="+function.getBody(request.getReader())+" WHERE `threadId`='"+requestId+"'");
  228. outputResult(response, 0, requestId, null, false);
  229. break;
  230. }
  231. } catch (ParseException | NamingException | SQLException | ClassNotFoundException | InterruptedException ex) {
  232. Logger.getLogger(SAS.class.getName()).log(Level.SEVERE, null, ex);
  233. }
  234. }
  235. private void outputResult(HttpServletResponse response, int errorCode, String requestId, HashMap extra, boolean terminate) throws IOException, SQLException, ClassNotFoundException {
  236. JSONObject array = new JSONObject();
  237. array.put("result", errorCode);
  238. array.put("resultText", code.getCodeText(errorCode));
  239. array.put("requestId", requestId);
  240. //extra.forEach((k, v) -> System.out.println("key: "+k+" value:"+v));
  241. if(extra != null) {
  242. if(extra.containsKey("confirmation")) {
  243. array.put("confirmation", extra.get("confirmation"));
  244. } else {
  245. JSONObject userinfo = new JSONObject();
  246. extra.forEach((k, v) -> userinfo.put(k, v));
  247. array.put("userInfo", userinfo);
  248. }
  249. }
  250. System.out.println("============= "+req+" ============= >> SAS");
  251. System.out.print(array);
  252. System.out.println("********************************************************");
  253. if(terminate) {
  254. sql.threadUpdate(requestId, "terminate", null);
  255. }
  256. try (PrintWriter out = response.getWriter()) {
  257. /* TODO output your page here. You may use following sample code. */
  258. out.println(array);
  259. }
  260. }
  261. }