1212
1313package Invokers;
1414
15- import java.io.File;
16- import java.io.IOException;
1715import java.io.InputStream;
1816import java.io.UnsupportedEncodingException;
1917import java.lang.reflect.Type;
18+ import java.net.HttpRetryException;
19+ import java.net.InetSocketAddress;
20+ import java.net.Proxy;
2021import java.net.URLConnection;
2122import java.net.URLEncoder;
2223import java.security.GeneralSecurityException;
2324import java.security.KeyStore;
24- import java.security.NoSuchAlgorithmException;
2525import java.security.SecureRandom;
2626import java.security.cert.Certificate;
2727import java.security.cert.CertificateException;
28- import java.security.cert.CertificateFactory;
29- import java.security.cert.X509Certificate;
30- import java.text.DateFormat;
31- import java.text.ParseException;
32- import java.text.SimpleDateFormat;
33- import java.util.ArrayList;
34- import java.util.Collection;
35- import java.util.Collections;
36- import java.util.Date;
37- import java.util.HashMap;
38- import java.util.List;
39- import java.util.Map;
40- import java.util.Map.Entry;
41- import java.util.TimeZone;
4228import java.util.concurrent.TimeUnit;
4329import java.util.regex.Matcher;
4430import java.util.regex.Pattern;
45-
4631import javax.net.ssl.HostnameVerifier;
4732import javax.net.ssl.KeyManager;
4833import javax.net.ssl.SSLContext;
4934import javax.net.ssl.SSLSession;
5035import javax.net.ssl.TrustManager;
5136import javax.net.ssl.TrustManagerFactory;
5237import javax.net.ssl.X509TrustManager;
53-
5438import org.apache.logging.log4j.Logger;
55-
5639import com.cybersource.authsdk.core.Authorization;
5740import com.cybersource.authsdk.core.ConfigException;
5841import com.cybersource.authsdk.core.MerchantConfig;
5942import com.cybersource.authsdk.log.Log4j;
6043import com.cybersource.authsdk.payloaddigest.PayloadDigest;
6144import com.cybersource.authsdk.util.GlobalLabelParameters;
6245import com.cybersource.authsdk.util.PropertiesUtil;
63- import com.squareup.okhttp.Call;
64- import com.squareup.okhttp.Callback;
65- import com.squareup.okhttp.FormEncodingBuilder;
66- import com.squareup.okhttp.Headers;
67- import com.squareup.okhttp.MediaType;
68- import com.squareup.okhttp.MultipartBuilder;
69- import com.squareup.okhttp.OkHttpClient;
70- import com.squareup.okhttp.Request;
71- import com.squareup.okhttp.RequestBody;
72- import com.squareup.okhttp.Response;
73- import com.squareup.okhttp.internal.http.HttpMethod;
74- import com.squareup.okhttp.logging.HttpLoggingInterceptor;
75- import com.squareup.okhttp.logging.HttpLoggingInterceptor.Level;
76-
7746import Invokers.auth.ApiKeyAuth;
7847import Invokers.auth.Authentication;
7948import Invokers.auth.HttpBasicAuth;
8049import Invokers.auth.OAuth;
50+ import okhttp3.Authenticator;
51+ import okhttp3.Call;
52+ import okhttp3.Callback;
53+ import okhttp3.Credentials;
54+ import okhttp3.FormBody;
55+ import okhttp3.FormBody.Builder;
56+ import okhttp3.Headers;
57+ import okhttp3.MediaType;
58+ import okhttp3.MultipartBody;
59+ import okhttp3.OkHttpClient;
60+ import okhttp3.Request;
61+ import okhttp3.RequestBody;
62+ import okhttp3.Response;
63+ import okhttp3.Route;
64+ import okhttp3.internal.http.HttpMethod;
65+ import okhttp3.logging.HttpLoggingInterceptor;
66+ import okhttp3.logging.HttpLoggingInterceptor.Level;
8167import okio.BufferedSink;
8268import okio.Okio;
8369
@@ -149,6 +135,12 @@ public class ApiClient {
149135 */
150136 public ApiClient() {
151137 httpClient = new OkHttpClient();
138+
139+ httpClient = new OkHttpClient.Builder()
140+ .connectTimeout(1, TimeUnit.SECONDS)
141+ .writeTimeout(60, TimeUnit.SECONDS)
142+ .readTimeout(60, TimeUnit.SECONDS)
143+ .build();
152144
153145 verifyingSsl = true ;
154146
@@ -180,6 +172,54 @@ public class ApiClient {
180172
181173 public ApiClient(MerchantConfig merchantConfig) {
182174 this();
175+ final boolean useProxy = merchantConfig.isUseProxyEnabled();
176+ final String username = merchantConfig.getProxyUser();
177+ final String password = merchantConfig.getProxyPassword();
178+ int proxyPort = merchantConfig.getProxyPort();
179+ String proxyHost = merchantConfig.getProxyAddress();
180+
181+ Authenticator proxyAuthenticator;
182+
183+ if (useProxy && (proxyHost != null && ! proxyHost.isEmpty())) {
184+ if ((username != null && ! username.isEmpty()) &&
185+ (password != null && ! password.isEmpty())) {
186+ proxyAuthenticator = new Authenticator() {
187+ private int proxyCounter = 0;
188+
189+ @Override
190+ public Request authenticate(Route route, Response response) throws IOException {
191+ if (proxyCounter++ > 0) {
192+ if (response.code() == 407) {
193+ throw new HttpRetryException(" Proxy Authentication Missing or Incorrect." , 407);
194+ } else {
195+ throw new IOException(response.message());
196+ }
197+ }
198+
199+ String credential = Credentials.basic(username, password);
200+ return response.request().newBuilder().header("Proxy-Authorization", credential).build();
201+ }
202+ };
203+ } else {
204+ proxyAuthenticator = new Authenticator() {
205+ @Override
206+ public Request authenticate(Route route, Response response) throws IOException {
207+ return response.request().newBuilder().build();
208+ }
209+ };
210+ }
211+
212+ httpClient = new OkHttpClient.Builder()
213+ .connectTimeout(1, TimeUnit.SECONDS)
214+ .writeTimeout(60, TimeUnit.SECONDS)
215+ .readTimeout(60, TimeUnit.SECONDS)
216+ .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
217+ .proxyAuthenticator(proxyAuthenticator)
218+ .build();
219+
220+ this.setHttpClient(httpClient);
221+ }
222+
183223 this.merchantConfig = merchantConfig;
184224
185225 }
@@ -664,7 +704,7 @@ public class ApiClient {
664704 * @return Timeout in milliseconds
665705 */
666706 public int getConnectTimeout() {
667- return httpClient.getConnectTimeout ();
707+ return httpClient.connectTimeoutMillis ();
668708 }
669709
670710 /**
@@ -676,7 +716,7 @@ public class ApiClient {
676716 * @return Api client
677717 */
678718 public ApiClient setConnectTimeout(int connectionTimeout) {
679- httpClient.setConnectTimeout( connectionTimeout, TimeUnit.MILLISECONDS);
719+ this. httpClient = this.httpClient.newBuilder().connectTimeout( connectionTimeout, TimeUnit.MILLISECONDS).build( );
680720 return this;
681721 }
682722
@@ -1096,12 +1136,12 @@ public class ApiClient {
10961136 public <T > void executeAsync(Call call, final Type returnType, final ApiCallback<T > callback) {
10971137 call.enqueue(new Callback() {
10981138 @Override
1099- public void onFailure(Request request , IOException e) {
1139+ public void onFailure(Call call0 , IOException e) {
11001140 callback.onFailure(new ApiException(e), 0, null);
11011141 }
11021142
11031143 @Override
1104- public void onResponse(Response response ) throws IOException {
1144+ public void onResponse(Call call0 ) throws IOException {
11051145 T result;
11061146 try {
11071147 result = (T) handleResponse(response, returnType);
@@ -1133,11 +1173,7 @@ public class ApiClient {
11331173 if (response.isSuccessful()) {
11341174 if (response.code() == 204) {
11351175 if (response.body() != null) {
1136- try {
1137- response.body().close();
1138- } catch (IOException e) {
1139- throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap());
1140- }
1176+ response.body().close();
11411177 }
11421178 return null;
11431179 } else {
@@ -1387,6 +1423,8 @@ public class ApiClient {
13871423 reqBuilder.header(header.getKey(), parameterToString(header.getValue()));
13881424 }
13891425 }
1426+
1427+ reqBuilder.header("Connection", "close");
13901428 }
13911429
13921430 /**
@@ -1416,7 +1454,7 @@ public class ApiClient {
14161454 * @return RequestBody
14171455 */
14181456 public RequestBody buildRequestBodyFormEncoding(Map<String , Object > formParams) {
1419- FormEncodingBuilder formBuilder = new FormEncodingBuilder ();
1457+ FormBody.Builder formBuilder = new FormBody.Builder ();
14201458 for (Entry< String, Object> param : formParams.entrySet()) {
14211459 formBuilder.add(param.getKey(), parameterToString(param.getValue()));
14221460 }
@@ -1432,7 +1470,7 @@ public class ApiClient {
14321470 * @return RequestBody
14331471 */
14341472 public RequestBody buildRequestBodyMultipart(Map<String , Object > formParams) {
1435- MultipartBuilder mpBuilder = new MultipartBuilder ().type(MultipartBuilder .FORM);
1473+ MultipartBody.Builder mpBuilder = new MultipartBody.Builder ().setType(MultipartBody .FORM);
14361474 for (Entry< String, Object> param : formParams.entrySet()) {
14371475 if (param.getValue() instanceof File) {
14381476 File file = (File) param.getValue();
@@ -1549,11 +1587,11 @@ public class ApiClient {
15491587 if (keyManagers != null || trustManagers != null) {
15501588 SSLContext sslContext = SSLContext.getInstance("TLS");
15511589 sslContext.init(keyManagers, trustManagers, new SecureRandom());
1552- httpClient.setSslSocketFactory( sslContext.getSocketFactory());
1590+ httpClient = httpClient.newBuilder().sslSocketFactory( sslContext.getSocketFactory()).build( );
15531591 } else {
1554- httpClient.setSslSocketFactory( null);
1592+ httpClient = httpClient.newBuilder().sslSocketFactory( null).build( );
15551593 }
1556- httpClient.setHostnameVerifier( hostnameVerifier);
1594+ httpClient = httpClient.newBuilder(). hostnameVerifier(hostnameVerifier).build( );
15571595 } catch (GeneralSecurityException e) {
15581596 throw new RuntimeException(e);
15591597 }
0 commit comments