Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.7k
Apache HttpClient 5 integration#1017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
davidkron wants to merge1 commit intoscribejava:masterChoose a base branch fromdavidkron:apache-httpclient5
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionspom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletionsscribejava-httpclient-apache5/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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>com.github.scribejava</groupId> | ||
<artifactId>scribejava</artifactId> | ||
<version>8.3.2-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<groupId>com.github.scribejava</groupId> | ||
<artifactId>scribejava-httpclient-apache5</artifactId> | ||
<name>ScribeJava Apache HttpComponents HttpClient 5 support</name> | ||
<packaging>jar</packaging> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.scribejava</groupId> | ||
<artifactId>scribejava-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents.client5</groupId> | ||
<artifactId>httpclient5</artifactId> | ||
<version>5.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.scribejava</groupId> | ||
<artifactId>scribejava-core</artifactId> | ||
<version>${project.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.felix</groupId> | ||
<artifactId>maven-bundle-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
136 changes: 136 additions & 0 deletions...ent-apache5/src/main/java/com/github/scribejava/httpclient/apache5/ApacheHttpClient5.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.github.scribejava.httpclient.apache5; | ||
import com.github.scribejava.core.httpclient.AbstractAsyncOnlyHttpClient; | ||
import com.github.scribejava.core.httpclient.multipart.MultipartPayload; | ||
import com.github.scribejava.core.model.OAuthAsyncRequestCallback; | ||
import com.github.scribejava.core.model.OAuthConstants; | ||
import com.github.scribejava.core.model.OAuthRequest; | ||
import com.github.scribejava.core.model.Verb; | ||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; | ||
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder; | ||
import org.apache.hc.core5.http.nio.AsyncEntityProducer; | ||
import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer; | ||
import org.apache.hc.core5.http.nio.entity.FileEntityProducer; | ||
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer; | ||
import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
public class ApacheHttpClient5 extends AbstractAsyncOnlyHttpClient { | ||
private static final int ENTITY_CONSUMER_BUFFER_SIZE = 4096; | ||
private static final int ENTITY_CONSUMER_THREADS = 5; | ||
private final CloseableHttpAsyncClient client; | ||
private final ExecutorService entityConsumerExecutor; | ||
public ApacheHttpClient5() { | ||
this(ApacheHttpClient5Config.defaultConfig()); | ||
} | ||
public ApacheHttpClient5(ApacheHttpClient5Config config) { | ||
this(config.getHttpAsyncClientBuilder()); | ||
} | ||
public ApacheHttpClient5(HttpAsyncClientBuilder builder) { | ||
this(builder.build()); | ||
} | ||
public ApacheHttpClient5(CloseableHttpAsyncClient client) { | ||
this.client = client; | ||
this.client.start(); | ||
entityConsumerExecutor = Executors.newFixedThreadPool(ENTITY_CONSUMER_THREADS); | ||
} | ||
@Override | ||
public void close() throws IOException { | ||
client.close(); | ||
} | ||
@Override | ||
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
byte[] bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) { | ||
final AsyncEntityProducer entity = bodyContents == null ? null : new BasicAsyncEntityProducer(bodyContents); | ||
return doExecuteAsync(userAgent, headers, httpVerb, completeUrl, entity, callback, converter); | ||
} | ||
@Override | ||
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
MultipartPayload bodyContents, OAuthAsyncRequestCallback<T> callback, | ||
OAuthRequest.ResponseConverter<T> converter) { | ||
throw new UnsupportedOperationException("ApacheHttpClient does not support MultipartPayload yet."); | ||
} | ||
@Override | ||
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
String bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) { | ||
final AsyncEntityProducer entity = bodyContents == null ? null : new StringAsyncEntityProducer(bodyContents); | ||
return doExecuteAsync(userAgent, headers, httpVerb, completeUrl, entity, callback, converter); | ||
} | ||
@Override | ||
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
File bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) { | ||
final AsyncEntityProducer entity = bodyContents == null ? null : new FileEntityProducer(bodyContents); | ||
return doExecuteAsync(userAgent, headers, httpVerb, completeUrl, entity, callback, converter); | ||
} | ||
private <T> Future<T> doExecuteAsync(String userAgent, Map<String, String> headers, Verb httpVerb, | ||
String completeUrl, AsyncEntityProducer entityProducer, OAuthAsyncRequestCallback<T> callback, | ||
OAuthRequest.ResponseConverter<T> converter) { | ||
final AsyncRequestBuilder builder = getRequestBuilder(httpVerb); | ||
builder.setUri(completeUrl); | ||
if (httpVerb.isPermitBody()) { | ||
if (!headers.containsKey(CONTENT_TYPE)) { | ||
builder.addHeader(CONTENT_TYPE, DEFAULT_CONTENT_TYPE); | ||
} | ||
builder.setEntity(entityProducer); | ||
} | ||
for (Map.Entry<String, String> header : headers.entrySet()) { | ||
builder.addHeader(header.getKey(), header.getValue()); | ||
} | ||
if (userAgent != null) { | ||
builder.setHeader(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent); | ||
} | ||
final AsyncHttpEntityConsumer entityConsumer = new AsyncHttpEntityConsumer( | ||
ENTITY_CONSUMER_BUFFER_SIZE, entityConsumerExecutor); | ||
final ResponseWithEntityConsumer responseConsumer = new ResponseWithEntityConsumer(entityConsumer); | ||
final OAuthAsyncCompletionHandler<T> handler = new OAuthAsyncCompletionHandler<>(callback, converter); | ||
final Future<ResponseWithEntity> future = client.execute(builder.build(), responseConsumer, handler); | ||
return new ApacheHttpFuture<>(future, handler); | ||
} | ||
private static AsyncRequestBuilder getRequestBuilder(Verb httpVerb) { | ||
switch (httpVerb) { | ||
case GET: | ||
return AsyncRequestBuilder.get(); | ||
case PUT: | ||
return AsyncRequestBuilder.put(); | ||
case DELETE: | ||
return AsyncRequestBuilder.delete(); | ||
case HEAD: | ||
return AsyncRequestBuilder.head(); | ||
case POST: | ||
return AsyncRequestBuilder.post(); | ||
case PATCH: | ||
return AsyncRequestBuilder.patch(); | ||
case TRACE: | ||
return AsyncRequestBuilder.trace(); | ||
case OPTIONS: | ||
return AsyncRequestBuilder.options(); | ||
default: | ||
throw new IllegalArgumentException("message build error: unknown verb type"); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions...ache5/src/main/java/com/github/scribejava/httpclient/apache5/ApacheHttpClient5Config.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.github.scribejava.httpclient.apache5; | ||
import com.github.scribejava.core.httpclient.HttpClientConfig; | ||
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder; | ||
public class ApacheHttpClient5Config implements HttpClientConfig { | ||
private final HttpAsyncClientBuilder httpAsyncClientBuilder; | ||
public ApacheHttpClient5Config(HttpAsyncClientBuilder httpAsyncClientBuilder) { | ||
this.httpAsyncClientBuilder = httpAsyncClientBuilder; | ||
} | ||
public HttpAsyncClientBuilder getHttpAsyncClientBuilder() { | ||
return httpAsyncClientBuilder; | ||
} | ||
@Override | ||
public HttpClientConfig createDefaultConfig() { | ||
return defaultConfig(); | ||
} | ||
public static ApacheHttpClient5Config defaultConfig() { | ||
return new ApacheHttpClient5Config(HttpAsyncClientBuilder.create()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions...he5/src/main/java/com/github/scribejava/httpclient/apache5/ApacheHttpClient5Provider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.github.scribejava.httpclient.apache5; | ||
import com.github.scribejava.core.httpclient.HttpClient; | ||
import com.github.scribejava.core.httpclient.HttpClientConfig; | ||
import com.github.scribejava.core.httpclient.HttpClientProvider; | ||
public class ApacheHttpClient5Provider implements HttpClientProvider { | ||
@Override | ||
public HttpClient createClient(HttpClientConfig httpClientConfig) { | ||
if (httpClientConfig instanceof ApacheHttpClient5Config) { | ||
return new ApacheHttpClient5((ApacheHttpClient5Config) httpClientConfig); | ||
} | ||
return null; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions...ient-apache5/src/main/java/com/github/scribejava/httpclient/apache5/ApacheHttpFuture.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.scribejava.httpclient.apache5; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
class ApacheHttpFuture<T> implements Future<T> { | ||
private final Future<ResponseWithEntity> future; | ||
private final OAuthAsyncCompletionHandler<T> handler; | ||
ApacheHttpFuture(Future<ResponseWithEntity> future, OAuthAsyncCompletionHandler<T> handler) { | ||
this.future = future; | ||
this.handler = handler; | ||
} | ||
@Override | ||
public boolean cancel(boolean mayInterruptIfRunning) { | ||
return future.cancel(mayInterruptIfRunning); | ||
} | ||
@Override | ||
public boolean isCancelled() { | ||
return future.isCancelled(); | ||
} | ||
@Override | ||
public boolean isDone() { | ||
return future.isDone(); | ||
} | ||
@Override | ||
public T get() throws InterruptedException, ExecutionException { | ||
return handler.getResult(); | ||
} | ||
@Override | ||
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { | ||
return handler.getResult(timeout, unit); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions...ache5/src/main/java/com/github/scribejava/httpclient/apache5/AsyncHttpEntityConsumer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.github.scribejava.httpclient.apache5; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.apache.hc.core5.http.HttpEntity; | ||
import org.apache.hc.core5.http.io.entity.InputStreamEntity; | ||
import org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityConsumer; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.concurrent.Executor; | ||
/** | ||
* Compatibility with classic HttpClient API: | ||
* Consumes the async response as an input stream and creates an {@link HttpEntity}. | ||
*/ | ||
public class AsyncHttpEntityConsumer extends AbstractClassicEntityConsumer<HttpEntity> { | ||
public AsyncHttpEntityConsumer(int initialBufferSize, Executor executor) { | ||
super(initialBufferSize, executor); | ||
} | ||
@Override | ||
protected HttpEntity consumeData(ContentType contentType, InputStream inputStream) throws IOException { | ||
return new InputStreamEntity(inputStream, contentType); | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.