Function.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.HashMap;
  9. import org.json.simple.JSONObject;
  10. import org.json.simple.parser.JSONParser;
  11. import org.json.simple.parser.ParseException;
  12. /*
  13. * To change this license header, choose License Headers in Project Properties.
  14. * To change this template file, choose Tools | Templates
  15. * and open the template in the editor.
  16. */
  17. /**
  18. *
  19. * @author Deben
  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. "serverURL",
  34. "appURL",
  35. "storeURL",
  36. "confirmation",
  37. "result",
  38. "resultText",
  39. "email",
  40. "mail",
  41. "subject",
  42. "text"};
  43. HashMap<String, String> array = new HashMap();
  44. JSONParser parser = new JSONParser();
  45. Object obj = parser.parse(json);
  46. JSONObject jsonObject = (JSONObject) obj;
  47. boolean userInfo = jsonObject.containsKey("userInfo");
  48. System.out.println(obj);
  49. for(String key : checkList) {
  50. if(key.equals("confirmation") && jsonObject.containsKey("confirmation")) {
  51. if(!jsonObject.get(key).equals("approved")) {
  52. jsonObject.remove(key);
  53. jsonObject.put(key, "cancelled");
  54. }
  55. }
  56. if(jsonObject.get(key) != null && !jsonObject.get(key).getClass().getName().equals("org.json.simple.JSONObject")) {
  57. array.put(key, (String) jsonObject.get(key).toString());
  58. //System.out.println(key+", "+jsonObject.get(key));
  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. //System.out.println("userInfo: "+key+", "+userObject.get(key));
  70. }
  71. }
  72. }
  73. return array;
  74. }
  75. public String getBody(BufferedReader br) throws IOException {
  76. String tmp, body = "";
  77. while(null != (tmp = br.readLine())) {
  78. body += tmp;
  79. }
  80. return body;
  81. }
  82. public String getURL(String server) {
  83. switch(server) {
  84. case "ARS":
  85. return "http://localhost:8080/Implementation/ARS";
  86. case "SAS":
  87. return "http://localhost:8080/Implementation/SAS";
  88. case "APS":
  89. return "http://localhost:8080/Implementation/APS";
  90. case "APP":
  91. return "http://test.com";
  92. case "STORE":
  93. return "http://google.com";
  94. case "GCM":
  95. return "https://android.googleapis.com/gcm/send";
  96. case "APNS":
  97. return "http://apple.com";
  98. default:
  99. return null;
  100. }
  101. }
  102. public String makeRequest(String type, String url, String body) throws MalformedURLException, IOException {
  103. URL obj = new URL(url);
  104. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  105. switch(type) {
  106. case "GET":
  107. return "";
  108. case "POST":
  109. con.setRequestMethod("POST");
  110. con.setRequestProperty("content-type", "application/json");
  111. con.setDoOutput(true);
  112. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  113. wr.writeBytes(body);
  114. wr.flush();
  115. wr.close();
  116. BufferedReader in = new BufferedReader(
  117. new InputStreamReader(con.getInputStream()));
  118. String inputLine;
  119. StringBuilder response = new StringBuilder();
  120. while ((inputLine = in.readLine()) != null) {
  121. response.append(inputLine);
  122. }
  123. in.close();
  124. System.out.println("============= makeRequest =============>> FUNCTION");
  125. System.out.print(response.toString());
  126. System.out.println("********************************************************");
  127. return response.toString();
  128. default:
  129. return null;
  130. }
  131. }
  132. }