Wednesday 26 November 2014

Upgrading to Httpcomponents 4.3.5?

Did you encounter the following error after upgrading to httpcomponents 4.3.5?
java.lang.ClassNotFoundException: javax.naming.InvalidNameException not found by org.apache.httpcomponents.httpclient [923]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1500)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1923)
at java.lang.ClassLoader.loadClass(Unknown Source)

This happens because Httpclient OSGI bundle 4.3.5 doesnt import the package "javax.naming" by default.
The solution would be to edit the manifest.mf file of the bundle to include the javax.naming package.

The Import-Package section in MANIFEST.MF should look like with the only change is to include javax.naming:
Import-Package: javax.crypto,javax.crypto.spec,javax.naming,javax.net,javax.net.ssl,j
 avax.security.auth.x500,org.ietf.jgss,org.osgi.framework;version="[1.5,
 2)",org.osgi.service.cm;version="[1.3,2)",org.apache.commons.logging;ve
 rsion="[1.1.0,1.2.0)",net.sf.ehcache;resolution:=optional,net.spy.memca
 ched;resolution:=optional,org.apache.http.util;version="[4.3.0,4.4.0)",
 org.apache.http.io;version="[4.3.0,4.4.0)",org.apache.http.pool;version
 ="[4.3.0,4.4.0)",org.apache.http.impl.pool;version="[4.3.0,4.4.0)",org.
 apache.http.impl;version="[4.3.0,4.4.0)",org.apache.http.message;versio
 n="[4.3.0,4.4.0)",org.apache.http.impl.entity;version="[4.3.0,4.4.0)",o
 rg.apache.http.params;version="[4.3.0,4.4.0)",org.apache.http.impl.io;v
 ersion="[4.3.0,4.4.0)",org.apache.http;version="[4.3.0,4.4.0)",org.apac
 he.http.concurrent;version="[4.3.0,4.4.0)",org.apache.http.entity;versi
 on="[4.3.0,4.4.0)",org.apache.http.config;version="[4.3.0,4.4.0)",org.a
 pache.http.protocol;version="[4.3.0,4.4.0)"

Having done this,  it is important to note that httpclients imports more than just javax.naming.
The class org.apache.http.conn.ssl.AbstractVerifier.java imports the following javax.naming classes:

javax.naming.InvalidNameException
javax.naming.NamingException
javax.naming.directory.Attribute
javax.naming.directory.Attributes
javax.naming.ldap.LdapName
javax.naming.ldap.Rdn

If not resolved, such kind of errors may be encountered:
java.lang.NoClassDefFoundError: javax/naming/ldap/LdapName
    at org.apache.http.conn.ssl.AbstractVerifier.extractCNs(AbstractVerifier.java:277)
    at org.apache.http.conn.ssl.AbstractVerifier.getCNs(AbstractVerifier.java:265)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:157)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:140)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:286)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:276)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:254)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
To resolve all these classes, the OSGi manifest file needs the following package imports as well:
javax.naming.directory
javax.naming.ldap

The Import-Package section in MANIFEST.MF should look like this:

Import-Package: javax.crypto,javax.crypto.spec,javax.net,javax.naming,javax.nami
 ng.directory,javax.naming.ldap
,javax.net.ssl,javax.security.auth.x500,org.ietf.jgss,org.osgi.framework;version="[1.5,
 2)",org.osgi.service.cm;version="[1.3,2)",org.apache.commons.logging;ve
 rsion="[1.1.0,1.2.0)",net.sf.ehcache;resolution:=optional,net.spy.memca
 ched;resolution:=optional,org.apache.http.util;version="[4.3.0,4.4.0)",
 org.apache.http.io;version="[4.3.0,4.4.0)",org.apache.http.pool;version
 ="[4.3.0,4.4.0)",org.apache.http.impl.pool;version="[4.3.0,4.4.0)",org.
 apache.http.impl;version="[4.3.0,4.4.0)",org.apache.http.message;versio
 n="[4.3.0,4.4.0)",org.apache.http.impl.entity;version="[4.3.0,4.4.0)",o
 rg.apache.http.params;version="[4.3.0,4.4.0)",org.apache.http.impl.io;v
 ersion="[4.3.0,4.4.0)",org.apache.http;version="[4.3.0,4.4.0)",org.apac
 he.http.concurrent;version="[4.3.0,4.4.0)",org.apache.http.entity;versi
 on="[4.3.0,4.4.0)",org.apache.http.config;version="[4.3.0,4.4.0)",org.a
 pache.http.protocol;version="[4.3.0,4.4.0)"

Hope this was helpful!






 

No comments:

Post a Comment