Skip to content

Commit 1aaee59

Browse files
Update document comments.
Moved request payload of OAuthTokenCredential as protected method and added a setter for uri part
1 parent fa8d6b8 commit 1aaee59

4 files changed

Lines changed: 66 additions & 15 deletions

File tree

src/main/java/com/paypal/core/rest/APIContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public APIContext(String accessToken) {
6262
* API service. The token should be of the form 'Bearer xxxx..'.
6363
* See {@link OAuthTokenCredential} to generate OAuthToken
6464
* @param requestId
65-
* Unique requestId required for the call. Idempotency id, Calling
66-
* setMaskRequestId(true) will override the requestId getter to
67-
* return null, which can be used by the client to forcibly not
68-
* sent requestId in the API call.
65+
* Unique requestId required for the call. Idempotency id,
66+
* Calling setMaskRequestId(true) will override the requestId
67+
* getter to return null, which can be used by the client (null
68+
* check) to forcibly not sent requestId in the API call.
6969
*/
7070
public APIContext(String accessToken, String requestId) {
7171
this(accessToken);

src/main/java/com/paypal/core/rest/OAuthTokenCredential.java

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,29 @@
1616
import com.paypal.core.credential.ICredential;
1717
import com.paypal.sdk.util.UserAgentHeader;
1818

19+
/**
20+
* OAuthTokenCredential is used for generation of OAuth Token used by PayPal
21+
* REST API service. ClientID and ClientSecret are required by the class to
22+
* generate OAuth Token, the resulting token is of the form "Bearer xxxxxx". The
23+
* class has two constructors, one of it taking an additional configuration map
24+
* used for dynamic configuration. When using the constructor with out
25+
* configuration map the endpoint is fetched from the configuration that is used
26+
* during initialization. See {@link PayPalResource} for configuring the system.
27+
* When using a configuration map the class expects an entry by the name
28+
* "oauth.EndPoint" or "service.EndPoint" to retrieve the value of the endpoint
29+
* for the OAuth Service. If either are not present the configuration should
30+
* have a entry by the name "mode" with values sandbox or live wherein the
31+
* corresponding endpoints are default to PayPal endpoints.
32+
*
33+
* @author kjayakumar
34+
*
35+
*/
1936
public final class OAuthTokenCredential implements ICredential {
2037

2138
/**
2239
* OAuth URI path parameter
2340
*/
24-
private static final String OAUTH_TOKEN_PATH = "/v1/oauth2/token";
41+
private static String OAUTH_TOKEN_PATH = "/v1/oauth2/token";
2542

2643
/**
2744
* Client ID for OAuth
@@ -43,6 +60,17 @@ public final class OAuthTokenCredential implements ICredential {
4360
*/
4461
private Map<String, String> configurationMap;
4562

63+
/**
64+
* Sets the URI path for the OAuth Token service. If not set it defaults to
65+
* "/v1/oauth2/token"
66+
*
67+
* @param oauthTokenPath
68+
* the URI part to set
69+
*/
70+
public static void setOAUTH_TOKEN_PATH(String oauthTokenPath) {
71+
OAUTH_TOKEN_PATH = oauthTokenPath;
72+
}
73+
4674
/**
4775
* @param clientID
4876
* Client ID for the OAuth
@@ -58,10 +86,17 @@ public OAuthTokenCredential(String clientID, String clientSecret) {
5886
}
5987

6088
/**
89+
* Configuration Constructor for dynamic configuration
90+
*
6191
* @param clientID
6292
* Client ID for the OAuth
6393
* @param clientSecret
6494
* Client Secret for OAuth
95+
* @param configurationMap
96+
* Dynamic configuration map which should have an entry for
97+
* 'oauth.EndPoint' or 'service.EndPoint'. If either are not
98+
* present then there should be entry for 'mode' with values
99+
* sandbox/live, wherein PayPals endpoints are used.
65100
*/
66101
public OAuthTokenCredential(String clientID, String clientSecret,
67102
Map<String, String> configurationMap) {
@@ -73,16 +108,13 @@ public OAuthTokenCredential(String clientID, String clientSecret,
73108

74109
/**
75110
* Computes Access Token by placing a call to OAuth server using ClientID
76-
* and ClientSecret. The token is appended to the token type.
111+
* and ClientSecret. The token is appended to the token type (Bearer).
77112
*
78113
* @return the accessToken
79114
* @throws PayPalRESTException
80115
*/
81116
public String getAccessToken() throws PayPalRESTException {
82117
if (accessToken == null) {
83-
// Write Logic for passing in Detail to Identity Api Serv and
84-
// computing the token
85-
// Set the Value inside the accessToken and result
86118
accessToken = generateAccessToken();
87119
}
88120
return accessToken;
@@ -125,12 +157,13 @@ private String generateOAuthToken(String base64ClientID)
125157
httpConfiguration = getOAuthHttpConfiguration();
126158
connection.createAndconfigureHttpConnection(httpConfiguration);
127159
Map<String, String> headers = new HashMap<String, String>();
128-
headers.put(Constants.AUTHORIZATION_HEADER, "Basic " + base64ClientID);
160+
headers.put(Constants.AUTHORIZATION_HEADER, "Basic "
161+
+ base64ClientID);
129162
headers.put(Constants.HTTP_ACCEPT_HEADER, "*/*");
130163
UserAgentHeader userAgentHeader = new UserAgentHeader(
131164
PayPalResource.SDK_ID, PayPalResource.SDK_VERSION);
132165
headers.putAll(userAgentHeader.getHeader());
133-
String postRequest = "grant_type=client_credentials";
166+
String postRequest = getRequestPayload();
134167
String jsonResponse = connection.execute("", postRequest, headers);
135168
JsonParser parser = new JsonParser();
136169
JsonElement jsonElement = parser.parse(jsonResponse);
@@ -145,16 +178,25 @@ private String generateOAuthToken(String base64ClientID)
145178
return generatedToken;
146179
}
147180

181+
/**
182+
* Returns the request payload for OAuth Service. Override this method to
183+
* alter the payload
184+
*
185+
* @return Payload as String
186+
*/
187+
protected String getRequestPayload() {
188+
return "grant_type=client_credentials";
189+
}
190+
148191
/*
149192
* Get HttpConfiguration object for OAuth server
150193
*/
151194
private HttpConfiguration getOAuthHttpConfiguration() {
152195
HttpConfiguration httpConfiguration = new HttpConfiguration();
153196
httpConfiguration
154197
.setHttpMethod(Constants.HTTP_CONFIG_DEFAULT_HTTP_METHOD);
155-
String endPointUrl = (configurationMap.get(Constants.OAUTH_ENDPOINT) != null
156-
&& configurationMap.get(Constants.OAUTH_ENDPOINT).trim()
157-
.length() >= 0) ? configurationMap
198+
String endPointUrl = (configurationMap.get(Constants.OAUTH_ENDPOINT) != null && configurationMap
199+
.get(Constants.OAUTH_ENDPOINT).trim().length() >= 0) ? configurationMap
158200
.get(Constants.OAUTH_ENDPOINT) : configurationMap
159201
.get(Constants.ENDPOINT);
160202
if (endPointUrl == null || endPointUrl.trim().length() <= 0) {

src/main/java/com/paypal/core/rest/PayPalRESTException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
public class PayPalRESTException extends Exception {
77

8+
/**
9+
* Serial Version ID
10+
*/
11+
private static final long serialVersionUID = 1L;
12+
813
public PayPalRESTException(String message) {
914
super(message);
1015
}

src/main/java/com/paypal/core/rest/RESTUtil.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import com.paypal.sdk.openidconnect.CreateFromRefreshTokenParameters;
1010
import com.paypal.sdk.openidconnect.UserinfoParameters;
1111

12-
12+
/**
13+
* <code>RESTUtil</code> acts as utility class used by REST API system
14+
* @author kjayakumar
15+
*
16+
*/
1317
public final class RESTUtil {
1418

1519
private RESTUtil() {}

0 commit comments

Comments
 (0)