|
28 | 28 | * Represents the content of a batch request |
29 | 29 | */ |
30 | 30 | public class MSBatchRequestContent { |
31 | | - private final LinkedHashMap<String, MSBatchRequestStep> batchRequestStepsHashMap; |
| 31 | + private final LinkedHashMap<String, MSBatchRequestStep> batchRequestStepsHashMap; |
32 | 32 |
|
33 | | - /** |
| 33 | + /** |
34 | 34 | * Maximum number of requests that can be sent in a batch |
35 | 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 | | - /** |
| 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 | 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())); |
| 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())); |
132 | 132 |
|
133 | 133 | final Matcher protocolAndHostReplacementMatcher = protocolAndHostReplacementPattern.matcher(batchRequestStep.getRequest().url().toString()); |
134 | 134 |
|
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 | | - } |
| 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 | + } |
194 | 194 |
|
195 | 195 | } |
0 commit comments