/
Util.java
272 lines (235 loc) · 9.79 KB
/
Util.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazon.pay.api;
import com.amazon.pay.api.exceptions.AmazonPayClientException;
import com.amazon.pay.api.types.Environment;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.SimpleTimeZone;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Util {
public static final String JAVA_VERSION = System.getProperty("java.version");
public static final String OS_NAME = System.getProperty("os.name");
public static final String OS_VERSION = System.getProperty("os.version");
public static final String DEFAULT_ENCODING = "UTF-8";
/**
* Regex which matches any of the sequences that we need to fix up after
* URLEncoder.encode().
*/
private static final Pattern ENCODED_CHARACTERS_PATTERN;
static {
StringBuilder pattern = new StringBuilder();
pattern
.append(Pattern.quote("+"))
.append("|")
.append(Pattern.quote("*"))
.append("|")
.append(Pattern.quote("%7E"))
.append("|")
.append(Pattern.quote("%2F"));
ENCODED_CHARACTERS_PATTERN = Pattern.compile(pattern.toString());
}
/**
* Generates a url encoded string from the given string
* @param value the string to be encoded
* @param path to determine if the given value is a string or not
* @return the url encoded string
* @throws AmazonPayClientException When an error response is returned by Amazon Pay due to bad request or other issue
*/
public static String urlEncode(String value, final boolean path) throws AmazonPayClientException {
if (value == null) {
return "";
}
try {
if (path) {
value = value.replaceAll("/+", "/");
}
final String encoded = URLEncoder.encode(value, DEFAULT_ENCODING);
final Matcher matcher = ENCODED_CHARACTERS_PATTERN.matcher(encoded);
final StringBuffer buffer = new StringBuffer(encoded.length());
while (matcher.find()) {
String replacement = matcher.group(0);
if ("+".equals(replacement)) {
replacement = "%20";
} else if ("*".equals(replacement)) {
replacement = "%2A";
} else if ("%7E".equals(replacement)) {
replacement = "~";
} else if (path && "%2F".equals(replacement)) {
replacement = "/";
}
matcher.appendReplacement(buffer, replacement);
}
matcher.appendTail(buffer);
return buffer.toString();
} catch (UnsupportedEncodingException ex) {
throw new AmazonPayClientException("Encountered UnsupportedEncodingException:", ex);
}
}
/**
* Generates a lower case string from the given string
* @param str the string to be converted
* @return the lower case string
*/
public static String lowerCase(final String str) {
if (str == null || str.isEmpty()) {
return str;
}
return str.toLowerCase(Locale.ENGLISH);
}
/**
* Generates the current time stamp in "yyyyMMdd'T'HHmmss'Z'" format
* @return the formatted timestamp
*/
public static String getFormattedTimestamp() {
final Date now = new Date();
final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(
"yyyyMMdd'T'HHmmss'Z'");
dateTimeFormat.setTimeZone(new SimpleTimeZone(0, "UTC"));
return dateTimeFormat.format(now);
}
/**
* Builds the PrivateKey object fromt the private key string provided
* @param privateKeyString the private key string provided
* @return the PrivateKey object
* @throws AmazonPayClientException When an error response is returned by Amazon Pay due to bad request or other issue
*/
public static PrivateKey buildPrivateKeyFromString(final String privateKeyString) throws AmazonPayClientException {
Security.addProvider(new BouncyCastleProvider());
if (privateKeyString == null || privateKeyString.isEmpty()) {
throw new AmazonPayClientException("Private key string cannot be null or empty");
}
PemObject pemObject = getPEMObjectFromKey(privateKeyString);
if (pemObject == null) {
throw new AmazonPayClientException("Private key string provided is not valid");
}
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(pemObject.getContent());
PrivateKey privateKey = null;
try {
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
privateKey = keyFactory.generatePrivate(spec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new AmazonPayClientException(e.getMessage(), e);
}
return privateKey;
}
/**
* To read the contents of the private key
* @param privateKey the private key string
* @return private key pem object
* @throws AmazonPayClientException When an error response is returned by Amazon Pay due to bad request or other issue
*/
private static PemObject getPEMObjectFromKey(final String privateKey) throws AmazonPayClientException {
PemObject pemObject;
try {
final PemReader pemReader = new PemReader(new StringReader(privateKey));
pemObject = pemReader.readPemObject();
pemReader.close();
} catch (IOException e) {
throw new AmazonPayClientException(e.getMessage(), e);
}
return pemObject;
}
/**
* To get the service URI
* @param payConfiguration the PayConfiguration object
* @param action the action to be performed by the request
* @return the service URI
* @throws AmazonPayClientException When an error response is returned by Amazon Pay due to bad request or other issue
*/
public static URI getServiceURI(PayConfiguration payConfiguration, String action) throws AmazonPayClientException {
URI uri;
try {
String endpoint;
if (payConfiguration.getOverrideServiceURL() != null) {
endpoint = "https://" + payConfiguration.getOverrideServiceURL();
} else {
endpoint = ServiceConstants.endpointMappings.get(payConfiguration.getRegion());
}
uri = new URI(endpoint + getServiceVersionName(payConfiguration.getEnvironment(), action));
} catch (URISyntaxException e) {
throw new AmazonPayClientException(e.getMessage(), e);
}
return uri;
}
/**
* To get the service version name
* @param environment the environment (Sandbox/Live)
* @param action the action to be performed by the request
* @return the service version name
*/
private static String getServiceVersionName(Environment environment, String action) {
String serviceVersionName;
if (environment == Environment.SANDBOX) {
serviceVersionName = "/" + "sandbox" + "/" + action;
} else {
serviceVersionName = "/" + "live" + "/" + action;
}
return serviceVersionName;
}
/**
* Returns the next wait interval, in milliseconds, using an exponential
* backoff algorithm.
* @param retryCount The current retry count
* @return the wait time
*/
public static long getExponentialWaitTime(int retryCount) {
return ((long) Math.pow(2, retryCount) * 1000L);
}
/**
* Returns the header with idempotency key, if not provided by the Merchant.
* Used for Webstore POST requests.
* @param header Header sent by the merchant
* @return header Header with Idempotency key
*/
public static Map<String, String> updateHeader(Map<String, String> header) {
if (header == null || header.isEmpty()) {
final Map<String, String> headerMap = new HashMap<String, String>();
headerMap.put("x-amz-pay-idempotency-key", UUID.randomUUID().toString().replace("-", ""));
return headerMap;
}
boolean isIdempotencyKeyPresent = false;
for (Map.Entry<String, String> entry : header.entrySet()) {
if (entry.getKey().toLowerCase().equals("x-amz-pay-idempotency-key")) {
isIdempotencyKeyPresent = true;
break;
}
}
if (!isIdempotencyKeyPresent) {
header.put("x-amz-pay-idempotency-key", UUID.randomUUID().toString().replace("-", ""));
}
return header;
}
}