44import java .util .LinkedHashMap ;
55import java .util .Iterator ;
66import java .util .Map ;
7+ import java .util .Objects ;
78
89import javax .annotation .Nullable ;
910import javax .annotation .Nonnull ;
1213import com .google .gson .JsonElement ;
1314import com .google .gson .JsonObject ;
1415import com .google .gson .JsonParser ;
16+ import com .microsoft .graph .logger .DefaultLogger ;
17+ import com .microsoft .graph .logger .ILogger ;
1518import com .google .gson .JsonParseException ;
1619
1720import okhttp3 .MediaType ;
@@ -32,11 +35,21 @@ public class MSBatchResponseContent {
3235 private LinkedHashMap <String , Request > batchRequestsHashMap = new LinkedHashMap <>();
3336 private JsonArray batchResponseArray ;
3437 private String nextLink ;
38+ private final ILogger logger ;
3539
3640 /**
3741 * @param batchResponse OkHttp batch response on execution of batch requests
3842 */
3943 public MSBatchResponseContent (@ Nullable final Response batchResponse ) {
44+ this (new DefaultLogger (), batchResponse );
45+ }
46+
47+ /**
48+ * @param batchResponse OkHttp batch response on execution of batch requests
49+ * @param logger logger to use for telemetry
50+ */
51+ public MSBatchResponseContent (@ Nonnull final ILogger logger , @ Nullable final Response batchResponse ) {
52+ this .logger = Objects .requireNonNull (logger , "logger parameter cannot be null" );
4053 update (batchResponse );
4154 this .message = batchResponse .message ();
4255 this .protocol = batchResponse .protocol ();
@@ -47,8 +60,10 @@ public MSBatchResponseContent(@Nullable final Response batchResponse) {
4760 * @param baseUrl the base service URL without a trailing slash
4861 * @param batchRequestData the batch request payload data as a JSON string
4962 * @param batchResponseData the batch response body as a JSON string
63+ * @param logger logger to use for telemetry
5064 */
51- protected MSBatchResponseContent (@ Nonnull final String baseUrl , @ Nonnull final JsonObject batchRequestData , @ Nonnull final JsonObject batchResponseData ) {
65+ protected MSBatchResponseContent (@ Nonnull final ILogger logger , @ Nonnull final String baseUrl , @ Nonnull final JsonObject batchRequestData , @ Nonnull final JsonObject batchResponseData ) {
66+ this .logger = logger ;
5267 this .protocol = Protocol .HTTP_1_1 ;
5368 this .message = "OK" ;
5469 final Map <String , Request > requestMap = createBatchRequestsHashMap (baseUrl , batchRequestData );
@@ -157,10 +172,7 @@ public Iterator<Map.Entry<String, Response>> getResponsesIterator() {
157172 * @param batchResponse the response from the service.
158173 */
159174 public void update (@ Nonnull final Response batchResponse ) {
160- if (batchResponse == null )
161- throw new IllegalArgumentException ("Batch Response cannot be null" );
162-
163- final Map <String , Request > requestMap = createBatchRequestsHashMap (batchResponse );
175+ final Map <String , Request > requestMap = createBatchRequestsHashMap (Objects .requireNonNull (batchResponse , "Batch Response cannot be null" ));
164176 if (requestMap != null )
165177 batchRequestsHashMap .putAll (requestMap );
166178
@@ -171,7 +183,7 @@ public void update(@Nonnull final Response batchResponse) {
171183 updateFromResponseBody (stringToJSONObject (batchResponseData ));
172184 }
173185 } catch (final IOException e ) {
174- e . printStackTrace ( );
186+ logger . logError ( "error updating batch response content from response body" , e );
175187 }
176188 }
177189 }
@@ -208,7 +220,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
208220 final String baseUrl = batchResponse .request ().url ().toString ().replace ("$batch" , "" );
209221 return createBatchRequestsHashMap (baseUrl , requestJSONObject );
210222 } catch (IOException ex ) {
211- ex . printStackTrace ( );
223+ logger . logError ( "error parsing batch requests to get batch response content" , ex );
212224 return null ;
213225 }
214226 }
@@ -268,7 +280,7 @@ private Map<String, Request> createBatchRequestsHashMap(@Nonnull final String ba
268280 return batchRequestsHashMap ;
269281
270282 } catch (JsonParseException e ) {
271- e . printStackTrace ( );
283+ logger . logError ( "error parsing batch request" , e );
272284 }
273285 return null ;
274286 }
@@ -280,7 +292,7 @@ private JsonObject stringToJSONObject(final String input) {
280292 JsonObject = JsonParser .parseString (input ).getAsJsonObject ();
281293 }
282294 } catch (final Exception e ) {
283- e . printStackTrace ( );
295+ logger . logError ( "error parsing input into JSONObject" , e );
284296 }
285297 return JsonObject ;
286298 }
0 commit comments