Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

WIP Get the pending policies from http #668

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/src/main/java/org/flyve/mdm/agent/core/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
185 changes: 185 additions & 0 deletions app/src/main/java/org/flyve/mdm/agent/utils/ConnectionHTTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -607,6 +608,185 @@ public void run()
t.start();
}

public static String getPolicyName(Context context, int policyId, Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<HashMap<String,String>> arrPolicies = new ArrayList<>();
for(int i=0; i<jsonPolicies.length(); i++) {
JSONObject obj = jsonPolicies.getJSONObject(i);
String policyType = getPolicyName(context, obj.getInt("PluginFlyvemdmTask.PluginFlyvemdmPolicy.id"), header);
String policyValue = obj.getString("PluginFlyvemdmTask.value");

HashMap<String,String> 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
Expand Down Expand Up @@ -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);
}
Expand Down