Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
}
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ dependencies {

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

// https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
implementation 'com.google.code.gson:gson:2.8.6'
}

def pomConfig = {
Expand Down
155 changes: 81 additions & 74 deletions src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,145 +5,152 @@
import java.util.List;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonParseException;

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

public class MSBatchRequestContent {
private Map<String, MSBatchRequestStep> batchRequestStepsHashMap;
private final 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) {
if(batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS)
public MSBatchRequestContent(final List<MSBatchRequestStep> batchRequestStepsArray) {
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)
for (final MSBatchRequestStep requestStep : batchRequestStepsArray)
addBatchRequestStep(requestStep);
}

/*
* Creates empty batch request content
*/
public MSBatchRequestContent() {
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
*
* @return true or false based on addition or no addition of batch request step
* given
*/
public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) {
if(batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()))
public boolean addBatchRequestStep(final MSBatchRequestStep batchRequestStep) {
if (batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()))
return false;
batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep);
return true;
}

/*
* @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
*
* @return true or false based on removal or no removal of batch request step
* with given id
*/
public boolean removeBatchRequestStepWithId(String requestId) {
public boolean removeBatchRequestStepWithId(final String requestId) {
boolean removed = false;
if(batchRequestStepsHashMap.containsKey(requestId)) {
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));
for (final 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() {
JSONObject batchRequestContentMap = new JSONObject();
JSONArray batchContentArray = new JSONArray();
for(Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) {
final JsonObject batchRequestContentMap = new JsonObject();
final JsonArray batchContentArray = new JsonArray();
for (final Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) {
batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue()));
}
batchRequestContentMap.put("requests", batchContentArray);
String content = batchRequestContentMap.toString();
batchRequestContentMap.add("requests", batchContentArray);

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

private JSONObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep){
JSONObject contentmap = new JSONObject();
contentmap.put("id", batchRequestStep.getRequestId());

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()));

private JsonObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep) {
final JsonObject contentmap = new JsonObject();
contentmap.add("id", new JsonPrimitive(batchRequestStep.getRequestId()));

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

contentmap.add("method", new JsonPrimitive(batchRequestStep.getRequest().method().toString()));

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

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

final List<String> arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds();
if (arrayOfDependsOnIds != null) {
final JsonArray array = new JsonArray();
for (final String dependsOnId : arrayOfDependsOnIds)
array.add(dependsOnId);
contentmap.add("dependsOn", array);
}
RequestBody body = batchRequestStep.getRequest().body();
if(body != null) {

final RequestBody body = batchRequestStep.getRequest().body();
if (body != null) {
try {
contentmap.put("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
}catch(IOException | ParseException e) {
contentmap.add("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
} catch (IOException | JsonParseException 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++) {
if (list == null || list.size() == 0)
return "";
final 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();

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

}
Loading