| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + package com.github.scribejava.core.httpclient; | ||
| 2 | + | ||
| 3 | + import com.github.scribejava.core.model.OAuthRequestAsync; | ||
| 4 | + import com.github.scribejava.core.model.Response; | ||
| 5 | + import com.github.scribejava.core.model.Verb; | ||
| 6 | + import java.io.File; | ||
| 7 | + import java.io.IOException; | ||
| 8 | + import java.util.Map; | ||
| 9 | + import java.util.concurrent.ExecutionException; | ||
| 10 | + | ||
| 11 | + public abstract class AbstractAsyncOnlyHttpClient implements HttpClient { | ||
| 12 | + | ||
| 13 | + @Override | ||
| 14 | + public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 15 | + byte[] bodyContents) throws InterruptedException, ExecutionException, IOException { | ||
| 16 | + return executeAsync(userAgent, headers, httpVerb, completeUrl, bodyContents, null, | ||
| 17 | + (OAuthRequestAsync.ResponseConverter<Response>) null).get(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + @Override | ||
| 21 | + public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 22 | + String bodyContents) throws InterruptedException, ExecutionException, IOException { | ||
| 23 | + return executeAsync(userAgent, headers, httpVerb, completeUrl, bodyContents, null, | ||
| 24 | + (OAuthRequestAsync.ResponseConverter<Response>) null).get(); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + @Override | ||
| 28 | + public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 29 | + File bodyContents) throws InterruptedException, ExecutionException, IOException { | ||
| 30 | + return executeAsync(userAgent, headers, httpVerb, completeUrl, bodyContents, null, | ||
| 31 | + (OAuthRequestAsync.ResponseConverter<Response>) null).get(); | ||
| 32 | + } | ||
| 33 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,10 +2,12 @@ | |||
| 2 | 2 | ||
| 3 | 3 | import com.github.scribejava.core.model.OAuthAsyncRequestCallback; | |
| 4 | 4 | import com.github.scribejava.core.model.OAuthRequestAsync; | |
| 5 | + import com.github.scribejava.core.model.Response; | ||
| 5 | 6 | import com.github.scribejava.core.model.Verb; | |
| 6 | 7 | import java.io.File; | |
| 7 | 8 | import java.io.IOException; | |
| 8 | 9 | import java.util.Map; | |
| 10 | + import java.util.concurrent.ExecutionException; | ||
| 9 | 11 | import java.util.concurrent.Future; | |
| 10 | 12 | ||
| 11 | 13 | public interface HttpClient { | |
@@ -23,4 +25,12 @@ <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb h | |||
| 23 | 25 | <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | |
| 24 | 26 | File bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequestAsync.ResponseConverter<T> converter); | |
| 25 | 27 | ||
| 28 | + Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 29 | + byte[] bodyContents) throws InterruptedException, ExecutionException, IOException; | ||
| 30 | + | ||
| 31 | + Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 32 | + String bodyContents) throws InterruptedException, ExecutionException, IOException; | ||
| 33 | + | ||
| 34 | + Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 35 | + File bodyContents) throws InterruptedException, ExecutionException, IOException; | ||
| 26 | 36 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | 15 | import java.io.IOException; | |
| 16 | 16 | import java.util.ServiceLoader; | |
| 17 | + import java.util.concurrent.ExecutionException; | ||
| 17 | 18 | import java.util.concurrent.Future; | |
| 18 | 19 | ||
| 19 | 20 | /** | |
@@ -66,8 +67,8 @@ public OAuthConfig getConfig() { | |||
| 66 | 67 | ||
| 67 | 68 | public abstract void signRequest(T token, AbstractRequest request); | |
| 68 | 69 | ||
| 69 | - public <T> Future<T> execute(OAuthRequestAsync request, OAuthAsyncRequestCallback<T> callback, | ||
| 70 | - OAuthRequestAsync.ResponseConverter<T> converter) { | ||
| 70 | + public <R> Future<R> execute(OAuthRequestAsync request, OAuthAsyncRequestCallback<R> callback, | ||
| 71 | + OAuthRequestAsync.ResponseConverter<R> converter) { | ||
| 71 | 72 | ||
| 72 | 73 | final File filePayload = request.getFilePayload(); | |
| 73 | 74 | if (filePayload != null) { | |
@@ -86,6 +87,20 @@ public Future<Response> execute(OAuthRequestAsync request, OAuthAsyncRequestCall | |||
| 86 | 87 | return execute(request, callback, null); | |
| 87 | 88 | } | |
| 88 | 89 | ||
| 90 | + public Response execute(OAuthRequestAsync request) throws InterruptedException, ExecutionException, IOException { | ||
| 91 | + final File filePayload = request.getFilePayload(); | ||
| 92 | + if (filePayload != null) { | ||
| 93 | + return httpClient.execute(config.getUserAgent(), request.getHeaders(), request.getVerb(), | ||
| 94 | + request.getCompleteUrl(), filePayload); | ||
| 95 | + } else if (request.getStringPayload() != null) { | ||
| 96 | + return httpClient.execute(config.getUserAgent(), request.getHeaders(), request.getVerb(), | ||
| 97 | + request.getCompleteUrl(), request.getStringPayload()); | ||
| 98 | + } else { | ||
| 99 | + return httpClient.execute(config.getUserAgent(), request.getHeaders(), request.getVerb(), | ||
| 100 | + request.getCompleteUrl(), request.getByteArrayPayload()); | ||
| 101 | + } | ||
| 102 | + } | ||
| 103 | + | ||
| 89 | 104 | /** | |
| 90 | 105 | * the same as {@link OAuthRequest#send()} | |
| 91 | 106 | * | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | package com.github.scribejava.httpclient.ahc; | |
| 2 | 2 | ||
| 3 | + import com.github.scribejava.core.httpclient.AbstractAsyncOnlyHttpClient; | ||
| 3 | 4 | import com.github.scribejava.core.model.AbstractRequest; | |
| 4 | - import com.github.scribejava.core.httpclient.HttpClient; | ||
| 5 | 5 | import com.github.scribejava.core.model.OAuthAsyncRequestCallback; | |
| 6 | 6 | import com.github.scribejava.core.model.OAuthConstants; | |
| 7 | 7 | import com.github.scribejava.core.model.OAuthRequestAsync; | |
@@ -18,7 +18,7 @@ | |||
| 18 | 18 | import org.asynchttpclient.AsyncHttpClientConfig; | |
| 19 | 19 | import org.asynchttpclient.BoundRequestBuilder; | |
| 20 | 20 | ||
| 21 | - public class AhcHttpClient implements HttpClient { | ||
| 21 | + public class AhcHttpClient extends AbstractAsyncOnlyHttpClient { | ||
| 22 | 22 | ||
| 23 | 23 | private final AsyncHttpClient client; | |
| 24 | 24 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | package com.github.scribejava.httpclient.ning; | |
| 2 | 2 | ||
| 3 | + import com.github.scribejava.core.httpclient.AbstractAsyncOnlyHttpClient; | ||
| 3 | 4 | import com.github.scribejava.core.model.AbstractRequest; | |
| 4 | - import com.github.scribejava.core.httpclient.HttpClient; | ||
| 5 | 5 | import com.github.scribejava.core.model.OAuthAsyncRequestCallback; | |
| 6 | 6 | import com.github.scribejava.core.model.OAuthConstants; | |
| 7 | 7 | import com.github.scribejava.core.model.OAuthRequestAsync; | |
@@ -15,7 +15,7 @@ | |||
| 15 | 15 | import com.ning.http.client.AsyncHttpClientConfig; | |
| 16 | 16 | import java.io.File; | |
| 17 | 17 | ||
| 18 | - public class NingHttpClient implements HttpClient { | ||
| 18 | + public class NingHttpClient extends AbstractAsyncOnlyHttpClient { | ||
| 19 | 19 | ||
| 20 | 20 | private final AsyncHttpClient client; | |
| 21 | 21 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,11 +5,8 @@ | |||
| 5 | 5 | import com.github.scribejava.core.model.Response; | |
| 6 | 6 | import okhttp3.Call; | |
| 7 | 7 | import okhttp3.Callback; | |
| 8 | - import okhttp3.Headers; | ||
| 9 | 8 | ||
| 10 | 9 | import java.io.IOException; | |
| 11 | - import java.util.HashMap; | ||
| 12 | - import java.util.Map; | ||
| 13 | 10 | ||
| 14 | 11 | class OAuthAsyncCompletionHandler<T> implements Callback { | |
| 15 | 12 | ||
@@ -38,15 +35,8 @@ public void onFailure(Call call, IOException e) { | |||
| 38 | 35 | @Override | |
| 39 | 36 | public void onResponse(Call call, okhttp3.Response okHttpResponse) throws IOException { | |
| 40 | 37 | try { | |
| 41 | - final Headers headers = okHttpResponse.headers(); | ||
| 42 | - final Map<String, String> headersMap = new HashMap<>(); | ||
| 43 | 38 | ||
| 44 | - for (String name : headers.names()) { | ||
| 45 | - headersMap.put(name, headers.get(name)); | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - final Response response = new Response(okHttpResponse.code(), okHttpResponse.message(), headersMap, | ||
| 49 | - okHttpResponse.body().byteStream()); | ||
| 39 | + final Response response = OkHttpHttpClient.convertResponse(okHttpResponse); | ||
| 50 | 40 | ||
| 51 | 41 | @SuppressWarnings("unchecked") | |
| 52 | 42 | final T t = converter == null ? (T) response : converter.convert(response); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,8 +18,12 @@ | |||
| 18 | 18 | import java.util.concurrent.Future; | |
| 19 | 19 | ||
| 20 | 20 | import static com.github.scribejava.core.model.AbstractRequest.DEFAULT_CONTENT_TYPE; | |
| 21 | + import com.github.scribejava.core.model.Response; | ||
| 21 | 22 | import java.io.File; | |
| 23 | + import java.util.HashMap; | ||
| 24 | + import java.util.concurrent.ExecutionException; | ||
| 22 | 25 | import okhttp3.Cache; | |
| 26 | + import okhttp3.Headers; | ||
| 23 | 27 | ||
| 24 | 28 | public class OkHttpHttpClient implements HttpClient { | |
| 25 | 29 | ||
@@ -73,6 +77,38 @@ public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, | |||
| 73 | 77 | private <T> Future<T> doExecuteAsync(String userAgent, Map<String, String> headers, Verb httpVerb, | |
| 74 | 78 | String completeUrl, BodyType bodyType, Object bodyContents, OAuthAsyncRequestCallback<T> callback, | |
| 75 | 79 | OAuthRequestAsync.ResponseConverter<T> converter) { | |
| 80 | + final Call call = createCall(userAgent, headers, httpVerb, completeUrl, bodyType, bodyContents); | ||
| 81 | + final OkHttpFuture<T> okHttpFuture = new OkHttpFuture<>(call); | ||
| 82 | + call.enqueue(new OAuthAsyncCompletionHandler<>(callback, converter, okHttpFuture)); | ||
| 83 | + return okHttpFuture; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + @Override | ||
| 87 | + public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 88 | + byte[] bodyContents) throws InterruptedException, ExecutionException, IOException { | ||
| 89 | + return doExecute(userAgent, headers, httpVerb, completeUrl, BodyType.BYTE_ARRAY, bodyContents); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + @Override | ||
| 93 | + public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 94 | + String bodyContents) throws InterruptedException, ExecutionException, IOException { | ||
| 95 | + return doExecute(userAgent, headers, httpVerb, completeUrl, BodyType.STRING, bodyContents); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + @Override | ||
| 99 | + public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 100 | + File bodyContents) throws InterruptedException, ExecutionException, IOException { | ||
| 101 | + return doExecute(userAgent, headers, httpVerb, completeUrl, BodyType.FILE, bodyContents); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + private Response doExecute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 105 | + BodyType bodyType, Object bodyContents) throws IOException { | ||
| 106 | + final Call call = createCall(userAgent, headers, httpVerb, completeUrl, bodyType, bodyContents); | ||
| 107 | + return convertResponse(call.execute()); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + private Call createCall(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, | ||
| 111 | + BodyType bodyType, Object bodyContents) { | ||
| 76 | 112 | final Request.Builder requestBuilder = new Request.Builder(); | |
| 77 | 113 | requestBuilder.url(completeUrl); | |
| 78 | 114 | ||
@@ -101,10 +137,7 @@ private <T> Future<T> doExecuteAsync(String userAgent, Map<String, String> heade | |||
| 101 | 137 | } | |
| 102 | 138 | ||
| 103 | 139 | // create a new call | |
| 104 | - final Call call = client.newCall(requestBuilder.build()); | ||
| 105 | - final OkHttpFuture<T> okHttpFuture = new OkHttpFuture<>(call); | ||
| 106 | - call.enqueue(new OAuthAsyncCompletionHandler<>(callback, converter, okHttpFuture)); | ||
| 107 | - return okHttpFuture; | ||
| 140 | + return client.newCall(requestBuilder.build()); | ||
| 108 | 141 | } | |
| 109 | 142 | ||
| 110 | 143 | private enum BodyType { | |
@@ -129,4 +162,17 @@ RequestBody createBody(MediaType mediaType, Object bodyContents) { | |||
| 129 | 162 | ||
| 130 | 163 | abstract RequestBody createBody(MediaType mediaType, Object bodyContents); | |
| 131 | 164 | } | |
| 165 | + | ||
| 166 | + static Response convertResponse(okhttp3.Response okHttpResponse) { | ||
| 167 | + final Headers headers = okHttpResponse.headers(); | ||
| 168 | + final Map<String, String> headersMap = new HashMap<>(); | ||
| 169 | + | ||
| 170 | + for (String name : headers.names()) { | ||
| 171 | + headersMap.put(name, headers.get(name)); | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + return new Response(okHttpResponse.code(), okHttpResponse.message(), headersMap, | ||
| 175 | + okHttpResponse.body().byteStream()); | ||
| 176 | + | ||
| 177 | + } | ||
| 132 | 178 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments