All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.scribejava.httpclient.ahc.AhcHttpClient Maven / Gradle / Ivy
package com.github.scribejava.httpclient.ahc;
import com.github.scribejava.core.httpclient.AbstractAsyncOnlyHttpClient;
import com.github.scribejava.core.httpclient.MultipartPayload;
import com.github.scribejava.core.java8.Consumer;
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.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClient;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Future;
import java.io.File;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.BoundRequestBuilder;
public class AhcHttpClient extends AbstractAsyncOnlyHttpClient {
private final AsyncHttpClient client;
public AhcHttpClient() {
this(AhcHttpClientConfig.defaultConfig());
}
public AhcHttpClient(AhcHttpClientConfig ahcConfig) {
final AsyncHttpClientConfig clientConfig = ahcConfig.getClientConfig();
client = clientConfig == null ? new DefaultAsyncHttpClient() : new DefaultAsyncHttpClient(clientConfig);
}
public AhcHttpClient(AsyncHttpClient ahcClient) {
client = ahcClient;
}
@Override
public void close() throws IOException {
client.close();
}
@Override
public Future executeAsync(String userAgent, Map headers, Verb httpVerb, String completeUrl,
final byte[] bodyContents, OAuthAsyncRequestCallback callback,
OAuthRequest.ResponseConverter converter) {
return doExecuteAsync(userAgent, headers, httpVerb, completeUrl, new ByteArrayConsumer(bodyContents), callback,
converter);
}
@Override
public Future executeAsync(String userAgent, Map headers, Verb httpVerb, String completeUrl,
MultipartPayload bodyContents, OAuthAsyncRequestCallback callback,
OAuthRequest.ResponseConverter converter) {
throw new UnsupportedOperationException("AhcHttpClient does not support MultipartPayload yet.");
}
@Override
public Future executeAsync(String userAgent, Map headers, Verb httpVerb, String completeUrl,
final String bodyContents, OAuthAsyncRequestCallback callback,
OAuthRequest.ResponseConverter converter) {
return doExecuteAsync(userAgent, headers, httpVerb, completeUrl, new StringConsumer(bodyContents), callback,
converter);
}
@Override
public Future executeAsync(String userAgent, Map headers, Verb httpVerb, String completeUrl,
final File bodyContents, OAuthAsyncRequestCallback callback,
OAuthRequest.ResponseConverter converter) {
return doExecuteAsync(userAgent, headers, httpVerb, completeUrl, new FileConsumer(bodyContents), callback,
converter);
}
private Future doExecuteAsync(String userAgent, Map headers, Verb httpVerb,
String completeUrl, Consumer bodySetter, OAuthAsyncRequestCallback callback,
OAuthRequest.ResponseConverter converter) {
final BoundRequestBuilder boundRequestBuilder;
switch (httpVerb) {
case GET:
boundRequestBuilder = client.prepareGet(completeUrl);
break;
case POST:
boundRequestBuilder = client.preparePost(completeUrl);
break;
case PUT:
boundRequestBuilder = client.preparePut(completeUrl);
break;
case DELETE:
boundRequestBuilder = client.prepareDelete(completeUrl);
break;
default:
throw new IllegalArgumentException("message build error: unknown verb type");
}
if (httpVerb.isPermitBody()) {
if (!headers.containsKey(CONTENT_TYPE)) {
boundRequestBuilder.addHeader(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
}
bodySetter.accept(boundRequestBuilder);
}
for (Map.Entry header : headers.entrySet()) {
boundRequestBuilder.addHeader(header.getKey(), header.getValue());
}
if (userAgent != null) {
boundRequestBuilder.setHeader(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
}
return boundRequestBuilder.execute(new OAuthAsyncCompletionHandler<>(callback, converter));
}
private static class ByteArrayConsumer implements Consumer {
private final byte[] bodyContents;
private ByteArrayConsumer(byte[] bodyContents) {
this.bodyContents = bodyContents;
}
@Override
public void accept(BoundRequestBuilder requestBuilder) {
requestBuilder.setBody(bodyContents);
}
}
private static class StringConsumer implements Consumer {
private final String bodyContents;
private StringConsumer(String bodyContents) {
this.bodyContents = bodyContents;
}
@Override
public void accept(BoundRequestBuilder requestBuilder) {
requestBuilder.setBody(bodyContents);
}
}
private static class FileConsumer implements Consumer {
private final File bodyContents;
private FileConsumer(File bodyContents) {
this.bodyContents = bodyContents;
}
@Override
public void accept(BoundRequestBuilder requestBuilder) {
requestBuilder.setBody(bodyContents);
}
}
}