Skip to content

Commit bb2ad90

Browse files
committed
- adds defensive programming for json parsing
1 parent 8c51f4b commit bb2ad90

File tree

2 files changed

+94
-84
lines changed

2 files changed

+94
-84
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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;
@@ -163,14 +164,18 @@ private String getHeaderValuesAsString(final List<String> list) {
163164
}
164165

165166
private JsonObject requestBodyToJSONObject(final Request request) throws IOException, JsonParseException {
166-
if (request == null || request.body() == null)
167-
return null;
168-
final Request copy = request.newBuilder().build();
169-
final Buffer buffer = new Buffer();
170-
copy.body().writeTo(buffer);
171-
final String requestBody = buffer.readUtf8();
172-
final JsonObject JsonObject = JsonParser.parseString(requestBody).getAsJsonObject();
173-
return JsonObject;
167+
if (request != null && request.body() != null) {
168+
final Request copy = request.newBuilder().build();
169+
final Buffer buffer = new Buffer();
170+
copy.body().writeTo(buffer);
171+
final String requestBody = buffer.readUtf8();
172+
if(requestBody != null && requestBody != "") {
173+
final JsonElement requestBodyElement = JsonParser.parseString(requestBody);
174+
if(requestBodyElement != null && requestBodyElement.isJsonObject())
175+
return requestBodyElement.getAsJsonObject();
176+
}
177+
}
178+
return null;
174179
}
175180

