diff --git a/generator/cybersource-java-template/libraries/okhttp-gson/ApiClient.mustache b/generator/cybersource-java-template/libraries/okhttp-gson/ApiClient.mustache index 1ae421994..6ecb9752d 100644 --- a/generator/cybersource-java-template/libraries/okhttp-gson/ApiClient.mustache +++ b/generator/cybersource-java-template/libraries/okhttp-gson/ApiClient.mustache @@ -12,37 +12,22 @@ package Invokers; -import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; +import java.net.HttpRetryException; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.net.URLConnection; import java.net.URLEncoder; import java.security.GeneralSecurityException; import java.security.KeyStore; -import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.Certificate; import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.TimeZone; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; - import javax.net.ssl.HostnameVerifier; import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; @@ -50,9 +35,7 @@ import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; - import org.apache.logging.log4j.Logger; - import com.cybersource.authsdk.core.Authorization; import com.cybersource.authsdk.core.ConfigException; import com.cybersource.authsdk.core.MerchantConfig; @@ -60,24 +43,27 @@ import com.cybersource.authsdk.log.Log4j; import com.cybersource.authsdk.payloaddigest.PayloadDigest; import com.cybersource.authsdk.util.GlobalLabelParameters; import com.cybersource.authsdk.util.PropertiesUtil; -import com.squareup.okhttp.Call; -import com.squareup.okhttp.Callback; -import com.squareup.okhttp.FormEncodingBuilder; -import com.squareup.okhttp.Headers; -import com.squareup.okhttp.MediaType; -import com.squareup.okhttp.MultipartBuilder; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.Request; -import com.squareup.okhttp.RequestBody; -import com.squareup.okhttp.Response; -import com.squareup.okhttp.internal.http.HttpMethod; -import com.squareup.okhttp.logging.HttpLoggingInterceptor; -import com.squareup.okhttp.logging.HttpLoggingInterceptor.Level; - import Invokers.auth.ApiKeyAuth; import Invokers.auth.Authentication; import Invokers.auth.HttpBasicAuth; import Invokers.auth.OAuth; +import okhttp3.Authenticator; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.Credentials; +import okhttp3.FormBody; +import okhttp3.FormBody.Builder; +import okhttp3.Headers; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.Route; +import okhttp3.internal.http.HttpMethod; +import okhttp3.logging.HttpLoggingInterceptor; +import okhttp3.logging.HttpLoggingInterceptor.Level; import okio.BufferedSink; import okio.Okio; @@ -149,6 +135,12 @@ public class ApiClient { */ public ApiClient() { httpClient = new OkHttpClient(); + + httpClient = new OkHttpClient.Builder() + .connectTimeout(1, TimeUnit.SECONDS) + .writeTimeout(60, TimeUnit.SECONDS) + .readTimeout(60, TimeUnit.SECONDS) + .build(); verifyingSsl = true; @@ -180,6 +172,54 @@ public class ApiClient { public ApiClient(MerchantConfig merchantConfig) { this(); + final boolean useProxy = merchantConfig.isUseProxyEnabled(); + final String username = merchantConfig.getProxyUser(); + final String password = merchantConfig.getProxyPassword(); + int proxyPort = merchantConfig.getProxyPort(); + String proxyHost = merchantConfig.getProxyAddress(); + + Authenticator proxyAuthenticator; + + if (useProxy && (proxyHost != null && !proxyHost.isEmpty())) { + if ((username != null && !username.isEmpty()) && + (password != null && !password.isEmpty())) { + proxyAuthenticator = new Authenticator() { + private int proxyCounter = 0; + + @Override + public Request authenticate(Route route, Response response) throws IOException { + if (proxyCounter++ > 0) { + if (response.code() == 407) { + throw new HttpRetryException("Proxy Authentication Missing or Incorrect.", 407); + } else { + throw new IOException(response.message()); + } + } + + String credential = Credentials.basic(username, password); + return response.request().newBuilder().header("Proxy-Authorization", credential).build(); + } + }; + } else { + proxyAuthenticator = new Authenticator() { + @Override + public Request authenticate(Route route, Response response) throws IOException { + return response.request().newBuilder().build(); + } + }; + } + + httpClient = new OkHttpClient.Builder() + .connectTimeout(1, TimeUnit.SECONDS) + .writeTimeout(60, TimeUnit.SECONDS) + .readTimeout(60, TimeUnit.SECONDS) + .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort))) + .proxyAuthenticator(proxyAuthenticator) + .build(); + + this.setHttpClient(httpClient); + } + this.merchantConfig = merchantConfig; } @@ -664,7 +704,7 @@ public class ApiClient { * @return Timeout in milliseconds */ public int getConnectTimeout() { - return httpClient.getConnectTimeout(); + return httpClient.connectTimeoutMillis(); } /** @@ -676,7 +716,7 @@ public class ApiClient { * @return Api client */ public ApiClient setConnectTimeout(int connectionTimeout) { - httpClient.setConnectTimeout(connectionTimeout, TimeUnit.MILLISECONDS); + this.httpClient = this.httpClient.newBuilder().connectTimeout(connectionTimeout, TimeUnit.MILLISECONDS).build(); return this; } @@ -1096,12 +1136,12 @@ public class ApiClient { public void executeAsync(Call call, final Type returnType, final ApiCallback callback) { call.enqueue(new Callback() { @Override - public void onFailure(Request request, IOException e) { + public void onFailure(Call call0, IOException e) { callback.onFailure(new ApiException(e), 0, null); } @Override - public void onResponse(Response response) throws IOException { + public void onResponse(Call call0) throws IOException { T result; try { result = (T) handleResponse(response, returnType); @@ -1133,11 +1173,7 @@ public class ApiClient { if (response.isSuccessful()) { if (response.code() == 204) { if (response.body() != null) { - try { - response.body().close(); - } catch (IOException e) { - throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap()); - } + response.body().close(); } return null; } else { @@ -1387,6 +1423,8 @@ public class ApiClient { reqBuilder.header(header.getKey(), parameterToString(header.getValue())); } } + + reqBuilder.header("Connection", "close"); } /** @@ -1416,7 +1454,7 @@ public class ApiClient { * @return RequestBody */ public RequestBody buildRequestBodyFormEncoding(Map formParams) { - FormEncodingBuilder formBuilder = new FormEncodingBuilder(); + FormBody.Builder formBuilder = new FormBody.Builder(); for (Entry param : formParams.entrySet()) { formBuilder.add(param.getKey(), parameterToString(param.getValue())); } @@ -1432,7 +1470,7 @@ public class ApiClient { * @return RequestBody */ public RequestBody buildRequestBodyMultipart(Map formParams) { - MultipartBuilder mpBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); + MultipartBody.Builder mpBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); for (Entry param : formParams.entrySet()) { if (param.getValue() instanceof File) { File file = (File) param.getValue(); @@ -1549,11 +1587,11 @@ public class ApiClient { if (keyManagers != null || trustManagers != null) { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(keyManagers, trustManagers, new SecureRandom()); - httpClient.setSslSocketFactory(sslContext.getSocketFactory()); + httpClient = httpClient.newBuilder().sslSocketFactory(sslContext.getSocketFactory()).build(); } else { - httpClient.setSslSocketFactory(null); + httpClient = httpClient.newBuilder().sslSocketFactory(null).build(); } - httpClient.setHostnameVerifier(hostnameVerifier); + httpClient = httpClient.newBuilder().hostnameVerifier(hostnameVerifier).build(); } catch (GeneralSecurityException e) { throw new RuntimeException(e); } diff --git a/generator/cybersource-java-template/libraries/okhttp-gson/GzipRequestInterceptor.mustache b/generator/cybersource-java-template/libraries/okhttp-gson/GzipRequestInterceptor.mustache index 23224cf5d..27c536c21 100644 --- a/generator/cybersource-java-template/libraries/okhttp-gson/GzipRequestInterceptor.mustache +++ b/generator/cybersource-java-template/libraries/okhttp-gson/GzipRequestInterceptor.mustache @@ -2,7 +2,7 @@ package {{invokerPackage}}; -import com.squareup.okhttp.*; +import okhttp3.*; import okio.Buffer; import okio.BufferedSink; import okio.GzipSink; diff --git a/generator/cybersource-java-template/libraries/okhttp-gson/ProgressRequestBody.mustache b/generator/cybersource-java-template/libraries/okhttp-gson/ProgressRequestBody.mustache index 0f15c1f41..d9ceeab12 100644 --- a/generator/cybersource-java-template/libraries/okhttp-gson/ProgressRequestBody.mustache +++ b/generator/cybersource-java-template/libraries/okhttp-gson/ProgressRequestBody.mustache @@ -2,8 +2,8 @@ package {{invokerPackage}}; -import com.squareup.okhttp.MediaType; -import com.squareup.okhttp.RequestBody; +import okhttp3.MediaType; +import okhttp3.RequestBody; import java.io.IOException; diff --git a/generator/cybersource-java-template/libraries/okhttp-gson/ProgressResponseBody.mustache b/generator/cybersource-java-template/libraries/okhttp-gson/ProgressResponseBody.mustache index 3f5313369..34713eca1 100644 --- a/generator/cybersource-java-template/libraries/okhttp-gson/ProgressResponseBody.mustache +++ b/generator/cybersource-java-template/libraries/okhttp-gson/ProgressResponseBody.mustache @@ -2,8 +2,8 @@ package {{invokerPackage}}; -import com.squareup.okhttp.MediaType; -import com.squareup.okhttp.ResponseBody; +import okhttp3.MediaType; +import okhttp3.ResponseBody; import java.io.IOException; @@ -34,12 +34,12 @@ public class ProgressResponseBody extends ResponseBody { } @Override - public long contentLength() throws IOException { + public long contentLength() { return responseBody.contentLength(); } @Override - public BufferedSource source() throws IOException { + public BufferedSource source() { if (bufferedSource == null) { bufferedSource = Okio.buffer(source(responseBody.source())); } diff --git a/generator/cybersource-java-template/libraries/okhttp-gson/api.mustache b/generator/cybersource-java-template/libraries/okhttp-gson/api.mustache index 720f22425..e951a710a 100644 --- a/generator/cybersource-java-template/libraries/okhttp-gson/api.mustache +++ b/generator/cybersource-java-template/libraries/okhttp-gson/api.mustache @@ -72,7 +72,7 @@ public class {{classname}} { * @return Call to execute * @throws ApiException If fail to serialize the request body object */ - public com.squareup.okhttp.Call {{operationId}}Call({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { + public okhttp3.Call {{operationId}}Call({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { Object {{localVariablePrefix}}localVarPostBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; // create path and map variables @@ -104,10 +104,10 @@ public class {{classname}} { {{localVariablePrefix}}localVarHeaderParams.put("Content-Type", {{localVariablePrefix}}localVarContentType); if(progressListener != null) { - apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() { + apiClient.getHttpClient().networkInterceptors().add(new okhttp3.Interceptor() { @Override - public com.squareup.okhttp.Response intercept(com.squareup.okhttp.Interceptor.Chain chain) throws IOException { - com.squareup.okhttp.Response originalResponse = chain.proceed(chain.request()); + public okhttp3.Response intercept(okhttp3.Interceptor.Chain chain) throws IOException { + okhttp3.Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() .body(new ProgressResponseBody(originalResponse.body(), progressListener)) .build(); @@ -120,7 +120,7 @@ public class {{classname}} { } @SuppressWarnings("rawtypes") - private com.squareup.okhttp.Call {{operationId}}ValidateBeforeCall({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { + private okhttp3.Call {{operationId}}ValidateBeforeCall({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { {{^performBeanValidation}} {{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set @@ -129,7 +129,7 @@ public class {{classname}} { } {{/required}}{{/allParams}} - com.squareup.okhttp.Call {{localVariablePrefix}}call = {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener); + okhttp3.Call {{localVariablePrefix}}call = {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener); return {{localVariablePrefix}}call; {{/performBeanValidation}} @@ -185,7 +185,7 @@ public class {{classname}} { * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body */ public ApiResponse<{{#vendorExtensions.x-streaming}}InputStream{{/vendorExtensions.x-streaming}}{{^vendorExtensions.x-streaming}}{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}{{/vendorExtensions.x-streaming}}> {{operationId}}WithHttpInfo({{#allParams}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { - com.squareup.okhttp.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}null, null); + okhttp3.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}null, null); {{#returnType}}Type {{localVariablePrefix}}localVarReturnType = new TypeToken<{{{returnType}}}>(){}.getType(); return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call, {{localVariablePrefix}}localVarReturnType);{{/returnType}}{{^returnType}}return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call);{{/returnType}} } @@ -198,7 +198,7 @@ public class {{classname}} { * @return The request call * @throws ApiException If fail to process the API call, e.g. serializing the request body object */ - public com.squareup.okhttp.Call {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{localVariablePrefix}}callback) throws ApiException { + public okhttp3.Call {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{localVariablePrefix}}callback) throws ApiException { ProgressResponseBody.ProgressListener progressListener = null; ProgressRequestBody.ProgressRequestListener progressRequestListener = null; @@ -219,7 +219,7 @@ public class {{classname}} { }; } - com.squareup.okhttp.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener); + okhttp3.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener); {{#returnType}}Type {{localVariablePrefix}}localVarReturnType = new TypeToken<{{{returnType}}}>(){}.getType(); {{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, {{localVariablePrefix}}localVarReturnType, {{localVariablePrefix}}callback);{{/returnType}}{{^returnType}}{{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, {{localVariablePrefix}}callback);{{/returnType}} return {{localVariablePrefix}}call; diff --git a/generator/cybersource-java-template/libraries/okhttp-gson/auth/HttpBasicAuth.mustache b/generator/cybersource-java-template/libraries/okhttp-gson/auth/HttpBasicAuth.mustache index 320903b03..2c642ff62 100644 --- a/generator/cybersource-java-template/libraries/okhttp-gson/auth/HttpBasicAuth.mustache +++ b/generator/cybersource-java-template/libraries/okhttp-gson/auth/HttpBasicAuth.mustache @@ -4,13 +4,11 @@ package {{invokerPackage}}.auth; import {{invokerPackage}}.Pair; -import com.squareup.okhttp.Credentials; +import okhttp3.Credentials; import java.util.Map; import java.util.List; -import java.io.UnsupportedEncodingException; - public class HttpBasicAuth implements Authentication { private String username; private String password; diff --git a/src/main/java/Api/SecureFileShareApi.java b/src/main/java/Api/SecureFileShareApi.java index 9d2fb21f1..2e560eeb5 100644 --- a/src/main/java/Api/SecureFileShareApi.java +++ b/src/main/java/Api/SecureFileShareApi.java @@ -326,4 +326,4 @@ public void onRequestProgress(long bytesWritten, long contentLength, boolean don return call; } } - +