Skip to content

Commit 4003852

Browse files
committed
add Apache HttpComponents HttpClient support in separate module (thanks to https://github.com/sschwieb)
1 parent 2f5d9b1 commit 4003852

File tree

27 files changed

+240
-178
lines changed

27 files changed

+240
-178
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* add OAuth2Service signRequest method accepting just String, not OAuth2 Access Token Object.
1111
Remove signRequest from abstract OAuthService. 2.0 and 1.0a will be a bit more different now.
1212
* drop toString method from *Tokens to prevent leak of sensible data (token ans secrets) (thanks to https://github.com/rcaa)
13+
* add Apache HttpComponents HttpClient support in separate module (thanks to https://github.com/sschwieb)
1314

1415
[4.2.0]
1516
* DELETE in JdkClient permits, but not requires payload (thanks to https://github.com/miguelD73)

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
<scope>compile</scope>
9898
<optional>true</optional>
9999
</dependency>
100+
<dependency>
101+
<groupId>com.squareup.okhttp3</groupId>
102+
<artifactId>mockwebserver</artifactId>
103+
<version>3.9.0</version>
104+
<scope>test</scope>
105+
</dependency>
100106
</dependencies>
101107
<build>
102108
<pluginManagement>

scribejava-apis/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
<version>${project.version}</version>
3939
<scope>test</scope>
4040
</dependency>
41+
<dependency>
42+
<groupId>com.github.scribejava</groupId>
43+
<artifactId>scribejava-httpclient-apache</artifactId>
44+
<version>${project.version}</version>
45+
<scope>test</scope>
46+
</dependency>
4147
</dependencies>
4248

4349
<build>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import java.util.Random;
4+
import java.util.Scanner;
5+
import java.util.concurrent.ExecutionException;
6+
import com.github.scribejava.apis.FacebookApi;
7+
import com.github.scribejava.core.builder.ServiceBuilder;
8+
import com.github.scribejava.core.model.OAuth2AccessToken;
9+
import com.github.scribejava.core.model.OAuthRequest;
10+
import com.github.scribejava.core.model.Response;
11+
import com.github.scribejava.core.model.Verb;
12+
import com.github.scribejava.core.oauth.OAuth20Service;
13+
import com.github.scribejava.httpclient.apache.ApacheHttpClientConfig;
14+
import java.io.IOException;
15+
16+
public final class FacebookAsyncApacheExample {
17+
18+
private static final String NETWORK_NAME = "Facebook";
19+
private static final String PROTECTED_RESOURCE_URL = "https://graph.facebook.com/v2.8/me";
20+
21+
private FacebookAsyncApacheExample() {
22+
}
23+
24+
public static void main(String... args) throws InterruptedException, ExecutionException, IOException {
25+
// Replace these with your client id and secret
26+
final String clientId = "your client id";
27+
final String clientSecret = "your client secret";
28+
final String secretState = "secret" + new Random().nextInt(999_999);
29+
30+
try (OAuth20Service service = new ServiceBuilder(clientId)
31+
.apiSecret(clientSecret)
32+
.state(secretState)
33+
.callback("http://www.example.com/oauth_callback/")
34+
.httpClientConfig(ApacheHttpClientConfig.defaultConfig())
35+
.build(FacebookApi.instance())) {
36+
final Scanner in = new Scanner(System.in, "UTF-8");
37+
38+
System.out.println("=== " + NETWORK_NAME + "'s Async OAuth Workflow ===");
39+
System.out.println();
40+
41+
// Obtain the Authorization URL
42+
System.out.println("Fetching the Authorization URL...");
43+
final String authorizationUrl = service.getAuthorizationUrl();
44+
System.out.println("Got the Authorization URL!");
45+
System.out.println("Now go and authorize ScribeJava here:");
46+
System.out.println(authorizationUrl);
47+
System.out.println("And paste the authorization code here");
48+
System.out.print(">>");
49+
final String code = in.nextLine();
50+
System.out.println();
51+
52+
System.out.println("And paste the state from server here. We have set 'secretState'='"
53+
+ secretState + "'.");
54+
System.out.print(">>");
55+
final String value = in.nextLine();
56+
if (secretState.equals(value)) {
57+
System.out.println("State value does match!");
58+
} else {
59+
System.out.println("Ooops, state value does not match!");
60+
System.out.println("Expected = " + secretState);
61+
System.out.println("Got = " + value);
62+
System.out.println();
63+
}
64+
65+
// Trade the Request Token and Verfier for the Access Token
66+
System.out.println("Trading the Request Token for an Access Token...");
67+
final OAuth2AccessToken accessToken = service.getAccessTokenAsync(code).get();
68+
System.out.println("Got the Access Token!");
69+
System.out.println("(if your curious the raw answer looks like this: " + accessToken.getRawResponse()
70+
+ "')");
71+
System.out.println();
72+
73+
// Now let's go and ask for a protected resource!
74+
System.out.println("Now we're going to access a protected resource...");
75+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
76+
service.signRequest(accessToken, request);
77+
final Response response = service.execute(request);
78+
System.out.println("Got it! Lets see what we found...");
79+
System.out.println();
80+
System.out.println(response.getCode());
81+
System.out.println(response.getBody());
82+
83+
System.out.println();
84+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
85+
}
86+
}
87+
}

scribejava-core/pom.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414
<name>ScribeJava Core</name>
1515
<packaging>jar</packaging>
1616

17-
<dependencies>
18-
<dependency>
19-
<groupId>com.squareup.okhttp3</groupId>
20-
<artifactId>mockwebserver</artifactId>
21-
<version>3.8.1</version>
22-
<scope>test</scope>
23-
</dependency>
24-
</dependencies>
25-
2617
<build>
2718
<plugins>
2819
<plugin>

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/HttpClient.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
import com.github.scribejava.core.model.OAuthRequest;
55
import com.github.scribejava.core.model.Response;
66
import com.github.scribejava.core.model.Verb;
7+
import java.io.Closeable;
78
import java.io.File;
89
import java.io.IOException;
910
import java.util.Map;
1011
import java.util.concurrent.ExecutionException;
1112
import java.util.concurrent.Future;
1213

13-
public interface HttpClient {
14+
public interface HttpClient extends Closeable {
1415
String DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded";
1516
String CONTENT_TYPE = "Content-Type";
1617
String CONTENT_LENGTH = "Content-Length";
1718

18-
void close() throws IOException;
19-
2019
<T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
2120
byte[] bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter);
2221

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/jdk/JDKHttpClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ public class JDKHttpClient implements HttpClient {
2121

2222
private final JDKHttpClientConfig config;
2323

24+
public JDKHttpClient() {
25+
this(JDKHttpClientConfig.defaultConfig());
26+
}
27+
2428
public JDKHttpClient(JDKHttpClientConfig clientConfig) {
2529
config = clientConfig;
2630
}
2731

2832
@Override
29-
public void close() throws IOException {
33+
public void close() {
3034
}
3135

3236
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import com.github.scribejava.core.model.OAuthConfig;
1010
import com.github.scribejava.core.model.OAuthRequest;
1111
import com.github.scribejava.core.model.Response;
12+
import java.io.Closeable;
1213
import java.io.File;
1314

1415
import java.io.IOException;
1516
import java.util.ServiceLoader;
1617
import java.util.concurrent.ExecutionException;
1718
import java.util.concurrent.Future;
1819

19-
public abstract class OAuthService implements AutoCloseable {
20+
public abstract class OAuthService implements Closeable {
2021

2122
private final OAuthConfig config;
2223
private final HttpClient httpClient;

scribejava-core/src/test/java/com/github/scribejava/core/AbstractClientTest.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,48 @@
1717
import org.junit.Before;
1818
import org.junit.Test;
1919

20-
import java.io.IOException;
2120
import java.util.concurrent.TimeUnit;
2221

2322
import static org.junit.Assert.assertEquals;
2423

2524
public abstract class AbstractClientTest {
2625

27-
class TestCallback<T> implements OAuthAsyncRequestCallback<T> {
26+
private OAuthService oAuthService;
2827

29-
private Throwable throwable;
30-
private T response;
28+
private static class TestCallback implements OAuthAsyncRequestCallback<Response> {
29+
30+
private Response response;
3131

3232
@Override
33-
public void onCompleted(T response) {
33+
public void onCompleted(Response response) {
3434
this.response = response;
3535
}
3636

3737
@Override
3838
public void onThrowable(Throwable throwable) {
39-
this.throwable = throwable;
4039
}
41-
}
4240

43-
private OAuthService<?> oAuthService;
44-
private HttpClient client;
41+
public Response getResponse() {
42+
return response;
43+
}
44+
}
4545

4646
@Before
4747
public void setUp() {
48-
client = createNewClient();
4948
oAuthService = new OAuth20Service(null,
50-
new OAuthConfig("test", "test", null, null, null, null, null, null, null, client));
49+
new OAuthConfig("test", "test", null, null, null, null, null, null, null, createNewClient()));
5150
}
5251

5352
@After
54-
public void shutDown() throws IOException {
55-
client.close();
53+
public void shutDown() throws Exception {
54+
oAuthService.close();
5655
}
5756

5857
protected abstract HttpClient createNewClient();
5958

6059
@Test
6160
public void shouldSendGetRequest() throws Exception {
62-
final String expectedResponseBody = "response body";
61+
final String expectedResponseBody = "response body for test shouldSendGetRequest";
6362

6463
final MockWebServer server = new MockWebServer();
6564
server.enqueue(new MockResponse().setBody(expectedResponseBody));
@@ -80,7 +79,7 @@ public void shouldSendGetRequest() throws Exception {
8079

8180
@Test
8281
public void shouldSendPostRequest() throws Exception {
83-
final String expectedResponseBody = "response body";
82+
final String expectedResponseBody = "response body for test shouldSendPostRequest";
8483
final String expectedRequestBody = "request body";
8584

8685
final MockWebServer server = new MockWebServer();
@@ -101,7 +100,6 @@ public void shouldSendPostRequest() throws Exception {
101100
assertEquals("POST", recordedRequest.getMethod());
102101
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
103102

104-
105103
// request with empty body
106104
request = new OAuthRequest(Verb.POST, baseUrl.toString());
107105
response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
@@ -117,7 +115,7 @@ public void shouldSendPostRequest() throws Exception {
117115

118116
@Test
119117
public void shouldReadResponseStream() throws Exception {
120-
final String expectedResponseBody = "response body";
118+
final String expectedResponseBody = "response body for test shouldReadResponseStream";
121119

122120
final MockWebServer server = new MockWebServer();
123121
server.enqueue(new MockResponse().setBody(expectedResponseBody));
@@ -138,7 +136,7 @@ public void shouldReadResponseStream() throws Exception {
138136

139137
@Test
140138
public void shouldCallCallback() throws Exception {
141-
final String expectedResponseBody = "response body";
139+
final String expectedResponseBody = "response body for test shouldCallCallback";
142140

143141
final MockWebServer server = new MockWebServer();
144142
server.enqueue(new MockResponse().setBody(expectedResponseBody));
@@ -148,10 +146,10 @@ public void shouldCallCallback() throws Exception {
148146

149147
final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
150148

151-
final TestCallback<Response> callback = new TestCallback<>();
149+
final TestCallback callback = new TestCallback();
152150
oAuthService.execute(request, callback).get();
153151

154-
assertEquals(expectedResponseBody, callback.response.getBody());
152+
assertEquals(expectedResponseBody, callback.getResponse().getBody());
155153

156154
server.shutdown();
157155
}
@@ -167,11 +165,11 @@ public void shouldPassErrors() throws Exception {
167165

168166
final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
169167

170-
final TestCallback<Response> callback = new TestCallback<>();
168+
final TestCallback callback = new TestCallback();
171169
final Response response = oAuthService.execute(request, callback).get();
172170

173171
assertEquals(500, response.getCode());
174-
assertEquals(500, callback.response.getCode());
172+
assertEquals(500, callback.getResponse().getCode());
175173

176174
server.shutdown();
177175
}

scribejava-core/src/test/java/com/github/scribejava/core/httpclient/jdk/JDKHttpClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class JDKHttpClientTest extends AbstractClientTest {
77

88
@Override
99
protected HttpClient createNewClient() {
10-
return new JDKHttpClient(JDKHttpClientConfig.defaultConfig());
10+
return new JDKHttpClient();
1111
}
1212
}

0 commit comments

Comments
 (0)