8000 Can define a limit to the amount of content to log · sdecima/google-http-java-client@36e0401 · GitHub
[go: up one dir, main page]

Skip to content

Commit 36e0401

Browse files
committed
Can define a limit to the amount of content to log
1 parent db70575 commit 36e0401

File tree

6 files changed

+323
-6
lines changed

6 files changed

+323
-6
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,27 @@ static String executeAndGetValueOfSomeCustomHeader(HttpRequest request) {
9696
* Defaults to {@code false}.
9797
* </p>
9898
*/
99+
@Deprecated
99100
private boolean disableContentLogging;
100101

102+
/**
103+
* Determines the limit to the content size that will be logged during {@link #execute()}.
104+
*
105+
* <p>
106+
* If the content size is greater than this limit then it will not be logged.
107+
* </p>
108+
*
109+
* <p>
110+
* Can be set to {@code 0} to disable content logging. This is useful for example if content has
111+
* sensitive data such as authentication information.
112+
* </p>
113+
*
114+
* <p>
115+
* Defaults to {@code 100000 (100KB)}.
116+
* </p>
117+
*/
118+
private int contentLoggingLimit = 100000;
119+
101120
/** HTTP request content or {@code null} for none. */
102121
private HttpContent content;
103122

@@ -278,11 +297,35 @@ public HttpRequest setBackOffPolicy(BackOffPolicy backOffPolicy) {
278297
* {@link Level#ALL} is loggable which forces all logging).
279298
*
280299
* @since 1.5
300+
* @deprecated (scheduled to be removed in 1.8) Use {@link #getContentLoggingLimit}
281301
*/
302+
@Deprecated
282303
public boolean getDisableContentLogging() {
283304
return disableContentLogging;
284305
}
285306

307+
/**
308+
* Returns the limit to the content size that will be logged during {@link #execute()}.
309+
*
310+
* <p>
311+
* If the content size is greater than this limit then it will not be logged.
312+
* </p>
313+
*
314+
* <p>
315+
* Can be set to {@code 0} to disable content logging. This is useful for example if content has
316+
* sensitive data such as authentication information.
317+
* </p>
318+
*
319+
* <p>
320+
* Defaults to {@code 100000 (100KB)}.
321+
* </p>
322+
*
323+
* @since 1.7
324+
*/
325+
public int getContentLoggingLimit() {
326+
return contentLoggingLimit;
327+
}
328+
286329
/**
287 97AE 330
* Returns whether to disable request content logging during {@link #execute()} (unless
288331
* {@link Level#ALL} is loggable which forces all logging).
@@ -293,12 +336,39 @@ public boolean getDisableContentLogging() {
293336
* </p>
294337
*
295338
* @since 1.5
339+
* @deprecated (scheduled to be removed in 1.8) Use {@link #setContentLoggingLimit}
296340
*/
341+
@Deprecated
297342
public HttpRequest setDisableContentLogging(boolean disableContentLogging) {
298343
this.disableContentLogging = disableContentLogging;
299344
return this;
300345
}
301346

347+
/**
348+
* Set the limit to the content size that will be logged during {@link #execute()}.
349+
*
350+
* <p>
351+
* If the content size is greater than this limit then it will not be logged.
352+
* </p>
353+
*
354+
* <p>
355+
* Can be set to {@code 0} to disable content logging. This is useful for example if content has
356+
* sensitive data such as authentication information.
357+
* </p>
358+
*
359+
* <p>
360+
* Defaults to {@code 100000 (100KB)}.
361+
* </p>
362+
*
363+
* @since 1.7
364+
*/
365+
public HttpRequest setContentLoggingLimit(int contentLoggingLimit) {
366+
Preconditions.checkArgument(contentLoggingLimit >= 0,
367+
"The content logging limit must be non-negative.");
368+
this.contentLoggingLimit = contentLoggingLimit;
369+
return this;
370+
}
371+
302372
/**
303373
* Returns the timeout in milliseconds to establish a connection or {@code 0} for an infinite
304374
* timeout.
@@ -640,7 +710,10 @@ public HttpResponse execute() throws IOException {
640710
if (contentLength != 0 && contentEncoding == null
641711
&& LogContent.isTextBasedContentType(contentType)
642712
&& (loggable && !disableContentLogging || logger.isLoggable(Level.ALL))) {
643-
content = new LogContent(content, contentType, contentEncoding, contentLength);
713+
if (contentLength <= contentLoggingLimit && contentLoggingLimit != 0) {
714+
content = new LogContent(content, contentType, contentEncoding, contentLength,
715+
contentLoggingLimit);
716+
}
644717
}
645718
// gzip
646719
if (enableGZipContent) {

google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,27 @@ public final class HttpResponse {
9393
* {@code false}.
9494
* </p>
9595
*/
96+
@Deprecated
9697
private boolean disableContentLogging;
9798

99+
/**
100+
* Determines the limit to the content size that will be logged during {@link #getContent()}.
101+
*
102+
* <p>
103+
* If the content size is greater than this limit then it will not be logged.
104+
* </p>
105+
*
106+
* <p>
107+
* Can be set to {@code 0} to disable content logging. This is useful for example if content has
108+
* sensitive data such as authentication information.
109+
* </p>
110+
*
111+
* <p>
112+
* Defaults to {@code 100000 (100KB)}.
113+
* </p>
114+
*/
115+
private int contentLoggingLimit = 100000;
116+
98117
HttpResponse(HttpRequest request, LowLevelHttpResponse response) {
99118
this.request = request;
100119
transport = request.getTransport();
@@ -191,11 +210,35 @@ public final class HttpResponse {
191210
* {@link Level#ALL} is loggable which forces all logging).
192211
*
193212
* @since 1.5
213+
* @deprecated (scheduled to be removed in 1.8) Use {@link #getContentLoggingLimit}
194214
*/
215+
@Deprecated
195216
public boolean getDisableContentLogging() {
196217
return disableContentLogging;
197218
}
198219

220+
/**
221+
* Returns the limit to the content size that will be logged during {@link #getContent()}.
222+
*
223+
* <p>
224+
* If the content size is greater than this limit then it will not be logged.
225+
* </p>
226+
*
227+
* <p>
228+
* Can be set to {@code 0} to disable content logging. This is useful for example if content has
229+
* sensitive data such as authentication information.
230+
* </p>
231+
*
232+
* <p>
233+
* Defaults to {@code 100000 (100KB)}.
234+
* </p>
235+
*
236+
* @since 1.7
237+
*/
238+
public int getContentLoggingLimit() {
239+
return contentLoggingLimit;
240+
}
241+
199242
/**
200243
* Sets whether to disable response content logging during {@link #getContent()} (unless
201244
* {@link Level#ALL} is loggable which forces all logging).
@@ -206,12 +249,39 @@ public boolean getDisableContentLogging() {
206249
* </p>
207250
*
208251
* @since 1.5
252+
* @deprecated (scheduled to be removed in 1.8) Use {@link #setContentLoggingLimit}
209253
*/
254+
@Deprecated
210255
public HttpResponse setDisableContentLogging(boolean disableContentLogging) {
211256
this.disableContentLogging = disableContentLogging;
212257
return this;
213258
}
214259

260+
/**
261+
* Set the limit to the content size that will be logged during {@link #getContent()}.
262+
*
263+
* <p>
264+
* If the content size is greater than this limit then it will not be logged.
265+
* </p>
266+
*
267+
* <p>
268+
* Can be set to {@code 0} to disable content logging. This is useful for example if content has
269+
* sensitive data such as authentication information.
270+
* </p>
271+
*
272+
* <p>
273+
* Defaults to {@code 100000 (100KB)}.
274+
* </p>
275+
*
276+
* @since 1.7
277+
*/
278+
public HttpResponse setContentLoggingLimit(int contentLoggingLimit) {
279+
Preconditions.checkArgument(contentLoggingLimit >= 0,
280+
"The content logging limit must be non-negative.");
281+
this.contentLoggingLimit = contentLoggingLimit;
282+
return this;
283+
}
284+
215285
/**
216286
* Returns the content encoding or {@code null} for none.
217287
*
@@ -333,7 +403,12 @@ public InputStream getContent() throws IOException {
333403
// print content using a buffered input stream that can be re-read
334404
String contentType = this.contentType;
335405
if (debugContentByteArray.length != 0 && LogContent.isTextBasedContentType(contentType)) {
336-
logger.config(Strings.fromBytesUtf8(debugContentByteArray));
406+
if (debugContentByteArray.length <= contentLoggingLimit) {
407+
logger.config(Strings.fromBytesUtf8(debugContentByteArray));
408+
} else {
409+
logger.config("Content will not be logged because the content length " + contentLength
410+
+ " is greater than the content logging limit " + contentLoggingLimit);
411+
}
337412
}
338413
}
339414
this.content = content;

google-http-client/src/main/java/com/google/api/client/http/LogContent.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.api.client.http;
1616

1717
import com.google.api.client.util.Strings;
18+
import com.google.common.base.Preconditions;
1819

1920
import java.io.ByteArrayOutputStream;
2021
import java.io.IOException;
@@ -41,23 +42,29 @@ final class LogContent implements HttpContent {
4142
private final String contentType;
4243
private final String contentEncoding;
4344
private final long contentLength;
45+
private final int contentLoggingLimit;
4446

4547
/**
4648
* @param httpContent another source of HTTP content
4749
*/
48-
LogContent(
49-
HttpContent httpContent, String contentType, String contentEncoding, long contentLength) {
50+
LogContent(HttpContent httpContent, String contentType, String contentEncoding,
51+
long contentLength, int contentLoggingLimit) {
5052
this.httpContent = httpContent;
5153
this.contentType = contentType;
5254
this.contentLength = contentLength;
5355
this.contentEncoding = contentEncoding;
56+
Preconditions.checkArgument(contentLoggingLimit >= 0,
57+
"The content logging limit must be non-negative.");
58+
this.contentLoggingLimit = contentLoggingLimit;
5459
}
5560

5661
public void writeTo(OutputStream out) throws IOException {
5762
ByteArrayOutputStream debugStream = new ByteArrayOutputStream();
5863
httpContent.writeTo(debugStream);
5964
byte[] debugContent = debugStream.toByteArray();
60-
HttpTransport.LOGGER.config(Strings.fromBytesUtf8(debugContent));
65+
if (debugContent.length <= contentLoggingLimit) {
66+
HttpTransport.LOGGER.config(Strings.fromBytesUtf8(debugContent));
67+
}
6168
out.write(debugContent);
6269
}
6370

@@ -73,6 +80,10 @@ public String getType() {
7380
return contentType;
7481
}
7582

83+
int getContentLoggingLimit() {
84+
return contentLoggingLimit;
85+
}
86+
7687
/**
7788
* Returns whether the given content type is text rather than binary data.
7889
*

google-http-client/src/test/java/com/google/api/client/http/HttpRequestTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,61 @@ public void setContent(HttpContent content) throws IOException {
563563
request.execute();
564564
}
565565

566+
public void testContentLoggingLimit() throws IOException {
567+
568+
class MyTransport extends MockHttpTransport {
569+
570+
boolean expectLogContent;
571+
572+
@Override
573+
public LowLevelHttpRequest buildPostRequest(String url) throws IOException {
574+
return new MockLowLevelHttpRequest() {
575+
@Override
576+
public void setContent(HttpContent content) throws IOException {
577+
if (expectLogContent) {
578+
assertEquals(LogContent.class, content.getClass());
579+
} else {
580+
assertEquals(ByteArrayContent.class, content.getClass());
581+
}
582+
super.setContent(content);
583+
}
584+
};
585+
}
586+
}
587+
MyTransport transport = new MyTransport();
588+
// Set the logging level.
589+
HttpTransport.LOGGER.setLevel(java.util.logging.Level.CONFIG);
590+
// Create content of length 300.
591+
byte[] content = new byte[300];
592+
Arrays.fill(content, (byte) ' ');
593+
HttpRequest request =
594+
transport.createRequestFactory().buildPostRequest(HttpTesting.SIMPLE_GENERIC_URL,
595+
new ByteArrayContent("text/html", content));
596+
597+
// Set the content logging limit to be equal to the length of the content.
598+
transport.expectLogContent = true;
599+
request.setContentLoggingLimit(300);
600+
request.execute();
601+
602+
// Set the content logging limit to be less than the length of the content.
603+
transport.expectLogContent = false;
604+
request.setContentLoggingLimit(299);
605+
request.execute();
606+
607+
// Set the content logging limit to 0 to disable content logging.
608+
transport.expectLogContent = false;
609+
request.setContentLoggingLimit(0);
610+
request.execute();
611+
612+
// Assert that an exception is thrown if content logging limit < 0.
613+
try {
614+
request.setContentLoggingLimit(-1);
615+
fail("Expected: " + IllegalArgumentException.class);
616+
} catch (IllegalArgumentException e) {
617+
// Expected
618+
}
619+
}
620+
566621
public void testUserAgent() {
567622
assertTrue(HttpRequest.USER_AGENT_SUFFIX.contains("Google-HTTP-Java-Client"));
568623
assertTrue(HttpRequest.USER_AGENT_SUFFIX.contains("gzip"));

0 commit comments

Comments
 (0)
0