|
1 | 1 | package com.microsoft.graph.content; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
| 4 | +import java.util.Arrays; |
4 | 5 | import java.util.HashMap; |
5 | 6 | import java.util.List; |
6 | 7 | import java.util.Map; |
| 8 | +import java.util.concurrent.ThreadLocalRandom; |
7 | 9 |
|
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; |
12 | 15 |
|
13 | 16 | import okhttp3.Headers; |
14 | 17 | import okhttp3.Request; |
15 | 18 | import okhttp3.RequestBody; |
16 | 19 | import okio.Buffer; |
17 | 20 |
|
18 | 21 | public class MSBatchRequestContent { |
19 | | - private Map<String, MSBatchRequestStep> batchRequestStepsHashMap; |
20 | | - |
| 22 | + private final Map<String, MSBatchRequestStep> batchRequestStepsHashMap; |
| 23 | + |
21 | 24 | // Maximum number of requests that can be sent in a batch |
22 | 25 | public static final int MAX_NUMBER_OF_REQUESTS = 20; |
23 | | - |
| 26 | + |
24 | 27 | /* |
25 | 28 | * Creates Batch request content using list provided |
26 | 29 | * |
27 | 30 | * @param batchRequestStepsArray List of batch steps for batching |
28 | 31 | */ |
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) |
31 | 34 | throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS); |
32 | | - |
| 35 | + |
33 | 36 | this.batchRequestStepsHashMap = new HashMap<>(); |
34 | | - for(MSBatchRequestStep requestStep: batchRequestStepsArray) |
| 37 | + for (final MSBatchRequestStep requestStep : batchRequestStepsArray) |
35 | 38 | addBatchRequestStep(requestStep); |
36 | 39 | } |
37 | | - |
| 40 | + |
38 | 41 | /* |
39 | 42 | * Creates empty batch request content |
40 | 43 | */ |
41 | 44 | public MSBatchRequestContent() { |
42 | 45 | batchRequestStepsHashMap = new HashMap<String, MSBatchRequestStep>(); |
43 | 46 | } |
44 | | - |
| 47 | + |
45 | 48 | /* |
46 | 49 | * @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 |
48 | 53 | */ |
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) |
51 | 57 | return false; |
52 | 58 | batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep); |
53 | 59 | return true; |
54 | 60 | } |
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 | + |
56 | 79 | /* |
57 | 80 | * @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 |
59 | 84 | */ |
60 | | - public boolean removeBatchRequestStepWithId(String requestId) { |
| 85 | + public boolean removeBatchRequestStepWithId(final String requestId) { |
61 | 86 | boolean removed = false; |
62 | | - if(batchRequestStepsHashMap.containsKey(requestId)) { |
| 87 | + if (batchRequestStepsHashMap.containsKey(requestId)) { |
63 | 88 | batchRequestStepsHashMap.remove(requestId); |
64 | 89 | 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 | + ; |
68 | 94 | } |
69 | 95 | } |
70 | 96 | } |
71 | 97 | return removed; |
72 | 98 | } |
73 | | - |
| 99 | + |
74 | 100 | /* |
75 | 101 | * @return Batch request content's json as String |
76 | 102 | */ |
77 | 103 | 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()) { |
81 | 107 | batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue())); |
82 | 108 | } |
83 | | - batchRequestContentMap.put("requests", batchContentArray); |
84 | | - |
85 | | - String content = batchRequestContentMap.toString(); |
| 109 | + batchRequestContentMap.add("requests", batchContentArray); |
| 110 | + |
| 111 | + final String content = batchRequestContentMap.toString(); |
86 | 112 | return content; |
87 | 113 | } |
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()))); |
107 | 131 | } |
108 | | - contentmap.put("headers", headerMap); |
| 132 | + contentmap.add("headers", headerMap); |
109 | 133 | } |
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); |
116 | 141 | } |
117 | | - |
118 | | - RequestBody body = batchRequestStep.getRequest().body(); |
119 | | - if(body != null) { |
| 142 | + |
| 143 | + final RequestBody body = batchRequestStep.getRequest().body(); |
| 144 | + if (body != null) { |
120 | 145 | 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) { |
123 | 148 | e.printStackTrace(); |
124 | | - } |
| 149 | + } |
125 | 150 | } |
126 | 151 | return contentmap; |
127 | 152 | } |
128 | | - |
| 153 | + |
129 | 154 | 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++) { |
133 | 159 | builder.append(";"); |
134 | 160 | builder.append(list.get(i)); |
135 | 161 | } |
136 | 162 | return builder.toString(); |
137 | 163 | } |
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(); |
143 | 170 | 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; |
147 | 174 | } |
148 | 175 |
|
149 | 176 | } |
0 commit comments