Skip to content

Commit c6c3c2a

Browse files
authored
Merge pull request #68 from microsoftgraph/release/1.0.3
Release/1.0.3
2 parents 7fe8640 + e5a3aef commit c6c3c2a

File tree

9 files changed

+278
-225
lines changed

9 files changed

+278
-225
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "automatic"
3+
}

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ dependencies {
3030

3131
api 'com.squareup.okhttp3:okhttp:3.12.1'
3232

33-
// https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
34-
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
33+
implementation 'com.google.code.gson:gson:2.8.6'
3534
}
3635

3736
def pomConfig = {

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 = 2
28+
mavenPatchVersion = 3
2929
mavenArtifactSuffix =
3030
nightliesUrl = http://dl.bintray.com/MicrosoftGraph/Maven
3131

readme.md

Lines changed: 3 additions & 10 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.2'
23+
implementation 'com.microsoft.graph:microsoft-graph-core:1.0.3'
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.2</version>
35+
<version>1.0.3</version>
3636
</dependency>
3737
```
3838

@@ -118,15 +118,8 @@ For known issues, see [issues](https://github.com/MicrosoftGraph/msgraph-sdk-jav
118118

119119
The Microsoft Graph SDK is open for contribution. To contribute to this project, see [Contributing](https://github.com/microsoftgraph/msgraph-sdk-java-core/blob/master/CONTRIBUTING.md).
120120

121-
<!-- ALL-CONTRIBUTORS-LIST:START -->
122-
<!-- prettier-ignore -->
123-
| [<img src="https://avatars2.githubusercontent.com/u/3197588?v=4" width="100px;"/><br /><sub><b>Deepak Agrawal</b></sub>](https://github.com/deepak2016)<br />[:computer:](https://github.com/microsoftgraph/msgraph-sdk-java-core/commits?author=deepak2016 "Code") | [<img src="https://avatars3.githubusercontent.com/u/16473684?v=4" width="100px;"/><br /><sub><b>Nakul Sabharwal</b></sub>](https://github.com/NakulSabharwal)<br/>[:computer:](https://github.com/microsoftgraph/msgraph-sdk-java-core/commits?author=NakulSabharwal "Code")[:clipboard:](https://github.com/microsoftgraph/msgraph-sdk-java-core/commits?author=NakulSabharwal "Reviewed Pull Requests")|
124-
| :---: | :---: |
125-
<!-- ALL-CONTRIBUTORS-LIST:END -->
126-
127-
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind are welcome!
128-
129121
## 6. Supported Java versions
122+
130123
The Microsoft Graph SDK for Java library is supported at runtime for Java 7+ and [Android API revision 15](http://source.android.com/source/build-numbers.html) and greater.
131124

132125
## 7. License
Lines changed: 101 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,176 @@
11
package com.microsoft.graph.content;
22

33
import java.io.IOException;
4+
import java.util.Arrays;
45
import java.util.HashMap;
56
import java.util.List;
67
import java.util.Map;
8+
import java.util.concurrent.ThreadLocalRandom;
79

8-
import org.json.simple.JSONArray;
9-
import org.json.simple.JSONObject;
10-
import org.json.simple.parser.JSONParser;
11-
import org.json.simple.parser.ParseException;
10+
import com.google.gson.JsonArray;
11+
import com.google.gson.JsonObject;
12+
import com.google.gson.JsonParser;
13+
import com.google.gson.JsonPrimitive;
14+
import com.google.gson.JsonParseException;
1215

1316
import okhttp3.Headers;
1417
import okhttp3.Request;
1518
import okhttp3.RequestBody;
1619
import okio.Buffer;
1720

1821
public class MSBatchRequestContent {
19-
private Map<String, MSBatchRequestStep> batchRequestStepsHashMap;
20-
22+
private final Map<String, MSBatchRequestStep> batchRequestStepsHashMap;
23+
2124
// Maximum number of requests that can be sent in a batch
2225
public static final int MAX_NUMBER_OF_REQUESTS = 20;
23-
26+
2427
/*
2528
* Creates Batch request content using list provided
2629
*
2730
* @param batchRequestStepsArray List of batch steps for batching
2831
*/
29-
public MSBatchRequestContent(List<MSBatchRequestStep> batchRequestStepsArray) {
30-
if(batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS)
32+
public MSBatchRequestContent(final List<MSBatchRequestStep> batchRequestStepsArray) {
33+
if (batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS)
3134
throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS);
32-
35+
3336
this.batchRequestStepsHashMap = new HashMap<>();
34-
for(MSBatchRequestStep requestStep: batchRequestStepsArray)
37+
for (final MSBatchRequestStep requestStep : batchRequestStepsArray)
3538
addBatchRequestStep(requestStep);
3639
}
37-
40+
3841
/*
3942
* Creates empty batch request content
4043
*/
4144
public MSBatchRequestContent() {
4245
batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>();
4346
}
44-
47+
4548
/*
4649
* @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
50+
*
51+
* @return true or false based on addition or no addition of batch request step
52+
* given
4853
*/
49-
public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) {
50-
if(batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()))
54+
public boolean addBatchRequestStep(final MSBatchRequestStep batchRequestStep) {
55+
if (batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()) ||
56+
batchRequestStepsHashMap.size() >= MAX_NUMBER_OF_REQUESTS)
5157
return false;
5258
batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep);
5359
return true;
5460
}
55-
61+
62+
/**
63+
* Add steps to batch from OkHttp.Request
64+
* @param request the request to add to the batch
65+
* @param arrayOfDependsOnIds ids of steps this step depends on
66+
* @return the step id
67+
*/
68+
public String addBatchRequestStep(final Request request, final String... arrayOfDependsOnIds) {
69+
String requestId;
70+
do {
71+
requestId = Integer.toString(ThreadLocalRandom.current().nextInt());
72+
} while(batchRequestStepsHashMap.keySet().contains(requestId));
73+
if(addBatchRequestStep(new MSBatchRequestStep(requestId, request, Arrays.asList(arrayOfDependsOnIds))))
74+
return requestId;
75+
else
76+
throw new IllegalArgumentException("unable to add step to batch. Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS);
77+
}
78+
5679
/*
5780
* @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
81+
*
82+
* @return true or false based on removal or no removal of batch request step
83+
* with given id
5984
*/
60-
public boolean removeBatchRequestStepWithId(String requestId) {
85+
public boolean removeBatchRequestStepWithId(final String requestId) {
6186
boolean removed = false;
62-
if(batchRequestStepsHashMap.containsKey(requestId)) {
87+
if (batchRequestStepsHashMap.containsKey(requestId)) {
6388
batchRequestStepsHashMap.remove(requestId);
6489
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));
90+
for (final Map.Entry<String, MSBatchRequestStep> steps : batchRequestStepsHashMap.entrySet()) {
91+
if (steps.getValue() != null && steps.getValue().getArrayOfDependsOnIds() != null) {
92+
while (steps.getValue().getArrayOfDependsOnIds().remove(requestId))
93+
;
6894
}
6995
}
7096
}
7197
return removed;
7298
}
73-
99+
74100
/*
75101
* @return Batch request content's json as String
76102
*/
77103
public String getBatchRequestContent() {
78-
JSONObject batchRequestContentMap = new JSONObject();
79-
JSONArray batchContentArray = new JSONArray();
80-
for(Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) {
104+
final JsonObject batchRequestContentMap = new JsonObject();
105+
final JsonArray batchContentArray = new JsonArray();
106+
for (final Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) {
81107
batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue()));
82108
}
83-
batchRequestContentMap.put("requests", batchContentArray);
84-
85-
String content = batchRequestContentMap.toString();
109+
batchRequestContentMap.add("requests", batchContentArray);
110+
111+
final String content = batchRequestContentMap.toString();
86112
return content;
87113
}
88-
89-
private JSONObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep){
90-
JSONObject contentmap = new JSONObject();
91-
contentmap.put("id", batchRequestStep.getRequestId());
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()));
114+
115+
private JsonObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep) {
116+
final JsonObject contentmap = new JsonObject();
117+
contentmap.add("id", new JsonPrimitive(batchRequestStep.getRequestId()));
118+
119+
final String url = batchRequestStep.getRequest().url().toString()
120+
.replaceAll("https://graph.microsoft.com/v1.0/", "").replaceAll("http://graph.microsoft.com/v1.0/", "")
121+
.replaceAll("https://graph.microsoft.com/beta/", "").replaceAll("http://graph.microsoft.com/beta/", "");
122+
contentmap.add("url", new JsonPrimitive(url));
123+
124+
contentmap.add("method", new JsonPrimitive(batchRequestStep.getRequest().method().toString()));
125+
126+
final Headers headers = batchRequestStep.getRequest().headers();
127+
if (headers != null && headers.size() != 0) {
128+
final JsonObject headerMap = new JsonObject();
129+
for (final Map.Entry<String, List<String>> entry : headers.toMultimap().entrySet()) {
130+
headerMap.add(entry.getKey(), new JsonPrimitive(getHeaderValuesAsString(entry.getValue())));
107131
}
108-
contentmap.put("headers", headerMap);
132+
contentmap.add("headers", headerMap);
109133
}
110-
111-
List<String> arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds();
112-
if(arrayOfDependsOnIds != null) {
113-
JSONArray array = new JSONArray();
114-
for(String dependsOnId : arrayOfDependsOnIds) array.add(dependsOnId);
115-
contentmap.put("dependsOn", array);
134+
135+
final List<String> arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds();
136+
if (arrayOfDependsOnIds != null) {
137+
final JsonArray array = new JsonArray();
138+
for (final String dependsOnId : arrayOfDependsOnIds)
139+
array.add(dependsOnId);
140+
contentmap.add("dependsOn", array);
116141
}
117-
118-
RequestBody body = batchRequestStep.getRequest().body();
119-
if(body != null) {
142+
143+
final RequestBody body = batchRequestStep.getRequest().body();
144+
if (body != null) {
120145
try {
121-
contentmap.put("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
122-
}catch(IOException | ParseException e) {
146+
contentmap.add("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
147+
} catch (IOException | JsonParseException e) {
123148
e.printStackTrace();
124-
}
149+
}
125150
}
126151
return contentmap;
127152
}
128-
153+
129154
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++) {
155+
if (list == null || list.size() == 0)
156+
return "";
157+
final StringBuilder builder = new StringBuilder(list.get(0));
158+
for (int i = 1; i < list.size(); i++) {
133159
builder.append(";");
134160
builder.append(list.get(i));
135161
}
136162
return builder.toString();
137163
}
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();
164+
165+
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();
143170
copy.body().writeTo(buffer);
144-
String requestBody = buffer.readUtf8();
145-
JSONObject jsonObject = (JSONObject)new JSONParser().parse(requestBody);
146-
return jsonObject;
171+
final String requestBody = buffer.readUtf8();
172+
final JsonObject JsonObject = JsonParser.parseString(requestBody).getAsJsonObject();
173+
return JsonObject;
147174
}
148175

149176
}

0 commit comments

Comments
 (0)