Skip to content

Commit b993c3c

Browse files
Updated core for opensourcing
1 parent 54b1b60 commit b993c3c

6 files changed

Lines changed: 259 additions & 217 deletions

File tree

src/main/java/com/paypal/core/Constants.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ private Constants() {}
5454

5555
// PayPal Device IP Address Header
5656
public static final String PAYPAL_DEVICE_IPADDRESS_HEADER = "X-PAYPAL-DEVICE-IPADDRESS";
57+
58+
// User Agent Header
59+
public static final String USER_AGENT_HEADER = "User-Agent";
60+
61+
// PayPal Request ID Header
62+
public static final String PAYPAL_REQUEST_ID_HEADER = "PayPal-Request-Id";
63+
64+
// Authorization Header
65+
public static final String AUTHORIZATION_HEADER = "Authorization";
5766

5867
// PayPal Sandbox Email Address for AA Header
5968
public static final String PAYPAL_SANDBOX_EMAIL_ADDRESS_HEADER = "X-PAYPAL-SANDBOX-EMAIL-ADDRESS";

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.HashMap;
55
import java.util.Map;
66

7-
87
import com.google.gson.JsonElement;
98
import com.google.gson.JsonParser;
109
import com.paypal.core.ConfigManager;
@@ -15,6 +14,7 @@
1514
import com.paypal.core.SDKUtil;
1615
import com.paypal.core.codec.binary.Base64;
1716
import com.paypal.core.credential.ICredential;
17+
import com.paypal.sdk.util.UserAgentHeader;
1818

1919
public final class OAuthTokenCredential implements ICredential {
2020

@@ -125,9 +125,11 @@ private String generateOAuthToken(String base64ClientID)
125125
httpConfiguration = getOAuthHttpConfiguration();
126126
connection.createAndconfigureHttpConnection(httpConfiguration);
127127
Map<String, String> headers = new HashMap<String, String>();
128-
headers.put("Authorization", "Basic " + base64ClientID);
128+
headers.put(Constants.AUTHORIZATION_HEADER, "Basic " + base64ClientID);
129129
headers.put(Constants.HTTP_ACCEPT_HEADER, "*/*");
130-
headers.put("User-Agent", RESTConfiguration.formUserAgentHeader());
130+
UserAgentHeader userAgentHeader = new UserAgentHeader(
131+
PayPalResource.SDK_ID, PayPalResource.SDK_VERSION);
132+
headers.putAll(userAgentHeader.getHeader());
131133
String postRequest = "grant_type=client_credentials";
132134
String jsonResponse = connection.execute("", postRequest, headers);
133135
JsonParser parser = new JsonParser();

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

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import java.util.Map;
1010
import java.util.Properties;
1111

12+
import com.paypal.core.APICallPreHandler;
1213
import com.paypal.core.ConfigManager;
1314
import com.paypal.core.ConnectionManager;
15+
import com.paypal.core.Constants;
1416
import com.paypal.core.HttpConfiguration;
1517
import com.paypal.core.HttpConnection;
1618
import com.paypal.core.LoggingManager;
@@ -188,8 +190,8 @@ public static <T> T configureAndExecute(APIContext apiContext,
188190
accessToken = apiContext.getAccessToken();
189191
requestId = apiContext.getRequestId();
190192
}
191-
return configureAndExecute(cMap, accessToken, httpMethod,
192-
resourcePath, null, payLoad, requestId, clazz);
193+
return configureAndExecute(cMap, accessToken, httpMethod, resourcePath,
194+
null, payLoad, requestId, clazz);
193195
}
194196

