Skip to content

Commit 7d2ce91

Browse files
committed
switch to jackson dependency to parse json responses (thanks to https://github.com/galimru)
1 parent 4f84b31 commit 7d2ce91

File tree

16 files changed

+153
-117
lines changed

16 files changed

+153
-117
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* add PMD checks on compile
33
* add all OAuth error codes from supported RFCs (incl. "invalid_token") (thanks to https://github.com/echorebel)
44
* Update LinkedIn Example to API v2 (thanks to https://github.com/peternees)
5+
* switch to jackson dependency to parse json responses (thanks to https://github.com/galimru)
56

67
[6.5.1]
78
* cleanup deprecates methods

pom.xml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,21 @@
5353
</developers>
5454

5555
<dependencies>
56+
<dependency>
57+
<groupId>com.fasterxml.jackson.core</groupId>
58+
<artifactId>jackson-databind</artifactId>
59+
<version>2.9.9</version>
60+
</dependency>
5661
<dependency>
5762
<groupId>junit</groupId>
5863
<artifactId>junit</artifactId>
5964
<version>4.12</version>
6065
<scope>test</scope>
6166
</dependency>
62-
<dependency>
63-
<groupId>com.google.code.gson</groupId>
64-
<artifactId>gson</artifactId>
65-
<version>2.8.5</version>
66-
<scope>test</scope>
67-
</dependency>
6867
<dependency>
6968
<groupId>com.squareup.okhttp3</groupId>
7069
<artifactId>mockwebserver</artifactId>
71-
<version>3.14.0</version>
70+
<version>3.14.2</version>
7271
<scope>test</scope>
7372
</dependency>
7473
</dependencies>
@@ -78,7 +77,7 @@
7877
<plugin>
7978
<groupId>org.apache.felix</groupId>
8079
<artifactId>maven-bundle-plugin</artifactId>
81-
<version>4.1.0</version>
80+
<version>4.2.0</version>
8281
<executions>
8382
<execution>
8483
<id>bundle-manifest</id>
@@ -92,7 +91,7 @@
9291
<plugin>
9392
<groupId>org.apache.maven.plugins</groupId>
9493
<artifactId>maven-jar-plugin</artifactId>
95-
<version>3.1.1</version>
94+
<version>3.1.2</version>
9695
<configuration>
9796
<archive>
9897
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
@@ -107,7 +106,7 @@
107106
<dependency>
108107
<groupId>com.puppycrawl.tools</groupId>
109108
<artifactId>checkstyle</artifactId>
110-
<version>8.18</version>
109+
<version>8.20</version>
111110
</dependency>
112111
</dependencies>
113112
</plugin>
@@ -122,7 +121,7 @@
122121
<plugin>
123122
<groupId>org.apache.maven.plugins</groupId>
124123
<artifactId>maven-surefire-plugin</artifactId>
125-
<version>2.22.1</version>
124+
<version>2.22.2</version>
126125
</plugin>
127126
<plugin>
128127
<groupId>org.apache.maven.plugins</groupId>
@@ -144,7 +143,7 @@
144143
<plugins>
145144
<plugin>
146145
<artifactId>maven-compiler-plugin</artifactId>
147-
<version>3.8.0</version>
146+
<version>3.8.1</version>
148147
<configuration>
149148
<encoding>UTF-8</encoding>
150149
<release>${java.release}</release>
@@ -175,7 +174,7 @@
175174
<plugin>
176175
<groupId>org.apache.maven.plugins</groupId>
177176
<artifactId>maven-source-plugin</artifactId>
178-
<version>3.0.1</version>
177+
<version>3.1.0</version>
179178
<executions>
180179
<execution>
181180
<id>attach-sources</id>
@@ -228,7 +227,7 @@
228227
<plugin>
229228
<groupId>org.apache.maven.plugins</groupId>
230229
<artifactId>maven-pmd-plugin</artifactId>
231-
<version>3.11.0</version>
230+
<version>3.12.0</version>
232231
<dependencies>
233232
<dependency>
234233
<groupId>net.sourceforge.pmd</groupId>
@@ -274,7 +273,7 @@
274273

275274
<properties>
276275
<java.release>7</java.release>
277-
<pmdVersion>6.12.0</pmdVersion>
276+
<pmdVersion>6.14.0</pmdVersion>
278277
</properties>
279278

280279
<profiles>

scribejava-apis/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<version>6.5.2-SNAPSHOT</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
11-
11+
1212
<groupId>com.github.scribejava</groupId>
1313
<artifactId>scribejava-apis</artifactId>
1414
<name>ScribeJava APIs</name>

scribejava-apis/src/main/java/com/github/scribejava/apis/facebook/FacebookAccessTokenJsonExtractor.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.github.scribejava.apis.facebook;
22

33
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
4-
import java.util.regex.Pattern;
4+
import java.io.IOException;
5+
import java.util.Map;
56

67
/**
78
* non standard Facebook Extractor
89
*/
910
public class FacebookAccessTokenJsonExtractor extends OAuth2AccessTokenJsonExtractor {
1011

11-
private static final Pattern MESSAGE_REGEX_PATTERN = Pattern.compile("\"message\"\\s*:\\s*\"([^\"]*?)\"");
12-
private static final Pattern TYPE_REGEX_PATTERN = Pattern.compile("\"type\"\\s*:\\s*\"([^\"]*?)\"");
13-
private static final Pattern CODE_REGEX_PATTERN = Pattern.compile("\"code\"\\s*:\\s*\"?([^\",}]*?)[\",}]");
14-
private static final Pattern FBTRACE_ID_REGEX_PATTERN = Pattern.compile("\"fbtrace_id\"\\s*:\\s*\"([^\"]*?)\"");
15-
1612
protected FacebookAccessTokenJsonExtractor() {
1713
}
1814

@@ -35,13 +31,13 @@ public static FacebookAccessTokenJsonExtractor instance() {
3531
* ID.","type":"OAuthException","code":101,"fbtrace_id":"CvDR+X4WWIx"}}'
3632
*/
3733
@Override
38-
public void generateError(String response) {
39-
extractParameter(response, MESSAGE_REGEX_PATTERN, false);
34+
public void generateError(String rawResponse) throws IOException {
35+
@SuppressWarnings("unchecked")
36+
final Map<String, String> response = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER
37+
.readValue(rawResponse, Map.class);
4038

41-
throw new FacebookAccessTokenErrorResponse(extractParameter(response, MESSAGE_REGEX_PATTERN, false),
42-
extractParameter(response, TYPE_REGEX_PATTERN, false),
43-
extractParameter(response, CODE_REGEX_PATTERN, false),
44-
extractParameter(response, FBTRACE_ID_REGEX_PATTERN, false), response);
39+
throw new FacebookAccessTokenErrorResponse(response.get("message"), response.get("type"), response.get("code"),
40+
response.get("fbtrace_id"), rawResponse);
4541
}
4642

4743
}

scribejava-apis/src/main/java/com/github/scribejava/apis/fitbit/FitBitJsonTokenExtractor.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
44
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
55
import com.github.scribejava.core.oauth2.OAuth2Error;
6-
7-
import java.util.regex.Pattern;
6+
import java.io.IOException;
7+
import java.util.List;
8+
import java.util.Map;
89

910
public class FitBitJsonTokenExtractor extends OAuth2AccessTokenJsonExtractor {
1011

11-
private static final Pattern USER_ID_REGEX_PATTERN = Pattern.compile("\"user_id\"\\s*:\\s*\"(\\S*?)\"");
12-
private static final Pattern ERROR_REGEX_PATTERN = Pattern.compile("\"errorType\"\\s*:\\s*\"(\\S*?)\"");
13-
private static final Pattern ERROR_DESCRIPTION_REGEX_PATTERN = Pattern.compile("\"message\"\\s*:\\s*\"([^\"]*?)\"");
1412
protected FitBitJsonTokenExtractor() {
1513
}
1614

@@ -25,27 +23,29 @@ public static FitBitJsonTokenExtractor instance() {
2523

2624
@Override
2725
protected FitBitOAuth2AccessToken createToken(String accessToken, String tokenType, Integer expiresIn,
28-
String refreshToken, String scope, String response) {
26+
String refreshToken, String scope, Map<String, String> response, String rawResponse) {
2927
return new FitBitOAuth2AccessToken(accessToken, tokenType, expiresIn, refreshToken, scope,
30-
extractParameter(response, USER_ID_REGEX_PATTERN, false), response);
28+
response.get("user_id"), rawResponse);
3129
}
3230

3331
/**
3432
* Related documentation: https://dev.fitbit.com/build/reference/web-api/oauth2/
3533
*/
3634
@Override
37-
public void generateError(String response) {
38-
final String errorInString = extractParameter(response, ERROR_REGEX_PATTERN, true);
39-
final String errorDescription = extractParameter(response, ERROR_DESCRIPTION_REGEX_PATTERN, false);
35+
public void generateError(String rawResponse) throws IOException {
36+
@SuppressWarnings("unchecked")
37+
final Map<String, List<Map<String, String>>> response = OAuth2AccessTokenJsonExtractor.OBJECT_MAPPER
38+
.readValue(rawResponse, Map.class);
4039

40+
final Map<String, String> errorResponse = response.get("errors").get(0);
4141
OAuth2Error errorCode;
4242
try {
43-
errorCode = OAuth2Error.parseFrom(errorInString);
43+
errorCode = OAuth2Error.parseFrom(extractRequiredParameter(errorResponse, "errorType", rawResponse));
4444
} catch (IllegalArgumentException iaE) {
4545
//non oauth standard error code
4646
errorCode = null;
4747
}
4848

49-
throw new OAuth2AccessTokenErrorResponse(errorCode, errorDescription, null, response);
49+
throw new OAuth2AccessTokenErrorResponse(errorCode, errorResponse.get("message"), null, rawResponse);
5050
}
5151
}

scribejava-apis/src/main/java/com/github/scribejava/apis/openid/OpenIdJsonTokenExtractor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.github.scribejava.apis.openid;
22

33
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
4-
import java.util.regex.Pattern;
4+
import java.util.Map;
55

66
/**
77
* additionally parses OpenID id_token
88
*/
99
public class OpenIdJsonTokenExtractor extends OAuth2AccessTokenJsonExtractor {
1010

11-
private static final Pattern ID_TOKEN_REGEX_PATTERN = Pattern.compile("\"id_token\"\\s*:\\s*\"(\\S*?)\"");
12-
1311
protected OpenIdJsonTokenExtractor() {
1412
}
1513

@@ -24,8 +22,8 @@ public static OpenIdJsonTokenExtractor instance() {
2422

2523
@Override
2624
protected OpenIdOAuth2AccessToken createToken(String accessToken, String tokenType, Integer expiresIn,
27-
String refreshToken, String scope, String response) {
25+
String refreshToken, String scope, Map<String, String> response, String rawResponse) {
2826
return new OpenIdOAuth2AccessToken(accessToken, tokenType, expiresIn, refreshToken, scope,
29-
extractParameter(response, ID_TOKEN_REGEX_PATTERN, false), response);
27+
response.get("id_token"), rawResponse);
3028
}
3129
}

scribejava-apis/src/main/java/com/github/scribejava/apis/salesforce/SalesforceJsonTokenExtractor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.github.scribejava.apis.salesforce;
22

33
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
4-
import java.util.regex.Pattern;
4+
import java.util.Map;
55

66
/**
77
* This extractor parses in addition to the standard Extractor the instance_url
88
* of the used Salesforce organization.
99
*/
1010
public class SalesforceJsonTokenExtractor extends OAuth2AccessTokenJsonExtractor {
1111

12-
private static final Pattern INSTANCE_URL_REGEX_PATTERN = Pattern.compile("\"instance_url\"\\s*:\\s*\"(\\S*?)\"");
13-
1412
protected SalesforceJsonTokenExtractor() {
1513
}
1614

@@ -25,8 +23,8 @@ public static SalesforceJsonTokenExtractor instance() {
2523

2624
@Override
2725
protected SalesforceToken createToken(String accessToken, String tokenType, Integer expiresIn,
28-
String refreshToken, String scope, String response) {
26+
String refreshToken, String scope, Map<String, String> response, String rawResponse) {
2927
return new SalesforceToken(accessToken, tokenType, expiresIn, refreshToken, scope,
30-
extractParameter(response, INSTANCE_URL_REGEX_PATTERN, true), response);
28+
extractRequiredParameter(response, "instance_url", rawResponse), rawResponse);
3129
}
3230
}

scribejava-apis/src/main/java/com/github/scribejava/apis/vk/VKJsonTokenExtractor.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.github.scribejava.apis.vk;
22

33
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
4-
import java.util.regex.Pattern;
4+
import java.util.Map;
55

66
/**
77
* additionally parses email
88
*/
99
public class VKJsonTokenExtractor extends OAuth2AccessTokenJsonExtractor {
1010

11-
private static final Pattern EMAIL_REGEX_PATTERN = Pattern.compile("\"email\"\\s*:\\s*\"(\\S*?)\"");
12-
1311
protected VKJsonTokenExtractor() {
1412
}
1513

@@ -24,8 +22,8 @@ public static VKJsonTokenExtractor instance() {
2422

2523
@Override
2624
protected VKOAuth2AccessToken createToken(String accessToken, String tokenType, Integer expiresIn,
27-
String refreshToken, String scope, String response) {
28-
return new VKOAuth2AccessToken(accessToken, tokenType, expiresIn, refreshToken, scope,
29-
extractParameter(response, EMAIL_REGEX_PATTERN, false), response);
25+
String refreshToken, String scope, Map<String, String> response, String rawResponse) {
26+
return new VKOAuth2AccessToken(accessToken, tokenType, expiresIn, refreshToken, scope, response.get("email"),
27+
rawResponse);
3028
}
3129
}

scribejava-apis/src/test/java/com/github/scribejava/apis/fitbit/FitBitJsonTokenExtractorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.scribejava.core.model.OAuth2AccessTokenErrorResponse;
44
import com.github.scribejava.core.oauth2.OAuth2Error;
5+
import java.io.IOException;
56

67
import org.hamcrest.FeatureMatcher;
78
import org.junit.Rule;
@@ -23,7 +24,7 @@ public class FitBitJsonTokenExtractorTest {
2324
public ExpectedException thrown = ExpectedException.none();
2425

2526
@Test
26-
public void testErrorExtraction() {
27+
public void testErrorExtraction() throws IOException {
2728

2829
final FitBitJsonTokenExtractor extractor = new FitBitJsonTokenExtractor();
2930

scribejava-core/src/main/java/com/github/scribejava/core/extractors/AbstractOAuth1JSONTokenExtractor.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
package com.github.scribejava.core.extractors;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.github.scribejava.core.exceptions.OAuthException;
45
import com.github.scribejava.core.model.OAuth1Token;
6+
import com.github.scribejava.core.model.OAuthConstants;
57
import com.github.scribejava.core.model.Response;
6-
import com.github.scribejava.core.utils.OAuthEncoder;
78
import com.github.scribejava.core.utils.Preconditions;
89

910
import java.io.IOException;
10-
import java.util.regex.Matcher;
11-
import java.util.regex.Pattern;
11+
import java.util.Map;
1212

1313

1414
public abstract class AbstractOAuth1JSONTokenExtractor<T extends OAuth1Token> implements TokenExtractor<T> {
1515

16-
private static final Pattern OAUTH_TOKEN_PATTERN = Pattern.compile("\"oauth_token\"\\s*:\\s*\"(\\S*?)\"");
17-
private static final Pattern OAUTH_TOKEN_SECRET_PATTERN
18-
= Pattern.compile("\"oauth_token_secret\"\\s*:\\s*\"(\\S*?)\"");
16+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
1917

2018
@Override
2119
public T extract(Response response) throws IOException {
22-
final String body = response.getBody();
23-
Preconditions.checkEmptyString(body, "Response body is incorrect. Can't extract a token from an empty string");
24-
final String token = extract(body, OAUTH_TOKEN_PATTERN);
25-
final String secret = extract(body, OAUTH_TOKEN_SECRET_PATTERN);
26-
return createToken(token, secret, body);
27-
}
20+
final String rawBody = response.getBody();
21+
Preconditions.checkEmptyString(rawBody,
22+
"Response body is incorrect. Can't extract a token from an empty string");
23+
24+
@SuppressWarnings("unchecked")
25+
final Map<String, String> body = OBJECT_MAPPER.readValue(rawBody, Map.class);
2826

29-
private String extract(String response, Pattern p) {
30-
final Matcher matcher = p.matcher(response);
31-
if (matcher.find() && matcher.groupCount() >= 1) {
32-
return OAuthEncoder.decode(matcher.group(1));
33-
} else {
27+
final String token = body.get(OAuthConstants.TOKEN);
28+
final String secret = body.get(OAuthConstants.TOKEN_SECRET);
29+
30+
if (token == null || secret == null) {
3431
throw new OAuthException("Response body is incorrect. Can't extract token and secret from this: '"
35-
+ response + '\'', null);
32+
+ rawBody + '\'', null);
3633
}
34+
35+
return createToken(token, secret, rawBody);
3736
}
3837

3938
protected abstract T createToken(String token, String secret, String response);

0 commit comments

Comments
 (0)