Skip to content

Commit 339089f

Browse files
author
Nakul Sabharwal
committed
Added comments and removed suppressed warning
1 parent c6f7886 commit 339089f

File tree

9 files changed

+143
-141
lines changed

9 files changed

+143
-141
lines changed

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

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@
77

88
import org.json.simple.JSONArray;
99
import org.json.simple.JSONObject;
10+
import org.json.simple.parser.JSONParser;
1011
import org.json.simple.parser.ParseException;
1112

12-
import com.microsoft.graph.httpcore.RequestSerializer;
13-
1413
import okhttp3.Headers;
14+
import okhttp3.Request;
1515
import okhttp3.RequestBody;
16+
import okio.Buffer;
1617

1718
public class MSBatchRequestContent {
1819
private Map<String, MSBatchRequestStep> batchRequestStepsHashMap;
20+
21+
// Maximum number of requests that can be sent in a batch
1922
public static final int MAX_NUMBER_OF_REQUESTS = 20;
2023

24+
/*
25+
* Creates Batch request content using list provided
26+
*
27+
* @param batchRequestStepsArray List of batch steps for batching
28+
*/
2129
public MSBatchRequestContent(List<MSBatchRequestStep> batchRequestStepsArray) {
2230
if(batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS)
2331
throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS);
@@ -27,31 +35,43 @@ public MSBatchRequestContent(List<MSBatchRequestStep> batchRequestStepsArray) {
2735
addBatchRequestStep(requestStep);
2836
}
2937

38+
/*
39+
* Creates empty batch request content
40+
*/
3041
public MSBatchRequestContent() {
3142
batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>();
3243
}
3344

45+
/*
46+
* @param batchRequestStep Batch request step adding to batch content
47+
* @return true or false based on addition or no addition of batch request step given
48+
*/
3449
public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) {
3550
if(batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()))
3651
return false;
3752
batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep);
3853
return true;
3954
}
4055

56+
/*
57+
* @param requestId Id of Batch request step to be removed
58+
* @return true or false based on removal or no removal of batch request step with given id
59+
*/
4160
public boolean removeBatchRequestStepWithId(String requestId) {
4261
boolean removed = false;
4362
if(batchRequestStepsHashMap.containsKey(requestId)) {
4463
batchRequestStepsHashMap.remove(requestId);
4564
removed = true;
46-
}
47-
for(Map.Entry<String, MSBatchRequestStep> steps : batchRequestStepsHashMap.entrySet()) {
48-
while(steps.getValue().getArrayOfDependsOnIds().remove(requestId))
49-
removed = true;
65+
for(Map.Entry<String, MSBatchRequestStep> steps : batchRequestStepsHashMap.entrySet()) {
66+
while(steps.getValue().getArrayOfDependsOnIds().remove(requestId));
67+
}
5068
}
5169
return removed;
5270
}
5371

54-
@SuppressWarnings("unchecked")
72+
/*
73+
* @return Batch request content's json as String
74+
*/
5575
public String getBatchRequestContent() {
5676
JSONObject batchRequestContentMap = new JSONObject();
5777
JSONArray batchContentArray = new JSONArray();
@@ -60,11 +80,10 @@ public String getBatchRequestContent() {
6080
}
6181
batchRequestContentMap.put("requests", batchContentArray);
6282

63-
String content = batchRequestContentMap.toString();
83+
String content = batchRequestContentMap.toString();
6484
return content;
6585
}
6686

