Skip to content

Commit 0b78779

Browse files
authored
Merge pull request #73 from microsoftgraph/dev
release 1.0.4
2 parents c6c3c2a + 727bdb4 commit 0b78779

File tree

7 files changed

+108
-29
lines changed

7 files changed

+108
-29
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mavenGroupId = com.microsoft.graph
2525
mavenArtifactId = microsoft-graph-core
2626
mavenMajorVersion = 1
2727
mavenMinorVersion = 0
28-
mavenPatchVersion = 3
28+
mavenPatchVersion = 4
2929
mavenArtifactSuffix =
3030
nightliesUrl = http://dl.bintray.com/MicrosoftGraph/Maven
3131

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repositories {
2020
2121
dependencies {
2222
// Include the sdk as a dependency
23-
implementation 'com.microsoft.graph:microsoft-graph-core:1.0.3'
23+
implementation 'com.microsoft.graph:microsoft-graph-core:1.0.4'
2424
}
2525
```
2626

@@ -32,7 +32,7 @@ Add the dependency in `dependencies` in pom.xml
3232
<dependency>
3333
<groupId>com.microsoft.graph</groupId>
3434
<artifactId>microsoft-graph-core</artifactId>
35-
<version>1.0.3</version>
35+
<version>1.0.4</version>
3636
</dependency>
3737
```
3838

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
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.microsoft.graph.httpcore;
2+
3+
import java.io.IOException;
4+
import java.util.concurrent.ThreadLocalRandom;
5+
6+
import com.microsoft.graph.httpcore.middlewareoption.MiddlewareType;
7+
import com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions;
8+
9+
import okhttp3.Interceptor;
10+
import okhttp3.MediaType;
11+
import okhttp3.Protocol;
12+
import okhttp3.Request;
13+
import okhttp3.Response;
14+
import okhttp3.ResponseBody;
15+
16+
/**
17+
* DO NOT USE IN PRODUCTION
18+
* interceptor that randomly fails the responses for unit testing purposes
19+
*/
20+
public class ChaosHttpHandler implements Interceptor {
21+
public final MiddlewareType MIDDLEWARE_TYPE = MiddlewareType.RETRY;
22+
/*
23+
* constant string being used
24+
*/
25+
private final String RETRY_AFTER = "Retry-After";
26+
/**
27+
* Denominator for the failure rate (i.e. 1/X)
28+
*/
29+
private final Integer failureRate = 3;
30+
/**
31+
* default value to return on retry after
32+
*/
33+
private final String retryAfterValue = "10";
34+
/**
35+
* body to respond on failed requests
36+
*/
37+
private final String responseBody = "{\"error\": {\"code\": \"TooManyRequests\",\"innerError\": {\"code\": \"429\",\"date\": \"2020-08-18T12:51:51\",\"message\": \"Please retry after\",\"request-id\": \"94fb3b52-452a-4535-a601-69e0a90e3aa2\",\"status\": \"429\"},\"message\": \"Please retry again later.\"}}";
38+
public static final int MSClientErrorCodeTooManyRequests = 429;
39+
40+
@Override
41+
public Response intercept(Chain chain) throws IOException {
42+
Request request = chain.request();
43+
44+
if(request.tag(TelemetryOptions.class) == null)
45+
request = request.newBuilder().tag(TelemetryOptions.class, new TelemetryOptions()).build();
46+
request.tag(TelemetryOptions.class).setFeatureUsage(TelemetryOptions.RETRY_HANDLER_ENABLED_FLAG);
47+
48+
final Integer dice = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
49+
50+
if(dice % failureRate == 0) {
51+
return new Response
52+
.Builder()
53+
.request(request)
54+
.protocol(Protocol.HTTP_1_1)
55+
.code(MSClientErrorCodeTooManyRequests)
56+
.message("Too Many Requests")
57+
.addHeader(RETRY_AFTER, retryAfterValue)
58+
.body(ResponseBody.create(MediaType.get("application/json"), responseBody))
59+
.build();
60+
} else {
61+
return chain.proceed(request);
62+
}
63+
}
64+
65+
}

src/main/java/com/microsoft/graph/httpcore/RetryHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ public Response intercept(Chain chain) throws IOException {
147147
while(retryRequest(response, executionCount, request, retryOption)) {
148148
request = request.newBuilder().addHeader(RETRY_ATTEMPT_HEADER, String.valueOf(executionCount)).build();
149149
executionCount++;
150+
if(response != null && response.body() != null) {
151+
response.body().close();
152+
}
150153
response = chain.proceed(request);
151154
}
152155
return response;

src/main/java/com/microsoft/graph/httpcore/TelemetryHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class TelemetryHandler implements Interceptor{
1212

1313
public static final String SDK_VERSION = "SdkVersion";
14-
public static final String VERSION = "v1.0.3";
14+
public static final String VERSION = "v1.0.4";
1515
public static final String GRAPH_VERSION_PREFIX = "graph-java-core";
1616
public static final String JAVA_VERSION_PREFIX = "java";
1717
public static final String CLIENT_REQUEST_ID = "client-request-id";

0 commit comments

Comments
 (0)