Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:master
base:master
Choose a base branch
Loading
fromdavidkron:apache-httpclient5
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionspom.xml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@
<module>scribejava-httpclient-ning</module>
<module>scribejava-httpclient-okhttp</module>
<module>scribejava-httpclient-apache</module>
<module>scribejava-httpclient-apache5</module>
<module>scribejava-httpclient-armeria</module>
</modules>

Expand Down
49 changes: 49 additions & 0 deletionsscribejava-httpclient-apache5/pom.xml
View file
Open in desktop
Original file line numberDiff line numberDiff 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>
View file
Open in desktop
Original file line numberDiff line numberDiff 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");
}
}
}
Original file line numberDiff line numberDiff 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());
}
}
Original file line numberDiff line numberDiff 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;
}
}
Original file line numberDiff line numberDiff 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);
}
}
Original file line numberDiff line numberDiff 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);
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp