From 6468aa775ca34cd3b052b9b78562ce0db951a1e4 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 18 May 2015 16:47:05 -0400 Subject: [PATCH] Polish Issue: SPR-12893 --- .../http/client/OkHttpClientHttpRequest.java | 46 ++++++++++--------- .../OkHttpClientHttpRequestFactory.java | 37 +++++++-------- .../http/client/OkHttpClientHttpResponse.java | 12 ++--- ...ttpAsyncClientHttpRequestFactoryTests.java | 2 +- .../OkHttpClientHttpRequestFactoryTests.java | 2 +- 5 files changed, 49 insertions(+), 50 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequest.java index 9e14d12994..ef424692cc 100644 --- a/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequest.java @@ -18,6 +18,7 @@ package org.springframework.http.client; import java.io.IOException; import java.net.URI; +import java.net.URL; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; @@ -32,6 +33,7 @@ import com.squareup.okhttp.Response; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.util.StringUtils; import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.SettableListenableFuture; @@ -63,23 +65,24 @@ class OkHttpClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest @Override public HttpMethod getMethod() { - return method; + return this.method; } @Override public URI getURI() { - return uri; + return this.uri; } @Override protected ListenableFuture executeInternal(HttpHeaders headers, - byte[] bufferedOutput) throws IOException { - RequestBody body = bufferedOutput.length > 0 ? - RequestBody.create(getContentType(headers), bufferedOutput) : null; + byte[] content) throws IOException { - Request.Builder builder = new Request.Builder(). - url(this.uri.toURL()). - method(this.method.name(), body); + MediaType contentType = getContentType(headers); + RequestBody body = (content.length > 0 ? RequestBody.create(contentType, content) : null); + + URL url = this.uri.toURL(); + String methodName = this.method.name(); + Request.Builder builder = new Request.Builder().url(url).method(methodName, body); for (Map.Entry> entry : headers.entrySet()) { String headerName = entry.getKey(); @@ -89,12 +92,12 @@ class OkHttpClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest } Request request = builder.build(); - return new ListenableFutureCall(client.newCall(request)); + return new OkHttpListenableFuture(this.client.newCall(request)); } private MediaType getContentType(HttpHeaders headers) { - org.springframework.http.MediaType contentType = headers.getContentType(); - return contentType != null ? MediaType.parse(contentType.toString()) : null; + String rawContentType = headers.getFirst("Content-Type"); + return (StringUtils.hasText(rawContentType) ? MediaType.parse(rawContentType) : null); } @Override @@ -106,25 +109,24 @@ class OkHttpClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest throw new IOException(ex.getMessage(), ex); } catch (ExecutionException ex) { - if (ex.getCause() instanceof IOException) { - throw (IOException) ex.getCause(); - } - else { - throw new IOException(ex.getMessage(), ex); + Throwable cause = ex.getCause(); + if (cause instanceof IOException) { + throw (IOException) cause; } + throw new IOException(cause.getMessage(), cause); } } - private static class ListenableFutureCall extends - SettableListenableFuture { + private static class OkHttpListenableFuture extends SettableListenableFuture { private final Call call; - public ListenableFutureCall(Call call) { + public OkHttpListenableFuture(Call call) { this.call = call; this.call.enqueue(new Callback() { - @Override - public void onResponse(Response response) throws IOException { + + @Override + public void onResponse(Response response) { set(new OkHttpClientHttpResponse(response)); } @@ -137,7 +139,7 @@ class OkHttpClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest @Override protected void interruptTask() { - call.cancel(); + this.call.cancel(); } } diff --git a/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequestFactory.java index c1f2b0b053..031b7dacbc 100644 --- a/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequestFactory.java @@ -34,8 +34,7 @@ import org.springframework.util.Assert; * @since 4.2 */ public class OkHttpClientHttpRequestFactory - implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory, - DisposableBean { + implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory, DisposableBean { private final OkHttpClient client; @@ -43,29 +42,27 @@ public class OkHttpClientHttpRequestFactory /** - * Create a new {@code OkHttpClientHttpRequestFactory} with a default - * {@link OkHttpClient}. + * Create a factory with a default {@link OkHttpClient} instance. */ public OkHttpClientHttpRequestFactory() { - client = new OkHttpClient(); - defaultClient = true; + this.client = new OkHttpClient(); + this.defaultClient = true; } /** - * Create a new {@code OkHttpClientHttpRequestFactory} with the given - * {@link OkHttpClient}. - * @param okHttpClient the client to use + * Create a factory with the given {@link OkHttpClient} instance. + * @param client the client to use */ - public OkHttpClientHttpRequestFactory(OkHttpClient okHttpClient) { - Assert.notNull(okHttpClient, "'okHttpClient' must not be null"); - client = okHttpClient; - defaultClient = false; + public OkHttpClientHttpRequestFactory(OkHttpClient client) { + Assert.notNull(client, "'client' must not be null"); + this.client = client; + this.defaultClient = false; } /** - * Sets the underlying read timeout (in milliseconds). - * A timeout value of 0 specifies an infinite timeout. + * Sets the underlying read timeout in milliseconds. + * A value of 0 specifies an infinite timeout. * @see OkHttpClient#setReadTimeout(long, TimeUnit) */ public void setReadTimeout(int readTimeout) { @@ -73,8 +70,8 @@ public class OkHttpClientHttpRequestFactory } /** - * Sets the underlying write timeout (in milliseconds). - * A timeout value of 0 specifies an infinite timeout. + * Sets the underlying write timeout in milliseconds. + * A value of 0 specifies an infinite timeout. * @see OkHttpClient#setWriteTimeout(long, TimeUnit) */ public void setWriteTimeout(int writeTimeout) { @@ -82,8 +79,8 @@ public class OkHttpClientHttpRequestFactory } /** - * Sets the underlying connect timeout (in milliseconds). - * A timeout value of 0 specifies an infinite timeout. + * Sets the underlying connect timeout in milliseconds. + * A value of 0 specifies an infinite timeout. * @see OkHttpClient#setConnectTimeout(long, TimeUnit) */ public void setConnectTimeout(int connectTimeout) { @@ -107,7 +104,7 @@ public class OkHttpClientHttpRequestFactory @Override public void destroy() throws Exception { - if (defaultClient) { + if (this.defaultClient) { // Clean up the client if we created it in the constructor if (this.client.getCache() != null) { this.client.getCache().close(); diff --git a/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpResponse.java index 370d1fdde1..55f702d2a8 100644 --- a/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpResponse.java @@ -18,7 +18,6 @@ package org.springframework.http.client; import java.io.IOException; import java.io.InputStream; -import java.util.Map; import com.squareup.okhttp.Response; @@ -48,17 +47,17 @@ class OkHttpClientHttpResponse extends AbstractClientHttpResponse { @Override public int getRawStatusCode() { - return response.code(); + return this.response.code(); } @Override public String getStatusText() { - return response.message(); + return this.response.message(); } @Override public InputStream getBody() throws IOException { - return response.body().byteStream(); + return this.response.body().byteStream(); } @Override @@ -78,9 +77,10 @@ class OkHttpClientHttpResponse extends AbstractClientHttpResponse { @Override public void close() { try { - response.body().close(); + this.response.body().close(); } - catch (IOException ignored) { + catch (IOException ex) { + // Ignore } } } diff --git a/spring-web/src/test/java/org/springframework/http/client/OkHttpAsyncClientHttpRequestFactoryTests.java b/spring-web/src/test/java/org/springframework/http/client/OkHttpAsyncClientHttpRequestFactoryTests.java index 82ce0f9394..d7233db604 100644 --- a/spring-web/src/test/java/org/springframework/http/client/OkHttpAsyncClientHttpRequestFactoryTests.java +++ b/spring-web/src/test/java/org/springframework/http/client/OkHttpAsyncClientHttpRequestFactoryTests.java @@ -16,8 +16,8 @@ package org.springframework.http.client; -import com.squareup.okhttp.OkHttpClient; import org.junit.Test; + import org.springframework.http.HttpMethod; /** diff --git a/spring-web/src/test/java/org/springframework/http/client/OkHttpClientHttpRequestFactoryTests.java b/spring-web/src/test/java/org/springframework/http/client/OkHttpClientHttpRequestFactoryTests.java index 32399d4c4f..7c6759a73a 100644 --- a/spring-web/src/test/java/org/springframework/http/client/OkHttpClientHttpRequestFactoryTests.java +++ b/spring-web/src/test/java/org/springframework/http/client/OkHttpClientHttpRequestFactoryTests.java @@ -16,8 +16,8 @@ package org.springframework.http.client; -import com.squareup.okhttp.OkHttpClient; import org.junit.Test; + import org.springframework.http.HttpMethod; /**