Skip to content

Commit 6dbc22e

Browse files
blakesmithkdavisk6
authored andcommitted
GH-1053: Add Google HTTP Client support (#1057)
Fixes #1053 Add Google HTTP Client submodule
1 parent 976e2c1 commit 6dbc22e

File tree

5 files changed

+241
-0
lines changed

5 files changed

+241
-0
lines changed

googlehttpclient/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Google Http Client - Feign Client
2+
3+
This module is a feign [Client](https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/Client.java) to use the java [Google Http Client](https://github.com/googleapis/google-http-java-client).
4+
5+
To use this, add to your classpath (via maven, or otherwise). Then cofigure Feign to use the GoogleHttpClient:
6+
7+
```java
8+
GitHub github = Feign.builder()
9+
.client(new GoogleHttpClient())
10+
.target(GitHub.class, "https://api.github.com");
11+
```

googlehttpclient/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2019 The Feign Authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7+
in compliance with the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software distributed under the License
12+
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13+
or implied. See the License for the specific language governing permissions and limitations under
14+
the License.
15+
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
20+
<parent>
21+
<groupId>io.github.openfeign</groupId>
22+
<artifactId>parent</artifactId>
23+
<version>10.3.1-SNAPSHOT</version>
24+
</parent>
25+
26+
<artifactId>feign-googlehttpclient</artifactId>
27+
<name>Feign Google HTTP Client</name>
28+
<description>Feign Google HTTP Client</description>
29+
30+
<properties>
31+
<main.basedir>${project.basedir}/..</main.basedir>
32+
</properties>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>${project.groupId}</groupId>
37+
<artifactId>feign-core</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>com.google.http-client</groupId>
42+
<artifactId>google-http-client</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>${project.groupId}</groupId>
47+
<artifactId>feign-core</artifactId>
48+
<type>test-jar</type>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>com.squareup.okhttp3</groupId>
54+
<artifactId>mockwebserver</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
</project>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* Copyright 2012-2019 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package feign.googlehttpclient;
16+
17+
import com.google.api.client.http.ByteArrayContent;
18+
import com.google.api.client.http.GenericUrl;
19+
import com.google.api.client.http.HttpContent;
20+
import com.google.api.client.http.HttpHeaders;
21+
import com.google.api.client.http.HttpTransport;
22+
import com.google.api.client.http.HttpRequestFactory;
23+
import com.google.api.client.http.HttpRequest;
24+
import com.google.api.client.http.HttpResponse;
25+
import com.google.api.client.http.javanet.NetHttpTransport;
26+
27+
import java.io.IOException;
28+
import java.util.Collection;
29+
import java.util.Map;
30+
import java.util.HashMap;
31+
32+
import feign.Client;
33+
import feign.Request;
34+
import feign.Response;
35+
import feign.Util;
36+
37+
38+
/**
39+
* This module directs Feign's http requests to
40+
* <a href="https://developers.google.com/api-client-library/java/google-http-java-client/">Google HTTP Client</a>.
41+
*
42+
* <pre>
43+
* GitHub github = Feign.builder().client(new GoogleHttpCliest()).target(GitHub.class,
44+
* "https://api.github.com");
45+
*/
46+
public class GoogleHttpClient implements Client {
47+
private final HttpTransport transport;
48+
private final HttpRequestFactory requestFactory;
49+
50+
public GoogleHttpClient() {
51+
this(new NetHttpTransport());
52+
}
53+
54+
public GoogleHttpClient(final HttpTransport transport) {
55+
this.transport = transport;
56+
this.requestFactory = transport.createRequestFactory();
57+
}
58+
59+
@Override
60+
public final Response execute(final Request inputRequest,
61+
final Request.Options options) throws IOException {
62+
final HttpRequest request = convertRequest(inputRequest, options);
63+
final HttpResponse response = request.execute();
64+
return convertResponse(inputRequest, response);
65+
}
66+
67+
private final HttpRequest convertRequest(final Request inputRequest,
68+
final Request.Options options) throws IOException {
69+
// Setup the request body
70+
HttpContent content = null;
71+
if (inputRequest.requestBody().length() > 0) {
72+
final Collection<String> contentTypeValues = inputRequest.headers().get("Content-Type");
73+
String contentType = null;
74+
if (contentTypeValues != null && contentTypeValues.size() > 0) {
75+
contentType = contentTypeValues.iterator().next();
76+
} else {
77+
contentType = "application/octet-stream";
78+
}
79+
content = new ByteArrayContent(contentType, inputRequest.requestBody().asBytes());
80+
}
81+
82+
// Build the request
83+
final HttpRequest request = requestFactory.buildRequest(inputRequest.httpMethod().name(),
84+
new GenericUrl(inputRequest.url()),
85+
content);
86+
// Setup headers
87+
final HttpHeaders headers = new HttpHeaders();
88+
for (final Map.Entry<String, Collection<String>> header : inputRequest.headers().entrySet()) {
89+
headers.set(header.getKey(), header.getValue());
90+
}
91+
// Some servers don't do well with no Accept header
92+
if (inputRequest.headers().get("Accept") == null) {
93+
headers.setAccept("*/*");
94+
}
95+
request.setHeaders(headers);
96+
97+
// Setup request options
98+
request.setReadTimeout(options.readTimeoutMillis())
99+
.setConnectTimeout(options.connectTimeoutMillis())
100+
.setFollowRedirects(options.isFollowRedirects())
101+
.setThrowExceptionOnExecuteError(false);
102+
return request;
103+
}
104+
105+
private final Response convertResponse(final Request inputRequest,
106+
final HttpResponse inputResponse) throws IOException {
107+
final HttpHeaders headers = inputResponse.getHeaders();
108+
Integer contentLength = null;
109+
if (headers.getContentLength() != null && headers.getContentLength() <= Integer.MAX_VALUE) {
110+
contentLength = inputResponse.getHeaders().getContentLength().intValue();
111+
}
112+
return Response.builder()
113+
.body(inputResponse.getContent(), contentLength)
114+
.status(inputResponse.getStatusCode())
115+
.reason(inputResponse.getStatusMessage())
116+
.headers(toMap(inputResponse.getHeaders()))
117+
.request(inputRequest)
118+
.build();
119+
}
120+
121+
private final Map<String, Collection<String>> toMap(final HttpHeaders headers) {
122+
final Map<String, Collection<String>> map = new HashMap<String, Collection<String>>();
123+
for (final String header : headers.keySet()) {
124+
map.put(header, headers.getHeaderStringValues(header));
125+
}
126+
return map;
127+
}
128+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2012-2019 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package feign.googlehttpclient;
16+
17+
import feign.Feign;
18+
import feign.Feign.Builder;
19+
import feign.client.AbstractClientTest;
20+
21+
public class GoogleHttpClientTest extends AbstractClientTest {
22+
@Override
23+
public Builder newBuilder() {
24+
return Feign.builder()
25+
.client(new GoogleHttpClient());
26+
}
27+
28+
// Google http client doesn't support PATCH. See: https://github.com/googleapis/google-http-java-client/issues/167
29+
@Override
30+
public void noResponseBodyForPatch() {
31+
}
32+
33+
@Override
34+
public void testPatch() {
35+
}
36+
}

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>jaxrs</module>
3737
<module>jaxrs2</module>
3838
<module>okhttp</module>
39+
<module>googlehttpclient</module>
3940
<module>ribbon</module>
4041
<module>sax</module>
4142
<module>slf4j</module>
@@ -67,6 +68,7 @@
6768
<main.basedir>${project.basedir}</main.basedir>
6869

6970
<okhttp3.version>3.6.0</okhttp3.version>
71+
<googlehttpclient.version>1.31.0</googlehttpclient.version>
7072
<gson.version>2.5</gson.version>
7173
<slf4j.version>1.7.13</slf4j.version>
7274
<bouncy.version>1.60</bouncy.version>
@@ -286,6 +288,12 @@
286288
<version>${okhttp3.version}</version>
287289
</dependency>
288290

291+
<dependency>
292+
<groupId>com.google.http-client</groupId>
293+
<artifactId>google-http-client</artifactId>
294+
<version>${googlehttpclient.version}</version>
295+
</dependency>
296+
289297
<dependency>
290298
<groupId>org.bouncycastle</groupId>
291299
<artifactId>bcprov-jdk15on</artifactId>

0 commit comments

Comments
 (0)