Visitar URL original
add posibility to use externally created http client · scribejava/scribejava@1433630 · GitHub
Skip to content

Commit 1433630

Browse files
committed
add posibility to use externally created http client
1 parent 5602952 commit 1433630

7 files changed

Lines changed: 123 additions & 5 deletions

File tree

‎changelog‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[SNAPSHOT]
2+
* add posibility to use externally created http client
23
* make ScribeJava compilable under jdk7 (checkstyle downgraded for jdk 1.7)
34
* add travis CI (check [oracle|open]jdk7 oraclejdk8)
45

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import java.util.Scanner;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.apis.VkontakteApi;
6+
import com.github.scribejava.core.model.ForceTypeOfHttpRequest;
7+
import com.github.scribejava.core.model.OAuth2AccessToken;
8+
import com.github.scribejava.core.model.OAuthRequestAsync;
9+
import com.github.scribejava.core.model.Response;
10+
import com.github.scribejava.core.model.ScribeJavaConfig;
11+
import com.github.scribejava.core.model.Verb;
12+
import com.github.scribejava.core.oauth.OAuth20Service;
13+
import com.github.scribejava.httpclient.ahc.AhcHttpClient;
14+
import java.io.IOException;
15+
import java.util.concurrent.ExecutionException;
16+
import org.asynchttpclient.DefaultAsyncHttpClient;
17+
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
18+
19+
public final class VkontakteExternalHttpExample {
20+
21+
private static final String NETWORK_NAME = "Vkontakte.ru";
22+
private static final String PROTECTED_RESOURCE_URL = "https://api.vk.com/method/users.get";
23+
24+
private VkontakteExternalHttpExample() {
25+
}
26+
27+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
28+
// Replace these with your client id and secret
29+
final String clientId = "your client id";
30+
final String clientSecret = "your client secret";
31+
ScribeJavaConfig.setForceTypeOfHttpRequests(ForceTypeOfHttpRequest.FORCE_ASYNC_ONLY_HTTP_REQUESTS);
32+
33+
//create any http cleint externally
34+
final DefaultAsyncHttpClientConfig httpClientConfig = new DefaultAsyncHttpClientConfig.Builder()
35+
.setMaxConnections(5)
36+
.setRequestTimeout(10_000)
37+
.setPooledConnectionIdleTimeout(1_000)
38+
.setReadTimeout(1_000)
39+
.build();
40+
//wrap it
41+
try (DefaultAsyncHttpClient ahcHttpClient = new DefaultAsyncHttpClient(httpClientConfig)) {
42+
//wrap it
43+
final AhcHttpClient wrappedAHCHttpClient = new AhcHttpClient(ahcHttpClient);
44+
45+
final OAuth20Service service = new ServiceBuilder()
46+
.httpClient(wrappedAHCHttpClient)
47+
.apiKey(clientId)
48+
.apiSecret(clientSecret)
49+
.scope("wall,offline") // replace with desired scope
50+
.callback("http://your.site.com/callback")
51+
.build(VkontakteApi.instance());
52+
final Scanner in = new Scanner(System.in);
53+
54+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
55+
System.out.println();
56+
57+
// Obtain the Authorization URL
58+
System.out.println("Fetching the Authorization URL...");
59+
final String authorizationUrl = service.getAuthorizationUrl();
60+
System.out.println("Got the Authorization URL!");
61+
System.out.println("Now go and authorize ScribeJava here:");
62+
System.out.println(authorizationUrl);
63+
System.out.println("And paste the authorization code here");
64+
System.out.print(">>");
65+
final String code = in.nextLine();
66+
System.out.println();
67+
68+
// Trade the Request Token and Verfier for the Access Token
69+
System.out.println("Trading the Request Token for an Access Token...");
70+
final OAuth2AccessToken accessToken = service.getAccessTokenAsync(code, null).get();
71+
System.out.println("Got the Access Token!");
72+
System.out.println("(if your curious it looks like this: " + accessToken
73+
+ ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
74+
System.out.println();
75+
76+
// Now let's go and ask for a protected resource!
77+
System.out.println("Now we're going to access a protected resource...");
78+
final OAuthRequestAsync request = new OAuthRequestAsync(Verb.GET, PROTECTED_RESOURCE_URL, service);
79+
service.signRequest(accessToken, request);
80+
final Response response = request.sendAsync(null).get();
81+
System.out.println("Got it! Lets see what we found...");
82+
System.out.println();
83+
System.out.println(response.getCode());
84+
System.out.println(response.getBody());
85+
86+
System.out.println();
87+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
88+
}
89+
}
90+
}

