Skip to content

Commit

Permalink
Merge pull request #5 from bd2kccd/hpc-parameters
Browse files Browse the repository at this point in the history
Hpc parameters
  • Loading branch information
chirayukong authored Mar 30, 2017
2 parents fa5f61e + 10cf155 commit 2bc2713
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 71 deletions.
135 changes: 64 additions & 71 deletions src/main/java/edu/pitt/dbmi/ccd/rest/client/RestHttpsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

}
}
14 changes: 14 additions & 0 deletions src/main/resources/log4j2-test.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2bc2713

Please sign in to comment.