|
24 | 24 | import okhttp3.RequestBody; |
25 | 25 | import okio.Buffer; |
26 | 26 |
|
| 27 | +/** |
| 28 | + * Represents the content of a batch request |
| 29 | + */ |
27 | 30 | public class MSBatchRequestContent { |
28 | | - private final LinkedHashMap<String, MSBatchRequestStep> batchRequestStepsHashMap; |
29 | | - |
30 | | - // Maximum number of requests that can be sent in a batch |
31 | | - public static final int MAX_NUMBER_OF_REQUESTS = 20; |
32 | | - |
33 | | - /* |
34 | | - * Creates Batch request content using list provided |
35 | | - * |
36 | | - * @param batchRequestStepsArray List of batch steps for batching |
37 | | - */ |
38 | | - public MSBatchRequestContent(@Nonnull final List<MSBatchRequestStep> batchRequestStepsArray) { |
39 | | - if (batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS) |
40 | | - throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS); |
41 | | - |
42 | | - this.batchRequestStepsHashMap = new LinkedHashMap<>(); |
43 | | - for (final MSBatchRequestStep requestStep : batchRequestStepsArray) |
44 | | - addBatchRequestStep(requestStep); |
45 | | - } |
46 | | - |
47 | | - /* |
48 | | - * Creates empty batch request content |
49 | | - */ |
50 | | - public MSBatchRequestContent() { |
51 | | - this.batchRequestStepsHashMap = new LinkedHashMap<>(); |
52 | | - } |
53 | | - |
54 | | - /* |
55 | | - * @param batchRequestStep Batch request step adding to batch content |
56 | | - * |
57 | | - * @return true or false based on addition or no addition of batch request step |
58 | | - * given |
59 | | - */ |
60 | | - public boolean addBatchRequestStep(@Nonnull final MSBatchRequestStep batchRequestStep) { |
61 | | - if (batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()) || |
62 | | - batchRequestStepsHashMap.size() >= MAX_NUMBER_OF_REQUESTS) |
63 | | - return false; |
64 | | - batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep); |
65 | | - return true; |
66 | | - } |
67 | | - |
68 | | - /** |
69 | | - * Add steps to batch from OkHttp.Request |
70 | | - * @param request the request to add to the batch |
71 | | - * @param arrayOfDependsOnIds ids of steps this step depends on |
72 | | - * @return the step id |
73 | | - */ |
74 | | - @Nonnull |
75 | | - public String addBatchRequestStep(@Nonnull final Request request, @Nullable final String... arrayOfDependsOnIds) { |
76 | | - String requestId; |
77 | | - do { |
78 | | - requestId = Integer.toString(ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE)); |
79 | | - } while(batchRequestStepsHashMap.keySet().contains(requestId)); |
80 | | - if(addBatchRequestStep(new MSBatchRequestStep(requestId, request, Arrays.asList(arrayOfDependsOnIds)))) |
81 | | - return requestId; |
82 | | - else |
83 | | - throw new IllegalArgumentException("unable to add step to batch. Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS); |
84 | | - } |
85 | | - |
86 | | - /* |
87 | | - * @param requestId Id of Batch request step to be removed |
88 | | - * |
89 | | - * @return true or false based on removal or no removal of batch request step |
90 | | - * with given id |
91 | | - */ |
92 | | - public boolean removeBatchRequestStepWithId(@Nonnull final String requestId) { |
93 | | - boolean removed = false; |
94 | | - if (batchRequestStepsHashMap.containsKey(requestId)) { |
95 | | - batchRequestStepsHashMap.remove(requestId); |
96 | | - removed = true; |
97 | | - for (final Map.Entry<String, MSBatchRequestStep> steps : batchRequestStepsHashMap.entrySet()) { |
98 | | - if (steps.getValue() != null && steps.getValue().getArrayOfDependsOnIds() != null) { |
99 | | - while (steps.getValue().getArrayOfDependsOnIds().remove(requestId)) |
100 | | - ; |
101 | | - } |
102 | | - } |
103 | | - } |
104 | | - return removed; |
105 | | - } |
106 | | - |
107 | | - /* |
108 | | - * @return Batch request content's json as String |
109 | | - */ |
110 | | - @Nonnull |
111 | | - public String getBatchRequestContent() { |
112 | | - final JsonObject batchRequestContentMap = new JsonObject(); |
113 | | - final JsonArray batchContentArray = new JsonArray(); |
114 | | - for (final Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) { |
115 | | - batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue())); |
116 | | - } |
117 | | - batchRequestContentMap.add("requests", batchContentArray); |
118 | | - |
119 | | - final String content = batchRequestContentMap.toString(); |
120 | | - return content; |
121 | | - } |
122 | | - |
123 | | - private static final Pattern protocolAndHostReplacementPattern = Pattern.compile("(?i)^http[s]?:\\/\\/graph\\.microsoft\\.com\\/(?>v1\\.0|beta)\\/?"); // (?i) case insensitive |
124 | | - private JsonObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep) { |
125 | | - final JsonObject contentmap = new JsonObject(); |
126 | | - contentmap.add("id", new JsonPrimitive(batchRequestStep.getRequestId())); |
| 31 | + private final LinkedHashMap<String, MSBatchRequestStep> batchRequestStepsHashMap; |
| 32 | + |
| 33 | + /** |
| 34 | + * Maximum number of requests that can be sent in a batch |
| 35 | + */ |
| 36 | + public static final int MAX_NUMBER_OF_REQUESTS = 20; |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates Batch request content using list provided |
| 40 | + * |
| 41 | + * @param batchRequestStepsArray List of batch steps for batching |
| 42 | + */ |
| 43 | + public MSBatchRequestContent(@Nonnull final List<MSBatchRequestStep> batchRequestStepsArray) { |
| 44 | + if (batchRequestStepsArray.size() > MAX_NUMBER_OF_REQUESTS) |
| 45 | + throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS); |
| 46 | + |
| 47 | + this.batchRequestStepsHashMap = new LinkedHashMap<>(); |
| 48 | + for (final MSBatchRequestStep requestStep : batchRequestStepsArray) |
| 49 | + addBatchRequestStep(requestStep); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates empty batch request content |
| 54 | + */ |
| 55 | + public MSBatchRequestContent() { |
| 56 | + this.batchRequestStepsHashMap = new LinkedHashMap<>(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Adds a step to the current batch |
| 61 | + * @param batchRequestStep Batch request step adding to batch content |
| 62 | + * @return true or false based on addition or no addition of batch request step |
| 63 | + * given |
| 64 | + */ |
| 65 | + public boolean addBatchRequestStep(@Nonnull final MSBatchRequestStep batchRequestStep) { |
| 66 | + if (batchRequestStepsHashMap.containsKey(batchRequestStep.getRequestId()) || |
| 67 | + batchRequestStepsHashMap.size() >= MAX_NUMBER_OF_REQUESTS) |
| 68 | + return false; |
| 69 | + batchRequestStepsHashMap.put(batchRequestStep.getRequestId(), batchRequestStep); |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Add steps to batch from OkHttp.Request |
| 75 | + * @param request the request to add to the batch |
| 76 | + * @param arrayOfDependsOnIds ids of steps this step depends on |
| 77 | + * @return the step id |
| 78 | + */ |
| 79 | + @Nonnull |
| 80 | + public String addBatchRequestStep(@Nonnull final Request request, @Nullable final String... arrayOfDependsOnIds) { |
| 81 | + String requestId; |
| 82 | + do { |
| 83 | + requestId = Integer.toString(ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE)); |
| 84 | + } while(batchRequestStepsHashMap.keySet().contains(requestId)); |
| 85 | + if(addBatchRequestStep(new MSBatchRequestStep(requestId, request, Arrays.asList(arrayOfDependsOnIds)))) |
| 86 | + return requestId; |
| 87 | + else |
| 88 | + throw new IllegalArgumentException("unable to add step to batch. Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param requestId Id of Batch request step to be removed |
| 93 | + * |
| 94 | + * @return true or false based on removal or no removal of batch request step |
| 95 | + * with given id |
| 96 | + */ |
| 97 | + public boolean removeBatchRequestStepWithId(@Nonnull final String requestId) { |
| 98 | + boolean removed = false; |
| 99 | + if (batchRequestStepsHashMap.containsKey(requestId)) { |
| 100 | + batchRequestStepsHashMap.remove(requestId); |
| 101 | + removed = true; |
| 102 | + for (final Map.Entry<String, MSBatchRequestStep> steps : batchRequestStepsHashMap.entrySet()) { |
| 103 | + if (steps.getValue() != null && steps.getValue().getArrayOfDependsOnIds() != null) { |
| 104 | + while (steps.getValue().getArrayOfDependsOnIds().remove(requestId)) |
| 105 | + ; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + return removed; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return Batch request content's json as String |
| 114 | + */ |
| 115 | + @Nonnull |
| 116 | + public String getBatchRequestContent() { |
| 117 | + final JsonObject batchRequestContentMap = new JsonObject(); |
| 118 | + final JsonArray batchContentArray = new JsonArray(); |
| 119 | + for (final Map.Entry<String, MSBatchRequestStep> requestStep : batchRequestStepsHashMap.entrySet()) { |
| 120 | + batchContentArray.add(getBatchRequestObjectFromRequestStep(requestStep.getValue())); |
| 121 | + } |
| 122 | + batchRequestContentMap.add("requests", batchContentArray); |
| 123 | + |
| 124 | + final String content = batchRequestContentMap.toString(); |
| 125 | + return content; |
| 126 | + } |
| 127 | + |
| 128 | + private static final Pattern protocolAndHostReplacementPattern = Pattern.compile("(?i)^http[s]?:\\/\\/graph\\.microsoft\\.com\\/(?>v1\\.0|beta)\\/?"); // (?i) case insensitive |
| 129 | + private JsonObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep batchRequestStep) { |
| 130 | + final JsonObject contentmap = new JsonObject(); |
| 131 | + contentmap.add("id", new JsonPrimitive(batchRequestStep.getRequestId())); |
127 | 132 |
|
128 | 133 | final Matcher protocolAndHostReplacementMatcher = protocolAndHostReplacementPattern.matcher(batchRequestStep.getRequest().url().toString()); |
129 | 134 |
|
130 | | - final String url = protocolAndHostReplacementMatcher.replaceAll(""); |
131 | | - contentmap.add("url", new JsonPrimitive(url)); |
132 | | - |
133 | | - contentmap.add("method", new JsonPrimitive(batchRequestStep.getRequest().method().toString())); |
134 | | - |
135 | | - final Headers headers = batchRequestStep.getRequest().headers(); |
136 | | - if (headers != null && headers.size() != 0) { |
137 | | - final JsonObject headerMap = new JsonObject(); |
138 | | - for (final Map.Entry<String, List<String>> entry : headers.toMultimap().entrySet()) { |
139 | | - headerMap.add(entry.getKey(), new JsonPrimitive(getHeaderValuesAsString(entry.getValue()))); |
140 | | - } |
141 | | - contentmap.add("headers", headerMap); |
142 | | - } |
143 | | - |
144 | | - final List<String> arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds(); |
145 | | - if (arrayOfDependsOnIds != null) { |
146 | | - final JsonArray array = new JsonArray(); |
147 | | - for (final String dependsOnId : arrayOfDependsOnIds) |
148 | | - array.add(dependsOnId); |
149 | | - contentmap.add("dependsOn", array); |
150 | | - } |
151 | | - |
152 | | - final RequestBody body = batchRequestStep.getRequest().body(); |
153 | | - if (body != null) { |
154 | | - try { |
155 | | - contentmap.add("body", requestBodyToJSONObject(batchRequestStep.getRequest())); |
156 | | - } catch (IOException | JsonParseException e) { |
157 | | - e.printStackTrace(); |
158 | | - } |
159 | | - } |
160 | | - return contentmap; |
161 | | - } |
162 | | - |
163 | | - private String getHeaderValuesAsString(final List<String> list) { |
164 | | - if (list == null || list.size() == 0) |
165 | | - return ""; |
166 | | - final StringBuilder builder = new StringBuilder(list.get(0)); |
167 | | - for (int i = 1; i < list.size(); i++) { |
168 | | - builder.append(";"); |
169 | | - builder.append(list.get(i)); |
170 | | - } |
171 | | - return builder.toString(); |
172 | | - } |
173 | | - |
174 | | - private JsonObject requestBodyToJSONObject(final Request request) throws IOException, JsonParseException { |
175 | | - if (request == null || request.body() == null) |
176 | | - return null; |
177 | | - final Request copy = request.newBuilder().build(); |
178 | | - final Buffer buffer = new Buffer(); |
179 | | - copy.body().writeTo(buffer); |
180 | | - final String requestBody = buffer.readUtf8(); |
181 | | - if(requestBody == null || requestBody == "") |
182 | | - return null; |
183 | | - final JsonElement requestBodyElement = JsonParser.parseString(requestBody); |
184 | | - if(requestBodyElement == null || !requestBodyElement.isJsonObject()) |
185 | | - return null; |
186 | | - else |
187 | | - return requestBodyElement.getAsJsonObject(); |
188 | | - } |
| 135 | + final String url = protocolAndHostReplacementMatcher.replaceAll(""); |
| 136 | + contentmap.add("url", new JsonPrimitive(url)); |
| 137 | + |
| 138 | + contentmap.add("method", new JsonPrimitive(batchRequestStep.getRequest().method().toString())); |
| 139 | + |
| 140 | + final Headers headers = batchRequestStep.getRequest().headers(); |
| 141 | + if (headers != null && headers.size() != 0) { |
| 142 | + final JsonObject headerMap = new JsonObject(); |
| 143 | + for (final Map.Entry<String, List<String>> entry : headers.toMultimap().entrySet()) { |
| 144 | + headerMap.add(entry.getKey(), new JsonPrimitive(getHeaderValuesAsString(entry.getValue()))); |
| 145 | + } |
| 146 | + contentmap.add("headers", headerMap); |
| 147 | + } |
| 148 | + |
| 149 | + final List<String> arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds(); |
| 150 | + if (arrayOfDependsOnIds != null) { |
| 151 | + final JsonArray array = new JsonArray(); |
| 152 | + for (final String dependsOnId : arrayOfDependsOnIds) |
| 153 | + array.add(dependsOnId); |
| 154 | + contentmap.add("dependsOn", array); |
| 155 | + } |
| 156 | + |
| 157 | + final RequestBody body = batchRequestStep.getRequest().body(); |
| 158 | + if (body != null) { |
| 159 | + try { |
| 160 | + contentmap.add("body", requestBodyToJSONObject(batchRequestStep.getRequest())); |
| 161 | + } catch (IOException | JsonParseException e) { |
| 162 | + e.printStackTrace(); |
| 163 | + } |
| 164 | + } |
| 165 | + return contentmap; |
| 166 | + } |
| 167 | + |
| 168 | + private String getHeaderValuesAsString(final List<String> list) { |
| 169 | + if (list == null || list.size() == 0) |
| 170 | + return ""; |
| 171 | + final StringBuilder builder = new StringBuilder(list.get(0)); |
| 172 | + for (int i = 1; i < list.size(); i++) { |
| 173 | + builder.append(";"); |
| 174 | + builder.append(list.get(i)); |
| 175 | + } |
| 176 | + return builder.toString(); |
| 177 | + } |
| 178 | + |
| 179 | + private JsonObject requestBodyToJSONObject(final Request request) throws IOException, JsonParseException { |
| 180 | + if (request == null || request.body() == null) |
| 181 | + return null; |
| 182 | + final Request copy = request.newBuilder().build(); |
| 183 | + final Buffer buffer = new Buffer(); |
| 184 | + copy.body().writeTo(buffer); |
| 185 | + final String requestBody = buffer.readUtf8(); |
| 186 | + if(requestBody == null || requestBody == "") |
| 187 | + return null; |
| 188 | + final JsonElement requestBodyElement = JsonParser.parseString(requestBody); |
| 189 | + if(requestBodyElement == null || !requestBodyElement.isJsonObject()) |
| 190 | + return null; |
| 191 | + else |
| 192 | + return requestBodyElement.getAsJsonObject(); |
| 193 | + } |
189 | 194 |
|
190 | 195 | } |
0 commit comments