Created
October 31, 2012 04:56
-
-
Save jmchilton/3984862 to your computer and use it in GitHub Desktop.
Example of initializing GalaxyInstance object when OS doesn't trust remote certificate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.net.ssl.HostnameVerifier; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLSession; | |
import com.sun.jersey.api.client.config.ClientConfig; | |
import com.sun.jersey.api.client.config.DefaultClientConfig; | |
import com.sun.jersey.api.json.JSONConfiguration; | |
import com.sun.jersey.client.urlconnection.HTTPSProperties; | |
... | |
static GalaxyInstance get() { | |
final String galaxyInstanceUrl = getTestInstanceUrl(); | |
final String galaxyApiKey = getTestApiKey(); | |
DefaultWebResourceFactoryImpl factory = new DefaultWebResourceFactoryImpl(galaxyInstanceUrl, galaxyApiKey) { | |
protected com.sun.jersey.api.client.Client getJerseyClient() { | |
final ClientConfig clientConfig = new DefaultClientConfig() ; | |
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); | |
javax.net.ssl.TrustManager x509 = new javax.net.ssl.X509TrustManager() { | |
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException { | |
return; | |
} | |
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException { | |
return; | |
} | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
}; | |
SSLContext ctx = null; | |
try { | |
ctx = SSLContext.getInstance("SSL"); | |
ctx.init(null, new javax.net.ssl.TrustManager[]{x509}, null); | |
} catch (java.security.GeneralSecurityException ex) { | |
} | |
clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties( | |
new HostnameVerifier() { | |
public boolean verify(String arg0, SSLSession arg1) { | |
return true; | |
} | |
}, ctx)); | |
return com.sun.jersey.api.client.Client.create(clientConfig); | |
} | |
}; | |
return GalaxyInstanceFactory.get(factory); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment