Skip to content

Commit

Permalink
Update to force TLS 1.2 for connections to Rapid
Browse files Browse the repository at this point in the history
  • Loading branch information
incarnate committed Apr 21, 2017
1 parent 315941b commit 87c2cb8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/eway/payment/rapid/sdk/RapidClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@

import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.net.ssl.SSLContext;

public class RapidClientImpl implements RapidClient {

Expand Down Expand Up @@ -129,6 +132,14 @@ private void validateAPIParam() {
}
isValid = true;
LOGGER.info("Initiate client[" + rapidEndpoint + "] successful!");
} catch (NoSuchAlgorithmException e) {
LOGGER.error("Error using TLS 1.2 to connect to Rapid: no such algorithm", e);
isValid = false;
addErrorCode(Constant.COMMUNICATION_FAILURE_ERROR_CODE);
} catch (KeyManagementException e) {
LOGGER.error("Error using TLS 1.2 to connect to Rapid: key management", e);
isValid = false;
addErrorCode(Constant.COMMUNICATION_FAILURE_ERROR_CODE);
} catch (Exception e) {
LOGGER.error("Error loading or connecting to endpoint", e);
isValid = false;
Expand Down Expand Up @@ -171,8 +182,16 @@ private void parserRapidEnpointToGetWebUrl() throws Exception {
*/
private void verifyEndpointUrl(String endpointUrl) throws Exception {
URL url = new URL(endpointUrl);

SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(null,null,null);
SSLContext oldContext = SSLContext.getDefault();
SSLContext.setDefault(context);

URLConnection conn = url.openConnection();
conn.connect();

SSLContext.setDefault(oldContext);
}

public CreateTransactionResponse create(PaymentMethod paymentMethod, Transaction transaction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLContext;

import javax.ws.rs.core.MediaType;

Expand Down Expand Up @@ -67,17 +72,26 @@ public final V doWork(T input) throws RapidSdkException {
*/
protected final <U, K> U doPost(K request, Class<U> responseClass) throws RapidSdkException {
try {
WebResource resouce = getWebResource();
WebResource resource = getWebResource();
for (String path : getRequestPath()) {
if (!StringUtils.isBlank(path)) {
resouce = resouce.path(path);
resource = resource.path(path);
}
}

ObjectMapper mapper = new ObjectMapper();
requestJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request);

return resouce.type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).post(responseClass, request);
SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(null,null,null);
SSLContext oldContext = SSLContext.getDefault();
SSLContext.setDefault(context);

U response = resource.type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).post(responseClass, request);

SSLContext.setDefault(oldContext);

return response;
} catch (ClientHandlerException e) {
throw new CommunicationFailureException("Internal system error communicating with Rapid API", e);
} catch (UniformInterfaceException e) {
Expand All @@ -92,6 +106,10 @@ protected final <U, K> U doPost(K request, Class<U> responseClass) throws RapidS
}
} catch (IOException e) {
throw new SystemErrorException(e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
throw new CommunicationFailureException("Error using TLS 1.2 to connect to Rapid: no such algorithm", e);
} catch (KeyManagementException e) {
throw new CommunicationFailureException("Error using TLS 1.2 to connect to Rapid: key management", e);
}
}

Expand All @@ -107,17 +125,27 @@ protected final <U, K> U doPost(K request, Class<U> responseClass) throws RapidS
*/
protected final <U, K> U doPut(K request, Class<U> responseClass) throws RapidSdkException {
try {
WebResource resouce = getWebResource();
WebResource resource = getWebResource();
for (String path : getRequestPath()) {
if (!StringUtils.isBlank(path)) {
resouce = resouce.path(path);
resource = resource.path(path);
}
}

ObjectMapper mapper = new ObjectMapper();
requestJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request);

return resouce.type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).put(responseClass, request);
SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(null,null,null);
SSLContext oldContext = SSLContext.getDefault();
SSLContext.setDefault(context);

U response = resource.type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).put(responseClass, request);

SSLContext.setDefault(oldContext);

return response;

} catch (ClientHandlerException e) {
throw new CommunicationFailureException("Internal system error communicating with Rapid API", e);
} catch (UniformInterfaceException e) {
Expand All @@ -132,6 +160,10 @@ protected final <U, K> U doPut(K request, Class<U> responseClass) throws RapidSd
}
} catch (IOException e) {
throw new SystemErrorException(e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
throw new CommunicationFailureException("Error using TLS 1.2 to connect to Rapid: no such algorithm", e);
} catch (KeyManagementException e) {
throw new CommunicationFailureException("Error using TLS 1.2 to connect to Rapid: key management", e);
}
}

Expand All @@ -152,7 +184,17 @@ protected final <U> U doGet(String request, Class<U> responseClass) throws Rapid
}
}
try {
return resouce.path(request).get(responseClass);

SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(null,null,null);
SSLContext oldContext = SSLContext.getDefault();
SSLContext.setDefault(context);

U response = resouce.path(request).get(responseClass);

SSLContext.setDefault(oldContext);

return response;
} catch (ClientHandlerException e) {
throw new CommunicationFailureException("Internal system error communicating with Rapid API", e);
} catch (UniformInterfaceException e) {
Expand All @@ -166,6 +208,10 @@ protected final <U> U doGet(String request, Class<U> responseClass) throws Rapid
throw new SystemErrorException(e.getMessage(), e);
}

} catch (NoSuchAlgorithmException e) {
throw new CommunicationFailureException("Error using TLS 1.2 to connect to Rapid: no such algorithm", e);
} catch (KeyManagementException e) {
throw new CommunicationFailureException("Error using TLS 1.2 to connect to Rapid: key management", e);
}
}

Expand Down

0 comments on commit 87c2cb8

Please sign in to comment.