diff --git a/src/main/java/edu/pitt/dbmi/ccd/rest/client/RestHttpsClient.java b/src/main/java/edu/pitt/dbmi/ccd/rest/client/RestHttpsClient.java index c22feba..498ea19 100644 --- a/src/main/java/edu/pitt/dbmi/ccd/rest/client/RestHttpsClient.java +++ b/src/main/java/edu/pitt/dbmi/ccd/rest/client/RestHttpsClient.java @@ -25,75 +25,68 @@ public class RestHttpsClient { - private final String email; - private final String password; - private final String scheme; - private final String hostname; - private final int port; - - private final CloseableHttpClient httpClient; - private final HttpClientContext localContext; - - public RestHttpsClient(String email, String password, String scheme, - String hostname, int port) throws Exception { - this.email = email; - this.password = password; - this.scheme = scheme; - this.hostname = hostname; - this.port = port; - this.httpClient = createHttpClient(); - this.localContext = establishCustomContext(httpClient); - } - - private CloseableHttpClient createHttpClient() throws Exception { - // Trust own CA and all self-signed certs - SSLContextBuilder builder = new SSLContextBuilder(); - builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); - - SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( - builder.build(), - new String[] { "TLSv1.2" }, - null, - SSLConnectionSocketFactory.getDefaultHostnameVerifier()); - - CredentialsProvider credsProvider = new BasicCredentialsProvider(); - credsProvider.setCredentials(new AuthScope(hostname, port), - new UsernamePasswordCredentials(email, password)); - - CloseableHttpClient httpClient = HttpClients.custom() - .setSSLSocketFactory(sslsf) - .setDefaultCredentialsProvider(credsProvider).build(); - - return httpClient; - } - - private HttpClientContext establishCustomContext( - CloseableHttpClient httpclient) throws Exception { - HttpHost target = new HttpHost(hostname, port, scheme); - - // Create AuthCache instance - AuthCache authCache = new BasicAuthCache(); - // Generate BASIC scheme object and add it to the local - // auth cache - BasicScheme basicAuth = new BasicScheme(); - authCache.put(target, basicAuth); - - // Add AuthCache to the execution context - HttpClientContext localContext = HttpClientContext.create(); - localContext.setAuthCache(authCache); - - return localContext; - } - - public CloseableHttpClient getHttpClient() { - return httpClient; - } - - public HttpClientContext getLocalContext() { - return localContext; - } - - public static void main(String[] args) throws Exception { - - } + private final String email; + private final String password; + private final String scheme; + private final String hostname; + private final int port; + + private final CloseableHttpClient httpClient; + private final HttpClientContext localContext; + + public RestHttpsClient(String email, String password, String scheme, String hostname, int port) throws Exception { + this.email = email; + this.password = password; + this.scheme = scheme; + this.hostname = hostname; + this.port = port; + this.httpClient = createHttpClient(); + this.localContext = establishCustomContext(httpClient); + } + + private CloseableHttpClient createHttpClient() throws Exception { + // Trust own CA and all self-signed certs + SSLContextBuilder builder = new SSLContextBuilder(); + builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); + + SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), new String[] { "TLSv1.2" }, + null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); + + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope(hostname, port), new UsernamePasswordCredentials(email, password)); + + CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf) + .setDefaultCredentialsProvider(credsProvider).build(); + + return httpClient; + } + + private HttpClientContext establishCustomContext(CloseableHttpClient httpclient) throws Exception { + HttpHost target = new HttpHost(hostname, port, scheme); + + // Create AuthCache instance + AuthCache authCache = new BasicAuthCache(); + // Generate BASIC scheme object and add it to the local + // auth cache + BasicScheme basicAuth = new BasicScheme(); + authCache.put(target, basicAuth); + + // Add AuthCache to the execution context + HttpClientContext localContext = HttpClientContext.create(); + localContext.setAuthCache(authCache); + + return localContext; + } + + public CloseableHttpClient getHttpClient() { + return httpClient; + } + + public HttpClientContext getLocalContext() { + return localContext; + } + + public static void main(String[] args) throws Exception { + + } } diff --git a/src/main/resources/log4j2-test.properties b/src/main/resources/log4j2-test.properties new file mode 100644 index 0000000..cd7045f --- /dev/null +++ b/src/main/resources/log4j2-test.properties @@ -0,0 +1,14 @@ +status = error +dest = err +name = PropertiesConfig + +filter.threshold.type = ThresholdFilter +filter.threshold.level = off + +appender.console.type = Console +appender.console.name = STDOUT +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = %m%n + +rootLogger.level = off +rootLogger.appenderRef.stdout.ref = STDOUT \ No newline at end of file