Advertisement
Guest User

new

a guest
Apr 29th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.96 KB | None | 0 0
  1. package com.processmonitoring.request_handler;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.json.simple.JSONObject;
  7. import org.json.simple.JSONValue;
  8. import org.json.simple.parser.ParseException;
  9.  
  10. import com.processmonitoring.bean.CcsOutgoingMessage;
  11. import com.processmonitoring.database.DatabaseHandler;
  12. import com.processmonitoring.server.MessageHelper;
  13. import com.processmonitoring.server.XMPPServer;
  14. import com.processmonitoring.util.Util;
  15.  
  16. /**
  17.  * Handles request from android and desktop client
  18.  */
  19. public class RequestHandler {
  20.     private static XMPPServer xmppServer;
  21.  
  22.     public static void setServer(XMPPServer xmppServer) {
  23.         RequestHandler.xmppServer = xmppServer;
  24.     }
  25.  
  26.     public static void handleRequest(String tokenID, String data) {
  27.         try {
  28.             Map<String, Object> listData = (Map<String, Object>) JSONValue.parseWithException(data);
  29.             String type = (String) listData.get("requestType");
  30.             String login;
  31.             String password;
  32.             int userID;
  33.             String token;
  34.             Map<String, String> dataPayload = new HashMap<String, String>();
  35.             switch (type) {
  36.             case "user-authentication":
  37.                 login = (String) listData.get("login");
  38.                 password = (String) listData.get("password");
  39.                
  40.                 dataPayload.put("requestType", "user-authentication");
  41.                
  42.                 if (DatabaseHandler.checkAuthentication(login, password)) {
  43.                     String masterKey = DatabaseHandler.getMasterKey(login);
  44.                     System.out.println("key == " + masterKey);
  45.                     dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, masterKey);
  46.                     //RequestHandler.sendResponseToDevice(tokenID, "user-authentication", masterKey);
  47.                 } else {
  48.                     dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, "failed");             
  49.                     //RequestHandler.sendResponseToDevice(tokenID, "user-authentication", "failed");
  50.                 }
  51.                 RequestHandler.sendDataToDevice(tokenID, dataPayload);
  52.                 break;
  53.             case "account_registration":
  54.                 login = (String) listData.get("login");
  55.                
  56.                 //Map<String, String> dataPayload = new HashMap<String, String>();
  57.                 dataPayload.put("requestType", "account_registration");
  58.                
  59.                 if (DatabaseHandler.checkRegistration(login)) {
  60.                     DatabaseHandler.addAccount(login, (String) listData.get("password"), (String) listData.get("key"));
  61.                     dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, "success");
  62.                     RequestHandler.sendDataToDevice(tokenID, dataPayload);
  63.                     //RequestHandler.sendResponseToDevice(tokenID, "account_registration", "success");
  64.                 } else {
  65.                     dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, "failed");
  66.                     //RequestHandler.sendResponseToDevice(tokenID, "account_registration", "failed");
  67.                 }
  68.                 RequestHandler.sendDataToDevice(tokenID, dataPayload);
  69.                 break;
  70.             case "device_registration":
  71.                 System.out.println("device registration " + (String) listData.get("login"));
  72.                 DatabaseHandler.addDevice((String) listData.get("login"), (String) listData.get("user_name"), "", "", 0,
  73.                         0);
  74.                 sendListDevices(tokenID, (String) listData.get("login"));
  75.                 break;
  76.             case "save-device-info":
  77.                 userID = Integer.parseInt((String) listData.get("user-id"));
  78.                 DatabaseHandler.updateDeviceInfo(userID, tokenID, data, 0, 0);
  79.                 break;
  80.             case "get-list-devices":
  81.                 sendListDevices(tokenID, (String) listData.get("login"));
  82.                 break;
  83.             case "get-list-apps":
  84.                 userID = Integer.parseInt((String) listData.get("user-id"));
  85.                 JSONObject jsonListApps = new JSONObject();
  86.                 String appList = DatabaseHandler.getListApps(userID);
  87.                 Map<String, Object> mapListApps = (Map<String, Object>) JSONValue.parseWithException(appList);
  88.  
  89.                 for (Map.Entry<String, Object> entry : mapListApps.entrySet()) {
  90.                     if (!entry.getKey().equals("requestType") && !entry.getKey().equals("user-id")) {
  91.                         jsonListApps.put(entry.getKey(), entry.getValue());
  92.                     }
  93.                 }
  94.                 // Send list with devices to android device
  95.                 dataPayload.put("requestType", "list-apps");
  96.                 dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, jsonListApps.toString());
  97.                 RequestHandler.sendDataToDevice(tokenID, dataPayload);
  98.                 //RequestHandler.sendResponseToDevice(tokenID, "list-apps", jsonListApps.toString());
  99.                 break;
  100.             case "update-blacklist":
  101.                 System.out.println("update-blacklist " + listData.entrySet().toString());
  102.                 userID = Integer.parseInt((String) listData.get("user-id"));
  103.                 System.out.println("userID = " + userID);
  104.                 System.out.println("data list  = " + data);
  105.                 // update only apps list in specific row in mysql database
  106.                 DatabaseHandler.updateListApps(userID, data);
  107.                 // Send new list of apps to specific android device
  108.  
  109.                 token = DatabaseHandler.getToken(userID);
  110.                 Map<String, Object> mapListApps2 = (Map<String, Object>) JSONValue.parseWithException(data);
  111.                 JSONObject jsonListApps2 = new JSONObject();
  112.                 for (Map.Entry<String, Object> entry : mapListApps2.entrySet()) {
  113.                     if (!entry.getKey().equals("requestType") && !entry.getKey().equals("user-id")) {
  114.                         jsonListApps2.put(entry.getKey(), entry.getValue());
  115.                     }
  116.  
  117.                 }
  118.                 dataPayload.put("requestType", "update-blacklist");
  119.                 dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, jsonListApps2.toString());
  120.                 RequestHandler.sendDataToDevice(token, dataPayload);
  121.                 //RequestHandler.sendResponseToDevice(token, "update-blacklist", jsonListApps2.toString());
  122.  
  123.                 break;
  124.             case "update-list":
  125.                 userID = Integer.parseInt((String) listData.get("user-id"));
  126.                 System.out.println("userID: " + userID);
  127.                 token = DatabaseHandler.getToken(userID);
  128.                 System.out.println("(String) listData.get( " + (String) listData.get("user-id"));
  129.                 //RequestHandler.sendResponseToDevice(token, "update-list", (String) listData.get("user-id"));
  130.                 dataPayload.put("requestType", "update-list");
  131.                 dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, (String) listData.get("user-id"));
  132.                 RequestHandler.sendDataToDevice(token, dataPayload);
  133.                 break;
  134.             case "get-file":
  135.                 userID = Integer.parseInt((String) listData.get("user-id"));
  136.                 System.out.println("GET FILE userID: " + userID);
  137.                 token = DatabaseHandler.getToken(userID);
  138.                 //RequestHandler.sendResponseToDevice(token, "get-file", (String) listData.get("user-id"));
  139.                 dataPayload.put("requestType", "get-file");
  140.                 dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, (String) listData.get("user-id"));
  141.                 RequestHandler.sendDataToDevice(token, dataPayload);
  142.                 break;
  143.             case "delete-device":
  144.                 System.out.println("delete-device");
  145.                 userID = Integer.parseInt((String) listData.get("user-id"));
  146.                 DatabaseHandler.deleteDevice(userID);
  147.                 break;
  148.             case "get-list-files":
  149.                 System.out.println("get-list-files - datapayload" + listData.entrySet().toString());
  150.                 userID = Integer.parseInt((String) listData.get("user-id"));
  151.                 token = DatabaseHandler.getToken(userID);
  152.                 String directory = (String) listData.get("directory");
  153.                 System.out.println("token = " + token);
  154.                 System.out.println("tokenID = " + tokenID);
  155.                
  156.                 dataPayload.put("requestType", "get-list-files");
  157.                 dataPayload.put("directory", directory);
  158.                 dataPayload.put("token", tokenID);
  159.                
  160.                 RequestHandler.sendDataToDevice(token, dataPayload);
  161.                 //requestListFilesFromDevice("get-list-files", token, tokenID, directory);
  162.                 break;
  163.             case "list-files":
  164.                 System.out.println("list-files ------------------------------------" + data);
  165.                 String tokenSender = (String) listData.get("token");
  166.                 System.out.println("tokenSender " + tokenSender);
  167.                
  168.                 Map<String, Object> mapListFiles = (Map<String, Object>) JSONValue.parseWithException(data);
  169.                 JSONObject jsonListFiles = new JSONObject();
  170.                 for (Map.Entry<String, Object> entry : mapListFiles.entrySet()) {
  171.                     System.out.println("PUT " + entry.getKey() + " " + entry.getValue());
  172.                     if (!entry.getKey().equals("requestType") && !entry.getKey().equals("token")) {
  173.                         jsonListFiles.put(entry.getKey(), entry.getValue());
  174.                     }
  175.                 }
  176.                 System.out.println("sending ==================");
  177.                 //RequestHandler.sendResponseToDevice(tokenSender, "list-files", jsonListFiles.toString());
  178.                 dataPayload.put("requestType", "list-files");
  179.                 dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, jsonListFiles.toString());
  180.                 RequestHandler.sendDataToDevice(tokenSender, dataPayload);
  181.                
  182.                 break;
  183.             case "send-file":
  184.                 System.out.println("send file ------------------------------------" + data);
  185.                 userID = Integer.parseInt((String) listData.get("user-id"));
  186.                 token = DatabaseHandler.getToken(userID);
  187.                 String path = (String) listData.get("directory");
  188.                 System.out.println("path " + path);
  189.                 //RequestHandler.sendResponseToDevice(token, "send-file", path);
  190.                 dataPayload.put("requestType", "send-file");
  191.                 dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, path);
  192.                 RequestHandler.sendDataToDevice(token, dataPayload);
  193.                 break;
  194.             default:
  195.                 break;
  196.             }
  197.         } catch (ParseException e) {
  198.             e.printStackTrace();
  199.         }
  200.     }
  201.  
  202.     public static String getListApps(int userID) {
  203.         JSONObject jsonListApps = new JSONObject();
  204.         String appList = DatabaseHandler.getListApps(userID);
  205.         Map<String, Object> mapListApps;
  206.         try {
  207.             mapListApps = (Map<String, Object>) JSONValue.parseWithException(appList);
  208.             for (Map.Entry<String, Object> entry : mapListApps.entrySet()) {
  209.                 if (!entry.getKey().equals("requestType") && !entry.getKey().equals("user-id")) {
  210.                     jsonListApps.put(entry.getKey(), entry.getValue());
  211.                 }
  212.             }
  213.         } catch (ParseException e) {
  214.             e.printStackTrace();
  215.         }
  216.         return jsonListApps.toString();
  217.     }
  218.  
  219.     public static void sendListDevices(String deviceToken, String login) {
  220.         System.out.println("=========== " + "send list devices " + login);
  221.         JSONObject jsonListDevices = new JSONObject();
  222.         // login = (String) listData.get("login");
  223.         Map<Integer, String> listDevices = DatabaseHandler.getDevices(login);
  224.         for (Map.Entry<Integer, String> entry : listDevices.entrySet()) {
  225.             System.out.println("device: " + entry.getKey() + " " + entry.getValue());
  226.             jsonListDevices.put(entry.getKey(), entry.getValue());
  227.         }
  228.         Map<String, String> dataPayload = new HashMap<String, String>();
  229.         dataPayload.put("requestType", "list-devices");
  230.         dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, jsonListDevices.toString());
  231.         RequestHandler.sendDataToDevice(deviceToken, dataPayload);
  232.         //RequestHandler.sendResponseToDevice(deviceToken, "list-devices", jsonListDevices.toString());
  233.     }
  234.  
  235.     /**
  236.      * Sends data to device it can be request or updated list of applications
  237.      */
  238.     /*public static void sendResponseToDevice(String deviceToken, String requestType, String data) {
  239.         String messageId = Util.getUniqueMessageId();
  240.         Map<String, String> dataPayload = new HashMap<String, String>();
  241.         dataPayload.put("requestType", requestType);
  242.         dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, data);
  243.         CcsOutgoingMessage message = new CcsOutgoingMessage(deviceToken, messageId, dataPayload);
  244.         String jsonRequest = MessageHelper.createJsonOutMessage(message);
  245.         xmppServer.send(jsonRequest);
  246.     }*/
  247.     public static void sendDataToDevice(String deviceReceiver, Map<String, String> dataPayload) {
  248.         String messageId = Util.getUniqueMessageId();
  249.         //Map<String, String> dataPayload = new HashMap<String, String>();
  250.         //dataPayload.put("requestType", requestType);
  251.         //dataPayload.put(Util.PAYLOAD_ATTRIBUTE_MESSAGE, data);
  252.         CcsOutgoingMessage message = new CcsOutgoingMessage(deviceReceiver, messageId, dataPayload);
  253.         String jsonRequest = MessageHelper.createJsonOutMessage(message);
  254.         xmppServer.send(jsonRequest);
  255.     }
  256.     /*public static void requestListFilesFromDevice(String requestType, String deviceToken, String tokenParent, String directory) {
  257.         String messageId = Util.getUniqueMessageId();
  258.         Map<String, String> dataPayload = new HashMap<String, String>();
  259.         dataPayload.put("requestType", requestType);
  260.         dataPayload.put("directory", directory);
  261.         dataPayload.put("token", tokenParent);
  262.         CcsOutgoingMessage message = new CcsOutgoingMessage(deviceToken, messageId, dataPayload);
  263.         String jsonRequest = MessageHelper.createJsonOutMessage(message);
  264.         xmppServer.send(jsonRequest);
  265.     }*/
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement