Skip to content

Commit 27a40d9

Browse files
committed
- replaces simple json by gson
1 parent 45c2b58 commit 27a40d9

File tree

5 files changed

+250
-213
lines changed

5 files changed

+250
-213
lines changed

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 = {

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

Lines changed: 81 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,145 +5,152 @@
55
import java.util.List;
66
import java.util.Map;
77

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;
8+
import com.google.gson.JsonArray;
9+
import com.google.gson.JsonObject;
10+
import com.google.gson.JsonParser;
11+
import com.google.gson.JsonPrimitive;
12+
import com.google.gson.JsonParseException;
1213

1314
import okhttp3.Headers;
1415
import okhttp3.Request;
1516
import okhttp3.RequestBody;
1617
import okio.Buffer;
1718

1819
public class MSBatchRequestContent {
19-
private Map<String, MSBatchRequestStep> batchRequestStepsHashMap;
20-
20+
private final Map<String, MSBatchRequestStep> batchRequestStepsHashMap;
21+
2122
// Maximum number of requests that can be sent in a batch
2223
public static final int MAX_NUMBER_OF_REQUESTS = 20;
23-
24+
2425
/*
2526
* Creates Batch request content using list provided
2627
*
2728
* @param batchRequestStepsArray List of batch steps for batching
2829
*/
29-
public MSBatchRequestContent(List<MSBatchRequestStep> batchRequestStepsArray) {
30-
if(batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS)
30+
public MSBatchRequestContent(final List<MSBatchRequestStep> batchRequestStepsArray) {
31+
if (batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS)
3132
throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS);
32-
33+
3334
this.batchRequestStepsHashMap = new HashMap<>();
34-
for(MSBatchRequestStep requestStep: batchRequestStepsArray)
35+
for (final MSBatchRequestStep requestStep : batchRequestStepsArray)
3536
addBatchRequestStep(requestStep);
3637
}
37-
38+
3839
/*
3940
* Creates empty batch request content
4041
*/
4142
public MSBatchRequestContent() {
4243
batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>();
4344
}
44-
45+
4546
/*
4647
* @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+
*
49+
* @return true or false based on addition or no addition of batch request step
50+
* given
4851
*/
49-
public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) {
50-
if(batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()))
52+
public boolean addBatchRequestStep(final MSBatchRequestStep batchRequestStep) {
53+
if (batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()))
5154
return false;
5255
batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep);
5356
return true;
5457
}
55-
58+
5659
/*
5760
* @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
61+
*
62+
* @return true or false based on removal or no removal of batch request step
63+
* with given id
5964
*/
60-
public boolean removeBatchRequestStepWithId(String requestId) {
65+
public boolean removeBatchRequestStepWithId(final String requestId) {
6166
boolean removed = false;
62-
if(batchRequestStepsHashMap.containsKey(requestId)) {
67+
if (batchRequestStepsHashMap.containsKey(requestId)) {
6368
batchRequestStepsHashMap.remove(requestId);
6469
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));
70+
for (final Map.Entry<String, MSBatchRequestStep> steps : batchRequestStepsHashMap.entrySet()) {
71+
if (steps.getValue() != null && steps.getValue().getArrayOfDependsOnIds() != null) {
72+
while (steps.getValue().getArrayOfDependsOnIds().remove(requestId))
73+
;
6874
}
6975
}
7076
}
7177
return removed;
7278
}
73-
79+
7480
/*
7581
* @return Batch request content's json as String
7682
*/
7783
public String getBatchRequestContent() {
78-
JSONObject batchRequestContentMap = new JSONObject();
79-
JSONArray batchContentArray = new JSONArray();
80-
for(Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) {
84+
final JsonObject batchRequestContentMap = new JsonObject();
85+
final JsonArray batchContentArray = new JsonArray();
86+
for (final Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) {
8187
batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue()));
8288
}
83-
batchRequestContentMap.put("requests", batchContentArray);
84-
85-
String content = batchRequestContentMap.toString();
89+
batchRequestContentMap.add("requests", batchContentArray);
90+
91+
final String content = batchRequestContentMap.toString();
8692
return content;
8793
}
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()));
94+
95+
private JsonObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep) {
96+
final JsonObject contentmap = new JsonObject();
97+
contentmap.add("id", new JsonPrimitive(batchRequestStep.getRequestId()));
98+
99+
final String url = batchRequestStep.getRequest().url().toString()
100+
.replaceAll("https://graph.microsoft.com/v1.0/", "").replaceAll("http://graph.microsoft.com/v1.0/", "")
101+
.replaceAll("https://graph.microsoft.com/beta/", "").replaceAll("http://graph.microsoft.com/beta/", "");
102+
contentmap.add("url", new JsonPrimitive(url));
103+
104+
contentmap.add("method", new JsonPrimitive(batchRequestStep.getRequest().method().toString()));
105+
106+
final Headers headers = batchRequestStep.getRequest().headers();
107+
if (headers != null && headers.size() != 0) {
108+
final JsonObject headerMap = new JsonObject();
109+
for (final Map.Entry<String, List<String>> entry : headers.toMultimap().entrySet()) {
110+
headerMap.add(entry.getKey(), new JsonPrimitive(getHeaderValuesAsString(entry.getValue())));
107111
}
108-
contentmap.put("headers", headerMap);
112+
contentmap.add("headers", headerMap);
109113
}
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);
114+
115+
final List<String> arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds();
116+
if (arrayOfDependsOnIds != null) {
117+
final JsonArray array = new JsonArray();
118+
for (final String dependsOnId : arrayOfDependsOnIds)
119+
array.add(dependsOnId);
120+
contentmap.add("dependsOn", array);
116121
}
117-
118-
RequestBody body = batchRequestStep.getRequest().body();
119-
if(body != null) {
122+
123+
final RequestBody body = batchRequestStep.getRequest().body();
124+
if (body != null) {
120125
try {
121-
contentmap.put("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
122-
}catch(IOException | ParseException e) {
126+
contentmap.add("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
127+
} catch (IOException | JsonParseException e) {
123128
e.printStackTrace();
124-
}
129+
}
125130
}
126131
return contentmap;
127132
}
128-
133+
129134
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++) {
135+
if (list == null || list.size() == 0)
136+
return "";
137+
final StringBuilder builder = new StringBuilder(list.get(0));
138+
for (int i = 1; i < list.size(); i++) {
133139
builder.append(";");
134140
builder.append(list.get(i));
135141
}
136142
return builder.toString();
137143
}
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();
144+
145+
private JsonObject requestBodyToJSONObject(final Request request) throws IOException, JsonParseException {
146+
if (request == null || request.body() == null)
147+
return null;
148+
final Request copy = request.newBuilder().build();
149+
final Buffer buffer = new Buffer();
143150
copy.body().writeTo(buffer);
144-
String requestBody = buffer.readUtf8();
145-
JSONObject jsonObject = (JSONObject)new JSONParser().parse(requestBody);
146-
return jsonObject;
151+
final String requestBody = buffer.readUtf8();
152+
final JsonObject JsonObject = JsonParser.parseString(requestBody).getAsJsonObject();
153+
return JsonObject;
147154
}
148155

149156
}

0 commit comments

Comments
 (0)