Skip to content

Commit

Permalink
Merge pull request #73 from microsoftgraph/dev
Browse files Browse the repository at this point in the history
release 1.0.4
  • Loading branch information
baywet authored Sep 16, 2020
2 parents c6c3c2a + 727bdb4 commit 0b78779
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 29 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mavenGroupId = com.microsoft.graph
mavenArtifactId = microsoft-graph-core
mavenMajorVersion = 1
mavenMinorVersion = 0
mavenPatchVersion = 3
mavenPatchVersion = 4
mavenArtifactSuffix =
nightliesUrl = http://dl.bintray.com/MicrosoftGraph/Maven

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ repositories {
dependencies {
// Include the sdk as a dependency
implementation 'com.microsoft.graph:microsoft-graph-core:1.0.3'
implementation 'com.microsoft.graph:microsoft-graph-core:1.0.4'
}
```

Expand All @@ -32,7 +32,7 @@ Add the dependency in `dependencies` in pom.xml
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph-core</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
</dependency>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
Expand All @@ -19,7 +20,7 @@
import okio.Buffer;

public class MSBatchRequestContent {
private final Map<String, MSBatchRequestStep> batchRequestStepsHashMap;
private final LinkedHashMap<String, MSBatchRequestStep> batchRequestStepsHashMap;

// Maximum number of requests that can be sent in a batch
public static final int MAX_NUMBER_OF_REQUESTS = 20;
Expand All @@ -33,7 +34,7 @@ public MSBatchRequestContent(final List<MSBatchRequestStep> batchRequestStepsArr
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<>();
this.batchRequestStepsHashMap = new LinkedHashMap<>();
for (final MSBatchRequestStep requestStep : batchRequestStepsArray)
addBatchRequestStep(requestStep);
}
Expand All @@ -42,7 +43,7 @@ public MSBatchRequestContent(final List<MSBatchRequestStep> batchRequestStepsArr
* Creates empty batch request content
*/
public MSBatchRequestContent() {
batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>();
this.batchRequestStepsHashMap = new LinkedHashMap<>();
}

/*
Expand All @@ -68,7 +69,7 @@ public boolean addBatchRequestStep(final MSBatchRequestStep batchRequestStep) {
public String addBatchRequestStep(final Request request, final String... arrayOfDependsOnIds) {
String requestId;
do {
requestId = Integer.toString(ThreadLocalRandom.current().nextInt());
requestId = Integer.toString(ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE));
} while(batchRequestStepsHashMap.keySet().contains(requestId));
if(addBatchRequestStep(new MSBatchRequestStep(requestId, request, Arrays.asList(arrayOfDependsOnIds))))
return requestId;
Expand Down Expand Up @@ -169,8 +170,13 @@ private JsonObject requestBodyToJSONObject(final Request request) throws IOExcep
final Buffer buffer = new Buffer();
copy.body().writeTo(buffer);
final String requestBody = buffer.readUtf8();
final JsonObject JsonObject = JsonParser.parseString(requestBody).getAsJsonObject();
return JsonObject;
if(requestBody == null || requestBody == "")
return null;
final JsonElement requestBodyElement = JsonParser.parseString(requestBody);
if(requestBodyElement == null || !requestBodyElement.isJsonObject())
return null;
else
return requestBodyElement.getAsJsonObject();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.microsoft.graph.content;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map;

Expand All @@ -21,7 +21,7 @@
public class MSBatchResponseContent {

private final Response batchResponse;
private Map<String, Request> batchRequestsHashMap;
private LinkedHashMap<String, Request> batchRequestsHashMap;
private JsonArray batchResponseArray;
private String nextLink;

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

for (final JsonElement response : responses) {
if(!response.isJsonObject())
continue;
final JsonObject jsonresponse = response.getAsJsonObject();
final JsonElement idElement = jsonresponse.get("id");
if (idElement != null) {
if (idElement != null && idElement.isJsonPrimitive()) {
final String id = idElement.getAsString();
if (id.compareTo(requestId) == 0) {
final Response.Builder builder = new Response.Builder();
Expand All @@ -62,14 +64,14 @@ public Response getResponseById(final String requestId) {

// Put status code of the corresponding request in JsonArray
final JsonElement statusElement = jsonresponse.get("status");
if (statusElement != null) {
if (statusElement != null && statusElement.isJsonPrimitive()) {
final Long status = statusElement.getAsLong();
builder.code(status.intValue());
}

// Put body from response array for corresponding id into constructing response
final JsonElement jsonBodyElement = jsonresponse.get("body");
if (jsonBodyElement != null) {
if (jsonBodyElement != null && jsonBodyElement.isJsonObject()) {
final JsonObject JsonObject = jsonBodyElement.getAsJsonObject();
final String bodyAsString = JsonObject.toString();
final ResponseBody responseBody = ResponseBody
Expand All @@ -80,11 +82,11 @@ public Response getResponseById(final String requestId) {
// Put headers from response array for corresponding id into constructing
// response
final JsonElement jsonheadersElement = jsonresponse.get("headers");
if (jsonheadersElement != null) {
if (jsonheadersElement != null && jsonheadersElement.isJsonObject()) {
final JsonObject jsonheaders = jsonheadersElement.getAsJsonObject();
for (final String key : jsonheaders.keySet()) {
final JsonElement strValueElement = jsonheaders.get(key);
if (strValueElement != null) {
if (strValueElement != null && strValueElement.isJsonPrimitive()) {
final String strvalue = strValueElement.getAsString();
for (final String value : strvalue.split(";")) {
builder.header(key, value);
Expand All @@ -107,7 +109,7 @@ public Response getResponseById(final String requestId) {
public Map<String, Response> getResponses() {
if (batchResponseArray == null)
return null;
final Map<String, Response> responsesMap = new HashMap<>();
final Map<String, Response> responsesMap = new LinkedHashMap<>();
for (final String id : batchRequestsHashMap.keySet()) {
responsesMap.put(id, getResponseById(id));
}
Expand All @@ -130,7 +132,7 @@ public void update(final Response batchResponse) {

final Map<String, Request> requestMap = createBatchRequestsHashMap(batchResponse);
if (batchRequestsHashMap == null)
batchRequestsHashMap = new HashMap<>();
batchRequestsHashMap = new LinkedHashMap<>();
if (requestMap != null)
batchRequestsHashMap.putAll(requestMap);

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

final JsonElement nextLinkElement = batchResponseObj.get("@odata.nextLink");
if (nextLinkElement != null)
if (nextLinkElement != null && nextLinkElement.isJsonPrimitive())
nextLink = nextLinkElement.getAsString();

if (batchResponseArray == null)
batchResponseArray = new JsonArray();

final JsonElement responseArrayElement = batchResponseObj.get("responses");
if (responseArrayElement != null) {
if (responseArrayElement != null && responseArrayElement.isJsonArray()) {
final JsonArray responseArray = responseArrayElement.getAsJsonArray();
batchResponseArray.addAll(responseArray);
}
Expand All @@ -172,29 +174,31 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
if (batchResponse == null)
return null;
try {
final Map<String, Request> batchRequestsHashMap = new HashMap<>();
final Map<String, Request> batchRequestsHashMap = new LinkedHashMap<>();
final JsonObject requestJSONObject = requestBodyToJSONObject(batchResponse.request());
final JsonElement requestArrayElement = requestJSONObject.get("requests");
if (requestArrayElement != null) {
if (requestArrayElement != null && requestArrayElement.isJsonArray()) {
final JsonArray requestArray = requestArrayElement.getAsJsonArray();
for (final JsonElement item : requestArray) {
if(!item.isJsonObject())
continue;
final JsonObject requestObject = item.getAsJsonObject();

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

final JsonElement urlElement = requestObject.get("url");
if (urlElement != null) {
if (urlElement != null && urlElement.isJsonPrimitive()) {
final StringBuilder fullUrl = new StringBuilder(
batchResponse.request().url().toString().replace("$batch", ""));
fullUrl.append(urlElement.getAsString());
builder.url(fullUrl.toString());
}
final JsonElement jsonHeadersElement = requestObject.get("headers");
if (jsonHeadersElement != null) {
if (jsonHeadersElement != null && jsonHeadersElement.isJsonObject()) {
final JsonObject jsonheaders = jsonHeadersElement.getAsJsonObject();
for (final String key : jsonheaders.keySet()) {
final JsonElement strvalueElement = jsonheaders.get(key);
if (strvalueElement != null) {
if (strvalueElement != null && strvalueElement.isJsonPrimitive()) {
final String strvalue = strvalueElement.getAsString();
for (final String value : strvalue.split("; ")) {
builder.header(key, value);
Expand All @@ -204,7 +208,8 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
}
final JsonElement jsonBodyElement = requestObject.get("body");
final JsonElement jsonMethodElement = requestObject.get("method");
if (jsonBodyElement != null && jsonMethodElement != null) {
if (jsonBodyElement != null && jsonMethodElement != null
&& jsonBodyElement.isJsonObject() && jsonMethodElement.isJsonPrimitive()) {
final JsonObject JsonObject = jsonBodyElement.getAsJsonObject();
final String bodyAsString = JsonObject.toString();
final RequestBody requestBody = RequestBody
Expand All @@ -214,7 +219,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
builder.method(jsonMethodElement.getAsString(), null);
}
final JsonElement jsonIdElement = requestObject.get("id");
if (jsonIdElement != null) {
if (jsonIdElement != null && jsonIdElement.isJsonPrimitive()) {
batchRequestsHashMap.put(jsonIdElement.getAsString(), builder.build());
}
}
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/microsoft/graph/httpcore/ChaosHttpHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.microsoft.graph.httpcore;

import java.io.IOException;
import java.util.concurrent.ThreadLocalRandom;

import com.microsoft.graph.httpcore.middlewareoption.MiddlewareType;
import com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
* DO NOT USE IN PRODUCTION
* interceptor that randomly fails the responses for unit testing purposes
*/
public class ChaosHttpHandler implements Interceptor {
public final MiddlewareType MIDDLEWARE_TYPE = MiddlewareType.RETRY;
/*
* constant string being used
*/
private final String RETRY_AFTER = "Retry-After";
/**
* Denominator for the failure rate (i.e. 1/X)
*/
private final Integer failureRate = 3;
/**
* default value to return on retry after
*/
private final String retryAfterValue = "10";
/**
* body to respond on failed requests
*/
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.\"}}";
public static final int MSClientErrorCodeTooManyRequests = 429;

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();

if(request.tag(TelemetryOptions.class) == null)
request = request.newBuilder().tag(TelemetryOptions.class, new TelemetryOptions()).build();
request.tag(TelemetryOptions.class).setFeatureUsage(TelemetryOptions.RETRY_HANDLER_ENABLED_FLAG);

final Integer dice = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);

if(dice % failureRate == 0) {
return new Response
.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(MSClientErrorCodeTooManyRequests)
.message("Too Many Requests")
.addHeader(RETRY_AFTER, retryAfterValue)
.body(ResponseBody.create(MediaType.get("application/json"), responseBody))
.build();
} else {
return chain.proceed(request);
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/microsoft/graph/httpcore/RetryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public Response intercept(Chain chain) throws IOException {
while(retryRequest(response, executionCount, request, retryOption)) {
request = request.newBuilder().addHeader(RETRY_ATTEMPT_HEADER, String.valueOf(executionCount)).build();
executionCount++;
if(response != null && response.body() != null) {
response.body().close();
}
response = chain.proceed(request);
}
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class TelemetryHandler implements Interceptor{

public static final String SDK_VERSION = "SdkVersion";
public static final String VERSION = "v1.0.3";
public static final String VERSION = "v1.0.4";
public static final String GRAPH_VERSION_PREFIX = "graph-java-core";
public static final String JAVA_VERSION_PREFIX = "java";
public static final String CLIENT_REQUEST_ID = "client-request-id";
Expand Down

0 comments on commit 0b78779

Please sign in to comment.