Skip to content

Commit 6468aa7

Browse files
committed
Polish
Issue: SPR-12893
1 parent 69fc2a8 commit 6468aa7

File tree

5 files changed

+49
-50
lines changed

5 files changed

+49
-50
lines changed

spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequest.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.net.URI;
21+
import java.net.URL;
2122
import java.util.List;
2223
import java.util.Map;
2324
import java.util.concurrent.ExecutionException;
@@ -32,6 +33,7 @@
3233

3334
import org.springframework.http.HttpHeaders;
3435
import org.springframework.http.HttpMethod;
36+
import org.springframework.util.StringUtils;
3537
import org.springframework.util.concurrent.ListenableFuture;
3638
import org.springframework.util.concurrent.SettableListenableFuture;
3739

@@ -63,23 +65,24 @@ public OkHttpClientHttpRequest(OkHttpClient client, URI uri, HttpMethod method)
6365

6466
@Override
6567
public HttpMethod getMethod() {
66-
return method;
68+
return this.method;
6769
}
6870

6971
@Override
7072
public URI getURI() {
71-
return uri;
73+
return this.uri;
7274
}
7375

7476
@Override
7577
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers,
76-
byte[] bufferedOutput) throws IOException {
77-
RequestBody body = bufferedOutput.length > 0 ?
78-
RequestBody.create(getContentType(headers), bufferedOutput) : null;
78+
byte[] content) throws IOException {
7979

80-
Request.Builder builder = new Request.Builder().
81-
url(this.uri.toURL()).
82-
method(this.method.name(), body);
80+
MediaType contentType = getContentType(headers);
81+
RequestBody body = (content.length > 0 ? RequestBody.create(contentType, content) : null);
82+
83+
URL url = this.uri.toURL();
84+
String methodName = this.method.name();
85+
Request.Builder builder = new Request.Builder().url(url).method(methodName, body);
8386

8487
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
8588
String headerName = entry.getKey();
@@ -89,12 +92,12 @@ protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders heade
8992
}
9093
Request request = builder.build();
9194

92-
return new ListenableFutureCall(client.newCall(request));
95+
return new OkHttpListenableFuture(this.client.newCall(request));
9396
}
9497

9598
private MediaType getContentType(HttpHeaders headers) {
96-
org.springframework.http.MediaType contentType = headers.getContentType();
97-
return contentType != null ? MediaType.parse(contentType.toString()) : null;
99+
String rawContentType = headers.getFirst("Content-Type");
100+
return (StringUtils.hasText(rawContentType) ? MediaType.parse(rawContentType) : null);
98101
}
99102

100103
@Override
@@ -106,25 +109,24 @@ public ClientHttpResponse execute() throws IOException {
106109
throw new IOException(ex.getMessage(), ex);
107110
}
108111
catch (ExecutionException ex) {
109-
if (ex.getCause() instanceof IOException) {
110-
throw (IOException) ex.getCause();
111-
}
112-
else {
113-
throw new IOException(ex.getMessage(), ex);
112+
Throwable cause = ex.getCause();
113+
if (cause instanceof IOException) {
114+
throw (IOException) cause;
114115
}
116+
throw new IOException(cause.getMessage(), cause);
115117
}
116118
}
117119

