15
15
import com .google .gson .JsonParseException ;
16
16
17
17
import okhttp3 .MediaType ;
18
+ import okhttp3 .Protocol ;
18
19
import okhttp3 .Request ;
19
20
import okhttp3 .RequestBody ;
20
21
import okhttp3 .Response ;
26
27
*/
27
28
public class MSBatchResponseContent {
28
29
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 <>();
31
33
private JsonArray batchResponseArray ;
32
34
private String nextLink ;
33
35
34
36
/**
35
37
* @param batchResponse OkHttp batch response on execution of batch requests
36
38
*/
37
39
public MSBatchResponseContent (@ Nullable final Response batchResponse ) {
38
- this .batchResponse = batchResponse ;
39
40
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 );
40
58
}
41
59
42
60
/**
@@ -66,14 +84,13 @@ public Response getResponseById(@Nonnull final String requestId) {
66
84
// Put corresponding request into the constructed response
67
85
builder .request (batchRequestsHashMap .get (requestId ));
68
86
// 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 );
71
89
72
90
// Put status code of the corresponding request in JsonArray
73
91
final JsonElement statusElement = jsonresponse .get ("status" );
74
92
if (statusElement != null && statusElement .isJsonPrimitive ()) {
75
- final Long status = statusElement .getAsLong ();
76
- builder .code (status .intValue ());
93
+ builder .code (statusElement .getAsInt ());
77
94
}
78
95
79
96
// Put body from response array for corresponding id into constructing response
@@ -82,7 +99,7 @@ public Response getResponseById(@Nonnull final String requestId) {
82
99
final JsonObject JsonObject = jsonBodyElement .getAsJsonObject ();
83
100
final String bodyAsString = JsonObject .toString ();
84
101
final ResponseBody responseBody = ResponseBody
85
- .create (MediaType .parse ("application/json; charset=utf-8" ), bodyAsString );
102
+ .create (bodyAsString , MediaType .parse ("application/json; charset=utf-8" ));
86
103
builder .body (responseBody );
87
104
}
88
105
@@ -144,37 +161,36 @@ public void update(@Nonnull final Response batchResponse) {
144
161
throw new IllegalArgumentException ("Batch Response cannot be null" );
145
162
146
163
final Map <String , Request > requestMap = createBatchRequestsHashMap (batchResponse );
147
- if (batchRequestsHashMap == null )
148
- batchRequestsHashMap = new LinkedHashMap <>();
149
164
if (requestMap != null )
150
165
batchRequestsHashMap .putAll (requestMap );
151
166
152
167
if (batchResponse .body () != null ) {
153
168
try {
154
169
final String batchResponseData = batchResponse .body ().string ();
155
170
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 ));
172
172
}
173
173
} catch (final IOException e ) {
174
174
e .printStackTrace ();
175
175
}
176
176
}
177
177
}
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
+ }
178
194
179
195
/**
180
196
* @return nextLink of batch response
@@ -188,8 +204,20 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
188
204
if (batchResponse == null )
189
205
return null ;
190
206
try {
191
- final Map <String , Request > batchRequestsHashMap = new LinkedHashMap <>();
192
207
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 <>();
193
221
final JsonElement requestArrayElement = requestJSONObject .get ("requests" );
194
222
if (requestArrayElement != null && requestArrayElement .isJsonArray ()) {
195
223
final JsonArray requestArray = requestArrayElement .getAsJsonArray ();
@@ -202,8 +230,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
202
230
203
231
final JsonElement urlElement = requestObject .get ("url" );
204
232
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 );
207
234
fullUrl .append (urlElement .getAsString ());
208
235
builder .url (fullUrl .toString ());
209
236
}
@@ -227,7 +254,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
227
254
final JsonObject JsonObject = jsonBodyElement .getAsJsonObject ();
228
255
final String bodyAsString = JsonObject .toString ();
229
256
final RequestBody requestBody = RequestBody
230
- .create (MediaType .parse ("application/json; charset=utf-8" ), bodyAsString );
257
+ .create (bodyAsString , MediaType .parse ("application/json; charset=utf-8" ));
231
258
builder .method (jsonMethodElement .getAsString (), requestBody );
232
259
} else if (jsonMethodElement != null ) {
233
260
builder .method (jsonMethodElement .getAsString (), null );
@@ -240,7 +267,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
240
267
}
241
268
return batchRequestsHashMap ;
242
269
243
- } catch (IOException | JsonParseException e ) {
270
+ } catch (JsonParseException e ) {
244
271
e .printStackTrace ();
245
272
}
246
273
return null ;
0 commit comments