Skip to content

Commit 56b24f6

Browse files
authored
Add HTTP logging config to HttpConfigOptions (#53)
* feat: Add logging level to HTTP config options * refactor: Ensure input stream requests can always be sent properly regardless of logging * refactor: Deprecate HttpLogging in favor of new config option * chore: Remove old logging config file * test: Test for setting HTTP logging level
1 parent ee749b0 commit 56b24f6

File tree

6 files changed

+78
-29
lines changed

6 files changed

+78
-29
lines changed

src/main/java/com/ibm/cloud/sdk/core/http/HttpClientSingleton.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
*/
1313
package com.ibm.cloud.sdk.core.http;
1414

15+
import com.ibm.cloud.sdk.core.http.HttpConfigOptions.LoggingLevel;
1516
import com.ibm.cloud.sdk.core.service.BaseService;
1617
import com.ibm.cloud.sdk.core.service.security.DelegatingSSLSocketFactory;
17-
import com.ibm.cloud.sdk.core.util.HttpLogging;
1818
import okhttp3.Authenticator;
1919
import okhttp3.ConnectionSpec;
2020
import okhttp3.OkHttpClient;
2121
import okhttp3.OkHttpClient.Builder;
2222
import okhttp3.TlsVersion;
23+
import okhttp3.logging.HttpLoggingInterceptor;
2324

2425
import javax.net.ssl.HostnameVerifier;
2526
import javax.net.ssl.SSLContext;
@@ -115,8 +116,6 @@ private OkHttpClient configureHttpClient() {
115116
builder.writeTimeout(60, TimeUnit.SECONDS);
116117
builder.readTimeout(90, TimeUnit.SECONDS);
117118

118-
builder.addNetworkInterceptor(HttpLogging.getLoggingInterceptor());
119-
120119
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).allEnabledCipherSuites().build();
121120
builder.connectionSpecs(Arrays.asList(spec, ConnectionSpec.CLEARTEXT));
122121

@@ -190,6 +189,35 @@ private OkHttpClient setProxyAuthenticator(OkHttpClient client, Authenticator pr
190189
return builder.build();
191190
}
192191

192+
/**
193+
* Sets the logging level for the specified {@link OkHttpClient} instance and returns
194+
* a new instance with the logging configured as requested.
195+
*
196+
* @param client the {@link OkHttpClient} instance to set the proxy authenticator on
197+
* @param loggingLevel the {@link LoggingLevel}
198+
* @return the new {@link OkHttpClient} instance with the logging configured
199+
*/
200+
private OkHttpClient setLoggingLevel(OkHttpClient client, LoggingLevel loggingLevel) {
201+
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
202+
203+
switch (loggingLevel) {
204+
case BODY:
205+
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
206+
break;
207+
case HEADERS:
208+
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
209+
break;
210+
case BASIC:
211+
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
212+
break;
213+
default:
214+
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
215+
}
216+
OkHttpClient.Builder builder = client.newBuilder().addNetworkInterceptor(loggingInterceptor);
217+
218+
return builder.build();
219+
}
220+
193221
/**
194222
* Specifically enable all TLS protocols. See: https://github.com/watson-developer-cloud/java-sdk/issues/610
195223
*
@@ -276,6 +304,9 @@ public OkHttpClient configureClient(OkHttpClient client, HttpConfigOptions optio
276304
if (options.getProxyAuthenticator() != null) {
277305
client = setProxyAuthenticator(client, options.getProxyAuthenticator());
278306
}
307+
if (options.getLoggingLevel() != null) {
308+
client = setLoggingLevel(client, options.getLoggingLevel());
309+
}
279310
}
280311
return client;
281312
}

src/main/java/com/ibm/cloud/sdk/core/http/HttpConfigOptions.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@
88
* Options class for configuring the HTTP client.
99
*/
1010
public class HttpConfigOptions {
11+
12+
/**
13+
* Levels of information to log when making HTTP requests, from least (NONE) to most (BODY).
14+
*/
15+
public enum LoggingLevel {
16+
NONE,
17+
BASIC,
18+
HEADERS,
19+
BODY,
20+
}
21+
1122
private boolean disableSslVerification;
1223
private Proxy proxy;
1324
private Authenticator proxyAuthenticator;
25+
private LoggingLevel loggingLevel;
1426

1527
public boolean shouldDisableSslVerification() {
1628
return this.disableSslVerification;
@@ -24,10 +36,15 @@ public Authenticator getProxyAuthenticator() {
2436
return this.proxyAuthenticator;
2537
}
2638

39+
public LoggingLevel getLoggingLevel() {
40+
return this.loggingLevel;
41+
}
42+
2743
public static class Builder {
2844
private boolean disableSslVerification;
2945
private Proxy proxy;
3046
private Authenticator proxyAuthenticator;
47+
private LoggingLevel loggingLevel;
3148

3249
public HttpConfigOptions build() {
3350
return new HttpConfigOptions(this);
@@ -67,11 +84,22 @@ public Builder proxyAuthenticator(Authenticator proxyAuthenticator) {
6784
return this;
6885
}
6986

87+
/**
88+
* Sets HTTP logging level to be used by the current client.
89+
*
90+
* @param loggingLevel the {@link LoggingLevel} specifying how much information should be logged
91+
* @return the builder
92+
*/
93+
public Builder loggingLevel(LoggingLevel loggingLevel) {
94+
this.loggingLevel = loggingLevel;
95+
return this;
96+
}
7097
}
7198

7299
private HttpConfigOptions(Builder builder) {
73100
this.disableSslVerification = builder.disableSslVerification;
74101
this.proxy = builder.proxy;
75102
this.proxyAuthenticator = builder.proxyAuthenticator;
103+
this.loggingLevel = builder.loggingLevel;
76104
}
77105
}

src/main/java/com/ibm/cloud/sdk/core/http/InputStreamRequestBody.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@
1212
*/
1313
package com.ibm.cloud.sdk.core.http;
1414

15-
import java.io.ByteArrayInputStream;
16-
import java.io.ByteArrayOutputStream;
17-
import java.io.IOException;
18-
import java.io.InputStream;
19-
2015
import okhttp3.MediaType;
2116
import okhttp3.RequestBody;
2217
import okhttp3.internal.Util;
23-
import okhttp3.logging.HttpLoggingInterceptor;
2418
import okio.BufferedSink;
2519
import okio.Okio;
2620
import okio.Source;
2721
import org.apache.commons.io.IOUtils;
2822

29-
import com.ibm.cloud.sdk.core.util.HttpLogging;
23+
import java.io.ByteArrayInputStream;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.InputStream;
3027

3128
/**
3229
* RequestBody that takes an {@link InputStream}.
@@ -53,18 +50,15 @@ private InputStreamRequestBody(InputStream inputStream, MediaType mediaType) {
5350
this.inputStream = inputStream;
5451
this.mediaType = mediaType;
5552

56-
// if we're logging everything, we'll need to store the bytes to reuse later
57-
if (HttpLogging.getLoggingInterceptor().getLevel().equals(HttpLoggingInterceptor.Level.BODY)) {
58-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
59-
60-
try {
61-
IOUtils.copy(inputStream, outputStream);
62-
} catch (IOException e) {
63-
e.printStackTrace();
64-
}
53+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
6554

66-
this.bytes = outputStream.toByteArray();
55+
try {
56+
IOUtils.copy(inputStream, outputStream);
57+
} catch (IOException e) {
58+
e.printStackTrace();
6759
}
60+
61+
this.bytes = outputStream.toByteArray();
6862
}
6963

7064
/*

src/main/java/com/ibm/cloud/sdk/core/util/HttpLogging.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
* Instantiates a new HTTP logging. The logging level will be determinate by the {@link Logger} used in this class.
2424
* Basic HTTP request response will be log if Level is INFO, HEADERS if level is FINE and all the bodies if Level is
2525
* ALL.
26+
*
27+
* @deprecated This class functionality is now handled by {@link com.ibm.cloud.sdk.core.http.HttpConfigOptions} and
28+
* should no longer be needed.
2629
*/
2730
public class HttpLogging {
2831
private static final Logger LOG = Logger.getLogger(HttpLogging.class.getName());

src/test/java/com/ibm/cloud/sdk/core/test/http/HttpConfigTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ public Request authenticate(@Nullable Route route, Response response) throws IOE
3535
.disableSslVerification(true)
3636
.proxy(proxy)
3737
.proxyAuthenticator(authenticator)
38+
.loggingLevel(HttpConfigOptions.LoggingLevel.HEADERS)
3839
.build();
3940

4041
assertEquals(true, configOptions.shouldDisableSslVerification());
4142
assertEquals(authenticator, configOptions.getProxyAuthenticator());
4243
assertEquals(proxy, configOptions.getProxy());
44+
assertEquals(HttpConfigOptions.LoggingLevel.HEADERS, configOptions.getLoggingLevel());
4345
}
4446
}

src/test/resources/logging.properties

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)