ClassNotFoundException thrown in servlet doPost method

808 Views Asked by At

I'm new in applet/servlet development, and I'm facing a problem that I haven't found a fix for.

Environment is the following:
OS: Solaris 5.10
AS: iPlanet6 sp8
JRE: j2re1.4.2_04

jsse.jar is included in servlet classpath.

My servlet must connect to a ldap server via ldaps; my ldap class looks as follows

public class MyLdapClass
{
    private static final String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";

    public MyLdapClass(String _strHost, int _iPort, String _strLogin, String _strPassword, String _strBaseDN)
    {
        ...

        environ.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
        environ.put(Context.SECURITY_PRINCIPAL, _strLogin);
        environ.put(Context.SECURITY_CREDENTIALS, _strPassword);

        String strUrl = "ldap://" + _strHost + ":" + _iPort;

        XTrustProvider.install();
        environ.put(Context.SECURITY_PROTOCOL, "ssl");

        environ.put(Context.PROVIDER_URL, strUrl);
        environ.put(Context.SECURITY_AUTHENTICATION, "simple");

        environ.put("com.sun.jndi.ldap.read.timeout", "5000");
    }

    private void connect() throws NamingException
    {
        try
        {
            this.context = new InitialLdapContext(this.environ, null);
        }
        catch (NamingException ne)
        {
            throw ne;
        }
    }

    private void _disconnect() throws NamingException
    {
        try
        {
            this.context.close();
        }
        catch (NamingException ne)
        {
            throw ne;
        }
    }
}

My servlet (portion of code that is related to my problem) looks as follows:

public class MyServlet extends HttpServlet
{
  MyLdapClass ldap;

  public void init(ServletConfig paramServletConfig) throws ServletException
  {
    ldap = new MyLdapClass("ipaddress", port, "login", "pwd", "baseDN");
  }

  public void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException
  {
    try
    {
        ldap.connect();
    }
    catch (NamingException ne)
    {
      throw ne;
    }

    ...
  }
}

When I post a request from applet to the servlet, an exception is thrown that looks like the following one:

javax.naming.CommunicationException: _ip:port_ [**Root exception is java.lang.ClassNotFoundException: javax/net/ssl/SSLSocketFactory**]
   at com.sun.jndi.ldap.Connection.<init>(Unknown Source)
   at com.sun.jndi.ldap.LdapClient.<init>(Unknown Source)
   at com.sun.jndi.ldap.LdapClient.getInstance(Unknown Source)
   at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
   at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
   at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
   at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
   at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
   at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
   at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
   at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
   at javax.naming.InitialContext.init(Unknown Source)
   at javax.naming.ldap.InitialLdapContext.<init>(Unknown Source)
   at MyLdapClass.connect(MyLdapClass.java:_row number_)
   at MyServlet.doPost(MyServlet.java:_row number_)
   at javax.servlet.http.HttpServlet.doPost(HttpServlet.java:_row number_)
   at com.iplanet.server.http.servlet.NSServletRunner.invokeServletService(NSServletRunner.java:937)
   at com.iplanet.server.http.servlet.NSServletRunner.Service(NSServletRunner.java:494)
Caused by: java.lang.ClassNotFoundException: javax/net/ssl/SSLSocketFactory
   at java.lang.Class.forName0(Native Method)
   at java.lang.Class.forName(Unknown Source)
   at com.sun.jndi.ldap.VersionHelper12.loadClass(Unknown Source)
   at com.sun.jndi.ldap.Connection.createSocket(Unknown Source)
   ... 20 more
Internal error: servlet service function had thrown ServletException (uri=/servlet/MyServlet): javax.servlet.ServletException: NamingException:__ip:port__, stack: javax.servlet.ServletException: NamingException:_ip:port_

The same occurs if I call ldap.connect() from Myservlet service method; instead, if I call ldap.connect() from init method of MyServlet class, everything works fine, but I have the requirement that connection must be opened on demand, and closed when request has been processed.

Am I missing something? How can I fix it? Thank you all in advance

Just to be complete, XTrustProvider code is the following:

package jas.frontend;

import java.security.AccessController;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivilegedAction;
import java.security.Provider;
import java.security.Security;
import java.security.cert.X509Certificate;

import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactorySpi;
import javax.net.ssl.X509TrustManager;

public final class XTrustProvider extends Provider {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final static String NAME = "XTrustJSSE";
    private final static String INFO = "XTrust JSSE Provider (implements trust factory with truststore validation disabled)";
    private final static double VERSION = 1.0D;

    public XTrustProvider() {
        super(NAME, VERSION, INFO);

        AccessController.doPrivileged(new PrivilegedAction() {

            public Object run() {
                put("TrustManagerFactory." + TrustManagerFactoryImpl.getAlgorithm(),
                        TrustManagerFactoryImpl.class.getName());
                return null;
            }
        });
    }

    public static void install() {
        if (Security.getProvider(NAME) == null) {
            Security.insertProviderAt(new XTrustProvider(), 2);
            Security.setProperty("ssl.TrustManagerFactory.algorithm", TrustManagerFactoryImpl.getAlgorithm());
        }
    }

    public final static class TrustManagerFactoryImpl extends TrustManagerFactorySpi {

        public TrustManagerFactoryImpl() {}

        public static String getAlgorithm() {
            return "XTrust509";
        }

        protected void engineInit(KeyStore keystore) throws KeyStoreException {}

        protected void engineInit(ManagerFactoryParameters mgrparams) throws InvalidAlgorithmParameterException {
            throw new InvalidAlgorithmParameterException(XTrustProvider.NAME + " does not use ManagerFactoryParameters");
        }

        protected TrustManager[] engineGetTrustManagers() {
            return new TrustManager[] { new X509TrustManager() {

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {}

                public void checkServerTrusted(X509Certificate[] certs, String authType) {}
            } };
        }
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

Finally I fixed it (thanks to Emanuele Gallo, not signed on SO, but a great guy anyway)!!!

Problem was that I added

/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar

in Classpath field (see picture below)

enter image description here

In order to work, instead, they must be added in Option field with -X option using bootclasspath property.

In othe rwords, in <iPlanet_install_path>/servers/https-<virtual_server_name>/config/jvm12.conf this was the wrong configuration

#jvm.option=-Xbootclasspath:/lib/tools.jar:/jre/lib/rt.jar
jvm.option=-Xrs
jvm.option=-Xnoagent
jvm.classpath=/opt/iplanet6/servers/plugins/servlets/examples/legacy/beans.10/SDKBeans10.jar:/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar

that worked absolutely fine when replaced with the following one

jvm.option=-Xrs
jvm.option=-Xnoagent
jvm.classpath=/opt/iplanet6/servers/plugins/servlets/examples/legacy/beans.10/SDKBeans10.jar
jvm.option=-Xbootclasspath:/opt/wbp/j2re1.4.2_04/lib/rt.jar:/opt/wbp/j2re1.4.2_04/lib/jce.jar:/opt/wbp/j2re1.4.2_04/lib/jsse.jar

NOTE 1: each key=value pair must be wtitten on a single line; here newline between '-' and Xbootclasspath.... depends on page formatting