From 1dd4c00f232eb0df6a50b6826967dd978215283c Mon Sep 17 00:00:00 2001 From: Rafa Hernandez Date: Tue, 25 Dec 2018 22:30:09 -0400 Subject: [PATCH 1/2] feat(routes): add search policiess routes Signed-off-by: Rafa Hernandez --- .../main/java/org/flyve/mdm/agent/core/Routes.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/src/main/java/org/flyve/mdm/agent/core/Routes.java b/app/src/main/java/org/flyve/mdm/agent/core/Routes.java index 791e1a07d3..5a2a15f743 100644 --- a/app/src/main/java/org/flyve/mdm/agent/core/Routes.java +++ b/app/src/main/java/org/flyve/mdm/agent/core/Routes.java @@ -120,4 +120,16 @@ public String pluginFlyvemdmFile(String fileId) { public String pluginFlyvemdmPackage(String fileId) { return url + "/PluginFlyvemdmPackage/" + fileId; } + + public String pluginFlyvemdmTask(int taskId) { + return url + "/PluginFlyvemdmTask/" + taskId; + } + + public String pluginFlyvemdmTaskSearchFleet(int fleetId) { + return url + "/search/PluginFlyvemdmTask/?criteria[0][link]=and&criteria[0][field]=9&criteria[0][searchtype]=equals&criteria[0][value]=PluginFlyvemdmFleet&criteria[1][link]=and&criteria[1][field]=10&criteria[1][searchtype]=equals&criteria[1][value]="+ fleetId +"&uid_cols=true&forcedisplay[0]=1&forcedisplay[1]=2&forcedisplay[2]=3&forcedisplay[3]=5&forcedisplay[4]=6&forcedisplay[5]=7&range=0-7&"; + } + + public String pluginFlyvemdmPolicy(int policyId) { + return url + "/PluginFlyvemdmPolicy/" + policyId; + } } From 5618c8c1d3d4837505d6e036b4ad6cf738660e06 Mon Sep 17 00:00:00 2001 From: Rafa Hernandez Date: Tue, 25 Dec 2018 22:31:42 -0400 Subject: [PATCH 2/2] feat(policies): search pending policies by taskId Signed-off-by: Rafa Hernandez --- .../flyve/mdm/agent/utils/ConnectionHTTP.java | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) diff --git a/app/src/main/java/org/flyve/mdm/agent/utils/ConnectionHTTP.java b/app/src/main/java/org/flyve/mdm/agent/utils/ConnectionHTTP.java index a0222ac053..369798b1f3 100644 --- a/app/src/main/java/org/flyve/mdm/agent/utils/ConnectionHTTP.java +++ b/app/src/main/java/org/flyve/mdm/agent/utils/ConnectionHTTP.java @@ -41,6 +41,7 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -607,6 +608,185 @@ public void run() t.start(); } + public static String getPolicyName(Context context, int policyId, Map header) { + try { + Routes routes = new Routes(context); + String url = routes.pluginFlyvemdmPolicy(policyId); + URL dataURL = new URL(url); + HttpURLConnection conn = (HttpURLConnection)dataURL.openConnection(); + + conn.setRequestMethod("GET"); + conn.setConnectTimeout(timeout); + conn.setReadTimeout(readtimeout); + + StringBuilder logHeader = new StringBuilder(); + if(header != null) { + for (Map.Entry entry : header.entrySet()) { + logHeader.append("- " + entry.getKey() + " : " + entry.getValue() + "\n"); + conn.setRequestProperty(entry.getKey(), entry.getValue()); + } + } else { + logHeader.append("Empty"); + } + + if(conn.getResponseCode() >= 400) { + InputStream is = conn.getErrorStream(); + final String result = inputStreamToString(is); + return ""; + } + + InputStream is = conn.getInputStream(); + final String requestResponse = inputStreamToString(is); + + String response = "\n URL:\n" + url + "\n\n Method:\n" + conn.getRequestMethod() + "\n\n Code:\n" + conn.getResponseCode() + " " + conn.getResponseMessage() + "\n\n Header:\n" + logHeader + "\n\n Response:\n" + requestResponse + "\n\n"; + Log(response); + + JSONObject jsonPolicy = new JSONObject(requestResponse); + return jsonPolicy.getString("symbol"); + + + } + catch (final Exception ex) + { + return ""; + } + } + + public static void getActivePolicies(final Context context, final int taskId, final Map header, final PoliciesDataCallback callback) + { + Thread t = new Thread(new Runnable() + { + public void run() + { + try + { + Routes routes = new Routes(context); + + // ------------------------------- + // FIRST STEP GET THE FLEET + // ------------------------------- + String url = routes.pluginFlyvemdmTask(taskId); + URL dataURL = new URL(url); + HttpURLConnection conn = (HttpURLConnection)dataURL.openConnection(); + + conn.setRequestMethod("GET"); + conn.setConnectTimeout(timeout); + conn.setReadTimeout(readtimeout); + + StringBuilder logHeader = new StringBuilder(); + if(header != null) { + for (Map.Entry entry : header.entrySet()) { + logHeader.append("- " + entry.getKey() + " : " + entry.getValue() + "\n"); + conn.setRequestProperty(entry.getKey(), entry.getValue()); + } + } else { + logHeader.append("Empty"); + } + + if(conn.getResponseCode() >= 400) { + InputStream is = conn.getErrorStream(); + final String result = inputStreamToString(is); + + ConnectionHTTP.runOnUI(new Runnable() + { + public void run() + { + callback.error(result); + } + }); + return; + } + + InputStream is = conn.getInputStream(); + final String requestResponse = inputStreamToString(is); + + String response = "\n URL:\n" + url + "\n\n Method:\n" + conn.getRequestMethod() + "\n\n Code:\n" + conn.getResponseCode() + " " + conn.getResponseMessage() + "\n\n Header:\n" + logHeader + "\n\n Response:\n" + requestResponse + "\n\n"; + Log(response); + + JSONObject jsonTask = new JSONObject(requestResponse); + String fleetId = jsonTask.getString("items_id_applied"); + + // ------------------------------- + // SECOND STEP GET THE POLICIES + // ------------------------------- + + url = routes.pluginFlyvemdmTaskSearchFleet(Integer.valueOf(fleetId)); + dataURL = new URL(url); + conn = (HttpURLConnection)dataURL.openConnection(); + + conn.setRequestMethod("GET"); + conn.setConnectTimeout(timeout); + conn.setReadTimeout(readtimeout); + + logHeader = new StringBuilder(); + if(header != null) { + for (Map.Entry entry : header.entrySet()) { + logHeader.append("- " + entry.getKey() + " : " + entry.getValue() + "\n"); + conn.setRequestProperty(entry.getKey(), entry.getValue()); + } + } else { + logHeader.append("Empty"); + } + + if(conn.getResponseCode() >= 400) { + is = conn.getErrorStream(); + final String result = inputStreamToString(is); + + ConnectionHTTP.runOnUI(new Runnable() + { + public void run() + { + callback.error(result); + } + }); + return; + } + + is = conn.getInputStream(); + final String requestResponse2 = inputStreamToString(is); + + response = "\n URL:\n" + url + "\n\n Method:\n" + conn.getRequestMethod() + "\n\n Code:\n" + conn.getResponseCode() + " " + conn.getResponseMessage() + "\n\n Header:\n" + logHeader + "\n\n Response:\n" + requestResponse2 + "\n\n"; + Log(response); + + // get all the policies + JSONArray jsonPolicies = new JSONObject(requestResponse2).getJSONArray("data"); + + final ArrayList> arrPolicies = new ArrayList<>(); + for(int i=0; i map = new HashMap<>(); + + map.put("type", policyType); + map.put("value", policyValue); + arrPolicies.add(map); + } + + ConnectionHTTP.runOnUI(new Runnable() { + public void run() { + callback.callback(arrPolicies); + } + }); + + } + catch (final Exception ex) + { + ConnectionHTTP.runOnUI(new Runnable() + { + public void run() + { + callback.error(EXCEPTION_HTTP + ex.getMessage()); + FlyveLog.e(ConnectionHTTP.class.getClass().getName() + ", getWebData",ex.getClass() + " : " + ex.getMessage()); + } + }); + } + } + }); + t.start(); + } + /** * Convert inputStream to String * @param stream InputStream to convert @@ -637,6 +817,11 @@ public interface DataCallback { void callback(String data); } + public interface PoliciesDataCallback { + void callback(ArrayList data); + void error(String message); + } + public interface ProgressCallback { void progress(int value); }