0

I'm slowly getting through my exceptions on my code using GWT + Hibernate, my server side class is as follows...

public class GreetingServiceImpl extends RemoteServiceServlet implements
    GreetingService {

public String greetServer(String input) throws IllegalArgumentException {
    // Verify that the input is valid. 
    if (!FieldVerifier.isValidName(input)) {
        // If the input is not valid, throw an IllegalArgumentException back to
        // the client.
        throw new IllegalArgumentException(
                "Name must be at least 4 characters long");
    }

    // Escape data from the client to avoid cross-site script vulnerabilities.
    input = escapeHtml(input);
    User user = new User();
    user.setFirstName(input);
    DaoFactory factory = new DaoFactory();
    try {
        factory.create(user);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "User : " + input + " added";
}

/**
 * Escape an html string. Escaping data received from the client helps to
 * prevent cross-site script vulnerabilities.
 * 
 * @param html the html string to escape
 * @return the escaped string
 */
private String escapeHtml(String html) {
    if (html == null) {
        return null;
    }
    return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
            .replaceAll(">", "&gt;");
}

This receives a string, sets the users firstname as this string and commits it to the db but I get the following error...

[ERROR] javax.servlet.ServletContext log: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.lang.String com.behaviour.app.client.GreetingService.greetServer(java.lang.String) throws java.lang.IllegalArgumentException' threw an unexpected exception: java.lang.ExceptionInInitializerError
    at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:58)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:70)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:351)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.ExceptionInInitializerError
    at com.behaviour.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:21)
    at com.behaviour.hibernate.DaoFactory.create(DaoFactory.java:9)
    at com.behaviour.app.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:100)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
    ... 34 more
Caused by: java.lang.ExceptionInInitializerError
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
    at java.sql.DriverManager.getConnection(DriverManager.java:620)
    at java.sql.DriverManager.getConnection(DriverManager.java:169)
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:113)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
    at com.behaviour.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:15)
    ... 42 more
Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:393)
    at java.security.AccessController.checkPermission(AccessController.java:553)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkPermission(DevAppServerFactory.java:166)
    at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkAccess(DevAppServerFactory.java:191)
    at java.lang.ThreadGroup.checkAccess(ThreadGroup.java:315)
    at java.lang.Thread.init(Thread.java:353)
    at java.lang.Thread.<init>(Thread.java:437)
    at java.util.TimerThread.<init>(Timer.java:478)
    at java.util.Timer.<init>(Timer.java:100)
    at java.util.Timer.<init>(Timer.java:146)
    at com.mysql.jdbc.Connection.<clinit>(Connection.java:201)
    ... 51 more

I'm not sure really how to approach this. The classes involved are as follows...

public class HibernateUtil 
{
  private static final SessionFactory sessionFactory;

   static 
   {
    try 
      {
       // Create the SessionFactory from hibernate.cfg.xml
       sessionFactory = new Configuration().configure().buildSessionFactory();
       } 
       catch (Throwable ex)
       {
         // Make sure you log the exception, as it might be swallowed
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);
       }
    }

    public static SessionFactory getSessionFactory()
    {
     return sessionFactory;
    }
 }

and

public class DaoFactory
{
   public void create(Object obj)throws Exception
   {
     Session session = HibernateUtil.getSessionFactory().getCurrentSession();
     session.beginTransaction();
     session.save(obj);
     session.getTransaction().commit();
   }
}

Thanks,

Apologies for not really explaining but I'm new to GWT and Hibernate.

4
  • You should not escape HTML when putting data in the database. Keep the data as it is. You should escape it when displaying it in HTML. How will you do when you'll have to include this data in a non HTML document (Excel, CSV, Swing application, whatever), or when you'll need this string in a JavaScript variable? Commented Aug 22, 2011 at 11:27
  • Yeah, I will sort this out when I use it more in production, thanks for the tip, at the moment I just want to get this running, then I will focus on the more secure aspects :) Commented Aug 22, 2011 at 11:32
  • also - the class that escapes HTML is being used in my front end class as well. Commented Aug 22, 2011 at 11:33
  • If you escape the same data twice, it won't look nice :-). Why not making it correctly from the beginning? See my answer for explanations about the exception you got. Commented Aug 22, 2011 at 11:35

1 Answer 1

2

You seem to be running your application in Google App Engine's development server.

If your server application is a Google App Engine application, it may not use Hibernate. The only way to persist data in a GAE application is to use the App Engine DataStore. The App Engine development server has a security manager that replicates what is done on the actual Google App Engine server, and which forbids the MySQL driver to create and modify thread groups, as the stack trace indicates.

If your server application is a traditional web application intended to be deployed in a standard Java EE appserver (Tomcat, Jetty, GlassFish, etc.), then make it a web application and deploy it in one of those servers.

Sign up to request clarification or add additional context in comments.

3 Comments

ah awesome, does that mean I can still use GWT, but just deploy on something like Glassfish instead of on Google App Engine?
Ah, thank you, apologies I'm still very new at this, how do I make my project run on a different server side technology? (I've built things with Tomcat and Glassfish before, but how do I explicitly state this in my GWT project?)
I've never used GWT, so I can't tell if you could have a project that is both a GWT project and a web project (assuming Eclipse here). But I guess it must be possible to have a pure GWT project (usd for the client-side), and a pure web project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.