Search code examples
javagoogle-app-enginejax-rsresteasyshiro

Making GAE Shiro and Resteasy work together


I am trying to make GAE-Shiro and Resteasy together. So I tried doing a quick port. However I am getting this error:

[ERROR] java.lang.RuntimeException: java.lang.InstantiationException: com.cilogi.shiro.guice.ServeModule
[ERROR]     at org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener.getModules(GuiceResteasyBootstrapServletContextListener.java:83)
[ERROR]     at org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener.contextInitialized(GuiceResteasyBootstrapServletContextListener.java:27)
[ERROR]     at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548)
[ERROR]     at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
[ERROR]     at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
[ERROR]     at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
[ERROR]     at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
[ERROR]     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
[ERROR]     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
[ERROR]     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
[ERROR]     at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
[ERROR]     at org.mortbay.jetty.Server.doStart(Server.java:224)
[ERROR]     at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
[ERROR]     at com.google.appengine.tools.development.JettyContainerService.startContainer(JettyContainerService.java:228)
[ERROR]     at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:255)
[ERROR]     at com.google.appengine.tools.development.AbstractServer.startup(AbstractServer.java:79)
[ERROR]     at com.google.appengine.tools.development.DevAppServerImpl$Servers.startup(DevAppServerImpl.java:451)
[ERROR]     at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:198)
[ERROR]     at com.google.appengine.tools.development.gwt.AppEngineLauncher.start(AppEngineLauncher.java:97)
[ERROR]     at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)
[ERROR]     at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1068)
[ERROR]     at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:811)
[ERROR]     at com.google.gwt.dev.DevMode.main(DevMode.java:311)
[ERROR] Caused by: java.lang.InstantiationException: com.cilogi.shiro.guice.ServeModule
[ERROR]     at java.lang.Class.newInstance0(Class.java:359)
[ERROR]     at java.lang.Class.newInstance(Class.java:327)
[ERROR]     at org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener.getModules(GuiceResteasyBootstrapServletContextListener.java:70)
[ERROR]     ... 22 more

Here is my current web.xml configuration

    <context-param>
        <param-name>resteasy.guice.modules</param-name>
<!--         <param-value>org.jboss.errai.ui.demo.server.MyServletModule</param-value> -->
        <param-value>com.cilogi.shiro.guice.ServeModule</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener
        </listener-class>
    </listener>

    <context-param>  
        <param-name>resteasy.servlet.mapping.prefix</param-name>  
        <param-value>/</param-value>  
    </context-param>

    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
       <param-name>user-base-url</param-name>
       <param-value>/user/admin</param-value>
    </context-param>

    <context-param>
        <param-name>static-base-url</param-name>
        <param-value></param-value>
    </context-param>


    <listener>
        <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>com.cilogi.shiro.guice.ServeContextListener</listener-class>
    </listener>


    <mime-mapping>
        <extension>manifest</extension>
        <mime-type>text/cache-manifest</mime-type>
    </mime-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list> 

Solution

  • I realise that this question is almost a year old, but I faced a similar issue recently with Shiro (note: not GAE-Shiro) and RESTEasy and figured I would post the solution I used here so that it may benefit anyone else who experiences this particular problem.

    The problem is that RESTEasy's GuiceResteasyBootstrapServletContextListener is trying to create a module that requires a parameter to be passed to it (in my case, it was a ShiroWebModule requiring a ServletContext parameter...in your case, it looks like com.cilogi.shiro.guice.ServeModule requires a String to be passed). However, when RESTEasy attempts to create the module, it does so by calling the no-args constructor, and then fails because there isn't one.

    The solution I decided to go with was to extend GuiceResteasyBootstrapServletContextListener and override the getModules(ServletContext ctx) method so that if a constructor that takes a ServletContext exists, then use it, otherwise, use the default no-args constructor.

    So for example:

    public class MyBootstrapServletContextListener extends GuiceResteasyBootstrapServletContextListener {
    
        @Override
        protected List getModules(final ServletContext context) {
            final List result = new ArrayList();
            final String modulesString = context.getInitParameter("resteasy.guice.modules");
    
            if (modulesString != null) {
                final String[] moduleStrings = modulesString.trim().split(",");
    
                for (final String moduleString : moduleStrings) {
                    try {
                        log.info("Found module: {}", moduleString);
                        final Class cls = Thread.currentThread().getContextClassLoader().loadClass(moduleString.trim());
                        final Module module = createModule(cls, context);
                        result.add(module);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    } catch (InstantiationException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalArgumentException e) {
                        throw new RuntimeException(e);
                    } catch (InvocationTargetException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
    
            return result;
        }
    
        private Module createModule(Class cls, ServletContext context) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            Constructor constructor = null;
    
            try {
                constructor = cls.getConstructor(ServletContext.class);
            } catch (NoSuchMethodException e) {
                log.info("Class {} has no constructor that takes just a ServletContext parameter; defaulting to no-args constructor.", cls);
            }
    
            return constructor == null ? (Module) cls.newInstance() : (Module) constructor.newInstance(context);
        }
    
    }
    

    Once this was in place, I changed web.xml to use MyBootstrapServletContextListener instead of RESTEasy's GuiceResteasyBootstrapServletContextListener, and RESTEasy no longer had a problem. You should be able to do something similar to allow RESTEasy to pass the required String to com.cilogi.shiro.guice.ServeModule.

    Again, I realise you probably no longer need a solution, but I happened upon this question while researching the a similar problem with Shiro, and hope to help anyone else that is trying to combine RESTEasy and Shiro in their web app.