Issue: SPR-12893
This commit is contained in:
Rossen Stoyanchev 2015-05-18 16:47:05 -04:00
parent 69fc2a8ab2
commit 6468aa775c
5 changed files with 49 additions and 50 deletions

View File

@ -18,6 +18,7 @@ package org.springframework.http.client;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URL;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -32,6 +33,7 @@ import com.squareup.okhttp.Response;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.util.StringUtils;
import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SettableListenableFuture; import org.springframework.util.concurrent.SettableListenableFuture;
@ -63,23 +65,24 @@ class OkHttpClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest
@Override @Override
public HttpMethod getMethod() { public HttpMethod getMethod() {
return method; return this.method;
} }
@Override @Override
public URI getURI() { public URI getURI() {
return uri; return this.uri;
} }
@Override @Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers,
byte[] bufferedOutput) throws IOException { byte[] content) throws IOException {
RequestBody body = bufferedOutput.length > 0 ?
RequestBody.create(getContentType(headers), bufferedOutput) : null;
Request.Builder builder = new Request.Builder(). MediaType contentType = getContentType(headers);
url(this.uri.toURL()). RequestBody body = (content.length > 0 ? RequestBody.create(contentType, content) : null);
method(this.method.name(), body);
URL url = this.uri.toURL();
String methodName = this.method.name();
Request.Builder builder = new Request.Builder().url(url).method(methodName, body);
for (Map.Entry<String, List<String>> entry : headers.entrySet()) { for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey(); String headerName = entry.getKey();
@ -89,12 +92,12 @@ class OkHttpClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest
} }
Request request = builder.build(); Request request = builder.build();
return new ListenableFutureCall(client.newCall(request)); return new OkHttpListenableFuture(this.client.newCall(request));
} }
private MediaType getContentType(HttpHeaders headers) { private MediaType getContentType(HttpHeaders headers) {
org.springframework.http.MediaType contentType = headers.getContentType(); String rawContentType = headers.getFirst("Content-Type");
return contentType != null ? MediaType.parse(contentType.toString()) : null; return (StringUtils.hasText(rawContentType) ? MediaType.parse(rawContentType) : null);
} }
@Override @Override
@ -106,25 +109,24 @@ class OkHttpClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest
throw new IOException(ex.getMessage(), ex); throw new IOException(ex.getMessage(), ex);
} }
catch (ExecutionException ex) { catch (ExecutionException ex) {
if (ex.getCause() instanceof IOException) { Throwable cause = ex.getCause();
throw (IOException) ex.getCause(); if (cause instanceof IOException) {
} throw (IOException) cause;
else {
throw new IOException(ex.getMessage(), ex);
} }
throw new IOException(cause.getMessage(), cause);
} }
} }
private static class ListenableFutureCall extends private static class OkHttpListenableFuture extends SettableListenableFuture<ClientHttpResponse> {
SettableListenableFuture<ClientHttpResponse> {
private final Call call; private final Call call;
public ListenableFutureCall(Call call) { public OkHttpListenableFuture(Call call) {
this.call = call; this.call = call;
this.call.enqueue(new Callback() { this.call.enqueue(new Callback() {
@Override @Override
public void onResponse(Response response) throws IOException { public void onResponse(Response response) {
set(new OkHttpClientHttpResponse(response)); set(new OkHttpClientHttpResponse(response));
} }
@ -137,7 +139,7 @@ class OkHttpClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest
@Override @Override
protected void interruptTask() { protected void interruptTask() {
call.cancel(); this.call.cancel();
} }
} }

View File

@ -34,8 +34,7 @@ import org.springframework.util.Assert;
* @since 4.2 * @since 4.2
*/ */
public class OkHttpClientHttpRequestFactory public class OkHttpClientHttpRequestFactory
implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory, implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory, DisposableBean {
DisposableBean {
private final OkHttpClient client; private final OkHttpClient client;
@ -43,29 +42,27 @@ public class OkHttpClientHttpRequestFactory
/** /**
* Create a new {@code OkHttpClientHttpRequestFactory} with a default * Create a factory with a default {@link OkHttpClient} instance.
* {@link OkHttpClient}.
*/ */
public OkHttpClientHttpRequestFactory() { public OkHttpClientHttpRequestFactory() {
client = new OkHttpClient(); this.client = new OkHttpClient();
defaultClient = true; this.defaultClient = true;
} }
/** /**
* Create a new {@code OkHttpClientHttpRequestFactory} with the given * Create a factory with the given {@link OkHttpClient} instance.
* {@link OkHttpClient}. * @param client the client to use
* @param okHttpClient the client to use
*/ */
public OkHttpClientHttpRequestFactory(OkHttpClient okHttpClient) { public OkHttpClientHttpRequestFactory(OkHttpClient client) {
Assert.notNull(okHttpClient, "'okHttpClient' must not be null"); Assert.notNull(client, "'client' must not be null");
client = okHttpClient; this.client = client;
defaultClient = false; this.defaultClient = false;
} }
/** /**
* Sets the underlying read timeout (in milliseconds). * Sets the underlying read timeout in milliseconds.
* A timeout value of 0 specifies an infinite timeout. * A value of 0 specifies an infinite timeout.
* @see OkHttpClient#setReadTimeout(long, TimeUnit) * @see OkHttpClient#setReadTimeout(long, TimeUnit)
*/ */
public void setReadTimeout(int readTimeout) { public void setReadTimeout(int readTimeout) {
@ -73,8 +70,8 @@ public class OkHttpClientHttpRequestFactory
} }
/** /**
* Sets the underlying write timeout (in milliseconds). * Sets the underlying write timeout in milliseconds.
* A timeout value of 0 specifies an infinite timeout. * A value of 0 specifies an infinite timeout.
* @see OkHttpClient#setWriteTimeout(long, TimeUnit) * @see OkHttpClient#setWriteTimeout(long, TimeUnit)
*/ */
public void setWriteTimeout(int writeTimeout) { public void setWriteTimeout(int writeTimeout) {
@ -82,8 +79,8 @@ public class OkHttpClientHttpRequestFactory
} }
/** /**
* Sets the underlying connect timeout (in milliseconds). * Sets the underlying connect timeout in milliseconds.
* A timeout value of 0 specifies an infinite timeout. * A value of 0 specifies an infinite timeout.
* @see OkHttpClient#setConnectTimeout(long, TimeUnit) * @see OkHttpClient#setConnectTimeout(long, TimeUnit)
*/ */
public void setConnectTimeout(int connectTimeout) { public void setConnectTimeout(int connectTimeout) {
@ -107,7 +104,7 @@ public class OkHttpClientHttpRequestFactory
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
if (defaultClient) { if (this.defaultClient) {
// Clean up the client if we created it in the constructor // Clean up the client if we created it in the constructor
if (this.client.getCache() != null) { if (this.client.getCache() != null) {
this.client.getCache().close(); this.client.getCache().close();

View File

@ -18,7 +18,6 @@ package org.springframework.http.client;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Map;
import com.squareup.okhttp.Response; import com.squareup.okhttp.Response;
@ -48,17 +47,17 @@ class OkHttpClientHttpResponse extends AbstractClientHttpResponse {
@Override @Override
public int getRawStatusCode() { public int getRawStatusCode() {
return response.code(); return this.response.code();
} }
@Override @Override
public String getStatusText() { public String getStatusText() {
return response.message(); return this.response.message();
} }
@Override @Override
public InputStream getBody() throws IOException { public InputStream getBody() throws IOException {
return response.body().byteStream(); return this.response.body().byteStream();
} }
@Override @Override
@ -78,9 +77,10 @@ class OkHttpClientHttpResponse extends AbstractClientHttpResponse {
@Override @Override
public void close() { public void close() {
try { try {
response.body().close(); this.response.body().close();
} }
catch (IOException ignored) { catch (IOException ex) {
// Ignore
} }
} }
} }

View File

@ -16,8 +16,8 @@
package org.springframework.http.client; package org.springframework.http.client;
import com.squareup.okhttp.OkHttpClient;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
/** /**

View File

@ -16,8 +16,8 @@
package org.springframework.http.client; package org.springframework.http.client;
import com.squareup.okhttp.OkHttpClient;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
/** /**