vSphere

 View Only
  • 1.  Not able to connect to HTTPS endpoint within Webclient Service layer - HELP!

    Posted Aug 04, 2017 11:09 AM

    Hi,

    We are currently hitting a show-stopper issue which is preventing us from being able to deliver and certify our plugin. Would you please be able to help?

    Our code originated from the plugin-seed 0.9.7. It uses a custom data adapter to connect to our appliance's REST API and retrieve data. This works well when we are connecting to an HTTP endpoint (using RestTemplate), and the data is all returned as expected.

    However, when attempting to connect to an HTTPS endpoint, the connection fails as it is not authorised (due to SSL). This is expected behaviour, and we have tried to circumvent this using the code provided here: http://www.baeldung.com/httpclient-ssl

    This code works as expected, and returns our data when built within a simple Java application with required dependencies (including introduced Apache HTTP libraries).

    However, when we try running the code within the plugin we get a failure on installation to the Virgo server, as follows:

    Application context creation failure for bundle 'biz.runecast.rc2plugin.rc2plugin-service' version '1.0.0'. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'RunecastDataClient' defined in URL [bundleentry://323.fwk1376790324/META-INF/spring/bundle-context.xml]: Instantiation of bean failed; nested exception is java.lang.LinkageError: loader constraint violation: when resolving method "org.springframework.web.client.RestTemplate.setRequestFactory(Lorg/springframework/http/client/ClientHttpRequestFactory;)V" the class loader (instance of org/eclipse/virgo/kernel/userregion/internal/equinox/KernelBundleClassLoader) of the current class, biz/runecast/rc2plugin/services/RunecastDataClient, and the class loader (instance of org/eclipse/virgo/kernel/userregion/internal/equinox/KernelBundleClassLoader) for the method's defining class, org/springframework/http/client/support/HttpAccessor, have different Class objects for the type org/springframework/http/client/ClientHttpRequestFactory used in the signature

    We believe all the required dependencies are there, equivalent to the dependencies in our simple application. These are included in the Manifest and included within the JAR on build (Bundle-Classpath). I have included the MANIFEST.MF with the post.

    The project builds the .JAR fine using ANT. When our class is being statically initialised, the error occurs at the point shown below:

    private static SSLContext sslContext; private static CloseableHttpClient httpClient; private static ClientHttpRequestFactory httpRequestFactory; private static RestTemplate restTemplate = new RestTemplate();     static {         try {             sslContext = new SSLContextBuilder()                     .loadTrustMaterial(null, new TrustStrategy() {                         @Override                         public boolean isTrusted(X509Certificate[] certificate, String authType)                                 throws CertificateException {                             return true;                         }                     }).build();         } catch (KeyManagementException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (NoSuchAlgorithmException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (KeyStoreException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         httpClient = HttpClients.custom()                 .setSSLContext(sslContext)                 .setSSLHostnameVerifier(new NoopHostnameVerifier())                 .build();         httpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);         restTemplate.setRequestFactory(httpRequestFactory); //<---- This is causing error    }

     

    This is preventing us from continuing any work on the plugin. We have no idea how to proceed towards a solution. Could you please let us know what we can do, or what we can provide, to diagnose and solve this problem ASAP?

    Many thanks,

    Warren



  • 2.  RE: Not able to connect to HTTPS endpoint within Webclient Service layer - HELP!

    Posted Aug 04, 2017 11:14 AM

    The code sample is as follows (hopefully the editor won't crush it this time!)

    private static SSLContext sslContext;
        private static CloseableHttpClient httpClient;
        private static ClientHttpRequestFactory httpRequestFactory;
        private static RestTemplate restTemplate = new RestTemplate();
        static {
        try {
        sslContext = new SSLContextBuilder()
        .loadTrustMaterial(null, new TrustStrategy() {
        @Override
        public boolean isTrusted(X509Certificate[] certificate, String authType)
        throws CertificateException {
        return true;
        }
        }).build();
        } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        httpClient = HttpClients.custom()
        .setSSLContext(sslContext)
        .setSSLHostnameVerifier(new NoopHostnameVerifier())
        .build();
        httpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        restTemplate.setRequestFactory(httpRequestFactory); //<---- This is causing error
        }


  • 3.  RE: Not able to connect to HTTPS endpoint within Webclient Service layer - HELP!

    Posted Aug 07, 2017 12:32 AM

    Update:

    We have further analyzed this issue, and found that by referencing the httpclient-osgi and httpcore-osgi libraries on the Virgo server (usr directory), it was possible to get this code to work.



  • 4.  RE: Not able to connect to HTTPS endpoint within Webclient Service layer - HELP!

    Broadcom Employee
    Posted Aug 07, 2017 11:01 AM

    Glad you've unblocked yourselves.

    Still if you have time I'd like to understand better the original root cause - referencing http*-osgi seems to untangle certain OSGi dependencies that were not properly wired in the first place.

    The issue / resolution makes me think that two components have conflicting versions of some class inside httpcomponents - and I suspect the conflict comes from the Bundle-ClassPath. From the MANIFEST.MF I see that you have packed a number of JARs inside your bundle - specifically spring-web*.jar and http*.jar. These JARs are unnecessary. Spring and - by association - commons-logging cannot be used in multiple versions easily with Virgo. While you are bundling the JARs you still have Import-Package declarations for packages from these bundles.

    My suggestion is for you to do one more test without the http*-osgi imports, and with the Bundle-ClassPath purged of all components - simply rely on the versions coming from the vSphere Client.

    In general, when hitting such OSGi wiring issues, you can always use the Virgo web console, available under server/admin/org.eclipse.virgo.management.console_3.6.4.RELEASE.jar - just drop it into server/pickup folder then open https://localhost:9443/admin (the credentials are admin / springsource) - there are a number of useful views that can show you  how wiring of packages happens between bundles. To see meaningful information in your case though, you need to set up the restTemplate during spring initialization - static initializer is in any case unnecessary for this case and if it fails it will prevent the bundle from installing - thus it won't be visible in the web console.



  • 5.  RE: Not able to connect to HTTPS endpoint within Webclient Service layer - HELP!

    Posted Aug 08, 2017 10:31 AM

    Hi, 

    Many thanks for this informative response - it is really helpful advice and guidance. We will install the web console, and think it will really help us in this area.

    We will also try to clean up the manifest to remove the Bundle-ClassPath elements and report back on the findings.

    Thanks again.

    Warren