Skip to content
Merged
7 changes: 1 addition & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ repositories {
mavenCentral()
}

def platformDependency = "org.apache.httpcomponents:httpclient:4.5.6"
if ( project.hasProperty("platform") && project.platform == "android" ) {
platformDependency = "org.apache.httpcomponents:httpclient-android:4.3.5"
}

dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
Expand All @@ -38,7 +33,7 @@ dependencies {
// Use JUnit test framework
testImplementation 'junit:junit:4.12'

api platformDependency
api 'com.squareup.okhttp3:okhttp:3.12.1'

// https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
Expand Down
187 changes: 112 additions & 75 deletions src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java
Original file line number Diff line number Diff line change
@@ -1,112 +1,149 @@
package com.microsoft.graph.content;

import java.util.ArrayList;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.Buffer;

public class MSBatchRequestContent {
private List<MSBatchRequestStep> batchRequestStepsArray;
private final int maxNumberOfRequests = 20;
private Map<String, MSBatchRequestStep> batchRequestStepsHashMap;

// Maximum number of requests that can be sent in a batch
public static final int MAX_NUMBER_OF_REQUESTS = 20;

/*
* Creates Batch request content using list provided
*
* @param batchRequestStepsArray List of batch steps for batching
*/
public MSBatchRequestContent(List<MSBatchRequestStep> batchRequestStepsArray) {
this.batchRequestStepsArray = new ArrayList<>();
if(batchRequestStepsArray.size() <= maxNumberOfRequests) {
for(MSBatchRequestStep requestStep: batchRequestStepsArray)
addBatchRequestStep(requestStep);
}
if(batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS)
throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS);

this.batchRequestStepsHashMap = new HashMap<>();
for(MSBatchRequestStep requestStep: batchRequestStepsArray)
addBatchRequestStep(requestStep);
}

/*
* Creates empty batch request content
*/
public MSBatchRequestContent() {
batchRequestStepsArray = new ArrayList<>();
batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>();
}

/*
* @param batchRequestStep Batch request step adding to batch content
* @return true or false based on addition or no addition of batch request step given
*/
public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) {
if(batchRequestStep.getRequestId().compareTo("") == 0)
if(batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()))
return false;
if(batchRequestStepsArray.size() == maxNumberOfRequests)
return false;
for(MSBatchRequestStep requestStep: batchRequestStepsArray) {
if(batchRequestStep.getRequestId().compareTo(requestStep.getRequestId()) == 0)
return false;
}
return batchRequestStepsArray.add(batchRequestStep);
batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep);
return true;
}

public boolean removeBatchRequesStepWithId(String requestId) {
boolean ret = false;
for (int i = batchRequestStepsArray.size()-1; i >= 0; i--)
{
MSBatchRequestStep requestStep = batchRequestStepsArray.get(i);
for (int j = requestStep.getArrayOfDependsOnIds().size() - 1; j >= 0; j--)
{
String dependsOnId = requestStep.getArrayOfDependsOnIds().get(j);
if(dependsOnId.compareTo(requestId) == 0)
{
requestStep.getArrayOfDependsOnIds().remove(j);
ret = true;
}
}
if(requestId.compareTo(requestStep.getRequestId()) == 0) {
batchRequestStepsArray.remove(i);
ret = true;
}
}
return ret;
/*
* @param requestId Id of Batch request step to be removed
* @return true or false based on removal or no removal of batch request step with given id
*/
public boolean removeBatchRequestStepWithId(String requestId) {
boolean removed = false;
if(batchRequestStepsHashMap.containsKey(requestId)) {
batchRequestStepsHashMap.remove(requestId);
removed = true;
for(Map.Entry<String, MSBatchRequestStep> steps : batchRequestStepsHashMap.entrySet()) {
if(steps.getValue() != null && steps.getValue().getArrayOfDependsOnIds() != null) {
while(steps.getValue().getArrayOfDependsOnIds().remove(requestId));
}
}
}
return removed;
}

/*
* @return Batch request content's json as String
*/
public String getBatchRequestContent() {
Map<String, List<Map<String, String>>> batchRequestContentMap = new HashMap<>();
List<Map<String, String>> batchContentArray = new ArrayList<>();
for(MSBatchRequestStep requestStep : batchRequestStepsArray) {
batchContentArray.add(getBatchRequestMapFromRequestStep(requestStep));
JSONObject batchRequestContentMap = new JSONObject();
JSONArray batchContentArray = new JSONArray();
for(Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) {
batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue()));
}
batchRequestContentMap.put("requests", batchContentArray);
return JSONValue.toJSONString(batchRequestContentMap);

String content = batchRequestContentMap.toString();
return content;
}