67-
@SuppressWarnings("unchecked")
6887
private JSONObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep){
6988
JSONObject contentmap = new JSONObject();
7089
contentmap.put("id", batchRequestStep.getRequestId());
@@ -82,7 +101,7 @@ private JSONObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep
82101
if(headers != null && headers.size() != 0) {
83102
JSONObject headerMap = new JSONObject();
84103
for(Map.Entry<String, List<String>> entry : headers.toMultimap().entrySet()) {
85-
headerMap.put(entry.getKey(), RequestSerializer.getHeaderValuesAsString(entry.getValue()));
104+
headerMap.put(entry.getKey(), getHeaderValuesAsString(entry.getValue()));
86105
}
87106
contentmap.put("headers", headerMap);
88107
}
@@ -97,12 +116,32 @@ private JSONObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep
97116
RequestBody body = batchRequestStep.getRequest().body();
98117
if(body != null) {
99118
try {
100-
contentmap.put("body", RequestSerializer.requestBodyToJSONObject(batchRequestStep.getRequest()));
119+
contentmap.put("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
101120
}catch(IOException | ParseException e) {
102121
e.printStackTrace();
103122
}
104123
}
105124
return contentmap;
106125
}
107126

127+
private String getHeaderValuesAsString(final List<String> list) {
128+
if(list == null || list.size() == 0)return "";
129+
StringBuilder builder = new StringBuilder(list.get(0));
130+
for(int i=1;i<list.size();i++) {
131+
builder.append(";");
132+
builder.append(list.get(i));
133+
}
134+
return builder.toString();
135+
}
136+
137+
private JSONObject requestBodyToJSONObject(final Request request) throws IOException, ParseException{
138+
if(request == null || request.body() == null)return null;
139+
Request copy = request.newBuilder().build();
140+
Buffer buffer = new Buffer();
141+
copy.body().writeTo(buffer);
142+
String requestBody = buffer.readUtf8();
143+
JSONObject jsonObject = (JSONObject)new JSONParser().parse(requestBody);
144+
return jsonObject;
145+
}
146+
108147
}

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

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.microsoft.graph.content;
22

33
import java.io.IOException;
4+
import java.util.HashMap;
45
import java.util.Map;
56

67
import org.json.simple.JSONArray;
78
import org.json.simple.JSONObject;
8-
9-
import com.microsoft.graph.httpcore.RequestSerializer;
9+
import org.json.simple.parser.JSONParser;
10+
import org.json.simple.parser.ParseException;
1011

1112
import okhttp3.MediaType;
1213
import okhttp3.Request;
14+
import okhttp3.RequestBody;
1315
import okhttp3.Response;
1416
import okhttp3.ResponseBody;
17+
import okio.Buffer;
1518

