| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.HashMap;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- /*
- * 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.
- */
- /**
- *
- * @author Deben
- */
- public class Function {
-
-
- public HashMap defragJSON(String json) throws ParseException {
-
- String[] checkList = {"function",
- "requestId",
- "username",
- "password",
- "userInfo",
- "serviceType",
- "serviceNumber",
- "apiKey",
- "deviceId",
- "notificationId",
- "serverURL",
- "appURL",
- "storeURL",
- "confirmation",
- "result",
- "resultText",
- "email",
- "mail",
- "subject",
- "text"};
-
- HashMap<String, String> array = new HashMap();
-
- JSONParser parser = new JSONParser();
- Object obj = parser.parse(json);
- JSONObject jsonObject = (JSONObject) obj;
-
- boolean userInfo = jsonObject.containsKey("userInfo");
- System.out.println(obj);
- for(String key : checkList) {
- if(key.equals("confirmation") && jsonObject.containsKey("confirmation")) {
- if(!jsonObject.get(key).equals("approved")) {
- jsonObject.remove(key);
- jsonObject.put(key, "cancelled");
- }
- }
- if(jsonObject.get(key) != null && !jsonObject.get(key).getClass().getName().equals("org.json.simple.JSONObject")) {
- array.put(key, (String) jsonObject.get(key).toString());
- //System.out.println(key+", "+jsonObject.get(key));
- }
- if(userInfo) {
- JSONObject userObject = (JSONObject) jsonObject.get("userInfo");
- if(userObject.get(key) != null) {
- if(key.equals("mail")) {
- array.put("email", (String) userObject.get(key));
- System.out.println("MAIL RENAME");
- continue;
- }
- array.put(key, (String) userObject.get(key).toString());
- //System.out.println("userInfo: "+key+", "+userObject.get(key));
- }
- }
- }
- return array;
- }
- public String getBody(BufferedReader br) throws IOException {
- String tmp, body = "";
-
- while(null != (tmp = br.readLine())) {
- body += tmp;
- }
- return body;
- }
-
- public String getURL(String server) {
- switch(server) {
- case "ARS":
- return "http://localhost:8080/Implementation/ARS";
- case "SAS":
- return "http://localhost:8080/Implementation/SAS";
- case "APS":
- return "http://localhost:8080/Implementation/APS";
- case "APP":
- return "http://test.com";
- case "STORE":
- return "http://google.com";
- case "GCM":
- return "https://android.googleapis.com/gcm/send";
- case "APNS":
- return "http://apple.com";
- default:
- return null;
- }
- }
- public String makeRequest(String type, String url, String body) throws MalformedURLException, IOException {
- URL obj = new URL(url);
- HttpURLConnection con = (HttpURLConnection) obj.openConnection();
-
-
- switch(type) {
- case "GET":
- return "";
- case "POST":
- con.setRequestMethod("POST");
- con.setRequestProperty("content-type", "application/json");
-
- con.setDoOutput(true);
- DataOutputStream wr = new DataOutputStream(con.getOutputStream());
- wr.writeBytes(body);
- wr.flush();
- wr.close();
-
- BufferedReader in = new BufferedReader(
- new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
-
- System.out.println("============= makeRequest =============>> FUNCTION");
- System.out.print(response.toString());
- System.out.println("********************************************************");
-
- return response.toString();
- default:
- return null;
- }
- }
-
- }
|