Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.paypal.base.rest;
  
  import java.io.File;
  import java.util.HashMap;
  import java.util.Map;
 
 
PayPalResource acts as a base class for REST enabled resources.
 
 public abstract class PayPalResource extends PayPalModel{
 
 	private static final Logger log = LoggerFactory.getLogger(PayPalResource.class);
 	
 	/*
 	 * The class relies on an implementation of APICallPreHandler (here
 	 * RESTAPICallPreHandler)to get access to endpoint, HTTP headers, and
 	 * payload.
 	 */
Map used in dynamic configuration
 
 	private static Map<StringStringconfigurationMap;

Configuration enabled flag
 
 	private static boolean configInitialized = false;

Last request sent to Service
 
 	private static final ThreadLocal<StringLASTREQUEST = new ThreadLocal<String>();

Last response returned form Service
 
 	private static final ThreadLocal<StringLASTRESPONSE = new ThreadLocal<String>();

Initialize the system using a File(Properties file). The system is initialized using the given file and if the initialization succeeds the default 'sdk_config.properties' can only be loaded by calling the method initializeToDefault()

Parameters:
file File object of a properties entity
Returns:
OAuthTokenCredential instance with client ID and client secret stored in configuration file.
Throws:
PayPalRESTException
 
 	public static OAuthTokenCredential initConfig(File filethrows PayPalRESTException {
 		try {
 			if (!file.exists()) {
 				throw new FileNotFoundException("File doesn't exist: "
 						+ file.getAbsolutePath());
 			}
 			FileInputStream fis = new FileInputStream(file);
 			return initConfig(fis);
 		} catch (IOException ioe) {
 			.error(ioe.getMessage(), ioe);
 			throw new PayPalRESTException(ioe.getMessage(), ioe);
 		}
 
 	}

Initialize using Properties. The system is initialized using the given properties object and if the initialization succeeds the default 'sdk_config.properties' can only be loaded by calling the method initializeToDefault()

Parameters:
properties Properties object
Returns:
OAuthTokenCredential instance with client ID and client secret in given properties.
 
 	public static OAuthTokenCredential initConfig(Properties properties) {
 		 = SDKUtil.constructMap(properties);
 		 = true;
 	}

Initialize using java.io.InputStream(of a Properties file).. The system is initialized using the given java.io.InputStream and if the initialization succeeds the default 'sdk_config.properties' can only be loaded by calling the method initializeToDefault(). The system is initialized with the information after loading defaults for the parameters that are not passed as part of the configuration. For defaults see com.paypal.base.ConfigManager

Parameters:
inputStream InputStream
Returns:
OAuthTokenCredential instance with client ID and client secret stored in given inputStream.
Throws:
PayPalRESTException
	public static OAuthTokenCredential initConfig(InputStream inputStream)
		try {
			Properties properties = new Properties();
			properties.load(inputStream);
			/*
			 * Create a Map instance and combine it with default values
			 */
			 = SDKUtil.constructMap(properties);
catch (IOException ioe) {
			.error(ioe.getMessage(), ioe);
			throw new PayPalRESTException(ioe.getMessage(), ioe);
		}
	}

Return Client ID from configuration Map
	public static String getClientID() {
	}

Returns Client Secret from configuration Map
	public static String getClientSecret() {
	}

Returns OAuthTokenCredential instance using client ID and client secret loaded from configuration.

Returns:
OAuthTokenCredential instance.
		}else{
		}
	}

