Skip to content

Commit 69fc2a8

Browse files
Luciano Leggierirstoyanchev
authored andcommitted
Support OkHttp as (Async)ClientHttpRequestFactory
This commit introduces support for OkHttp (http://square.github.io/okhttp/) as a backing implementation for ClientHttpRequestFactory and AsyncClientHttpRequestFactory. Issue: SPR-12893
1 parent d686f61 commit 69fc2a8

11 files changed

+433
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ project("spring-web") {
695695
optional("org.apache.httpcomponents:httpclient:${httpclientVersion}")
696696
optional("org.apache.httpcomponents:httpasyncclient:${httpasyncVersion}")
697697
optional("io.netty:netty-all:${nettyVersion}")
698+
optional("com.squareup.okhttp:okhttp:2.3.0")
698699
optional("com.fasterxml.jackson.core:jackson-databind:${jackson2Version}")
699700
optional("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jackson2Version}")
700701
optional("com.google.code.gson:gson:${gsonVersion}")
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.concurrent.ExecutionException;
24+
25+
import com.squareup.okhttp.Call;
26+
import com.squareup.okhttp.Callback;
27+
import com.squareup.okhttp.MediaType;
28+
import com.squareup.okhttp.OkHttpClient;
29+
import com.squareup.okhttp.Request;
30+
import com.squareup.okhttp.RequestBody;
31+
import com.squareup.okhttp.Response;
32+
33+
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.HttpMethod;
35+
import org.springframework.util.concurrent.ListenableFuture;
36+
import org.springframework.util.concurrent.SettableListenableFuture;
37+
38+
/**
39+
* {@link ClientHttpRequest} implementation that uses OkHttp to execute requests.
40+
*
41+
* <p>Created via the {@link OkHttpClientHttpRequestFactory}.
42+
*
43+
* @author Luciano Leggieri
44+
* @author Arjen Poutsma
45+
* @since 4.2
46+
*/
47+
class OkHttpClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest
48+
implements ClientHttpRequest {
49+
50+
private final OkHttpClient client;
51+
52+
private final URI uri;
53+
54+
private final HttpMethod method;
55+
56+
57+
public OkHttpClientHttpRequest(OkHttpClient client, URI uri, HttpMethod method) {
58+
this.client = client;
59+
this.uri = uri;
60+
this.method = method;
61+
}
62+
63+
64+
@Override
65+
public HttpMethod getMethod() {
66+
return method;
67+
}
68+
69+
@Override
70+
public URI getURI() {
71+
return uri;
72+
}
73+
74+
@Override
75+
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers,
76+
byte[] bufferedOutput) throws IOException {
77+
RequestBody body = bufferedOutput.length > 0 ?
78+
RequestBody.create(getContentType(headers), bufferedOutput) : null;
79+
80+
Request.Builder builder = new Request.Builder().
81+
url(this.uri.toURL()).
82+
method(this.method.name(), body);
83+
84+
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
85+
String headerName = entry.getKey();
86+
for (String headerValue : entry.getValue()) {
87+
builder.addHeader(headerName, headerValue);
88+
}
89+
}
90+
Request request = builder.build();
91+
92+
return new ListenableFutureCall(client.newCall(request));
93+
}
94+
95+
private MediaType getContentType(HttpHeaders headers) {
96+
org.springframework.http.MediaType contentType = headers.getContentType();
97+
return contentType != null ? MediaType.parse(contentType.toString()) : null;
98+
}
99+
100+
@Override
101+
public ClientHttpResponse execute() throws IOException {
102+
try {
103+
return executeAsync().get();
104+
}
105+
catch (InterruptedException ex) {
106+
throw new IOException(ex.getMessage(), ex);
107+
}
108+
catch (ExecutionException ex) {
109+
if (ex.getCause() instanceof IOException) {
110+
throw (IOException) ex.getCause();
111+
}
112+
else {
113+
throw new IOException(ex.getMessage(), ex);
114+
}
115+
}
116+
}
117+
118+
private static class ListenableFutureCall extends
119+
SettableListenableFuture<ClientHttpResponse> {
120+
121+
private final Call call;
122+
123+
public ListenableFutureCall(Call call) {
124+
this.call = call;
125+
this.call.enqueue(new Callback() {
126+
@Override
127+
public void onResponse(Response response) throws IOException {
128+
set(new OkHttpClientHttpResponse(response));
129+
}
130+
131+
@Override
132+
public void onFailure(Request request, IOException ex) {
133+
setException(ex);
134+
}
135+
});
136+
}
137+
138+
@Override
139+
protected void interruptTask() {
140+
call.cancel();
141+
}
142+
}
143+
144+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.net.URI;
20+
import java.util.concurrent.TimeUnit;
21+
22+
import com.squareup.okhttp.OkHttpClient;
23+
24+
import org.springframework.beans.factory.DisposableBean;
25+
import org.springframework.http.HttpMethod;
26+
import org.springframework.util.Assert;
27+
28+
/**
29+
* {@link ClientHttpRequestFactory} implementation that uses
30+
* <a href="http://square.github.io/okhttp/">OkHttp</a> to create requests.
31+
*
32+
* @author Luciano Leggieri
33+
* @author Arjen Poutsma
34+
* @since 4.2
35+
*/
36+
public class OkHttpClientHttpRequestFactory
37+
implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory,
38+
DisposableBean {
39+
40+
private final OkHttpClient client;
41+
42+
private final boolean defaultClient;
43+
44+
45+
/**
46+
* Create a new {@code OkHttpClientHttpRequestFactory} with a default
47+
* {@link OkHttpClient}.
48+
*/
49+
public OkHttpClientHttpRequestFactory() {
50+
client = new OkHttpClient();
51+
defaultClient = true;
52+
}
53+
54+
/**
55+
* Create a new {@code OkHttpClientHttpRequestFactory} with the given
56+
* {@link OkHttpClient}.
57+
* @param okHttpClient the client to use
58+
*/
59+
public OkHttpClientHttpRequestFactory(OkHttpClient okHttpClient) {
60+
Assert.notNull(okHttpClient, "'okHttpClient' must not be null");
61+
client = okHttpClient;
62+
defaultClient = false;
63+
}
64+
65+
66+
/**
67+
* Sets the underlying read timeout (in milliseconds).
68+
* A timeout value of 0 specifies an infinite timeout.
69+
* @see OkHttpClient#setReadTimeout(long, TimeUnit)
70+
*/
71+
public void setReadTimeout(int readTimeout) {
72+
this.client.setReadTimeout(readTimeout, TimeUnit.MILLISECONDS);
73+
}
74+
75+
/**
76+
* Sets the underlying write timeout (in milliseconds).
77+
* A timeout value of 0 specifies an infinite timeout.
78+
* @see OkHttpClient#setWriteTimeout(long, TimeUnit)
79+
*/
80+
public void setWriteTimeout(int writeTimeout) {
81+
this.client.setWriteTimeout(writeTimeout, TimeUnit.MILLISECONDS);
82+
}
83+
84+
/**
85+
* Sets the underlying connect timeout (in milliseconds).
86+
* A timeout value of 0 specifies an infinite timeout.
87+
* @see OkHttpClient#setConnectTimeout(long, TimeUnit)
88+
*/
89+
public void setConnectTimeout(int connectTimeout) {
90+
this.client.setConnectTimeout(connectTimeout, TimeUnit.MILLISECONDS);
91+
}
92+
93+
94+
@Override
95+
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) {
96+
return createRequestInternal(uri, httpMethod);
97+
}
98+
99+
@Override
100+
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) {
101+
return createRequestInternal(uri, httpMethod);
102+
}
103+
104+
private OkHttpClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMethod) {
105+
return new OkHttpClientHttpRequest(this.client, uri, httpMethod);
106+
}
107+
108+
@Override
109+
public void destroy() throws Exception {
110+
if (defaultClient) {
111+
// Clean up the client if we created it in the constructor
112+
if (this.client.getCache() != null) {
113+
this.client.getCache().close();
114+
}
115+
this.client.getDispatcher().getExecutorService().shutdown();
116+
}
117+
}
118+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.util.Map;
22+
23+
import com.squareup.okhttp.Response;
24+
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.util.Assert;
27+
28+
/**
29+
* {@link org.springframework.http.client.ClientHttpResponse} implementation that uses
30+
* OkHttp.
31+
*
32+
* @author Luciano Leggieri
33+
* @author Arjen Poutsma
34+
* @since 4.2
35+
*/
36+
class OkHttpClientHttpResponse extends AbstractClientHttpResponse {
37+
38+
private final Response response;
39+
40+
private HttpHeaders headers;
41+
42+
43+
public OkHttpClientHttpResponse(Response response) {
44+
Assert.notNull(response, "'response' must not be null");
45+
this.response = response;
46+
}
47+
48+
49+
@Override
50+
public int getRawStatusCode() {
51+
return response.code();
52+
}
53+
54+
@Override
55+
public String getStatusText() {
56+
return response.message();
57+
}
58+
59+
@Override
60+
public InputStream getBody() throws IOException {
61+
return response.body().byteStream();
62+
}
63+
64+
@Override
65+
public HttpHeaders getHeaders() {
66+
if (this.headers == null) {
67+
HttpHeaders headers = new HttpHeaders();
68+
for (String headerName : this.response.headers().names()) {
69+
for (String headerValue : this.response.headers(headerName)) {
70+
headers.add(headerName, headerValue);
71+
}
72+
}
73+
this.headers = headers;
74+
}
75+
return this.headers;
76+
}
77+
78+
@Override
79+
public void close() {
80+
try {
81+
response.body().close();
82+
}
83+
catch (IOException ignored) {
84+
}
85+
}
86+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected AsyncClientHttpRequestFactory createRequestFactory() {
3737
@Override
3838
@Test
3939
public void httpMethods() throws Exception {
40+
super.httpMethods();
4041
try {
4142
assertHttpMethod("patch", HttpMethod.PATCH);
4243
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected AsyncClientHttpRequestFactory createRequestFactory() {
3434
@Override
3535
@Test
3636
public void httpMethods() throws Exception {
37+
super.httpMethods();
3738
assertHttpMethod("patch", HttpMethod.PATCH);
3839
}
3940

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected ClientHttpRequestFactory createRequestFactory() {
4545
@Override
4646
@Test
4747
public void httpMethods() throws Exception {
48+
super.httpMethods();
4849
assertHttpMethod("patch", HttpMethod.PATCH);
4950
}
5051

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected AsyncClientHttpRequestFactory createRequestFactory() {
5050
@Override
5151
@Test
5252
public void httpMethods() throws Exception {
53+
super.httpMethods();
5354
assertHttpMethod("patch", HttpMethod.PATCH);
5455
}
5556

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected ClientHttpRequestFactory createRequestFactory() {
5050
@Override
5151
@Test
5252
public void httpMethods() throws Exception {
53+
super.httpMethods();
5354
assertHttpMethod("patch", HttpMethod.PATCH);
5455
}
5556

0 commit comments

Comments
 (0)