1619
public class MSBatchResponseContent {
1720

@@ -20,17 +23,20 @@ public class MSBatchResponseContent {
2023
private Map<String, Request> batchRequestsHashMap;
2124
private JSONArray batchResponseArray;
2225

26+
/*
27+
* @param batchResponse OkHttp batch response on execution of batch requests
28+
*/
2329
public MSBatchResponseContent(Response batchResponse) {
2430
if(batchResponse == null)
2531
throw new IllegalArgumentException("Batch Response cannot be null");
2632

27-
this.batchRequestsHashMap = RequestSerializer.createBatchRequestsHashMap(batchResponse);
33+
this.batchRequestsHashMap = createBatchRequestsHashMap(batchResponse);
2834
this.batchResponse = batchResponse;
2935
if(batchResponse.body() != null) {
3036
try {
3137
String batchResponseData = batchResponse.body().string();
3238
if(batchResponseData != null) {
33-
batchResponseObj = RequestSerializer.stringToJSONObject(batchResponseData);
39+
batchResponseObj = stringToJSONObject(batchResponseData);
3440
if(batchResponseObj != null) {
3541
batchResponseArray = (JSONArray)batchResponseObj.get("responses");
3642
}
@@ -41,6 +47,12 @@ public MSBatchResponseContent(Response batchResponse) {
4147
}
4248
}
4349

50+
/*
51+
* Returns OkHttp Response of given request Id
52+
*
53+
* @param requestId Request Id of batch step
54+
* @return OkHttp Response corresponding to requestId
55+
*/
4456
public Response getResponseById(String requestId) {
4557
if(batchResponseObj == null)
4658
return null;
@@ -107,4 +119,70 @@ public String nextLink() {
107119
Object nextLinkObject = batchResponseObj.get("nextLink");
108120
return nextLinkObject != null ? ((JSONObject)nextLinkObject).toString() : null;
109121
}
122+
123+
private Map<String, Request> createBatchRequestsHashMap(Response batchResponse) {
124+
if(batchResponse == null)return null;
125+
try {
126+
Map<String, Request> batchRequestsHashMap = new HashMap<>();
127+
JSONObject requestJSONObject = requestBodyToJSONObject(batchResponse.request());
128+
JSONArray requestArray = (JSONArray)requestJSONObject.get("requests");
129+
for(Object item : requestArray) {
130+
JSONObject requestObject = (JSONObject)item;
131+
132+
Request.Builder builder = new Request.Builder();
133+
134+
if(requestObject.get("url") != null) {
135+
StringBuilder fullUrl = new StringBuilder(batchResponse.request().url().toString().replace("$batch",""));
136+
fullUrl.append(requestObject.get("url").toString());
137+
builder.url(fullUrl.toString());
138+
}
139+
if(requestObject.get("headers") != null) {
140+
JSONObject jsonheaders = (JSONObject)requestObject.get("headers");
141+
for(Object key: jsonheaders.keySet()) {
142+
String strkey = (String)key;
143+
String strvalue = (String)jsonheaders.get(strkey);
144+
for(String value : strvalue.split("; ")) {
145+
builder.header(strkey, value);
146+
}
147+
}
148+
}
149+
if(requestObject.get("body") != null) {
150+
JSONObject jsonObject = (JSONObject)requestObject.get("body");
151+
String bodyAsString = jsonObject.toJSONString();
152+
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), bodyAsString);
153+
builder.method(requestObject.get("method").toString(), requestBody);
154+
} else {
155+
builder.method(requestObject.get("method").toString(), null);
156+
}
157+
batchRequestsHashMap.put(requestObject.get("id").toString(), builder.build());
158+
}
159+
return batchRequestsHashMap;
160+
161+
} catch (IOException | ParseException e) { e.printStackTrace(); }
162+
return null;
163+
}
164+
165+
private JSONObject stringToJSONObject(String input) {
166+
JSONParser parser = new JSONParser();
167+
JSONObject jsonObject = null;
168+
try {
169+
if(input != null) {
170+
jsonObject = (JSONObject) parser.parse(input);
171+
}
172+
}
173+
catch(Exception e) {
174+
e.printStackTrace();
175+
}
176+
return jsonObject;
177+
}
178+
179+
private JSONObject requestBodyToJSONObject(final Request request) throws IOException, ParseException{
180+
if(request == null || request.body() == null)return null;
181+
Request copy = request.newBuilder().build();
182+
Buffer buffer = new Buffer();
183+
copy.body().writeTo(buffer);
184+
String requestBody = buffer.readUtf8();
185+
JSONObject jsonObject = (JSONObject)new JSONParser().parse(requestBody);
186+
return jsonObject;
187+
}
110188
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ private HttpClients() {
1111
/**
1212
* Creates builder object for construction of custom
1313
* {@link OkHttpClient} instances.
14+
*
15+
* @return OkHttpClient.Builder() custom builder for developer to add its own interceptors to it
1416
*/
1517
public static Builder custom() {
1618
return new OkHttpClient.Builder();
@@ -19,6 +21,9 @@ public static Builder custom() {
1921
/**
2022
* Creates {@link OkHttpClient} instance with default
2123
* configuration and provided authProvider
24+
*
25+
* @param auth Use IAuthenticationProvider instance provided while constructing http client
26+
* @return OkHttpClient build with authentication provider given, default redirect and default retry handlers
2227
*/
2328
public static OkHttpClient createDefault(IAuthenticationProvider auth) {
2429
return new OkHttpClient.Builder().addInterceptor(new AuthenticationHandler(auth))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public interface IAuthenticationProvider {
77
* Authenticates the request
88
*
99
* @param request the request to authenticate
10+
* @return Request with Authorization header added to it
1011
*/
1112
Request authenticateRequest(Request request);
1213
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public RedirectHandler(RedirectOptions redirectOptions) {
4040
}
4141
}
4242

43-
public boolean isRedirected(Request request, Response response, int redirectCount, RedirectOptions redirectOptions) throws IOException {
43+
boolean isRedirected(Request request, Response response, int redirectCount, RedirectOptions redirectOptions) throws IOException {
4444
// Check max count of redirects reached
4545
if(redirectCount > redirectOptions.maxRedirects()) return false;
4646

@@ -61,7 +61,7 @@ public boolean isRedirected(Request request, Response response, int redirectCoun
6161
return false;
6262
}
6363

64-
public Request getRedirect(
64+
Request getRedirect(
6565
final Request request,
6666
final Response userResponse) throws ProtocolException {
6767
String location = userResponse.header("Location");

0 commit comments

Comments
 (0)