Skip to content

Commit

Permalink
Create new module for Apache Http 5 (#1065)
Browse files Browse the repository at this point in the history
Create new module for Apache Http Components Client version 5.x
  • Loading branch information
velo authored and kdavisk6 committed Sep 10, 2019
1 parent 744fd72 commit efc6cf4
Show file tree
Hide file tree
Showing 7 changed files with 453 additions and 16 deletions.
76 changes: 61 additions & 15 deletions core/src/main/java/feign/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.TimeUnit;
import feign.template.BodyTemplate;

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

public Request.Body expand(Map<String, ?> variables) {
if (bodyTemplate == null)
if (bodyTemplate == null) {
return this;
}

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

public List<String> getVariables() {
if (bodyTemplate == null)
if (bodyTemplate == null) {
return Collections.emptyList();
}
return bodyTemplate.getVariables();
}

Expand All @@ -73,7 +76,7 @@ public String bodyTemplate() {
}

public String asString() {
return encoding != null && data != null
return !isBinary()
? new String(data, encoding)
: "Binary data";
}
Expand All @@ -82,6 +85,10 @@ public static Body empty() {
return new Request.Body(null, null, null);
}

public boolean isBinary() {
return encoding == null || data == null;
}

}

public enum HttpMethod {
Expand All @@ -94,13 +101,14 @@ public enum HttpMethod {
*
* @deprecated {@link #create(HttpMethod, String, Map, byte[], Charset)}
*/
@Deprecated
public static Request create(String method,
String url,
Map<String, Collection<String>> headers,
byte[] body,
Charset charset) {
checkNotNull(method, "httpMethod of %s", method);
HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
final HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
return create(httpMethod, url, headers, body, charset);
}

Expand Down Expand Up @@ -156,6 +164,7 @@ public static Request create(HttpMethod httpMethod,
* @return the HttpMethod string
* @deprecated @see {@link #httpMethod()}
*/
@Deprecated
public String method() {
return httpMethod.name();
}
Expand Down Expand Up @@ -186,6 +195,7 @@ public Map<String, Collection<String>> headers() {
*
* @deprecated use {@link #requestBody()} instead
*/
@Deprecated
public Charset charset() {
return body.encoding;
}
Expand All @@ -197,6 +207,7 @@ public Charset charset() {
* @see #charset()
* @deprecated use {@link #requestBody()} instead
*/
@Deprecated
public byte[] body() {
return body.data;
}
Expand All @@ -207,10 +218,10 @@ public Body requestBody() {

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
final StringBuilder builder = new StringBuilder();
builder.append(httpMethod).append(' ').append(url).append(" HTTP/1.1\n");
for (String field : headers.keySet()) {
for (String value : valuesOrEmpty(headers, field)) {
for (final String field : headers.keySet()) {
for (final String value : valuesOrEmpty(headers, field)) {
builder.append(field).append(": ").append(value).append('\n');
}
}
Expand All @@ -226,40 +237,58 @@ public String toString() {
*/
public static class Options {

private final int connectTimeoutMillis;
private final int readTimeoutMillis;
private final long connectTimeout;
private final TimeUnit connectTimeoutUnit;
private final long readTimeout;
private final TimeUnit readTimeoutUnit;
private final boolean followRedirects;

public Options(int connectTimeoutMillis, int readTimeoutMillis, boolean followRedirects) {
this.connectTimeoutMillis = connectTimeoutMillis;
this.readTimeoutMillis = readTimeoutMillis;

public Options(long connectTimeout, TimeUnit connectTimeoutUnit,
long readTimeout, TimeUnit readTimeoutUnit,
boolean followRedirects) {
super();
this.connectTimeout = connectTimeout;
this.connectTimeoutUnit = connectTimeoutUnit;
this.readTimeout = readTimeout;
this.readTimeoutUnit = readTimeoutUnit;
this.followRedirects = followRedirects;
}

@Deprecated
public Options(int connectTimeoutMillis, int readTimeoutMillis, boolean followRedirects) {
this(connectTimeoutMillis, TimeUnit.MILLISECONDS,
readTimeoutMillis, TimeUnit.MILLISECONDS,
followRedirects);
}

@Deprecated
public Options(int connectTimeoutMillis, int readTimeoutMillis) {
this(connectTimeoutMillis, readTimeoutMillis, true);
}

public Options() {
this(10 * 1000, 60 * 1000);
this(10, TimeUnit.SECONDS, 60, TimeUnit.SECONDS, true);
}

/**
* Defaults to 10 seconds. {@code 0} implies no timeout.
*
* @see java.net.HttpURLConnection#getConnectTimeout()
*/
@Deprecated
public int connectTimeoutMillis() {
return connectTimeoutMillis;
return (int) connectTimeoutUnit.toMillis(connectTimeout);
}

/**
* Defaults to 60 seconds. {@code 0} implies no timeout.
*
* @see java.net.HttpURLConnection#getReadTimeout()
*/
@Deprecated
public int readTimeoutMillis() {
return readTimeoutMillis;
return (int) readTimeoutUnit.toMillis(readTimeout);
}


Expand All @@ -271,5 +300,22 @@ public int readTimeoutMillis() {
public boolean isFollowRedirects() {
return followRedirects;
}

public long connectTimeout() {
return connectTimeout;
}

public TimeUnit connectTimeoutUnit() {
return connectTimeoutUnit;
}

public long readTimeout() {
return readTimeout;
}

public TimeUnit readTimeoutUnit() {
return readTimeoutUnit;
}

}
}
12 changes: 12 additions & 0 deletions hc5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Apache Http Compoments 5
========================

This module directs Feign's http requests to Apache's [HttpClient 5](https://hc.apache.org/httpcomponents-client-5.0.x/index.html).

To use HttpClient with Feign, add the `feign-hc5` module to your classpath. Then, configure Feign to use the `ApacheHttp5Client`:

```java
GitHub github = Feign.builder()
.client(new ApacheHttp5Client())
.target(GitHub.class, "https://api.github.com");
```
65 changes: 65 additions & 0 deletions hc5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2012-2019 The Feign Authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
-->
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.openfeign</groupId>
<artifactId>parent</artifactId>
<version>10.4.1-SNAPSHOT</version>
</parent>

<artifactId>feign-hc5</artifactId>
<name>Feign Apache Http Client 5</name>
<description>Feign Apache HttpComponents Client 5</description>

<properties>
<main.basedir>${project.basedir}/..</main.basedir>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>feign-core</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.0-beta5</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>feign-core</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>feign-jaxrs2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit efc6cf4

Please sign in to comment.