Skip to content

Commit 171e4f2

Browse files
velokdavisk6
authored andcommitted
Create new module for Apache Http 5 (#1065)
Create new module for Apache Http Components Client version 5.x
1 parent 17b523f commit 171e4f2

File tree

7 files changed

+459
-16
lines changed

7 files changed

+459
-16
lines changed

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

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.HttpURLConnection;
2121
import java.nio.charset.Charset;
2222
import java.util.*;
23+
import java.util.concurrent.TimeUnit;
2324

2425
/** An immutable request to an http server. */
2526
public final class Request {
@@ -38,13 +39,17 @@ private Body(byte[] data, Charset encoding, BodyTemplate bodyTemplate) {
3839
}
3940

4041
public Request.Body expand(Map<String, ?> variables) {
41-
if (bodyTemplate == null) return this;
42+
if (bodyTemplate == null) {
43+
return this;
44+
}
4245

4346
return encoded(bodyTemplate.expand(variables).getBytes(encoding), encoding);
4447
}
4548

4649
public List<String> getVariables() {
47-
if (bodyTemplate == null) return Collections.emptyList();
50+
if (bodyTemplate == null) {
51+
return Collections.emptyList();
52+
}
4853
return bodyTemplate.getVariables();
4954
}
5055

@@ -70,12 +75,16 @@ public String bodyTemplate() {
7075
}
7176

7277
public String asString() {
73-
return encoding != null && data != null ? new String(data, encoding) : "Binary data";
78+
return !isBinary() ? new String(data, encoding) : "Binary data";
7479
}
7580

7681
public static Body empty() {
7782
return new Request.Body(null, null, null);
7883
}
84+
85+
public boolean isBinary() {
86+
return encoding == null || data == null;
87+
}
7988
}
8089

8190
public enum HttpMethod {
@@ -96,14 +105,15 @@ public enum HttpMethod {
96105
*
97106
* @deprecated {@link #create(HttpMethod, String, Map, byte[], Charset)}
98107
*/
108+
@Deprecated
99109
public static Request create(
100110
String method,
101111
String url,
102112
Map<String, Collection<String>> headers,
103113
byte[] body,
104114
Charset charset) {
105115
checkNotNull(method, "httpMethod of %s", method);
106-
HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
116+
final HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
107117
return create(httpMethod, url, headers, body, charset);
108118
}
109119

@@ -158,6 +168,7 @@ public static Request create(
158168
* @return the HttpMethod string
159169
* @deprecated @see {@link #httpMethod()}
160170
*/
171+
@Deprecated
161172
public String method() {
162173
return httpMethod.name();
163174
}
@@ -188,6 +199,7 @@ public Map<String, Collection<String>> headers() {
188199
*
189200
* @deprecated use {@link #requestBody()} instead
190201
*/
202+
@Deprecated
191203
public Charset charset() {
192204
return body.encoding;
193205
}
@@ -199,6 +211,7 @@ public Charset charset() {
199211
* @see #charset()
200212
* @deprecated use {@link #requestBody()} instead
201213
*/
214+
@Deprecated
202215
public byte[] body() {
203216
return body.data;
204217
}
@@ -209,10 +222,10 @@ public Body requestBody() {
209222

210223
@Override
211224
public String toString() {
212-
StringBuilder builder = new StringBuilder();
225+
final StringBuilder builder = new StringBuilder();
213226
builder.append(httpMethod).append(' ').append(url).append(" HTTP/1.1\n");
214-
for (String field : headers.keySet()) {
215-
for (String value : valuesOrEmpty(headers, field)) {
227+
for (final String field : headers.keySet()) {
228+
for (final String value : valuesOrEmpty(headers, field)) {
216229
builder.append(field).append(": ").append(value).append('\n');
217230
}
218231
}
@@ -228,40 +241,63 @@ public String toString() {
228241
*/
229242
public static class Options {
230243

231-
private final int connectTimeoutMillis;
232-
private final int readTimeoutMillis;
244+
private final long connectTimeout;
245+
private final TimeUnit connectTimeoutUnit;
246+
private final long readTimeout;
247+
private final TimeUnit readTimeoutUnit;
233248
private final boolean followRedirects;
234249

235-
public Options(int connectTimeoutMillis, int readTimeoutMillis, boolean followRedirects) {
236-
this.connectTimeoutMillis = connectTimeoutMillis;
237-
this.readTimeoutMillis = readTimeoutMillis;
250+
public Options(
251+
long connectTimeout,
252+
TimeUnit connectTimeoutUnit,
253+
long readTimeout,
254+
TimeUnit readTimeoutUnit,
255+
boolean followRedirects) {
256+
super();
257+
this.connectTimeout = connectTimeout;
258+
this.connectTimeoutUnit = connectTimeoutUnit;
259+
this.readTimeout = readTimeout;
260+
this.readTimeoutUnit = readTimeoutUnit;
238261
this.followRedirects = followRedirects;
239262
}
240263

264+
@Deprecated
265+
public Options(int connectTimeoutMillis, int readTimeoutMillis, boolean followRedirects) {
266+
this(
267+
connectTimeoutMillis,
268+
TimeUnit.MILLISECONDS,
269+
readTimeoutMillis,
270+
TimeUnit.MILLISECONDS,
271+
followRedirects);
272+
}
273+
274+
@Deprecated
241275
public Options(int connectTimeoutMillis, int readTimeoutMillis) {
242276
this(connectTimeoutMillis, readTimeoutMillis, true);
243277
}
244278

245279
public Options() {
246-
this(10 * 1000, 60 * 1000);
280+
this(10, TimeUnit.SECONDS, 60, TimeUnit.SECONDS, true);
247281
}
248282

249283
/**
250284
* Defaults to 10 seconds. {@code 0} implies no timeout.
251285
*
252286
* @see java.net.HttpURLConnection#getConnectTimeout()
253287
*/
288+
@Deprecated
254289
public int connectTimeoutMillis() {
255-
return connectTimeoutMillis;
290+
return (int) connectTimeoutUnit.toMillis(connectTimeout);
256291
}
257292

258293
/**
259294
* Defaults to 60 seconds. {@code 0} implies no timeout.
260295
*
261296
* @see java.net.HttpURLConnection#getReadTimeout()
262297
*/
298+
@Deprecated
263299
public int readTimeoutMillis() {
264-
return readTimeoutMillis;
300+
return (int) readTimeoutUnit.toMillis(readTimeout);
265301
}
266302

267303
/**
@@ -272,5 +308,21 @@ public int readTimeoutMillis() {
272308
public boolean isFollowRedirects() {
273309
return followRedirects;
274310
}
311+
312+
public long connectTimeout() {
313+
return connectTimeout;
314+
}
315+
316+
public TimeUnit connectTimeoutUnit() {
317+
return connectTimeoutUnit;
318+
}
319+
320+
public long readTimeout() {
321+
return readTimeout;
322+
}
323+
324+
public TimeUnit readTimeoutUnit() {
325+
return readTimeoutUnit;
326+
}
275327
}
276328
}

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)