176181
}

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

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -47,52 +47,54 @@ public Response getResponseById(final String requestId) {
4747
final JsonArray responses = batchResponseArray;
4848

4949
for (final JsonElement response : responses) {
50-
final JsonObject jsonresponse = response.getAsJsonObject();
51-
final JsonElement idElement = jsonresponse.get("id");
52-
if (idElement != null) {
53-
final String id = idElement.getAsString();
54-
if (id.compareTo(requestId) == 0) {
55-
final Response.Builder builder = new Response.Builder();
56-
57-
// Put corresponding request into the constructed response
58-
builder.request(batchRequestsHashMap.get(requestId));
59-
// copy protocol and message same as of batch response
60-
builder.protocol(batchResponse.protocol());
61-
builder.message(batchResponse.message());
62-
63-
// Put status code of the corresponding request in JsonArray
64-
final JsonElement statusElement = jsonresponse.get("status");
65-
if (statusElement != null) {
66-
final Long status = statusElement.getAsLong();
67-
builder.code(status.intValue());
68-
}
50+
if(response.isJsonObject()) {
51+
final JsonObject jsonresponse = response.getAsJsonObject();
52+
final JsonElement idElement = jsonresponse.get("id");
53+
if (idElement != null && idElement.isJsonPrimitive()) {
54+
final String id = idElement.getAsString();
55+
if (id.compareTo(requestId) == 0) {
56+
final Response.Builder builder = new Response.Builder();
6957

70-
// Put body from response array for corresponding id into constructing response
71-
final JsonElement jsonBodyElement = jsonresponse.get("body");
72-
if (jsonBodyElement != null) {
73-
final JsonObject JsonObject = jsonBodyElement.getAsJsonObject();
74-
final String bodyAsString = JsonObject.toString();
75-
final ResponseBody responseBody = ResponseBody
76-
.create(MediaType.parse("application/json; charset=utf-8"), bodyAsString);
77-
builder.body(responseBody);
78-
}
58+
// Put corresponding request into the constructed response
59+
builder.request(batchRequestsHashMap.get(requestId));
60+
// copy protocol and message same as of batch response
61+
builder.protocol(batchResponse.protocol());
62+
builder.message(batchResponse.message());
63+
64+
// Put status code of the corresponding request in JsonArray
65+
final JsonElement statusElement = jsonresponse.get("status");
66+
if (statusElement != null && statusElement.isJsonPrimitive()) {
67+
final Long status = statusElement.getAsLong();
68+
builder.code(status.intValue());
69+
}
70+
71+
// Put body from response array for corresponding id into constructing response
72+
final JsonElement jsonBodyElement = jsonresponse.get("body");
73+
if (jsonBodyElement != null && jsonBodyElement.isJsonObject()) {
74+
final JsonObject JsonObject = jsonBodyElement.getAsJsonObject();
75+
final String bodyAsString = JsonObject.toString();
76+
final ResponseBody responseBody = ResponseBody
77+
.create(MediaType.parse("application/json; charset=utf-8"), bodyAsString);
78+
builder.body(responseBody);
79+
}
7980

80-
// Put headers from response array for corresponding id into constructing
81-
// response
82-
final JsonElement jsonheadersElement = jsonresponse.get("headers");
83-
if (jsonheadersElement != null) {
84-
final JsonObject jsonheaders = jsonheadersElement.getAsJsonObject();
85-
for (final String key : jsonheaders.keySet()) {
86-
final JsonElement strValueElement = jsonheaders.get(key);
87-
if (strValueElement != null) {
88-
final String strvalue = strValueElement.getAsString();
89-
for (final String value : strvalue.split(";")) {
90-
builder.header(key, value);
81+
// Put headers from response array for corresponding id into constructing
82+
// response
83+
final JsonElement jsonheadersElement = jsonresponse.get("headers");
84+
if (jsonheadersElement != null && jsonheadersElement.isJsonObject()) {
85+
final JsonObject jsonheaders = jsonheadersElement.getAsJsonObject();
86+
for (final String key : jsonheaders.keySet()) {
87+
final JsonElement strValueElement = jsonheaders.get(key);
88+
if (strValueElement != null && strValueElement.isJsonPrimitive()) {
89+
final String strvalue = strValueElement.getAsString();
90+
for (final String value : strvalue.split(";")) {
91+
builder.header(key, value);
92+
}
9193
}
9294
}
9395
}
96+
return builder.build();
9497
}
95-
return builder.build();
9698
}
9799
}
98100
}
@@ -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
}
@@ -175,47 +177,50 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
175177
final Map<String, Request> batchRequestsHashMap = new HashMap<>();
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) {
181-
final JsonObject requestObject = item.getAsJsonObject();
183+
if(item.isJsonObject()) {
184+
final JsonObject requestObject = item.getAsJsonObject();
182185

183-
final Request.Builder builder = new Request.Builder();
186+
final Request.Builder builder = new Request.Builder();
184187

185-
final JsonElement urlElement = requestObject.get("url");
186-
if (urlElement != null) {
187-
final StringBuilder fullUrl = new StringBuilder(
188-
batchResponse.request().url().toString().replace("$batch", ""));
189-
fullUrl.append(urlElement.getAsString());
190-
builder.url(fullUrl.toString());
191-
}
192-
final JsonElement jsonHeadersElement = requestObject.get("headers");
193-
if (jsonHeadersElement != null) {
194-
final JsonObject jsonheaders = jsonHeadersElement.getAsJsonObject();
195-
for (final String key : jsonheaders.keySet()) {
196-
final JsonElement strvalueElement = jsonheaders.get(key);
197-
if (strvalueElement != null) {
198-
final String strvalue = strvalueElement.getAsString();
199-
for (final String value : strvalue.split("; ")) {
200-
builder.header(key, value);
188+
final JsonElement urlElement = requestObject.get("url");
189+
if (urlElement != null && urlElement.isJsonPrimitive()) {
190+
final StringBuilder fullUrl = new StringBuilder(
191+
batchResponse.request().url().toString().replace("$batch", ""));
192+
fullUrl.append(urlElement.getAsString());
193+
builder.url(fullUrl.toString());
194+
}
195+
final JsonElement jsonHeadersElement = requestObject.get("headers");
196+
if (jsonHeadersElement != null && jsonHeadersElement.isJsonObject()) {
197+
final JsonObject jsonheaders = jsonHeadersElement.getAsJsonObject();
198+
for (final String key : jsonheaders.keySet()) {
199+
final JsonElement strvalueElement = jsonheaders.get(key);
200+
if (strvalueElement != null && strvalueElement.isJsonPrimitive()) {
201+
final String strvalue = strvalueElement.getAsString();
202+
for (final String value : strvalue.split("; ")) {
203+
builder.header(key, value);
204+
}
201205
}
202206
}
203207
}
204-
}
205-
final JsonElement jsonBodyElement = requestObject.get("body");
206-
final JsonElement jsonMethodElement = requestObject.get("method");
207-
if (jsonBodyElement != null && jsonMethodElement != null) {
208-
final JsonObject JsonObject = jsonBodyElement.getAsJsonObject();
209-
final String bodyAsString = JsonObject.toString();
210-
final RequestBody requestBody = RequestBody
211-
.create(MediaType.parse("application/json; charset=utf-8"), bodyAsString);
212-
builder.method(jsonMethodElement.getAsString(), requestBody);
213-
} else if (jsonMethodElement != null) {
214-
builder.method(jsonMethodElement.getAsString(), null);
215-
}
216-
final JsonElement jsonIdElement = requestObject.get("id");
217-
if (jsonIdElement != null) {
218-
batchRequestsHashMap.put(jsonIdElement.getAsString(), builder.build());
208+
final JsonElement jsonBodyElement = requestObject.get("body");
209+
final JsonElement jsonMethodElement = requestObject.get("method");
210+
if (jsonBodyElement != null && jsonMethodElement != null
211+
&& jsonBodyElement.isJsonObject() && jsonMethodElement.isJsonPrimitive()) {
212+
final JsonObject JsonObject = jsonBodyElement.getAsJsonObject();
213+
final String bodyAsString = JsonObject.toString();
214+
final RequestBody requestBody = RequestBody
215+
.create(MediaType.parse("application/json; charset=utf-8"), bodyAsString);
216+
builder.method(jsonMethodElement.getAsString(), requestBody);
217+
} else if (jsonMethodElement != null) {
218+
builder.method(jsonMethodElement.getAsString(), null);
219+
}
220+
final JsonElement jsonIdElement = requestObject.get("id");
221+
if (jsonIdElement != null && jsonIdElement.isJsonPrimitive()) {
222+
batchRequestsHashMap.put(jsonIdElement.getAsString(), builder.build());
223+
}
219224
}
220225
}
221226
}

0 commit comments

Comments
 (0)