private Map<String, String> getBatchRequestMapFromRequestStep(MSBatchRequestStep batchRequestStep){
Map<String, String> contentmap = new HashMap<>();
private JSONObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep){
JSONObject contentmap = new JSONObject();
contentmap.put("id", batchRequestStep.getRequestId());
contentmap.put("url", batchRequestStep.getRequest().getRequestLine().getUri());
contentmap.put("method", batchRequestStep.getRequest().getRequestLine().getMethod());
Header[] headers = batchRequestStep.getRequest().getAllHeaders();
if(headers != null && headers.length != 0) {
JSONObject obj = new JSONObject();
for(Header header: headers) {
obj.put(header.getName(), header.getValue());
}
contentmap.put("headers", obj.toJSONString());
}
HttpEntity entity = null;
HttpRequest request = batchRequestStep.getRequest();
if(request instanceof HttpEntityEnclosingRequestBase) {
HttpEntityEnclosingRequestBase httprequest = (HttpEntityEnclosingRequestBase)request;
entity = httprequest.getEntity();
}
if(entity != null) {
try {
String body = EntityUtils.toString(entity);
contentmap.put("body", body);
}
catch(Exception e) {
e.printStackTrace();

String url = batchRequestStep.getRequest().url().toString();
url = url.replaceAll("https://graph.microsoft.com/v1.0/", "");
url = url.replaceAll("http://graph.microsoft.com/v1.0/", "");
url = url.replaceAll("https://graph.microsoft.com/beta/", "");
url = url.replaceAll("http://graph.microsoft.com/beta/", "");
contentmap.put("url", url);

contentmap.put("method", batchRequestStep.getRequest().method().toString());

Headers headers = batchRequestStep.getRequest().headers();
if(headers != null && headers.size() != 0) {
JSONObject headerMap = new JSONObject();
for(Map.Entry<String, List<String>> entry : headers.toMultimap().entrySet()) {
headerMap.put(entry.getKey(), getHeaderValuesAsString(entry.getValue()));
}
contentmap.put("headers", headerMap);
}

List<String> arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds();
if(arrayOfDependsOnIds != null) {
contentmap.put("dependsOn", JSONValue.toJSONString(arrayOfDependsOnIds));
JSONArray array = new JSONArray();
for(String dependsOnId : arrayOfDependsOnIds) array.add(dependsOnId);
contentmap.put("dependsOn", array);
}

RequestBody body = batchRequestStep.getRequest().body();
if(body != null) {
try {
contentmap.put("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
}catch(IOException | ParseException e) {
e.printStackTrace();
}
}
return contentmap;
}


private String getHeaderValuesAsString(final List<String> list) {
if(list == null || list.size() == 0)return "";
StringBuilder builder = new StringBuilder(list.get(0));
for(int i=1;i<list.size();i++) {
builder.append(";");
builder.append(list.get(i));
}
return builder.toString();
}

private JSONObject requestBodyToJSONObject(final Request request) throws IOException, ParseException{
if(request == null || request.body() == null)return null;
Request copy = request.newBuilder().build();
Buffer buffer = new Buffer();
copy.body().writeTo(buffer);
String requestBody = buffer.readUtf8();
JSONObject jsonObject = (JSONObject)new JSONParser().parse(requestBody);
return jsonObject;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import java.util.List;

import org.apache.http.HttpRequest;
import okhttp3.Request;

public class MSBatchRequestStep {
private String requestId;
private HttpRequest request;
private Request request;
private List<String> arrayOfDependsOnIds;

public MSBatchRequestStep(String requestId, HttpRequest request, List<String> arrayOfDependsOnIds) {
public MSBatchRequestStep(String requestId, Request request, List<String> arrayOfDependsOnIds) {
if(requestId == null)
throw new IllegalArgumentException("Request Id cannot be null.");
if(request == null)
new IllegalArgumentException("Request cannot be null.");

this.requestId = requestId;
this.request = request;
this.arrayOfDependsOnIds = arrayOfDependsOnIds;
Expand All @@ -19,7 +24,7 @@ public String getRequestId() {
return requestId;
}

public HttpRequest getRequest() {
public Request getRequest() {
return request;
}

Expand Down
Loading