|
5 | 5 | import java.util.List; |
6 | 6 | import java.util.Map; |
7 | 7 |
|
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; |
12 | 13 |
|
13 | 14 | import okhttp3.Headers; |
14 | 15 | import okhttp3.Request; |
15 | 16 | import okhttp3.RequestBody; |
16 | 17 | import okio.Buffer; |
17 | 18 |
|
18 | 19 | public class MSBatchRequestContent { |
19 | | - private Map<String, MSBatchRequestStep> batchRequestStepsHashMap; |
20 | | - |
| 20 | + private final Map<String, MSBatchRequestStep> batchRequestStepsHashMap; |
| 21 | + |
21 | 22 | // Maximum number of requests that can be sent in a batch |
22 | 23 | public static final int MAX_NUMBER_OF_REQUESTS = 20; |
23 | | - |
| 24 | + |
24 | 25 | /* |
25 | 26 | * Creates Batch request content using list provided |
26 | 27 | * |
27 | 28 | * @param batchRequestStepsArray List of batch steps for batching |
28 | 29 | */ |
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) |
31 | 32 | throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS); |
32 | | - |
| 33 | + |
33 | 34 | this.batchRequestStepsHashMap = new HashMap<>(); |
34 | | - for(MSBatchRequestStep requestStep: batchRequestStepsArray) |
| 35 | + for (final MSBatchRequestStep requestStep : batchRequestStepsArray) |
35 | 36 | addBatchRequestStep(requestStep); |
36 | 37 | } |
37 | | - |
| 38 | + |
38 | 39 | /* |
39 | 40 | * Creates empty batch request content |
40 | 41 | */ |
41 | 42 | public MSBatchRequestContent() { |
42 | 43 | batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>(); |
43 | 44 | } |
44 | | - |
| 45 | + |
45 | 46 | /* |
46 | 47 | * @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 |
48 | 51 | */ |
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())) |
51 | 54 | return false; |
52 | 55 | batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep); |
53 | 56 | return true; |
54 | 57 | } |
55 | | - |
| 58 | + |
56 | 59 | /* |
57 | 60 | * @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 |
59 | 64 | */ |
60 | | - public boolean removeBatchRequestStepWithId(String requestId) { |
| 65 | + public boolean removeBatchRequestStepWithId(final String requestId) { |
61 | 66 | boolean removed = false; |
62 | | - if(batchRequestStepsHashMap.containsKey(requestId)) { |
| 67 | + if (batchRequestStepsHashMap.containsKey(requestId)) { |
63 | 68 | batchRequestStepsHashMap.remove(requestId); |
64 | 69 | 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 | + ; |
68 | 74 | } |
69 | 75 | } |
70 | 76 | } |
71 | 77 | return removed; |
72 | 78 | } |
73 | | - |
| 79 | + |
74 | 80 | /* |
75 | 81 | * @return Batch request content's json as String |
76 | 82 | */ |
77 | 83 | 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()) { |
81 | 87 | batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue())); |
82 | 88 | } |
83 | | - batchRequestContentMap.put("requests", batchContentArray); |
84 | | - |
85 | | - String content = batchRequestContentMap.toString(); |
| 89 | + batchRequestContentMap.add("requests", batchContentArray); |
| 90 | + |
| 91 | + final String content = batchRequestContentMap.toString(); |
86 | 92 | return content; |
87 | 93 | } |
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()))); |
107 | 111 | } |
108 | | - contentmap.put("headers", headerMap); |
| 112 | + contentmap.add("headers", headerMap); |
109 | 113 | } |
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); |
116 | 121 | } |
117 | | - |
118 | | - RequestBody body = batchRequestStep.getRequest().body(); |
119 | | - if(body != null) { |
| 122 | + |
| 123 | + final RequestBody body = batchRequestStep.getRequest().body(); |
| 124 | + if (body != null) { |
120 | 125 | 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) { |
123 | 128 | e.printStackTrace(); |
124 | | - } |
| 129 | + } |
125 | 130 | } |
126 | 131 | return contentmap; |
127 | 132 | } |
128 | | - |
| 133 | + |
129 | 134 | 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++) { |
133 | 139 | builder.append(";"); |
134 | 140 | builder.append(list.get(i)); |
135 | 141 | } |
136 | 142 | return builder.toString(); |
137 | 143 | } |
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(); |
143 | 150 | 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; |
147 | 154 | } |
148 | 155 |
|
149 | 156 | } |
0 commit comments