‎scribejava-core/src/main/java/com/github/scribejava/core/builder/ServiceBuilder.java‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class ServiceBuilder {
3131

3232
//not-default httpclient only
3333
private HttpClient.Config httpClientConfig;
34+
private HttpClient httpClient;
3435

3536
public ServiceBuilder() {
3637
callback = OAuthConstants.OUT_OF_BAND;
@@ -139,6 +140,17 @@ public ServiceBuilder httpClientConfig(HttpClient.Config httpClientConfig) {
139140
return this;
140141
}
141142

143+
/**
144+
* takes precedence over httpClientConfig
145+
*
146+
* @param httpClient externally created HTTP client
147+
* @return the {@link ServiceBuilder} instance for method chaining
148+
*/
149+
public ServiceBuilder httpClient(HttpClient httpClient) {
150+
this.httpClient = httpClient;
151+
return this;
152+
}
153+
142154
public ServiceBuilder userAgent(String userAgent) {
143155
this.userAgent = userAgent;
144156
return this;
@@ -156,7 +168,7 @@ public void checkPreconditions() {
156168
private OAuthConfig createConfig() {
157169
checkPreconditions();
158170
return new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope, debugStream, state, responseType,
159-
userAgent, connectTimeout, readTimeout, httpClientConfig);
171+
userAgent, connectTimeout, readTimeout, httpClientConfig, httpClient);
160172
}
161173

162174
/**

‎scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthConfig.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ public class OAuthConfig {
2424

2525
//async version only
2626
private HttpClient.Config httpClientConfig;
27+
private HttpClient httpClient;
2728

2829
public OAuthConfig(String key, String secret) {
29-
this(key, secret, null, null, null, null, null, null, null, null, null, null);
30+
this(key, secret, null, null, null, null, null, null, null, null, null, null, null);
3031
}
3132

3233
public OAuthConfig(String apiKey, String apiSecret, String callback, SignatureType signatureType, String scope,
3334
OutputStream debugStream, String state, String responseType, String userAgent, Integer connectTimeout,
34-
Integer readTimeout, HttpClient.Config httpClientConfig) {
35+
Integer readTimeout, HttpClient.Config httpClientConfig, HttpClient httpClient) {
3536
this.apiKey = apiKey;
3637
this.apiSecret = apiSecret;
3738
this.callback = callback;
@@ -44,6 +45,7 @@ public OAuthConfig(String apiKey, String apiSecret, String callback, SignatureTy
4445
this.connectTimeout = connectTimeout;
4546
this.readTimeout = readTimeout;
4647
this.httpClientConfig = httpClientConfig;
48+
this.httpClient = httpClient;
4749
}
4850

4951
public String getApiKey() {
@@ -100,4 +102,8 @@ public Integer getReadTimeout() {
100102
public HttpClient.Config getHttpClientConfig() {
101103
return httpClientConfig;
102104
}
105+
106+
public HttpClient getHttpClient() {
107+
return httpClient;
108+
}
103109
}

‎scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuthService.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public OAuthService(OAuthConfig config) {
2929
this.config = config;
3030
final ForceTypeOfHttpRequest forceTypeOfHttpRequest = ScribeJavaConfig.getForceTypeOfHttpRequests();
3131
final HttpClient.Config httpClientConfig = config.getHttpClientConfig();
32+
final HttpClient externalHttpClient = config.getHttpClient();
3233

33-
if (httpClientConfig == null) {
34+
if (httpClientConfig == null && externalHttpClient == null) {
3435
if (ForceTypeOfHttpRequest.FORCE_ASYNC_ONLY_HTTP_REQUESTS == forceTypeOfHttpRequest) {
3536
throw new OAuthException("Cannot use sync operations, only async");
3637
}
@@ -46,7 +47,7 @@ public OAuthService(OAuthConfig config) {
4647
config.log("Cannot use async operations, only sync");
4748
}
4849

49-
httpClient = getClient(httpClientConfig);
50+
httpClient = externalHttpClient == null ? getClient(httpClientConfig) : externalHttpClient;
5051
}
5152
}
5253

‎scribejava-httpclient-ahc/src/main/java/com/github/scribejava/httpclient/ahc/AhcHttpClient.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public AhcHttpClient(AhcHttpClientConfig ahcConfig) {
2424
client = new DefaultAsyncHttpClient(ahcConfig.getClientConfig());
2525
}
2626

27+
public AhcHttpClient(DefaultAsyncHttpClient ahcClient) {
28+
client = ahcClient;
29+
}
30+
2731
@Override
2832
public void close() throws IOException {
2933
client.close();

‎scribejava-httpclient-ning/src/main/java/com/github/scribejava/httpclient/ning/NingHttpClient.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public NingHttpClient(NingHttpClientConfig ningConfig) {
2323
: new AsyncHttpClient(ningAsyncHttpProviderClassName, ningConfig.getConfig());
2424
}
2525

26+
public NingHttpClient(AsyncHttpClient client) {
27+
this.client = client;
28+
}
29+
2630
@Override
2731
public void close() {
2832
client.close();

0 commit comments

Comments
 (0)