195197
/**
@@ -219,8 +221,8 @@ public static <T> T configureAndExecute(APIContext apiContext,
219221
if (apiContext != null) {
220222
cMap = apiContext.getConfigurationMap();
221223
}
222-
return configureAndExecute(cMap, null, httpMethod,
223-
resourcePath, headersMap, payLoad, null, clazz);
224+
return configureAndExecute(cMap, null, httpMethod, resourcePath,
225+
headersMap, payLoad, null, clazz);
224226
}
225227

226228
private static <T> T configureAndExecute(
@@ -236,41 +238,35 @@ private static <T> T configureAndExecute(
236238
if (!configInitialized) {
237239
initializeToDefault();
238240
}
239-
cMap = new HashMap<String, String>(
240-
PayPalResource.configurationMap);
241+
cMap = new HashMap<String, String>(PayPalResource.configurationMap);
241242
}
242-
RESTConfiguration restConfiguration = createRESTConfiguration(
243-
cMap, httpMethod, resourcePath, headersMap,
243+
String contentType = (headersMap != null && headersMap
244+
.containsKey(Constants.HTTP_CONTENT_TYPE_HEADER)) ? headersMap
245+
.remove(Constants.HTTP_CONTENT_TYPE_HEADER) : null;
246+
247+
APICallPreHandler apiCallPreHandler = createAPICallPreHandler(
248+
cMap, payLoad, resourcePath, headersMap,
244249
accessToken, requestId);
245-
t = execute(restConfiguration, payLoad, resourcePath, clazz);
250+
HttpConfiguration httpConfiguration = createHttpConfiguration(
251+
cMap, httpMethod, contentType, apiCallPreHandler);
252+
t = execute(apiCallPreHandler, httpConfiguration, clazz);
246253
return t;
247254

248255
}
249256

250-
/**
251-
* Creates a {@link RESTConfiguration} based on configuration
252-
*
253-
* @param httpMethod
254-
* {@link HttpMethod}
255-
* @param resourcePath
256-
* Resource URI
257-
* @param accessToken
258-
* Access Token
259-
* @param requestId
260-
* Request Id
261-
* @return
262-
*/
263-
private static RESTConfiguration createRESTConfiguration(
264-
Map<String, String> configurationMap, HttpMethod httpMethod,
257+
private static APICallPreHandler createAPICallPreHandler(
258+
Map<String, String> configurationMap, String payLoad,
265259
String resourcePath, Map<String, String> headersMap,
266260
String accessToken, String requestId) {
261+
APICallPreHandler apiCallPreHandler = null;
267262
RESTConfiguration restConfiguration = new RESTConfiguration(
268263
configurationMap, headersMap);
269-
restConfiguration.setHttpMethod(httpMethod);
270264
restConfiguration.setResourcePath(resourcePath);
271265
restConfiguration.setRequestId(requestId);
272266
restConfiguration.setAuthorizationToken(accessToken);
273-
return restConfiguration;
267+
restConfiguration.setPayLoad(payLoad);
268+
apiCallPreHandler = restConfiguration;
269+
return apiCallPreHandler;
274270
}
275271

276272
/**
@@ -295,32 +291,27 @@ private static RESTConfiguration createRESTConfiguration(
295291
* @throws HttpErrorException
296292
* @throws InvalidResponseDataException
297293
*/
298-
private static <T> T execute(RESTConfiguration restConfiguration,
299-
String payLoad, String resourcePath, Class<T> clazz)
294+
private static <T> T execute(APICallPreHandler apiCallPreHandler,
295+
HttpConfiguration httpConfiguration, Class<T> clazz)
300296
throws PayPalRESTException {
301297
T t = null;
302298
ConnectionManager connectionManager;
303299
HttpConnection httpConnection;
304-
HttpConfiguration httpConfig;
305300
Map<String, String> headers;
306301
String responseString;
307302
try {
308303

309304
// REST Headers
310-
headers = restConfiguration.getHeaders();
311-
312-
// HTTPConfiguration Object
313-
httpConfig = restConfiguration.getHttpConfigurations();
305+
headers = apiCallPreHandler.getHeaderMap();
314306

315307
// HttpConnection Initialization
316308
connectionManager = ConnectionManager.getInstance();
317-
httpConnection = connectionManager.getConnection(httpConfig);
318-
httpConnection.createAndconfigureHttpConnection(httpConfig);
309+
httpConnection = connectionManager.getConnection(httpConfiguration);
310+
httpConnection.createAndconfigureHttpConnection(httpConfiguration);
319311

320-
LASTREQUEST.set(payLoad);
321-
responseString = httpConnection.execute(restConfiguration
322-
.getBaseURL().toURI().resolve(resourcePath).toString(),
323-
payLoad, headers);
312+
LASTREQUEST.set(apiCallPreHandler.getPayLoad());
313+
responseString = httpConnection.execute(null,
314+
apiCallPreHandler.getPayLoad(), headers);
324315
LASTRESPONSE.set(responseString);
325316
if (clazz != null) {
326317
t = JSONFormatter.fromJSON(responseString, clazz);
@@ -331,4 +322,42 @@ private static <T> T execute(RESTConfiguration restConfiguration,
331322
return t;
332323
}
333324

325+
private static HttpConfiguration createHttpConfiguration(
326+
Map<String, String> configurationMap, HttpMethod httpMethod,
327+
String contentType, APICallPreHandler apiCallPreHandler) {
328+
HttpConfiguration httpConfiguration = new HttpConfiguration();
329+
httpConfiguration.setHttpMethod(httpMethod.toString());
330+
httpConfiguration.setEndPointUrl(apiCallPreHandler.getEndPoint());
331+
httpConfiguration.setContentType((contentType != null && contentType
332+
.trim().length() > 0) ? (contentType)
333+
: Constants.HTTP_CONTENT_TYPE_JSON);
334+
httpConfiguration
335+
.setGoogleAppEngine(Boolean.parseBoolean(configurationMap
336+
.get(Constants.GOOGLE_APP_ENGINE)));
337+
if (Boolean.parseBoolean(configurationMap
338+
.get((Constants.USE_HTTP_PROXY)))) {
339+
httpConfiguration.setProxyPort(Integer.parseInt(configurationMap
340+
.get((Constants.HTTP_PROXY_PORT))));
341+
httpConfiguration.setProxyHost(configurationMap
342+
.get((Constants.HTTP_PROXY_HOST)));
343+
httpConfiguration.setProxyUserName(configurationMap
344+
.get((Constants.HTTP_PROXY_USERNAME)));
345+
httpConfiguration.setProxyPassword(configurationMap
346+
.get((Constants.HTTP_PROXY_PASSWORD)));
347+
}
348+
httpConfiguration.setConnectionTimeout(Integer
349+
.parseInt(configurationMap
350+
.get(Constants.HTTP_CONNECTION_TIMEOUT)));
351+
httpConfiguration.setMaxRetry(Integer.parseInt(configurationMap
352+
.get(Constants.HTTP_CONNECTION_RETRY)));
353+
httpConfiguration.setReadTimeout(Integer.parseInt(configurationMap
354+
.get(Constants.HTTP_CONNECTION_READ_TIMEOUT)));
355+
httpConfiguration.setMaxHttpConnection(Integer
356+
.parseInt(configurationMap
357+
.get(Constants.HTTP_CONNECTION_MAX_CONNECTION)));
358+
httpConfiguration.setIpAddress(configurationMap
359+
.get(Constants.DEVICE_IP_ADDRESS));
360+
return httpConfiguration;
361+
}
362+
334363
}

0 commit comments

Comments
 (0)