Function.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.BufferedReader;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.net.HttpURLConnection;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.util.HashMap;
  14. import org.json.simple.JSONObject;
  15. import org.json.simple.parser.JSONParser;
  16. import org.json.simple.parser.ParseException;
  17. /**
  18. *
  19. * @author Deben Oldert
  20. */
  21. public class Function {
  22. public HashMap defragJSON(String json) throws ParseException {
  23. String[] checkList = {"function",
  24. "requestId",
  25. "username",
  26. "password",
  27. "userInfo",
  28. "serviceType",
  29. "serviceNumber",
  30. "apiKey",
  31. "deviceId",
  32. "notificationId",
  33. "registerCode",
  34. "serverURL",
  35. "appURL",
  36. "storeURL",
  37. "confirmation",
  38. "result",
  39. "resultText",
  40. "email",
  41. "mail",
  42. "subject",
  43. "text"};
  44. HashMap<String, String> array = new HashMap();
  45. JSONParser parser = new JSONParser();
  46. Object obj = parser.parse(json);
  47. JSONObject jsonObject = (JSONObject) obj;
  48. boolean userInfo = jsonObject.containsKey("userInfo");
  49. System.out.println(obj);
  50. for(String key : checkList) {
  51. if(key.equals("confirmation") && jsonObject.containsKey("confirmation")) {
  52. if(!jsonObject.get(key).equals("approved")) {
  53. jsonObject.remove(key);
  54. jsonObject.put(key, "cancelled");
  55. }
  56. }
  57. if(jsonObject.get(key) != null && !jsonObject.get(key).getClass().getName().equals("org.json.simple.JSONObject")) {
  58. array.put(key, (String) jsonObject.get(key).toString());
  59. }
  60. if(userInfo) {
  61. JSONObject userObject = (JSONObject) jsonObject.get("userInfo");
  62. if(userObject.get(key) != null) {
  63. if(key.equals("mail")) {
  64. array.put("email", (String) userObject.get(key));
  65. System.out.println("MAIL RENAME");
  66. continue;
  67. }
  68. array.put(key, (String) userObject.get(key).toString());
  69. }
  70. }
  71. }
  72. return array;
  73. }
  74. public String getBody(BufferedReader br) throws IOException {
  75. String tmp, body = "";
  76. while(null != (tmp = br.readLine())) {
  77. body += tmp;
  78. }
  79. return body;
  80. }
  81. public String getURL(String server) {
  82. switch(server) {
  83. case "ARS":
  84. return "http://192.168.2.240:8080/Implementation/ARS";
  85. case "SAS":
  86. return "http://192.168.2.240:8080/Implementation/SAS";
  87. case "APS":
  88. return "http://192.168.2.240:8080/Implementation/APS";
  89. case "APP":
  90. return "http://www.implementation.deben.dev/Login";
  91. case "GCM":
  92. return "https://gcm-http.googleapis.com/gcm/send";
  93. case "APNS":
  94. return "http://apple.com";
  95. case "PLAYSTORE":
  96. return "http://77.163.130.26:8080/Implementation/Download?device=android";
  97. case "APPSTORE":
  98. return "http://77.163.130.26:8080/Implementation/Download?device=ios";
  99. default:
  100. return null;
  101. }
  102. }
  103. public String makeRequest(String type, String url, String body) throws MalformedURLException, IOException {
  104. URL obj = new URL(url);
  105. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  106. System.out.println(body);
  107. switch(type) {
  108. case "GET":
  109. return "";
  110. case "POST":
  111. con.setRequestMethod("POST");
  112. con.setRequestProperty("content-type", "application/json");
  113. if(url.contains("gcm")) {
  114. con.setRequestProperty("Authorization", "key=AIzaSyB67KpF-KSuZoPdnuy03TEIKRjHkBLEPpM");
  115. }
  116. con.setDoOutput(true);
  117. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  118. wr.writeBytes(body);
  119. wr.flush();
  120. wr.close();
  121. if(con.getResponseCode() != 200) {
  122. System.err.println(con.getResponseCode());
  123. return String.valueOf(con.getResponseCode());
  124. }
  125. BufferedReader in = new BufferedReader(
  126. new InputStreamReader(con.getInputStream()));
  127. String inputLine;
  128. StringBuilder response = new StringBuilder();
  129. while ((inputLine = in.readLine()) != null) {
  130. response.append(inputLine);
  131. }
  132. in.close();
  133. System.out.println("============= makeRequest =============>> FUNCTION");
  134. System.out.print(response.toString());
  135. System.out.println("********************************************************");
  136. return response.toString();
  137. default:
  138. return null;
  139. }
  140. }
  141. public String genRegCode(String str) {
  142. String first = str.substring(0, 1);
  143. String last = str.substring(str.length()-1);
  144. int firstCode = getCharCode(first);
  145. int lastCode = getCharCode(last);
  146. if(firstCode < 27 && firstCode > 0) {
  147. first = ""+firstCode;
  148. }
  149. else {
  150. return null;
  151. }
  152. if(lastCode < 27 && lastCode > 0) {
  153. last = ""+lastCode;
  154. }
  155. else {
  156. return null;
  157. }
  158. if(first.length() < 2) {
  159. first = "0"+first;
  160. }
  161. if(last.length() < 2) {
  162. last = "0"+last;
  163. }
  164. return first+last;
  165. }
  166. public boolean checkRegCode(String code, String str) {
  167. String newCode = genRegCode(str);
  168. return code.equals(newCode);
  169. }
  170. private int getCharCode(String letter) {
  171. letter = letter.toLowerCase();
  172. switch(letter) {
  173. case "a":
  174. return 1;
  175. case "b":
  176. return 2;
  177. case "c":
  178. return 3;
  179. case "d":
  180. return 4;
  181. case "e":
  182. return 5;
  183. case "f":
  184. return 6;
  185. case "g":
  186. return 7;
  187. case "h":
  188. return 8;
  189. case "i":
  190. return 9;
  191. case "j":
  192. return 10;
  193. case "k":
  194. return 11;
  195. case "l":
  196. return 12;
  197. case "m":
  198. return 13;
  199. case "n":
  200. return 14;
  201. case "o":
  202. return 15;
  203. case "p":
  204. return 16;
  205. case "q":
  206. return 17;
  207. case "r":
  208. return 18;
  209. case "s":
  210. return 19;
  211. case "t":
  212. return 20;
  213. case "u":
  214. return 21;
  215. case "v":
  216. return 22;
  217. case "w":
  218. return 23;
  219. case "x":
  220. return 24;
  221. case "y":
  222. return 25;
  223. case "z":
  224. return 26;
  225. default:
  226. return 0;
  227. }
  228. }
  229. }