118-
private static class ListenableFutureCall extends
119-
SettableListenableFuture<ClientHttpResponse> {
120+
private static class OkHttpListenableFuture extends SettableListenableFuture<ClientHttpResponse> {
120121

121122
private final Call call;
122123

123-
public ListenableFutureCall(Call call) {
124+
public OkHttpListenableFuture(Call call) {
124125
this.call = call;
125126
this.call.enqueue(new Callback() {
126-
@Override
127-
public void onResponse(Response response) throws IOException {
127+
128+
@Override
129+
public void onResponse(Response response) {
128130
set(new OkHttpClientHttpResponse(response));
129131
}
130132

@@ -137,7 +139,7 @@ public void onFailure(Request request, IOException ex) {
137139

138140
@Override
139141
protected void interruptTask() {
140-
call.cancel();
142+
this.call.cancel();
141143
}
142144
}
143145

spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpRequestFactory.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,56 +34,53 @@
3434
* @since 4.2
3535
*/
3636
public class OkHttpClientHttpRequestFactory
37-
implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory,
38-
DisposableBean {
37+
implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory, DisposableBean {
3938

4039
private final OkHttpClient client;
4140

4241
private final boolean defaultClient;
4342

4443

4544
/**
46-
* Create a new {@code OkHttpClientHttpRequestFactory} with a default
47-
* {@link OkHttpClient}.
45+
* Create a factory with a default {@link OkHttpClient} instance.
4846
*/
4947
public OkHttpClientHttpRequestFactory() {
50-
client = new OkHttpClient();
51-
defaultClient = true;
48+
this.client = new OkHttpClient();
49+
this.defaultClient = true;
5250
}
5351

5452
/**
55-
* Create a new {@code OkHttpClientHttpRequestFactory} with the given
56-
* {@link OkHttpClient}.
57-
* @param okHttpClient the client to use
53+
* Create a factory with the given {@link OkHttpClient} instance.
54+
* @param client the client to use
5855
*/
59-
public OkHttpClientHttpRequestFactory(OkHttpClient okHttpClient) {
60-
Assert.notNull(okHttpClient, "'okHttpClient' must not be null");
61-
client = okHttpClient;
62-
defaultClient = false;
56+
public OkHttpClientHttpRequestFactory(OkHttpClient client) {
57+
Assert.notNull(client, "'client' must not be null");
58+
this.client = client;
59+
this.defaultClient = false;
6360
}
6461

6562

6663
/**
67-
* Sets the underlying read timeout (in milliseconds).
68-
* A timeout value of 0 specifies an infinite timeout.
64+
* Sets the underlying read timeout in milliseconds.
65+
* A value of 0 specifies an infinite timeout.
6966
* @see OkHttpClient#setReadTimeout(long, TimeUnit)
7067
*/
7168
public void setReadTimeout(int readTimeout) {
7269
this.client.setReadTimeout(readTimeout, TimeUnit.MILLISECONDS);
7370
}
7471

7572
/**
76-
* Sets the underlying write timeout (in milliseconds).
77-
* A timeout value of 0 specifies an infinite timeout.
73+
* Sets the underlying write timeout in milliseconds.
74+
* A value of 0 specifies an infinite timeout.
7875
* @see OkHttpClient#setWriteTimeout(long, TimeUnit)
7976
*/
8077
public void setWriteTimeout(int writeTimeout) {
8178
this.client.setWriteTimeout(writeTimeout, TimeUnit.MILLISECONDS);
8279
}
8380

8481
/**
85-
* Sets the underlying connect timeout (in milliseconds).
86-
* A timeout value of 0 specifies an infinite timeout.
82+
* Sets the underlying connect timeout in milliseconds.
83+
* A value of 0 specifies an infinite timeout.
8784
* @see OkHttpClient#setConnectTimeout(long, TimeUnit)
8885
*/
8986
public void setConnectTimeout(int connectTimeout) {
@@ -107,7 +104,7 @@ private OkHttpClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMe
107104

108105
@Override
109106
public void destroy() throws Exception {
110-
if (defaultClient) {
107+
if (this.defaultClient) {
111108
// Clean up the client if we created it in the constructor
112109
if (this.client.getCache() != null) {
113110
this.client.getCache().close();

spring-web/src/main/java/org/springframework/http/client/OkHttpClientHttpResponse.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21-
import java.util.Map;
2221

2322
import com.squareup.okhttp.Response;
2423

@@ -48,17 +47,17 @@ public OkHttpClientHttpResponse(Response response) {
4847

4948
@Override
5049
public int getRawStatusCode() {
51-
return response.code();
50+
return this.response.code();
5251
}
5352

5453
@Override
5554
public String getStatusText() {
56-
return response.message();
55+
return this.response.message();
5756
}
5857

5958
@Override
6059
public InputStream getBody() throws IOException {
61-
return response.body().byteStream();
60+
return this.response.body().byteStream();
6261
}
6362

6463
@Override
@@ -78,9 +77,10 @@ public HttpHeaders getHeaders() {
7877
@Override
7978
public void close() {
8079
try {
81-
response.body().close();
80+
this.response.body().close();
8281
}
83-
catch (IOException ignored) {
82+
catch (IOException ex) {
83+
// Ignore
8484
}
8585
}
8686
}

spring-web/src/test/java/org/springframework/http/client/OkHttpAsyncClientHttpRequestFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.http.client;
1818

19-
import com.squareup.okhttp.OkHttpClient;
2019
import org.junit.Test;
20+
2121
import org.springframework.http.HttpMethod;
2222

2323
/**

spring-web/src/test/java/org/springframework/http/client/OkHttpClientHttpRequestFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.http.client;
1818

19-
import com.squareup.okhttp.OkHttpClient;
2019
import org.junit.Test;
20+
2121
import org.springframework.http.HttpMethod;
2222

2323
/**

0 commit comments

Comments
 (0)