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+ * instantiates 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 JsonObject batchRequestData , @ Nonnull final JsonObject batchResponseData ) {
52+ this .protocol = Protocol .HTTP_1_1 ;
53+ this .message = "OK" ;
54+ final Map <String , Request > requestMap = createBatchRequestsHashMap (baseUrl , batchRequestData );
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
@@ -82,7 +99,7 @@ public Response getResponseById(@Nonnull final String requestId) {
8299 final JsonObject JsonObject = jsonBodyElement .getAsJsonObject ();
83100 final String bodyAsString = JsonObject .toString ();
84101 final ResponseBody responseBody = ResponseBody
85- .create (MediaType .parse ("application/json; charset=utf-8" ), bodyAsString );
102+ .create (bodyAsString , MediaType .parse ("application/json; charset=utf-8" ));
86103 builder .body (responseBody );
87104 }
88105
@@ -144,37 +161,36 @@ 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 (stringToJSONObject (batchResponseData ));
172172 }
173173 } catch (final IOException e ) {
174174 e .printStackTrace ();
175175 }
176176 }
177177 }
178+ private void updateFromResponseBody (@ Nonnull final JsonObject batchResponseObj ) {
179+ if (batchResponseObj != null ) {
180+ final JsonElement nextLinkElement = batchResponseObj .get ("@odata.nextLink" );
181+ if (nextLinkElement != null && nextLinkElement .isJsonPrimitive ())
182+ nextLink = nextLinkElement .getAsString ();
183+
184+ if (batchResponseArray == null )
185+ batchResponseArray = new JsonArray ();
186+
187+ final JsonElement responseArrayElement = batchResponseObj .get ("responses" );
188+ if (responseArrayElement != null && responseArrayElement .isJsonArray ()) {
189+ final JsonArray responseArray = responseArrayElement .getAsJsonArray ();
190+ batchResponseArray .addAll (responseArray );
191+ }
192+ }
193+ }
178194
179195 /**
180196 * @return nextLink of batch response
@@ -188,8 +204,20 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
188204 if (batchResponse == null )
189205 return null ;
190206 try {
191- final Map <String , Request > batchRequestsHashMap = new LinkedHashMap <>();
192207 final JsonObject requestJSONObject = requestBodyToJSONObject (batchResponse .request ());
208+ final String baseUrl = batchResponse .request ().url ().toString ().replace ("$batch" , "" );
209+ return createBatchRequestsHashMap (baseUrl , requestJSONObject );
210+ } catch (IOException ex ) {
211+ ex .printStackTrace ();
212+ return null ;
213+ }
214+ }
215+ private Map <String , Request > createBatchRequestsHashMap (@ Nonnull final String baseUrl , @ Nonnull final JsonObject requestJSONObject ) {
216+ if (baseUrl == null || baseUrl == "" || requestJSONObject == null ) {
217+ return null ;
218+ }
219+ try {
220+ final Map <String , Request > batchRequestsHashMap = new LinkedHashMap <>();
193221 final JsonElement requestArrayElement = requestJSONObject .get ("requests" );
194222 if (requestArrayElement != null && requestArrayElement .isJsonArray ()) {
195223 final JsonArray requestArray = requestArrayElement .getAsJsonArray ();
@@ -202,8 +230,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
202230
203231 final JsonElement urlElement = requestObject .get ("url" );
204232 if (urlElement != null && urlElement .isJsonPrimitive ()) {
205- final StringBuilder fullUrl = new StringBuilder (
206- batchResponse .request ().url ().toString ().replace ("$batch" , "" ));
233+ final StringBuilder fullUrl = new StringBuilder (baseUrl );
207234 fullUrl .append (urlElement .getAsString ());
208235 builder .url (fullUrl .toString ());
209236 }
@@ -227,7 +254,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
227254 final JsonObject JsonObject = jsonBodyElement .getAsJsonObject ();
228255 final String bodyAsString = JsonObject .toString ();
229256 final RequestBody requestBody = RequestBody
230- .create (MediaType .parse ("application/json; charset=utf-8" ), bodyAsString );
257+ .create (bodyAsString , MediaType .parse ("application/json; charset=utf-8" ));
231258 builder .method (jsonMethodElement .getAsString (), requestBody );
232259 } else if (jsonMethodElement != null ) {
233260 builder .method (jsonMethodElement .getAsString (), null );
@@ -240,7 +267,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
240267 }
241268 return batchRequestsHashMap ;
242269
243- } catch (IOException | JsonParseException e ) {
270+ } catch (JsonParseException e ) {
244271 e .printStackTrace ();
245272 }
246273 return null ;
0 commit comments