/**
* =============================================================================
*
* ORCID (R) Open Source
* http://orcid.org
*
* Copyright (c) 2012-2014 ORCID, Inc.
* Licensed under an MIT-Style License (MIT)
* http://orcid.org/open-source-license
*
* This copyright and license information (including a link to the full license)
* shall be included in its entirety in all copies or substantial portion of
* the software.
*
* =============================================================================
*/
package org.orcid.util;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Set;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;
/**
* DANGER!!! For dev only!
*/
public class DevJerseyClientConfig extends DefaultClientConfig {
public DevJerseyClientConfig() {
super();
init();
}
public DevJerseyClientConfig(Class<?>... providers) {
super(providers);
init();
}
public DevJerseyClientConfig(Set<Class<?>> providers) {
super(providers);
init();
}
public void init() {
SSLContext ctx = createSslContext();
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession sslSession) {
if (hostname.equals("localhost")) {
return true;
}
return false;
}
}, ctx));
}
private SSLContext createSslContext() {
try {
// DANGER!!! Accepts all certs!
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(null, trustAllCerts, new SecureRandom());
return ssl;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
}
}
}