1515import com .google .gson .JsonParseException ;
1616
1717import okhttp3 .MediaType ;
18+ import okhttp3 .Protocol ;
1819import okhttp3 .Request ;
1920import okhttp3 .RequestBody ;
2021import okhttp3 .Response ;
2627 */
2728public class MSBatchResponseContent {
2829
29- private final Response batchResponse ;
30- private LinkedHashMap <String , Request > batchRequestsHashMap ;
30+ private final Protocol protocol ;
31+ private final String message ;
32+ private LinkedHashMap <String , Request > batchRequestsHashMap = new LinkedHashMap <>();
3133 private JsonArray batchResponseArray ;
3234 private String nextLink ;
3335
3436 /**
3537 * @param batchResponse OkHttp batch response on execution of batch requests
3638 */
3739 public MSBatchResponseContent (@ Nullable final Response batchResponse ) {
38- this .batchResponse = batchResponse ;
3940 update (batchResponse );
41+ this .message = batchResponse .message ();
42+ this .protocol = batchResponse .protocol ();
43+ }
44+ /**
45+ * intantiates a new response
46+ * internal only, used when the content executes the requests
47+ * @param baseUrl the base service URL without a trailing slash
48+ * @param batchRequestData the batch request payload data as a JSON string
49+ * @param batchResponseData the batch response body as a JSON string
50+ */
51+ protected MSBatchResponseContent (@ Nonnull final String baseUrl , @ Nonnull final String batchRequestData , @ Nonnull final String batchResponseData ) {
52+ this .protocol = Protocol .HTTP_1_1 ;
53+ this .message = "OK" ;
54+ final Map <String , Request > requestMap = createBatchRequestsHashMap (baseUrl , JsonParser .parseString (batchRequestData ).getAsJsonObject ());
55+ if (requestMap != null )
56+ batchRequestsHashMap .putAll (requestMap );
57+ updateFromResponseBody (batchResponseData );
4058 }
4159
4260 /**
@@ -66,14 +84,13 @@ public Response getResponseById(@Nonnull final String requestId) {
6684 // Put corresponding request into the constructed response
6785 builder .request (batchRequestsHashMap .get (requestId ));
6886 // copy protocol and message same as of batch response
69- builder .protocol (batchResponse . protocol () );
70- builder .message (batchResponse . message () );
87+ builder .protocol (protocol );
88+ builder .message (message );
7189
7290 // Put status code of the corresponding request in JsonArray
7391 final JsonElement statusElement = jsonresponse .get ("status" );
7492 if (statusElement != null && statusElement .isJsonPrimitive ()) {
75- final Long status = statusElement .getAsLong ();
76- builder .code (status .intValue ());
93+ builder .code (statusElement .getAsInt ());
7794 }
7895
7996 // Put body from response array for corresponding id into constructing response
@@ -144,37 +161,38 @@ public void update(@Nonnull final Response batchResponse) {
144161 throw new IllegalArgumentException ("Batch Response cannot be null" );
145162
146163 final Map <String , Request > requestMap = createBatchRequestsHashMap (batchResponse );
147- if (batchRequestsHashMap == null )
148- batchRequestsHashMap = new LinkedHashMap <>();
149164 if (requestMap != null )
150165 batchRequestsHashMap .putAll (requestMap );
151166
152167 if (batchResponse .body () != null ) {
153168 try {
154169 final String batchResponseData = batchResponse .body ().string ();
155170 if (batchResponseData != null ) {
156- final JsonObject batchResponseObj = stringToJSONObject (batchResponseData );
157- if (batchResponseObj != null ) {
158-
159- final JsonElement nextLinkElement = batchResponseObj .get ("@odata.nextLink" );
160- if (nextLinkElement != null && nextLinkElement .isJsonPrimitive ())
161- nextLink = nextLinkElement .getAsString ();
162-
163- if (batchResponseArray == null )
164- batchResponseArray = new JsonArray ();
165-
166- final JsonElement responseArrayElement = batchResponseObj .get ("responses" );
167- if (responseArrayElement != null && responseArrayElement .isJsonArray ()) {
168- final JsonArray responseArray = responseArrayElement .getAsJsonArray ();
169- batchResponseArray .addAll (responseArray );
170- }
171- }
171+ updateFromResponseBody (batchResponseData );
172172 }
173173 } catch (final IOException e ) {
174174 e .printStackTrace ();
175175 }
176176 }
177177 }
178+ private void updateFromResponseBody (@ Nonnull final String batchResponseData ) {
179+ final JsonObject batchResponseObj = stringToJSONObject (batchResponseData );
180+ if (batchResponseObj != null ) {
181+
182+ final JsonElement nextLinkElement = batchResponseObj .get ("@odata.nextLink" );
183+ if (nextLinkElement != null && nextLinkElement .isJsonPrimitive ())
184+ nextLink = nextLinkElement .getAsString ();
185+
186+ if (batchResponseArray == null )
187+ batchResponseArray = new JsonArray ();
188+
189+ final JsonElement responseArrayElement = batchResponseObj .get ("responses" );
190+ if (responseArrayElement != null && responseArrayElement .isJsonArray ()) {
191+ final JsonArray responseArray = responseArrayElement .getAsJsonArray ();
192+ batchResponseArray .addAll (responseArray );
193+ }
194+ }
195+ }
178196
179197 /**
180198 * @return nextLink of batch response
@@ -188,8 +206,21 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
188206 if (batchResponse == null )
189207 return null ;
190208 try {
191- final Map <String , Request > batchRequestsHashMap = new LinkedHashMap <>();
192209 final JsonObject requestJSONObject = requestBodyToJSONObject (batchResponse .request ());
210+ final String baseUrl = batchResponse .request ().url ().toString ().replace ("$batch" , "" );
211+ return createBatchRequestsHashMap (baseUrl , requestJSONObject );
212+ } catch (IOException ex ) {
213+ ex .printStackTrace ();
214+ return null ;
215+ }
216+ }
217+ @ Nullable
218+ protected Map <String , Request > createBatchRequestsHashMap (@ Nonnull final String baseUrl , @ Nonnull final JsonObject requestJSONObject ) {
219+ if (baseUrl == null || baseUrl == "" || requestJSONObject == null ) {
220+ return null ;
221+ }
222+ try {
223+ final Map <String , Request > batchRequestsHashMap = new LinkedHashMap <>();
193224 final JsonElement requestArrayElement = requestJSONObject .get ("requests" );
194225 if (requestArrayElement != null && requestArrayElement .isJsonArray ()) {
195226 final JsonArray requestArray = requestArrayElement .getAsJsonArray ();
@@ -202,8 +233,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
202233
203234 final JsonElement urlElement = requestObject .get ("url" );
204235 if (urlElement != null && urlElement .isJsonPrimitive ()) {
205- final StringBuilder fullUrl = new StringBuilder (
206- batchResponse .request ().url ().toString ().replace ("$batch" , "" ));
236+ final StringBuilder fullUrl = new StringBuilder (baseUrl );
207237 fullUrl .append (urlElement .getAsString ());
208238 builder .url (fullUrl .toString ());
209239 }
@@ -240,7 +270,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
240270 }
241271 return batchRequestsHashMap ;
242272
243- } catch (IOException | JsonParseException e ) {
273+ } catch (JsonParseException e ) {
244274 e .printStackTrace ();
245275 }
246276 return null ;
0 commit comments