0

I have the added external JARs to the 'lib' directory (GlassFish server config). In spite of that, my application runs into this exception.

exception

javax.servlet.ServletException: PWC1392: Error instantiating servlet class bose.MainServlet

root cause

java.lang.NoClassDefFoundError: org/expressme/openid/OpenIdException

root cause

java.lang.ClassNotFoundException: org.expressme.openid.OpenIdException

OR

randomly, I'm getting a

Servlet MainServlet is not available

The application runs perfecty locally. However, it is unable to run on Jelastic.

This is the JAR I've added: http://code.google.com/p/jopenid/

After navigating to this page ( http://boseapp1.jelastic.servint.net/login.jsp ), if one clicks on the Google/Yahoo image. the request goes to a servlet ( MainServlet ), where the OpenID library is being used. This is where the application stumbles into an exception.

Where am I going wrong ?

Here's the servlet code:

package bose;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.expressme.openid.Association;

import org.expressme.openid.Authentication;
import org.expressme.openid.Endpoint;
import org.expressme.openid.OpenIdException;
import org.expressme.openid.OpenIdManager;
/**
 *
 * @author root
 */
public class MainServlet extends HttpServlet {

        static final long ONE_HOUR = 3600000L;
    static final long TWO_HOUR = ONE_HOUR * 2L;
    static final String ATTR_MAC = "openid_mac";
    static final String ATTR_ALIAS = "openid_alias";

    private OpenIdManager manager;

    @Override
    public void init() throws ServletException {
        super.init();
        manager = new OpenIdManager();
        manager.setRealm("http://boseapp1.jelastic.servint.net/");
        manager.setReturnTo("http://boseapp1.jelastic.servint.net/openid");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

            // manager.setRealm(request.getrequest.getContextPath());
        //manager.setReturnTo(request.getContentType()+"/openid");

        String op = request.getParameter("op");
        if (op==null) {
                            // check sign on result from Google or Yahoo:
                            checkNonce(request.getParameter("openid.response_nonce"));
                            // get authentication:
                            byte[] mac_key = (byte[]) request.getSession().getAttribute(ATTR_MAC);
                            String alias = (String) request.getSession().getAttribute(ATTR_ALIAS);
                            Authentication authentication = manager.getAuthentication(request, mac_key, alias);
                            response.setContentType("text/html; charset=UTF-8");
                        //   showAuthentication(response.getWriter(), authentication);



                        request.getSession().setAttribute("name",authentication.getFullname());
                        request.getSession().setAttribute("email",authentication.getEmail());
                        request.getSession().setAttribute("gender",authentication.getGender());

                        if(User.checkUserPresence(authentication.getEmail()))
                        {
                            response.sendRedirect(request.getContextPath()+"/problem_list.jsp");
                            return;
                        }
                        else
                        {
                            response.sendRedirect(request.getContextPath()+"/completeProfile.jsp");
                            return;
                        }
        }
        if (op.equals("Google") || op.equals("Yahoo")) {
            // redirect to Google or Yahoo sign on page:
            Endpoint endpoint = manager.lookupEndpoint(op);
            Association association = manager.lookupAssociation(endpoint);
            request.getSession().setAttribute(ATTR_MAC, association.getRawMacKey());
            request.getSession().setAttribute(ATTR_ALIAS, endpoint.getAlias());
            String url = manager.getAuthenticationUrl(endpoint, association);
            response.sendRedirect(url);
        }
        else {
            throw new ServletException("Unsupported OP: " + op);
        }
    }

    void showAuthentication(PrintWriter pw, Authentication auth) {
        pw.print("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>Test JOpenID</title></head><body><h1>You have successfully signed on!</h1>");
        pw.print("<p>Identity: " + auth.getIdentity() + "</p>");
        pw.print("<p>Email: " + auth.getEmail() + "</p>");
        pw.print("<p>Full name: " + auth.getFullname() + "</p>");
        pw.print("<p>First name: " + auth.getFirstname() + "</p>");
        pw.print("<p>Last name: " + auth.getLastname() + "</p>");
        pw.print("<p>Gender: " + auth.getGender() + "</p>");
        pw.print("<p>Language: " + auth.getLanguage() + "</p>");
        pw.print("</body></html>");
        pw.flush();


    }

    void checkNonce(String nonce) {
        // check response_nonce to prevent replay-attack:
        if (nonce==null || nonce.length()<20)
            throw new OpenIdException("Verify failed.");
        // make sure the time of server is correct:
        long nonceTime = getNonceTime(nonce);
        long diff = Math.abs(System.currentTimeMillis() - nonceTime);
        if (diff > ONE_HOUR)
            throw new OpenIdException("Bad nonce time.");
        if (isNonceExist(nonce))
            throw new OpenIdException("Verify nonce failed.");
        storeNonce(nonce, nonceTime + TWO_HOUR);
    }

    // simulate a database that store all nonce:
    private Set<String> nonceDb = new HashSet<String>();

    // check if nonce is exist in database:
    boolean isNonceExist(String nonce) {
        return nonceDb.contains(nonce);
    }

    // store nonce in database:
    void storeNonce(String nonce, long expires) {
        nonceDb.add(nonce);
    }

    long getNonceTime(String nonce) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                    .parse(nonce.substring(0, 19) + "+0000")
                    .getTime();
        }
        catch(ParseException e) {
            throw new OpenIdException("Bad nonce time.");
        }
    }
}
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
sbose
  • 1,791
  • 5
  • 24
  • 46

1 Answers1

0


Have you restarted your application server after adding the library?
If yes, you can also try deploy your app to another server.

Marina Sprava
  • 76
  • 1
  • 4
  • Yes i have tried restarting it.Didn't work. i'm using Glassfish as thats where it has been configured and tested locally. Are you asking me to try out Jetty/Tomcat ? – sbose May 05 '12 at 06:07
  • But should it not work with Glassfish ? I've used Glassfish for development. – sbose May 05 '12 at 09:32
  • Hi! If you want to use GlassFish: 1) upload OpenID.jar to the lib folder of your project (WEB-INF/lib/OpenID.jar); 2) build your .WAR file; 3) deploy it to GlassFish. It has to work. – Marina Sprava May 18 '12 at 12:28
  • Thanks Maria, As far as I remember, I had done that the first time I deployed the application. It did not work. I shall try again and get back to you. – sbose May 19 '12 at 07:34