Skip to content

Commit 7e6125a

Browse files
Merge pull request #11 from microsoftgraph/okhttp
Core migration to OkHttp
2 parents 81d4291 + e731df3 commit 7e6125a

22 files changed

+1014
-653
lines changed

build.gradle

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ repositories {
2323
mavenCentral()
2424
}
2525

26-
def platformDependency = "org.apache.httpcomponents:httpclient:4.5.6"
27-
if ( project.hasProperty("platform") && project.platform == "android" ) {
28-
platformDependency = "org.apache.httpcomponents:httpclient-android:4.3.5"
29-
}
30-
3126
dependencies {
3227
// This dependency is exported to consumers, that is to say found on their compile classpath.
3328
api 'org.apache.commons:commons-math3:3.6.1'
@@ -38,7 +33,7 @@ dependencies {
3833
// Use JUnit test framework
3934
testImplementation 'junit:junit:4.12'
4035

41-
api platformDependency
36+
api 'com.squareup.okhttp3:okhttp:3.12.1'
4237

4338
// https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
4439
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
Lines changed: 112 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,149 @@
11
package com.microsoft.graph.content;
22

3-
import java.util.ArrayList;
3+
import java.io.IOException;
44
import java.util.HashMap;
55
import java.util.List;
66
import java.util.Map;
77

8-
import org.apache.http.Header;
9-
import org.apache.http.HttpEntity;
10-
import org.apache.http.HttpRequest;
11-
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
12-
import org.apache.http.util.EntityUtils;
8+
import org.json.simple.JSONArray;
139
import org.json.simple.JSONObject;
14-
import org.json.simple.JSONValue;
10+
import org.json.simple.parser.JSONParser;
11+
import org.json.simple.parser.ParseException;
12+
13+
import okhttp3.Headers;
14+
import okhttp3.Request;
15+
import okhttp3.RequestBody;
16+
import okio.Buffer;
1517

1618
public class MSBatchRequestContent {
17-
private List<MSBatchRequestStep> batchRequestStepsArray;
18-
private final int maxNumberOfRequests = 20;
19+
private Map<String, MSBatchRequestStep> batchRequestStepsHashMap;
20+
21+
// Maximum number of requests that can be sent in a batch
22+
public static final int MAX_NUMBER_OF_REQUESTS = 20;
1923

24+
/*
25+
* Creates Batch request content using list provided
26+
*
27+
* @param batchRequestStepsArray List of batch steps for batching
28+
*/
2029
public MSBatchRequestContent(List<MSBatchRequestStep> batchRequestStepsArray) {
21-
this.batchRequestStepsArray = new ArrayList<>();
22-
if(batchRequestStepsArray.size() <= maxNumberOfRequests) {
23-
for(MSBatchRequestStep requestStep: batchRequestStepsArray)
24-
addBatchRequestStep(requestStep);
25-
}
30+
if(batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS)
31+
throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS);
32+
33+
this.batchRequestStepsHashMap = new HashMap<>();
34+
for(MSBatchRequestStep requestStep: batchRequestStepsArray)
35+
addBatchRequestStep(requestStep);
2636
}
2737

38+
/*
39+
* Creates empty batch request content
40+
*/
2841
public MSBatchRequestContent() {
29-
batchRequestStepsArray = new ArrayList<>();
42+
batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>();
3043
}
3144

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+
*/
3249
public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) {
33-
if(batchRequestStep.getRequestId().compareTo("") == 0)
50+
if(batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()))
3451
return false;
35-
if(batchRequestStepsArray.size() == maxNumberOfRequests)
36-
return false;
37-
for(MSBatchRequestStep requestStep: batchRequestStepsArray) {
38-
if(batchRequestStep.getRequestId().compareTo(requestStep.getRequestId()) == 0)
39-
return false;
40-
}
41-
return batchRequestStepsArray.add(batchRequestStep);
52+
batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep);
53+
return true;
4254
}
4355

44-
public boolean removeBatchRequesStepWithId(String requestId) {
45-
boolean ret = false;
46-
for (int i = batchRequestStepsArray.size()-1; i >= 0; i--)
47-
{
48-
MSBatchRequestStep requestStep = batchRequestStepsArray.get(i);
49-
for (int j = requestStep.getArrayOfDependsOnIds().size() - 1; j >= 0; j--)
50-
{
51-
String dependsOnId = requestStep.getArrayOfDependsOnIds().get(j);
52-
if(dependsOnId.compareTo(requestId) == 0)
53-
{
54-
requestStep.getArrayOfDependsOnIds().remove(j);
55-
ret = true;
56-
}
57-
}
58-
if(requestId.compareTo(requestStep.getRequestId()) == 0) {
59-
batchRequestStepsArray.remove(i);
60-
ret = true;
61-
}
62-
}
63-
return ret;
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+
*/
60+
public boolean removeBatchRequestStepWithId(String requestId) {
61+
boolean removed = false;
62+
if(batchRequestStepsHashMap.containsKey(requestId)) {
63+
batchRequestStepsHashMap.remove(requestId);
64+
removed = true;
65+
for(Map.Entry<String, MSBatchRequestStep> steps : batchRequestStepsHashMap.entrySet()) {
66+
if(steps.getValue() != null && steps.getValue().getArrayOfDependsOnIds() != null) {
67+
while(steps.getValue().getArrayOfDependsOnIds().remove(requestId));
68+
}
69+
}
70+
}
71+
return removed;
6472
}
6573

74+
/*
75+
* @return Batch request content's json as String
76+
*/
6677
public String getBatchRequestContent() {
67-
Map<String, List<Map<String, String>>> batchRequestContentMap = new HashMap<>();
68-
List<Map<String, String>> batchContentArray = new ArrayList<>();
69-
for(MSBatchRequestStep requestStep : batchRequestStepsArray) {
70-
batchContentArray.add(getBatchRequestMapFromRequestStep(requestStep));
78+
JSONObject batchRequestContentMap = new JSONObject();
79+
JSONArray batchContentArray = new JSONArray();
80+
for(Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) {
81+
batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue()));
7182
}
7283
batchRequestContentMap.put("requests", batchContentArray);
73-
return JSONValue.toJSONString(batchRequestContentMap);
84+
85+
String content = batchRequestContentMap.toString();
86+
return content;
7487
}
7588

