users@jersey.java.net

[Jersey] can't use client with SSL

From: Aris Alexis <aris.alexis.gia_at_gmail.com>
Date: Fri, 9 Jan 2015 17:54:26 +0100

Hi,

I need to use for testing a client with SSL and I do not care about it's
validity.

I have googled a lot but none of the answers works for me:

Client secureClient =
ClientBuilder.newBuilder().sslContext(getSSLContext()).register(MoxyJsonFeature.class).build();


public SSLContext getSSLContext() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            java.security.cert.X509Certificate[] chck = null;
            ;
            return chck;
        }

        public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                throws CertificateException {
            // TODO Auto-generated method stub

        }

        public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                throws CertificateException {

        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws java.security.cert.CertificateException {
            // TODO Auto-generated method stub

        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws java.security.cert.CertificateException {
            // TODO Auto-generated method stub

        }
    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        return sc;
        } catch (Exception e) {
            e.printStackTrace();
        }

    return null;
}



I get this error (that is under a bug report because it is vague) :
javax.ws.rs.ProcessingException: Already connected
    at
org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)
    at
org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:667)
    at
org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:664)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
    at
org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:664)
    at
org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:424)
    at
org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:333)
    at
integration.AuthorizationIntegrationTest.loginTest(AuthorizationIntegrationTest.java:116)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)

can somebody provide a working solution please?

shouldn't be so difficult to do this yet there is only 1 SO thread with
non-working answers and a blog post that also doesn't work for me.

thanks in advance

Best Regards,
Aris Giachnis