Function.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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://localhost:8080/Implementation/ARS";
  85. case "SAS":
  86. return "http://localhost:8080/Implementation/SAS";
  87. case "APS":
  88. return "http://localhost:8080/Implementation/APS";
  89. case "APP":
  90. return "http://test.com";
  91. case "STORE":
  92. return "http://google.com";
  93. case "GCM":
  94. return "https://gcm-http.googleapis.com/gcm/send";
  95. case "APNS":
  96. return "http://apple.com";
  97. default:
  98. return null;
  99. }
  100. }
  101. public String makeRequest(String type, String url, String body) throws MalformedURLException, IOException {
  102. URL obj = new URL(url);
  103. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  104. System.out.println(body);
  105. switch(type) {
  106. case "GET":
  107. return "";
  108. case "POST":
  109. con.setRequestMethod("POST");
  110. con.setRequestProperty("content-type", "application/json");
  111. if(url.contains("gcm")) {
  112. con.setRequestProperty("Authorization", "key=AIzaSyB67KpF-KSuZoPdnuy03TEIKRjHkBLEPpM");
  113. }
  114. con.setDoOutput(true);
  115. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  116. wr.writeBytes(body);
  117. wr.flush();
  118. wr.close();
  119. if(con.getResponseCode() != 200) {
  120. System.err.println(con.getResponseCode());
  121. return String.valueOf(con.getResponseCode());
  122. }
  123. BufferedReader in = new BufferedReader(
  124. new InputStreamReader(con.getInputStream()));
  125. String inputLine;
  126. StringBuilder response = new StringBuilder();
  127. while ((inputLine = in.readLine()) != null) {
  128. response.append(inputLine);
  129. }
  130. in.close();
  131. System.out.println("============= makeRequest =============>> FUNCTION");
  132. System.out.print(response.toString());
  133. System.out.println("********************************************************");
  134. return response.toString();
  135. default:
  136. return null;
  137. }
  138. }
  139. public String genRegCode(String str) {
  140. String first = str.substring(0, 1);
  141. String last = str.substring(str.length()-1);
  142. int firstCode = getCharCode(first);
  143. int lastCode = getCharCode(last);
  144. if(firstCode < 27 && firstCode > 0) {
  145. first = ""+firstCode;
  146. }
  147. else {
  148. return null;
  149. }
  150. if(lastCode < 27 && lastCode > 0) {
  151. last = ""+lastCode;
  152. }
  153. else {
  154. return null;
  155. }
  156. if(first.length() < 2) {
  157. first = "0"+first;
  158. }
  159. if(last.length() < 2) {
  160. last = "0"+last;
  161. }
  162. return first+last;
  163. }
  164. public boolean checkRegCode(String code, String str) {
  165. String newCode = genRegCode(str);
  166. return code.equals(newCode);
  167. }
  168. private int getCharCode(String letter) {
  169. letter = letter.toLowerCase();
  170. switch(letter) {
  171. case "a":
  172. return 1;
  173. case "b":
  174. return 2;
  175. case "c":
  176. return 3;
  177. case "d":
  178. return 4;
  179. case "e":
  180. return 5;
  181. case "f":
  182. return 6;
  183. case "g":
  184. return 7;
  185. case "h":
  186. return 8;
  187. case "i":
  188. return 9;
  189. case "j":
  190. return 10;
  191. case "k":
  192. return 11;
  193. case "l":
  194. return 12;
  195. case "m":
  196. return 13;
  197. case "n":
  198. return 14;
  199. case "o":
  200. return 15;
  201. case "p":
  202. return 16;
  203. case "q":
  204. return 17;
  205. case "r":
  206. return 18;
  207. case "s":
  208. return 19;
  209. case "t":
  210. return 20;
  211. case "u":
  212. return 21;
  213. case "v":
  214. return 22;
  215. case "w":
  216. return 23;
  217. case "x":
  218. return 24;
  219. case "y":
  220. return 25;
  221. case "z":
  222. return 26;
  223. default:
  224. return 0;
  225. }
  226. }
  227. }