Initialize to default properties

	public static void initializeToDefault() throws PayPalRESTException {
		 = SDKUtil.combineDefaultMap(ConfigManager
	}

Returns the last request sent to the Service

Returns:
Last request sent to the server
	public static String getLastRequest() {
		return .get();
	}

Returns the last response returned by the Service

Returns:
Last response got from the Service
	public static String getLastResponse() {
		return .get();
	}

Configures and executes REST call: Supports JSON

Deprecated:
Parameters:
<T> Response Type for de-serialization
accessToken OAuth AccessToken to be used for the call.
httpMethod Http Method verb
resourcePath Resource URI path
payLoad Payload to Service
clazz java.lang.Class object used in De-serialization
Returns:
T
Throws:
PayPalRESTException
	public static <T> T configureAndExecute(String accessToken,
			HttpMethod httpMethodString resourcePathString payLoad,
			Class<T> clazzthrows PayPalRESTException {
		return configureAndExecute(nullaccessTokenhttpMethodresourcePath,
				nullpayLoadnullclazz);
	}

Configures and executes REST call: Supports JSON

Parameters:
<T> Response Type for de-serialization
apiContext APIContext to be used for the call.
httpMethod Http Method verb
resource Resource URI path
payLoad Payload to Service
clazz java.lang.Class object used in De-serialization
Returns:
T
Throws:
PayPalRESTException
	public static <T> T configureAndExecute(APIContext apiContext,
			HttpMethod httpMethodString resourcePathString payLoad,
			Class<T> clazzthrows PayPalRESTException {
t = null;
		Map<StringStringcMap = null;
		String accessToken = null;
		String requestId = null;
		Map<StringStringheadersMap = null;
		if (apiContext != null) {
			if (apiContext.getConfigurationMap() != null) {
				cMap = SDKUtil.combineDefaultMap(apiContext
else {
				}
				/*
				 * The Map returned here is already combined with default values
				 */
				cMap = new HashMap<StringString>(
			}
			headersMap = apiContext.getHTTPHeaders();
			accessToken = apiContext.getAccessToken();
			requestId = apiContext.getRequestId();
			APICallPreHandler apiCallPreHandler = createAPICallPreHandler(cMap,
					payLoadresourcePathheadersMapaccessTokenrequestId,
					apiContext.getSdkVersion());
			HttpConfiguration httpConfiguration = createHttpConfiguration(cMap,
					httpMethodapiCallPreHandler);
			t = execute(apiCallPreHandlerhttpConfigurationclazz);
		}
		return t;
	}

Configures and executes REST call: Supports JSON

Deprecated:
Parameters:
<T>
apiContext APIContext to be used for the call.
httpMethod Http Method verb
resourcePath Resource URI path
headersMap Optional headers Map
payLoad Payload to Service
clazz java.lang.Class object used in De-serialization
Returns:
T
Throws:
PayPalRESTException
	public static <T> T configureAndExecute(APIContext apiContext,
			HttpMethod httpMethodString resourcePath,
			Map<StringStringheadersMapString payLoadClass<T> clazz)
		Map<StringStringcMap = null;
		String accessToken = null;
		String requestId = null;
		if (apiContext != null) {
			cMap = apiContext.getConfigurationMap();
			accessToken = apiContext.getAccessToken();
			requestId = apiContext.getRequestId();
		}
		return configureAndExecute(cMapaccessTokenhttpMethodresourcePath,
				headersMappayLoadrequestIdclazz);
	}
	private static <T> T configureAndExecute(
			Map<StringStringconfigurationMapString accessToken,
			HttpMethod httpMethodString resourcePath,
			Map<StringStringheadersMapString payLoadString requestId,
			Class<T> clazzthrows PayPalRESTException {
t = null;
		Map<StringStringcMap = null;
		/*
		 * Check for null before combining with default
		 */
		if (configurationMap != null) {
			cMap = SDKUtil.combineDefaultMap(configurationMap);
else {
			}
			/*
			 * The Map returned here is already combined with default values
			 */
			cMap = new HashMap<StringString>(configurationMap);
		}
		APICallPreHandler apiCallPreHandler = createAPICallPreHandler(cMap,
				payLoadresourcePathheadersMapaccessTokenrequestIdnull);
		HttpConfiguration httpConfiguration = createHttpConfiguration(cMap,
				httpMethodapiCallPreHandler);
		t = execute(apiCallPreHandlerhttpConfigurationclazz);
		return t;
	}

Returns a implementation of com.paypal.base.APICallPreHandler for the underlying layer.

Parameters:
configurationMap configuration Map
payLoad Raw payload
resourcePath URI part of the resource operated on
headersMap Custom HTTP headers map
accessToken OAuth Token
requestId PayPal Request Id
sdkVersion com.paypal.base.SDKVersion instance
Returns:
			Map<StringStringconfigurationMapString payLoad,
			String resourcePathMap<StringStringheadersMap,
			String accessTokenString requestIdSDKVersion sdkVersion) {
		APICallPreHandler apiCallPreHandler = null;
		RESTAPICallPreHandler restAPICallPreHandler = new RESTAPICallPreHandler(
				configurationMapheadersMap);
		restAPICallPreHandler.setResourcePath(resourcePath);
		restAPICallPreHandler.setRequestId(requestId);
		restAPICallPreHandler.setAuthorizationToken(accessToken);
		restAPICallPreHandler.setPayLoad(payLoad);
		restAPICallPreHandler.setSdkVersion(sdkVersion);
		apiCallPreHandler = restAPICallPreHandler;
		return apiCallPreHandler;
	}

Execute the API call and return response

Parameters:
<T> Generic Type for response object construction
apiCallPreHandler Implementation of com.paypal.base.APICallPreHandler
httpConfiguration com.paypal.base.HttpConfiguration
clazz Response Object class
Returns:
Response Type
Throws:
PayPalRESTException
	private static <T> T execute(APICallPreHandler apiCallPreHandler,
			HttpConfiguration httpConfigurationClass<T> clazz)
t = null;
		ConnectionManager connectionManager;
		HttpConnection httpConnection;
		Map<StringStringheaders;
		String responseString;
		try {
			// REST Headers
			headers = apiCallPreHandler.getHeaderMap();
			// HttpConnection Initialization
			connectionManager = ConnectionManager.getInstance();
			httpConnection = connectionManager.getConnection(httpConfiguration);
			httpConnection.createAndconfigureHttpConnection(httpConfiguration);
			.set(apiCallPreHandler.getPayLoad());
			responseString = httpConnection.execute(null,
					apiCallPreHandler.getPayLoad(), headers);
			.set(responseString);
			if (clazz != null) {
				t = JSONFormatter.fromJSON(responseStringclazz);
			}
catch (HttpErrorException e) {
			throw PayPalRESTException.createFromHttpErrorException(e);
catch (Exception e) {
			throw new PayPalRESTException(e.getMessage(), e);
		}
		return t;
	}

Utility method that creates a com.paypal.base.HttpConfiguration object from the passed information

Parameters:
configurationMap Configuration to base the construction upon.
httpMethod HTTP Method
contentType Content-Type header
apiCallPreHandler com.paypal.base.APICallPreHandler for retrieving EndPoint
Returns:
			Map<StringStringconfigurationMapHttpMethod httpMethod,
			APICallPreHandler apiCallPreHandler) {
		HttpConfiguration httpConfiguration = new HttpConfiguration();
		httpConfiguration.setHttpMethod(httpMethod.toString());
		httpConfiguration.setEndPointUrl(apiCallPreHandler.getEndPoint());
		httpConfiguration
				.setGoogleAppEngine(Boolean.parseBoolean(configurationMap
		if (Boolean.parseBoolean(configurationMap
			httpConfiguration.setProxyPort(Integer.parseInt(configurationMap
			httpConfiguration.setProxyHost(configurationMap
			httpConfiguration.setProxyUserName(configurationMap
			httpConfiguration.setProxyPassword(configurationMap
		}
		httpConfiguration.setConnectionTimeout(Integer
				.parseInt(configurationMap
		httpConfiguration.setMaxRetry(Integer.parseInt(configurationMap
		httpConfiguration.setReadTimeout(Integer.parseInt(configurationMap
		httpConfiguration.setMaxHttpConnection(Integer
				.parseInt(configurationMap
		httpConfiguration.setIpAddress(configurationMap
		return httpConfiguration;
	}
New to GrepCode? Check out our FAQ X