76-
private Map<String, String> getBatchRequestMapFromRequestStep(MSBatchRequestStep batchRequestStep){
77-
Map<String, String> contentmap = new HashMap<>();
89+
private JSONObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep){
90+
JSONObject contentmap = new JSONObject();
7891
contentmap.put("id", batchRequestStep.getRequestId());
79-
contentmap.put("url", batchRequestStep.getRequest().getRequestLine().getUri());
80-
contentmap.put("method", batchRequestStep.getRequest().getRequestLine().getMethod());
81-
Header[] headers = batchRequestStep.getRequest().getAllHeaders();
82-
if(headers != null && headers.length != 0) {
83-
JSONObject obj = new JSONObject();
84-
for(Header header: headers) {
85-
obj.put(header.getName(), header.getValue());
86-
}
87-
contentmap.put("headers", obj.toJSONString());
88-
}
89-
HttpEntity entity = null;
90-
HttpRequest request = batchRequestStep.getRequest();
91-
if(request instanceof HttpEntityEnclosingRequestBase) {
92-
HttpEntityEnclosingRequestBase httprequest = (HttpEntityEnclosingRequestBase)request;
93-
entity = httprequest.getEntity();
94-
}
95-
if(entity != null) {
96-
try {
97-
String body = EntityUtils.toString(entity);
98-
contentmap.put("body", body);
99-
}
100-
catch(Exception e) {
101-
e.printStackTrace();
92+
93+
String url = batchRequestStep.getRequest().url().toString();
94+
url = url.replaceAll("https://graph.microsoft.com/v1.0/", "");
95+
url = url.replaceAll("http://graph.microsoft.com/v1.0/", "");
96+
url = url.replaceAll("https://graph.microsoft.com/beta/", "");
97+
url = url.replaceAll("http://graph.microsoft.com/beta/", "");
98+
contentmap.put("url", url);
99+
100+
contentmap.put("method", batchRequestStep.getRequest().method().toString());
101+
102+
Headers headers = batchRequestStep.getRequest().headers();
103+
if(headers != null && headers.size() != 0) {
104+
JSONObject headerMap = new JSONObject();
105+
for(Map.Entry<String, List<String>> entry : headers.toMultimap().entrySet()) {
106+
headerMap.put(entry.getKey(), getHeaderValuesAsString(entry.getValue()));
102107
}
108+
contentmap.put("headers", headerMap);
103109
}
110+
104111
List<String> arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds();
105112
if(arrayOfDependsOnIds != null) {
106-
contentmap.put("dependsOn", JSONValue.toJSONString(arrayOfDependsOnIds));
113+
JSONArray array = new JSONArray();
114+
for(String dependsOnId : arrayOfDependsOnIds) array.add(dependsOnId);
115+
contentmap.put("dependsOn", array);
107116
}
108117

118+
RequestBody body = batchRequestStep.getRequest().body();
119+
if(body != null) {
120+
try {
121+
contentmap.put("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
122+
}catch(IOException | ParseException e) {
123+
e.printStackTrace();
124+
}
125+
}
109126
return contentmap;
110127
}
111-
128+
129+
private String getHeaderValuesAsString(final List<String> list) {
130+
if(list == null || list.size() == 0)return "";
131+
StringBuilder builder = new StringBuilder(list.get(0));
132+
for(int i=1;i<list.size();i++) {
133+
builder.append(";");
134+
builder.append(list.get(i));
135+
}
136+
return builder.toString();
137+
}
138+
139+
private JSONObject requestBodyToJSONObject(final Request request) throws IOException, ParseException{
140+
if(request == null || request.body() == null)return null;
141+
Request copy = request.newBuilder().build();
142+
Buffer buffer = new Buffer();
143+
copy.body().writeTo(buffer);
144+
String requestBody = buffer.readUtf8();
145+
JSONObject jsonObject = (JSONObject)new JSONParser().parse(requestBody);
146+
return jsonObject;
147+
}
148+
112149
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
import java.util.List;
44

5-
import org.apache.http.HttpRequest;
5+
import okhttp3.Request;
66

77
public class MSBatchRequestStep {
88
private String requestId;
9-
private HttpRequest request;
9+
private Request request;
1010
private List<String> arrayOfDependsOnIds;
1111

12-
public MSBatchRequestStep(String requestId, HttpRequest request, List<String> arrayOfDependsOnIds) {
12+
public MSBatchRequestStep(String requestId, Request request, List<String> arrayOfDependsOnIds) {
13+
if(requestId == null)
14+
throw new IllegalArgumentException("Request Id cannot be null.");
15+
if(request == null)
16+
new IllegalArgumentException("Request cannot be null.");
17+
1318
this.requestId = requestId;
1419
this.request = request;
1520
this.arrayOfDependsOnIds = arrayOfDependsOnIds;
@@ -19,7 +24,7 @@ public String getRequestId() {
1924
return requestId;
2025
}
2126

22-
public HttpRequest getRequest() {
27+
public Request getRequest() {
2328
return request;
2429
}
2530

0 commit comments

Comments
 (0)