/src/main/java/com/paypal/core/nvp/PlatformAPICallPreHandler.java

https://gitlab.com/CORP-RESELLER/sdk-core-java · Java · 432 lines · 233 code · 40 blank · 159 comment · 44 complexity · f555c0a643660b18ff884f733a1b850f MD5 · raw file

  1. package com.paypal.core.nvp;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import com.paypal.core.APICallPreHandler;
  5. import com.paypal.core.Constants;
  6. import com.paypal.core.CredentialManager;
  7. import com.paypal.core.credential.CertificateCredential;
  8. import com.paypal.core.credential.ICredential;
  9. import com.paypal.core.credential.SignatureCredential;
  10. import com.paypal.core.credential.ThirdPartyAuthorization;
  11. import com.paypal.core.credential.TokenAuthorization;
  12. import com.paypal.exception.ClientActionRequiredException;
  13. import com.paypal.exception.InvalidCredentialException;
  14. import com.paypal.exception.MissingCredentialException;
  15. import com.paypal.sdk.exceptions.OAuthException;
  16. import com.paypal.sdk.util.UserAgentHeader;
  17. /**
  18. * <code>PlatformAPICallPreHandler</code> is an implementation of
  19. * {@link APICallPreHandler} for NVP based API service
  20. *
  21. */
  22. public class PlatformAPICallPreHandler implements APICallPreHandler {
  23. /**
  24. * Service Name
  25. */
  26. private final String serviceName;
  27. /**
  28. * API method
  29. */
  30. private final String method;
  31. /**
  32. * Raw payload from stubs
  33. */
  34. private final String rawPayLoad;
  35. /**
  36. * API Username for authentication
  37. */
  38. private String apiUserName;
  39. /**
  40. * {@link ICredential} for authentication
  41. */
  42. private ICredential credential;
  43. /**
  44. * Access token if any for authorization
  45. */
  46. private String accessToken;
  47. /**
  48. * TokenSecret if any for authorization
  49. */
  50. private String tokenSecret;
  51. /**
  52. * SDK Name used in tracking
  53. */
  54. private String sdkName;
  55. /**
  56. * SDK Version
  57. */
  58. private String sdkVersion;
  59. /**
  60. * PortName to which a particular operation is bound;
  61. */
  62. private String portName;
  63. /**
  64. * Internal variable to hold headers
  65. */
  66. private Map<String, String> headers;
  67. /**
  68. * Map used for to override ConfigManager configurations
  69. */
  70. private Map<String, String> configurationMap = null;
  71. /**
  72. * @deprecated Private Constructor
  73. */
  74. private PlatformAPICallPreHandler(String rawPayLoad, String serviceName,
  75. String method) {
  76. super();
  77. this.rawPayLoad = rawPayLoad;
  78. this.serviceName = serviceName;
  79. this.method = method;
  80. }
  81. /**
  82. * Private Constructor
  83. *
  84. * @param rawPayLoad
  85. * @param serviceName
  86. * @param method
  87. * @param configurationMap
  88. */
  89. private PlatformAPICallPreHandler(String rawPayLoad, String serviceName,
  90. String method, Map<String, String> configurationMap) {
  91. super();
  92. this.rawPayLoad = rawPayLoad;
  93. this.serviceName = serviceName;
  94. this.method = method;
  95. this.configurationMap = configurationMap;
  96. }
  97. /**
  98. * PlatformAPICallPreHandler
  99. *
  100. * @deprecated
  101. * @param serviceName
  102. * Service Name
  103. * @param rawPayLoad
  104. * Payload
  105. * @param method
  106. * API method
  107. * @param apiUserName
  108. * API Username
  109. * @param accessToken
  110. * Access Token
  111. * @param tokenSecret
  112. * Token Secret
  113. * @throws MissingCredentialException
  114. * @throws InvalidCredentialException
  115. */
  116. public PlatformAPICallPreHandler(String rawPayLoad, String serviceName,
  117. String method, String apiUserName, String accessToken,
  118. String tokenSecret) throws InvalidCredentialException,
  119. MissingCredentialException {
  120. this(rawPayLoad, serviceName, method);
  121. this.apiUserName = apiUserName;
  122. this.accessToken = accessToken;
  123. this.tokenSecret = tokenSecret;
  124. initCredential();
  125. }
  126. /**
  127. * PlatformAPICallPreHandler
  128. *
  129. * @deprecated
  130. * @param serviceName
  131. * Service Name
  132. * @param rawPayLoad
  133. * Payload
  134. * @param method
  135. * API method
  136. * @param credential
  137. * {@link ICredential} instance
  138. */
  139. public PlatformAPICallPreHandler(String rawPayLoad, String serviceName,
  140. String method, ICredential credential) {
  141. this(rawPayLoad, serviceName, method);
  142. if (credential == null) {
  143. throw new IllegalArgumentException(
  144. "Credential is null in PlatformAPICallPreHandler");
  145. }
  146. this.credential = credential;
  147. }
  148. /**
  149. * PlatformAPICallPreHandler
  150. *
  151. * @param serviceName
  152. * Service Name
  153. * @param rawPayLoad
  154. * Payload
  155. * @param method
  156. * API method
  157. * @param credential
  158. * {@link ICredential} instance
  159. * @param sdkName
  160. * SDK Name
  161. * @param sdkVersion
  162. * sdkVersion
  163. * @param portName
  164. * Port Name
  165. * @param configurationMap
  166. */
  167. public PlatformAPICallPreHandler(String rawPayLoad, String serviceName,
  168. String method, ICredential credential, String sdkName,
  169. String sdkVersion, String portName,
  170. Map<String, String> configurationMap) {
  171. this(rawPayLoad, serviceName, method, configurationMap);
  172. if (credential == null) {
  173. throw new IllegalArgumentException(
  174. "Credential is null in PlatformAPICallPreHandler");
  175. }
  176. this.credential = credential;
  177. this.sdkName = sdkName;
  178. this.sdkVersion = sdkVersion;
  179. this.portName = portName;
  180. }
  181. /**
  182. * PlatformAPICallPreHandler
  183. *
  184. * @param serviceName
  185. * Service Name
  186. * @param rawPayLoad
  187. * Payload
  188. * @param method
  189. * API method
  190. * @param apiUserName
  191. * API Username
  192. * @param accessToken
  193. * Access Token
  194. * @param tokenSecret
  195. * Token Secret
  196. * @param sdkName
  197. * SDK Name
  198. * @param sdkVersion
  199. * sdkVersion
  200. * @param portName
  201. * Port Name
  202. * @param configurationMap
  203. * @throws MissingCredentialException
  204. * @throws InvalidCredentialException
  205. */
  206. public PlatformAPICallPreHandler(String rawPayLoad, String serviceName,
  207. String method, String apiUserName, String accessToken,
  208. String tokenSecret, String sdkName, String sdkVersion,
  209. String portName, Map<String, String> configurationMap)
  210. throws InvalidCredentialException, MissingCredentialException {
  211. this(rawPayLoad, serviceName, method, configurationMap);
  212. this.apiUserName = apiUserName;
  213. this.accessToken = accessToken;
  214. this.tokenSecret = tokenSecret;
  215. this.sdkName = sdkName;
  216. this.sdkVersion = sdkVersion;
  217. this.portName = portName;
  218. initCredential();
  219. }
  220. /**
  221. * @return the sdkName
  222. */
  223. public String getSdkName() {
  224. return sdkName;
  225. }
  226. /**
  227. * @deprecated
  228. * @param sdkName
  229. * the sdkName to set
  230. */
  231. public void setSdkName(String sdkName) {
  232. this.sdkName = sdkName;
  233. }
  234. /**
  235. * @return the sdkVersion
  236. */
  237. public String getSdkVersion() {
  238. return sdkVersion;
  239. }
  240. /**
  241. * @deprecated
  242. * @param sdkVersion
  243. * the sdkVersion to set
  244. */
  245. public void setSdkVersion(String sdkVersion) {
  246. this.sdkVersion = sdkVersion;
  247. }
  248. /**
  249. * @return the portName
  250. */
  251. public String getPortName() {
  252. return portName;
  253. }
  254. /**
  255. * @deprecated
  256. * @param portName
  257. * the portName to set
  258. */
  259. public void setPortName(String portName) {
  260. this.portName = portName;
  261. }
  262. public Map<String, String> getHeaderMap() throws OAuthException {
  263. if (headers == null) {
  264. headers = new HashMap<String, String>();
  265. if (credential instanceof SignatureCredential) {
  266. SignatureHttpHeaderAuthStrategy signatureHttpHeaderAuthStrategy = new SignatureHttpHeaderAuthStrategy(
  267. getEndPoint());
  268. headers = signatureHttpHeaderAuthStrategy
  269. .generateHeaderStrategy((SignatureCredential) credential);
  270. } else if (credential instanceof CertificateCredential) {
  271. CertificateHttpHeaderAuthStrategy certificateHttpHeaderAuthStrategy = new CertificateHttpHeaderAuthStrategy(
  272. getEndPoint());
  273. headers = certificateHttpHeaderAuthStrategy
  274. .generateHeaderStrategy((CertificateCredential) credential);
  275. }
  276. headers.putAll(getDefaultHttpHeadersNVP());
  277. }
  278. return headers;
  279. }
  280. public String getPayLoad() {
  281. // No processing necessary for NVP return the raw payload
  282. return rawPayLoad;
  283. }
  284. public String getEndPoint() {
  285. String endPoint = searchEndpoint();
  286. if (endPoint != null) {
  287. if (endPoint.endsWith("/")) {
  288. endPoint += serviceName + "/" + method;
  289. } else {
  290. endPoint += "/" + serviceName + "/" + method;
  291. }
  292. } else if ((Constants.SANDBOX.equalsIgnoreCase(this.configurationMap
  293. .get(Constants.MODE).trim()))) {
  294. endPoint = Constants.PLATFORM_SANDBOX_ENDPOINT + serviceName + "/"
  295. + method;
  296. } else if ((Constants.LIVE.equalsIgnoreCase(this.configurationMap.get(
  297. Constants.MODE).trim()))) {
  298. endPoint = Constants.PLATFORM_LIVE_ENDPOINT + serviceName + "/"
  299. + method;
  300. }
  301. return endPoint;
  302. }
  303. public ICredential getCredential() {
  304. return credential;
  305. }
  306. public void validate() throws ClientActionRequiredException {
  307. String mode = configurationMap.get(Constants.MODE);
  308. if (mode == null && searchEndpoint() == null) {
  309. // Mandatory Mode not specified.
  310. throw new ClientActionRequiredException(
  311. "mode[production/live] OR endpoint not specified");
  312. }
  313. if ((mode != null)
  314. && (!mode.trim().equalsIgnoreCase(Constants.LIVE) && !mode
  315. .trim().equalsIgnoreCase(Constants.SANDBOX))) {
  316. // Mandatory Mode not specified.
  317. throw new ClientActionRequiredException(
  318. "mode[production/live] OR endpoint not specified");
  319. }
  320. }
  321. /*
  322. * Search a valid endpoint in the configuration, returning null if not found
  323. */
  324. private String searchEndpoint() {
  325. String endPoint = this.configurationMap.get(Constants.ENDPOINT + "."
  326. + getPortName()) != null ? this.configurationMap
  327. .get(Constants.ENDPOINT + "." + getPortName())
  328. : (this.configurationMap.get(Constants.ENDPOINT) != null ? this.configurationMap
  329. .get(Constants.ENDPOINT) : null);
  330. if (endPoint != null && endPoint.trim().length() <= 0) {
  331. endPoint = null;
  332. }
  333. return endPoint;
  334. }
  335. private ICredential getCredentials() throws InvalidCredentialException,
  336. MissingCredentialException {
  337. ICredential returnCredential;
  338. CredentialManager credentialManager = new CredentialManager(
  339. this.configurationMap);
  340. returnCredential = credentialManager.getCredentialObject(apiUserName);
  341. if (accessToken != null && accessToken.length() != 0) {
  342. ThirdPartyAuthorization tokenAuth = new TokenAuthorization(
  343. accessToken, tokenSecret);
  344. if (returnCredential instanceof SignatureCredential) {
  345. SignatureCredential sigCred = (SignatureCredential) returnCredential;
  346. sigCred.setThirdPartyAuthorization(tokenAuth);
  347. } else if (returnCredential instanceof CertificateCredential) {
  348. CertificateCredential certCred = (CertificateCredential) returnCredential;
  349. certCred.setThirdPartyAuthorization(tokenAuth);
  350. }
  351. }
  352. return returnCredential;
  353. }
  354. private Map<String, String> getDefaultHttpHeadersNVP() {
  355. Map<String, String> returnMap = new HashMap<String, String>();
  356. returnMap.put(Constants.PAYPAL_APPLICATION_ID_HEADER,
  357. getApplicationId());
  358. returnMap.put(Constants.PAYPAL_REQUEST_DATA_FORMAT_HEADER,
  359. Constants.PAYLOAD_FORMAT_NVP);
  360. returnMap.put(Constants.PAYPAL_RESPONSE_DATA_FORMAT_HEADER,
  361. Constants.PAYLOAD_FORMAT_NVP);
  362. returnMap.put(Constants.PAYPAL_REQUEST_SOURCE_HEADER, sdkName + "-"
  363. + sdkVersion);
  364. // Add user-agent header
  365. UserAgentHeader uaHeader = new UserAgentHeader(sdkName, sdkVersion);
  366. returnMap.putAll(uaHeader.getHeader());
  367. String sandboxEmailAddress = this.configurationMap
  368. .get(Constants.SANDBOX_EMAIL_ADDRESS);
  369. if (sandboxEmailAddress != null) {
  370. returnMap.put(Constants.PAYPAL_SANDBOX_EMAIL_ADDRESS_HEADER,
  371. sandboxEmailAddress);
  372. }
  373. return returnMap;
  374. }
  375. private String getApplicationId() {
  376. String applicationId = null;
  377. if (credential instanceof CertificateCredential) {
  378. applicationId = ((CertificateCredential) credential)
  379. .getApplicationId();
  380. } else if (credential instanceof SignatureCredential) {
  381. applicationId = ((SignatureCredential) credential)
  382. .getApplicationId();
  383. }
  384. return applicationId;
  385. }
  386. private void initCredential() throws InvalidCredentialException,
  387. MissingCredentialException {
  388. if (credential == null) {
  389. credential = getCredentials();
  390. }
  391. }
  392. }