Skip to content

Commit ea1f3c7

Browse files
authored
Merge branch 'dev' into release/1.0.4
2 parents 116d646 + 52d38ea commit ea1f3c7

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import java.io.IOException;
44
import java.util.Arrays;
5-
import java.util.HashMap;
5+
import java.util.LinkedHashMap;
66
import java.util.List;
77
import java.util.Map;
88
import java.util.concurrent.ThreadLocalRandom;
99

1010
import com.google.gson.JsonArray;
11+
import com.google.gson.JsonElement;
1112
import com.google.gson.JsonObject;
1213
import com.google.gson.JsonParser;
1314
import com.google.gson.JsonPrimitive;
@@ -19,7 +20,7 @@
1920
import okio.Buffer;
2021

2122
public class MSBatchRequestContent {
22-
private final Map<String, MSBatchRequestStep> batchRequestStepsHashMap;
23+
private final LinkedHashMap<String, MSBatchRequestStep> batchRequestStepsHashMap;
2324

2425
// Maximum number of requests that can be sent in a batch
2526
public static final int MAX_NUMBER_OF_REQUESTS = 20;
@@ -33,7 +34,7 @@ public MSBatchRequestContent(final List<MSBatchRequestStep> batchRequestStepsArr
3334
if (batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS)
3435
throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS);
3536

36-
this.batchRequestStepsHashMap = new HashMap<>();
37+
this.batchRequestStepsHashMap = new LinkedHashMap<>();
3738
for (final MSBatchRequestStep requestStep : batchRequestStepsArray)
3839
addBatchRequestStep(requestStep);
3940
}
@@ -42,7 +43,7 @@ public MSBatchRequestContent(final List<MSBatchRequestStep> batchRequestStepsArr
4243
* Creates empty batch request content
4344
*/
4445
public MSBatchRequestContent() {
45-
batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>();
46+
this.batchRequestStepsHashMap = new LinkedHashMap<>();
4647
}
4748

4849
/*
@@ -68,7 +69,7 @@ public boolean addBatchRequestStep(final MSBatchRequestStep batchRequestStep) {
6869
public String addBatchRequestStep(final Request request, final String... arrayOfDependsOnIds) {
6970
String requestId;
7071
do {
71-
requestId = Integer.toString(ThreadLocalRandom.current().nextInt());
72+
requestId = Integer.toString(ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE));
7273
} while(batchRequestStepsHashMap.keySet().contains(requestId));
7374
if(addBatchRequestStep(new MSBatchRequestStep(requestId, request, Arrays.asList(arrayOfDependsOnIds))))
7475
return requestId;
@@ -169,8 +170,13 @@ private JsonObject requestBodyToJSONObject(final Request request) throws IOExcep
169170
final Buffer buffer = new Buffer();
170171
copy.body().writeTo(buffer);
171172
final String requestBody = buffer.readUtf8();
172-
final JsonObject JsonObject = JsonParser.parseString(requestBody).getAsJsonObject();
173-
return JsonObject;
173+
if(requestBody == null || requestBody == "")
174+
return null;
175+
final JsonElement requestBodyElement = JsonParser.parseString(requestBody);
176+
if(requestBodyElement == null || !requestBodyElement.isJsonObject())
177+
return null;
178+
else
179+
return requestBodyElement.getAsJsonObject();
174180
}
175181

176182
}

src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.microsoft.graph.content;
22

33
import java.io.IOException;
4-
import java.util.HashMap;
4+
import java.util.LinkedHashMap;
55
import java.util.Iterator;
66
import java.util.Map;
77

@@ -21,7 +21,7 @@
2121
public class MSBatchResponseContent {
2222

2323
private final Response batchResponse;
24-
private Map<String, Request> batchRequestsHashMap;
24+
private LinkedHashMap<String, Request> batchRequestsHashMap;
2525
private JsonArray batchResponseArray;
2626
private String nextLink;
2727

@@ -47,9 +47,11 @@ public Response getResponseById(final String requestId) {
4747
final JsonArray responses = batchResponseArray;
4848

4949
for (final JsonElement response : responses) {
50+
if(!response.isJsonObject())
51+
continue;
5052
final JsonObject jsonresponse = response.getAsJsonObject();
5153
final JsonElement idElement = jsonresponse.get("id");
52-
if (idElement != null) {
54+
if (idElement != null && idElement.isJsonPrimitive()) {
5355
final String id = idElement.getAsString();
5456
if (id.compareTo(requestId) == 0) {
5557
final Response.Builder builder = new Response.Builder();
@@ -62,14 +64,14 @@ public Response getResponseById(final String requestId) {
6264

6365
// Put status code of the corresponding request in JsonArray
6466
final JsonElement statusElement = jsonresponse.get("status");
65-
if (statusElement != null) {
67+
if (statusElement != null && statusElement.isJsonPrimitive()) {
6668
final Long status = statusElement.getAsLong();
6769
builder.code(status.intValue());
6870
}
6971

7072
// Put body from response array for corresponding id into constructing response
7173
final JsonElement jsonBodyElement = jsonresponse.get("body");
72-
if (jsonBodyElement != null) {
74+
if (jsonBodyElement != null && jsonBodyElement.isJsonObject()) {
7375
final JsonObject JsonObject = jsonBodyElement.getAsJsonObject();
7476
final String bodyAsString = JsonObject.toString();
7577
final ResponseBody responseBody = ResponseBody
@@ -80,11 +82,11 @@ public Response getResponseById(final String requestId) {
8082
// Put headers from response array for corresponding id into constructing
8183
// response
8284
final JsonElement jsonheadersElement = jsonresponse.get("headers");
83-
if (jsonheadersElement != null) {
85+
if (jsonheadersElement != null && jsonheadersElement.isJsonObject()) {
8486
final JsonObject jsonheaders = jsonheadersElement.getAsJsonObject();
8587
for (final String key : jsonheaders.keySet()) {
8688
final JsonElement strValueElement = jsonheaders.get(key);
87-
if (strValueElement != null) {
89+
if (strValueElement != null && strValueElement.isJsonPrimitive()) {
8890
final String strvalue = strValueElement.getAsString();
8991
for (final String value : strvalue.split(";")) {
9092
builder.header(key, value);
@@ -107,7 +109,7 @@ public Response getResponseById(final String requestId) {
107109
public Map<String, Response> getResponses() {
108110
if (batchResponseArray == null)
109111
return null;
110-
final Map<String, Response> responsesMap = new HashMap<>();
112+
final Map<String, Response> responsesMap = new LinkedHashMap<>();
111113
for (final String id : batchRequestsHashMap.keySet()) {
112114
responsesMap.put(id, getResponseById(id));
113115
}
@@ -130,7 +132,7 @@ public void update(final Response batchResponse) {
130132

131133
final Map<String, Request> requestMap = createBatchRequestsHashMap(batchResponse);
132134
if (batchRequestsHashMap == null)
133-
batchRequestsHashMap = new HashMap<>();
135+
batchRequestsHashMap = new LinkedHashMap<>();
134136
if (requestMap != null)
135137
batchRequestsHashMap.putAll(requestMap);
136138

@@ -142,14 +144,14 @@ public void update(final Response batchResponse) {
142144
if (batchResponseObj != null) {
143145

144146
final JsonElement nextLinkElement = batchResponseObj.get("@odata.nextLink");
145-
if (nextLinkElement != null)
147+
if (nextLinkElement != null && nextLinkElement.isJsonPrimitive())
146148
nextLink = nextLinkElement.getAsString();
147149

148150
if (batchResponseArray == null)
149151
batchResponseArray = new JsonArray();
150152

151153
final JsonElement responseArrayElement = batchResponseObj.get("responses");
152-
if (responseArrayElement != null) {
154+
if (responseArrayElement != null && responseArrayElement.isJsonArray()) {
153155
final JsonArray responseArray = responseArrayElement.getAsJsonArray();
154156
batchResponseArray.addAll(responseArray);
155157
}
@@ -172,29 +174,31 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
172174
if (batchResponse == null)
173175
return null;
174176
try {
175-
final Map<String, Request> batchRequestsHashMap = new HashMap<>();
177+
final Map<String, Request> batchRequestsHashMap = new LinkedHashMap<>();
176178
final JsonObject requestJSONObject = requestBodyToJSONObject(batchResponse.request());
177179
final JsonElement requestArrayElement = requestJSONObject.get("requests");
178-
if (requestArrayElement != null) {
180+
if (requestArrayElement != null && requestArrayElement.isJsonArray()) {
179181
final JsonArray requestArray = requestArrayElement.getAsJsonArray();
180182
for (final JsonElement item : requestArray) {
183+
if(!item.isJsonObject())
184+
continue;
181185
final JsonObject requestObject = item.getAsJsonObject();
182186

183187
final Request.Builder builder = new Request.Builder();
184188

185189
final JsonElement urlElement = requestObject.get("url");
186-
if (urlElement != null) {
190+
if (urlElement != null && urlElement.isJsonPrimitive()) {
187191
final StringBuilder fullUrl = new StringBuilder(
188192
batchResponse.request().url().toString().replace("$batch", ""));
189193
fullUrl.append(urlElement.getAsString());
190194
builder.url(fullUrl.toString());
191195
}
192196
final JsonElement jsonHeadersElement = requestObject.get("headers");
193-
if (jsonHeadersElement != null) {
197+
if (jsonHeadersElement != null && jsonHeadersElement.isJsonObject()) {
194198
final JsonObject jsonheaders = jsonHeadersElement.getAsJsonObject();
195199
for (final String key : jsonheaders.keySet()) {
196200
final JsonElement strvalueElement = jsonheaders.get(key);
197-
if (strvalueElement != null) {
201+
if (strvalueElement != null && strvalueElement.isJsonPrimitive()) {
198202
final String strvalue = strvalueElement.getAsString();
199203
for (final String value : strvalue.split("; ")) {
200204
builder.header(key, value);
@@ -204,7 +208,8 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
204208
}
205209
final JsonElement jsonBodyElement = requestObject.get("body");
206210
final JsonElement jsonMethodElement = requestObject.get("method");
207-
if (jsonBodyElement != null && jsonMethodElement != null) {
211+
if (jsonBodyElement != null && jsonMethodElement != null
212+
&& jsonBodyElement.isJsonObject() && jsonMethodElement.isJsonPrimitive()) {
208213
final JsonObject JsonObject = jsonBodyElement.getAsJsonObject();
209214
final String bodyAsString = JsonObject.toString();
210215
final RequestBody requestBody = RequestBody
@@ -214,7 +219,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
214219
builder.method(jsonMethodElement.getAsString(), null);
215220
}
216221
final JsonElement jsonIdElement = requestObject.get("id");
217-
if (jsonIdElement != null) {
222+
if (jsonIdElement != null && jsonIdElement.isJsonPrimitive()) {
218223
batchRequestsHashMap.put(jsonIdElement.getAsString(), builder.build());
219224
}
220225
}

0 commit comments

Comments
 (0)