parent
69fc2a8ab2
commit
6468aa775c
|
|
@ -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<ClientHttpResponse> 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<String, List<String>> 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<ClientHttpResponse> {
|
||||
private static class OkHttpListenableFuture extends SettableListenableFuture<ClientHttpResponse> {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@
|
|||
|
||||
package org.springframework.http.client;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@
|
|||
|
||||
package org.springframework.http.client;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue