Skip to content

Commit efc6cf4

Browse files
velokdavisk6
authored andcommitted
Create new module for Apache Http 5 (OpenFeign#1065)
Create new module for Apache Http Components Client version 5.x
1 parent 744fd72 commit efc6cf4

7 files changed

Lines changed: 453 additions & 16 deletions

File tree

core/src/main/java/feign/Request.java

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.net.HttpURLConnection;
1919
import java.nio.charset.Charset;
2020
import java.util.*;
21+
import java.util.concurrent.TimeUnit;
2122
import feign.template.BodyTemplate;
2223

2324
/**
@@ -39,15 +40,17 @@ private Body(byte[] data, Charset encoding, BodyTemplate bodyTemplate) {
3940
}
4041

4142
public Request.Body expand(Map<String, ?> variables) {
42-
if (bodyTemplate == null)
43+
if (bodyTemplate == null) {
4344
return this;
45+
}
4446

4547
return encoded(bodyTemplate.expand(variables).getBytes(encoding), encoding);
4648
}
4749

4850
public List<String> getVariables() {
49-
if (bodyTemplate == null)
51+
if (bodyTemplate == null) {
5052
return Collections.emptyList();
53+
}
5154
return bodyTemplate.getVariables();
5255
}
5356

@@ -73,7 +76,7 @@ public String bodyTemplate() {
7376
}
7477

7578
public String asString() {
76-
return encoding != null && data != null
79+
return !isBinary()
7780
? new String(data, encoding)
7881
: "Binary data";
7982
}
@@ -82,6 +85,10 @@ public static Body empty() {
8285
return new Request.Body(null, null, null);
8386
}
8487

88+
public boolean isBinary() {
89+
return encoding == null || data == null;
90+
}
91+
8592
}
8693

8794
public enum HttpMethod {
@@ -94,13 +101,14 @@ public enum HttpMethod {
94101
*
95102
* @deprecated {@link #create(HttpMethod, String, Map, byte[], Charset)}
96103
*/
104+
@Deprecated
97105
public static Request create(String method,
98106
String url,
99107
Map<String, Collection<String>> headers,
100108
byte[] body,
101109
Charset charset) {
102110
checkNotNull(method, "httpMethod of %s", method);
103-
HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
111+
final HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
104112
return create(httpMethod, url, headers, body, charset);
105113
}
106114

@@ -156,6 +164,7 @@ public static Request create(HttpMethod httpMethod,
156164
* @return the HttpMethod string
157165
* @deprecated @see {@link #httpMethod()}
158166
*/
167+
@Deprecated
159168
public String method() {
160169
return httpMethod.name();
161170
}
@@ -186,6 +195,7 @@ public Map<String, Collection<String>> headers() {
186195
*
187196
* @deprecated use {@link #requestBody()} instead
188197
*/
198+
@Deprecated
189199
public Charset charset() {
190200
return body.encoding;
191201
}
@@ -197,6 +207,7 @@ public Charset charset() {
197207
* @see #charset()
198208
* @deprecated use {@link #requestBody()} instead
199209
*/
210+
@Deprecated
200211
public byte[] body() {
201212
return body.data;
202213
}
@@ -207,10 +218,10 @@ public Body requestBody() {
207218

208219
@Override
209220
public String toString() {
210-
StringBuilder builder = new StringBuilder();
221+
final StringBuilder builder = new StringBuilder();
211222
builder.append(httpMethod).append(' ').append(url).append(" HTTP/1.1\n");
212-
for (String field : headers.keySet()) {
213-
for (String value : valuesOrEmpty(headers, field)) {
223+
for (final String field : headers.keySet()) {
224+
for (final String value : valuesOrEmpty(headers, field)) {
214225
builder.append(field).append(": ").append(value).append('\n');
215226
}
216227
}
@@ -226,40 +237,58 @@ public String toString() {
226237
*/
227238
public static class Options {
228239

229-
private final int connectTimeoutMillis;
230-
private final int readTimeoutMillis;
240+
private final long connectTimeout;
241+
private final TimeUnit connectTimeoutUnit;
242+
private final long readTimeout;
243+
private final TimeUnit readTimeoutUnit;
231244
private final boolean followRedirects;
232245

233-
public Options(int connectTimeoutMillis, int readTimeoutMillis, boolean followRedirects) {
234-
this.connectTimeoutMillis = connectTimeoutMillis;
235-
this.readTimeoutMillis = readTimeoutMillis;
246+
247+
public Options(long connectTimeout, TimeUnit connectTimeoutUnit,
248+
long readTimeout, TimeUnit readTimeoutUnit,
249+
boolean followRedirects) {
250+
super();
251+
this.connectTimeout = connectTimeout;
252+
this.connectTimeoutUnit = connectTimeoutUnit;
253+
this.readTimeout = readTimeout;
254+
this.readTimeoutUnit = readTimeoutUnit;
236255
this.followRedirects = followRedirects;
237256
}
238257

258+
@Deprecated
259+
public Options(int connectTimeoutMillis, int readTimeoutMillis, boolean followRedirects) {
260+
this(connectTimeoutMillis, TimeUnit.MILLISECONDS,
261+
readTimeoutMillis, TimeUnit.MILLISECONDS,
262+
followRedirects);
263+
}
264+
265+
@Deprecated
239266
public Options(int connectTimeoutMillis, int readTimeoutMillis) {
240267
this(connectTimeoutMillis, readTimeoutMillis, true);
241268
}
242269

243270
public Options() {
244-
this(10 * 1000, 60 * 1000);
271+
this(10, TimeUnit.SECONDS, 60, TimeUnit.SECONDS, true);
245272
}
246273

247274
/**
248275
* Defaults to 10 seconds. {@code 0} implies no timeout.
249276
*
250277
* @see java.net.HttpURLConnection#getConnectTimeout()
251278
*/
279+
@Deprecated
252280
public int connectTimeoutMillis() {
253-
return connectTimeoutMillis;
281+
return (int) connectTimeoutUnit.toMillis(connectTimeout);
254282
}
255283

256284
/**
257285
* Defaults to 60 seconds. {@code 0} implies no timeout.
258286
*
259287
* @see java.net.HttpURLConnection#getReadTimeout()
260288
*/
289+
@Deprecated
261290
public int readTimeoutMillis() {
262-
return readTimeoutMillis;
291+
return (int) readTimeoutUnit.toMillis(readTimeout);
263292
}
264293

265294

@@ -271,5 +300,22 @@ public int readTimeoutMillis() {
271300
public boolean isFollowRedirects() {
272301
return followRedirects;
273302
}
303+
304+
public long connectTimeout() {
305+
return connectTimeout;
306+
}
307+
308+
public TimeUnit connectTimeoutUnit() {
309+
return connectTimeoutUnit;
310+
}
311+
312+
public long readTimeout() {
313+
return readTimeout;
314+
}
315+
316+
public TimeUnit readTimeoutUnit() {
317+
return readTimeoutUnit;
318+
}
319+
274320
}
275321
}

hc5/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Apache Http Compoments 5
2+
========================
3+
4+
This module directs Feign's http requests to Apache's [HttpClient 5](https://hc.apache.org/httpcomponents-client-5.0.x/index.html).
5+
6+
To use HttpClient with Feign, add the `feign-hc5` module to your classpath. Then, configure Feign to use the `ApacheHttp5Client`:
7+
8+
```java
9+
GitHub github = Feign.builder()
10+
.client(new ApacheHttp5Client())
11+
.target(GitHub.class, "https://api.github.com");
12+
```

hc5/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.4.1-SNAPSHOT</version>
24+
</parent>
25+
26+
<artifactId>feign-hc5</artifactId>
27+
<name>Feign Apache Http Client 5</name>
28+
<description>Feign Apache HttpComponents Client 5</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>org.apache.httpcomponents.client5</groupId>
42+
<artifactId>httpclient5</artifactId>
43+
<version>5.0-beta5</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>${project.groupId}</groupId>
48+
<artifactId>feign-core</artifactId>
49+
<type>test-jar</type>
50+
<scope>test</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>${project.groupId}</groupId>
55+
<artifactId>feign-jaxrs2</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>com.squareup.okhttp3</groupId>
61+
<artifactId>mockwebserver</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
</project>

0 commit comments

Comments
 (0)