Skip to content

Commit 61520f2

Browse files
committed
- fixes a bug where some higher level classes would write to stdout/stderr instead of using the provided logging infrastructure
1 parent cffa9d4 commit 61520f2

5 files changed

Lines changed: 70 additions & 11 deletions

File tree

‎src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.gson.JsonPrimitive;
2121
import com.microsoft.graph.core.ClientException;
2222
import com.microsoft.graph.core.IBaseClient;
23+
import com.microsoft.graph.logger.DefaultLogger;
24+
import com.microsoft.graph.logger.ILogger;
2325
import com.google.gson.JsonParseException;
2426

2527
import okhttp3.Headers;
@@ -37,16 +39,32 @@ public class MSBatchRequestContent {
3739
* Maximum number of requests that can be sent in a batch
3840
*/
3941
public static final int MAX_NUMBER_OF_REQUESTS = 20;
42+
private final ILogger logger;
4043

4144
/**
4245
* Creates Batch request content using list provided
4346
*
4447
* @param batchRequestStepsArray List of batch steps for batching
4548
*/
4649
public MSBatchRequestContent(@Nonnull final MSBatchRequestStep... batchRequestStepsArray) {
50+
this(new DefaultLogger(), batchRequestStepsArray);
51+
}
52+
53+
/**
54+
* Creates Batch request content using list provided
55+
*
56+
* @param batchRequestStepsArray List of batch steps for batching
57+
* @param logger logger to use for telemetry
58+
*/
59+
public MSBatchRequestContent(@Nonnull final ILogger logger, @Nonnull final MSBatchRequestStep... batchRequestStepsArray) {
4760
if (batchRequestStepsArray.length > MAX_NUMBER_OF_REQUESTS)
4861
throw new IllegalArgumentException("Number of batch request steps cannot exceed " + MAX_NUMBER_OF_REQUESTS);
4962

63+
if(logger == null)
64+
throw new IllegalArgumentException("logger cannot be null");
65+
66+
this.logger = logger;
67+
5068
this.batchRequestStepsHashMap = new LinkedHashMap<>();
5169
for (final MSBatchRequestStep requestStep : batchRequestStepsArray)
5270
if(requestStep != null)
@@ -136,7 +154,7 @@ public String getBatchRequestContent() {
136154
@Nonnull
137155
public MSBatchResponseContent execute(@Nonnull final IBaseClient client) {
138156
final JsonObject content = getBatchRequestContentAsJson();
139-
return new MSBatchResponseContent(client.getServiceRoot() + "/",
157+
return new MSBatchResponseContent(logger, client.getServiceRoot() + "/",
140158
content,
141159
client.customRequest("/$batch")
142160
.buildRequest()
@@ -157,7 +175,7 @@ public CompletableFuture<MSBatchResponseContent> executeAsync(@Nonnull final IBa
157175
return client.customRequest("/$batch")
158176
.buildRequest()
159177
.postAsync(content)
160-
.thenApply(resp -> new MSBatchResponseContent(client.getServiceRoot() + "/", content, resp.getAsJsonObject()));
178+
.thenApply(resp -> new MSBatchResponseContent(logger, client.getServiceRoot() + "/", content, resp.getAsJsonObject()));
161179
}
162180

163181
private static final Pattern protocolAndHostReplacementPattern = Pattern.compile("(?i)^http[s]?:\\/\\/graph\\.microsoft\\.com\\/(?>v1\\.0|beta)\\/?"); // (?i) case insensitive
@@ -194,7 +212,7 @@ private JsonObject getBatchRequestObjectFromRequestStep(final MSBatchRequestStep
194212
try {
195213
contentmap.add("body", requestBodyToJSONObject(batchRequestStep.getRequest()));
196214
} catch (IOException | JsonParseException e) {
197-
e.printStackTrace();
215+
logger.logError("error pasing the request JSON body", e);
198216
}
199217
}
200218
return contentmap;

‎src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.google.gson.JsonElement;
1313
import com.google.gson.JsonObject;
1414
import com.google.gson.JsonParser;
15+
import com.microsoft.graph.logger.DefaultLogger;
16+
import com.microsoft.graph.logger.ILogger;
1517
import com.google.gson.JsonParseException;
1618

1719
import okhttp3.MediaType;
@@ -32,11 +34,23 @@ public class MSBatchResponseContent {
3234
private LinkedHashMap<String, Request> batchRequestsHashMap = new LinkedHashMap<>();
3335
private JsonArray batchResponseArray;
3436
private String nextLink;
37+
private final ILogger logger;
3538

3639
/**
3740
* @param batchResponse OkHttp batch response on execution of batch requests
3841
*/
3942
public MSBatchResponseContent(@Nullable final Response batchResponse) {
43+
this(new DefaultLogger(), batchResponse);
44+
}
45+
46+
/**
47+
* @param batchResponse OkHttp batch response on execution of batch requests
48+
* @param logger logger to use for telemetry
49+
*/
50+
public MSBatchResponseContent(@Nonnull final ILogger logger, @Nullable final Response batchResponse) {
51+
if(logger == null)
52+
throw new IllegalArgumentException("logger parameter cannot be null");
53+
this.logger = logger;
4054
update(batchResponse);
4155
this.message = batchResponse.message();
4256
this.protocol = batchResponse.protocol();
@@ -47,8 +61,10 @@ public MSBatchResponseContent(@Nullable final Response batchResponse) {
4761
* @param baseUrl the base service URL without a trailing slash
4862
* @param batchRequestData the batch request payload data as a JSON string
4963
* @param batchResponseData the batch response body as a JSON string
64+
* @param logger logger to use for telemetry
5065
*/
51-
protected MSBatchResponseContent(@Nonnull final String baseUrl, @Nonnull final JsonObject batchRequestData, @Nonnull final JsonObject batchResponseData) {
66+
protected MSBatchResponseContent(@Nonnull final ILogger logger, @Nonnull final String baseUrl, @Nonnull final JsonObject batchRequestData, @Nonnull final JsonObject batchResponseData) {
67+
this.logger = logger;
5268
this.protocol = Protocol.HTTP_1_1;
5369
this.message = "OK";
5470
final Map<String, Request> requestMap = createBatchRequestsHashMap(baseUrl, batchRequestData);
@@ -171,7 +187,7 @@ public void update(@Nonnull final Response batchResponse) {
171187
updateFromResponseBody(stringToJSONObject(batchResponseData));
172188
}
173189
} catch (final IOException e) {
174-
e.printStackTrace();
190+
logger.logError("error updating batch response content from response body", e);
175191
}
176192
}
177193
}
@@ -208,7 +224,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
208224
final String baseUrl = batchResponse.request().url().toString().replace("$batch", "");
209225
return createBatchRequestsHashMap(baseUrl, requestJSONObject);
210226
} catch (IOException ex) {
211-
ex.printStackTrace();
227+
logger.logError("error parsing batch requests to get batch response content", ex);
212228
return null;
213229
}
214230
}
@@ -268,7 +284,7 @@ private Map<String, Request> createBatchRequestsHashMap(@Nonnull final String ba
268284
return batchRequestsHashMap;
269285

270286
} catch (JsonParseException e) {
271-
e.printStackTrace();
287+
logger.logError("error parsing batch request", e);
272288
}
273289
return null;
274290
}
@@ -280,7 +296,7 @@ private JsonObject stringToJSONObject(final String input) {
280296
JsonObject = JsonParser.parseString(input).getAsJsonObject();
281297
}
282298
} catch (final Exception e) {
283-
e.printStackTrace();
299+
logger.logError("error parsing input into JSONObject", e);
284300
}
285301
return JsonObject;
286302
}

‎src/main/java/com/microsoft/graph/httpcore/RetryHandler.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.microsoft.graph.httpcore.middlewareoption.MiddlewareType;
1010
import com.microsoft.graph.httpcore.middlewareoption.RetryOptions;
1111
import com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions;
12+
import com.microsoft.graph.logger.DefaultLogger;
13+
import com.microsoft.graph.logger.ILogger;
1214

1315
import okhttp3.Interceptor;
1416
import okhttp3.Request;
@@ -69,10 +71,22 @@ public class RetryHandler implements Interceptor{
6971
*/
7072
private final long DELAY_MILLISECONDS = 1000;
7173

74+
private final ILogger logger;
75+
7276
/**
7377
* @param retryOption Create Retry handler using retry option
7478
*/
7579
public RetryHandler(@Nullable final RetryOptions retryOption) {
80+
this(new DefaultLogger(), retryOption);
81+
}
82+
/**
83+
* @param retryOption Create Retry handler using retry option
84+
* @param logger logger to use for telemetry
85+
*/
86+
public RetryHandler(@Nonnull final ILogger logger, @Nullable final RetryOptions retryOption) {
87+
if(logger == null)
88+
throw new IllegalArgumentException("logger parameter cannot be null");
89+
this.logger = logger;
7690
this.mRetryOption = retryOption;
7791
if(this.mRetryOption == null) {
7892
this.mRetryOption = new RetryOptions();
@@ -111,7 +125,7 @@ && checkStatus(statusCode) && isBuffered(response, request)
111125
try {
112126
Thread.sleep(retryInterval);
113127
} catch (InterruptedException e) {
114-
e.printStackTrace();
128+
logger.logError("error retrying the request", e);
115129
}
116130
}
117131
return shouldRetry;

‎src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,12 @@ public String answer(InvocationOnMock invocation) throws Throwable {
111111
});
112112

113113
assertThrows("the number of steps cannot exceed 20", IllegalArgumentException.class, () -> {
114-
new MSBatchRequestContent(null, null, null, null, null, null, null, null, null, null, null, null, null,
114+
new MSBatchRequestContent(mock(ILogger.class), null, null, null, null, null, null, null, null, null, null, null, null, null,
115115
null, null, null, null, null, null, null, null, null);
116116
});
117+
assertThrows("the logger cannot be null", IllegalArgumentException.class, () -> {
118+
new MSBatchRequestContent((ILogger) null, (MSBatchRequestStep)null);
119+
});
117120

118121
new MSBatchRequestContent(mockStep, null); // addind a null step doesn't throw
119122
assertThrows("should throw argument exception", IllegalArgumentException.class, () -> {
@@ -188,7 +191,7 @@ public void executeBatchTest() throws Throwable {
188191
}
189192
@Test
190193
public void responseParsing() {
191-
final MSBatchResponseContent batchResponse = new MSBatchResponseContent("https://graph.microsoft.com/v1.0",
194+
final MSBatchResponseContent batchResponse = new MSBatchResponseContent(mock(ILogger.class), "https://graph.microsoft.com/v1.0",
192195
JsonParser.parseString("{\"requests\":[{\"id\":\"1\",\"method\":\"GET\",\"url\":\"/me\"}]}")
193196
.getAsJsonObject(),
194197
JsonParser.parseString("{\"responses\": [{\"id\": \"1\",\"status\": 200,\"body\": null}]}")

‎src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import static org.junit.Assert.assertFalse;
44
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertThrows;
56
import static org.junit.Assert.assertTrue;
7+
import static org.mockito.Mockito.mock;
68

79
import java.net.HttpURLConnection;
810

@@ -147,6 +149,12 @@ public void testGetRetryAfterMaxExceed() {
147149
long delay = retryHandler.getRetryAfter(TestResponse(), 190, 1);
148150
assertTrue(delay == 180000);
149151
}
152+
@Test
153+
public void defensiveProgramming() {
154+
assertThrows("logger cannot be null", IllegalArgumentException.class, () -> {
155+
new RetryHandler(null, mock(RetryOptions.class));
156+
});
157+
}
150158

151159
Response TestResponse() {
152160
return new Response.Builder()

0 commit comments

Comments
 (0)