groovy
  1. groovy
  2. GROOVY-4660

NPE in groovy.servlet.AbstractHttpServlet if deploy WAR to jetty

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.7.6
    • Fix Version/s: 1.8.6, 2.0-beta-3
    • Component/s: Groovlet / GSP
    • Labels:
      None
    • Environment:
      Jetty: 7.2.0.v20101020
    • Number of attachments :
      1

      Description

      I have generated a (non-grails) war using gsp's mapped to groovy.servlet.TemplateServlet. If I deploy the War to Tomcat it works. If I unpack the war then it works in Jetty. But it fails with an NPE if I try to deploy it as a War with Jetty:

      2011-02-04 11:04:06.030:INFO:data-pump-comparator-04.04.01-dev:GroovyTemplate: Servlet groovy.servlet.TemplateServlet initialized on
      class groovy.text.GStringTemplateEngine
      2011-02-04 11:04:06.030:WARN::/data-pump-comparator-04.04.01-dev/
      java.lang.NullPointerException
      at java.io.File.<init>(File.java:222)
      at groovy.servlet.AbstractHttpServlet.getScriptUriAsFile(AbstractHttpServlet.java:306)
      at groovy.servlet.TemplateServlet.service(TemplateServlet.java:388)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
      at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:533)
      at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:475)
      at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)

      I think this is down to a problm in groovy.servlet.AbstractHttpServlet

      TemplateServlet.service does this:

      File file = super.getScriptUriAsFile(request);

      AbstractHttpServlet.getScriptUriAsFile does this:
      String uri = getScriptUri(request);
      String real = servletContext.getRealPath(uri);
      return new File(real).getAbsoluteFile();

      The Javadocs for javax.servlet.ServletContext.getRealPath say:

      • This method returns null
      • if the servlet container cannot translate the virtual path
      • to a real path for any reason (such as when the content is
      • being made available from a .war archive).

      I think that is why the failure happens. AFAIK it also happens with WebLogic (http://groovy.329449.n5.nabble.com/Groovlet-GSP-NPE-when-deployed-as-a-war-to-Weblogic-td3320802.html)

        Activity

        Hide
        Guillaume Laforge added a comment -

        So how do you suggest fixing this? (Even better, a patch is warmly welcome)

        Show
        Guillaume Laforge added a comment - So how do you suggest fixing this? (Even better, a patch is warmly welcome)
        Hide
        Paul Cager added a comment -

        Sure - I'll see if I can work out a patch.

        As far as I can see getScriptUriAsFile is used instead of getScriptUri so that the file's DateLastUpdated timestamp may be used to determine if the cache is out of date. I believe we'd need to preserve that behaviour, but fall back to using a URI if the file is not available.

        Show
        Paul Cager added a comment - Sure - I'll see if I can work out a patch. As far as I can see getScriptUriAsFile is used instead of getScriptUri so that the file's DateLastUpdated timestamp may be used to determine if the cache is out of date. I believe we'd need to preserve that behaviour, but fall back to using a URI if the file is not available.
        Hide
        Paul Cager added a comment -

        Patch attached for you to look at.

        Summary:

        1) Minor change to AbstractHttpServlet.getScriptUriAsFile so that it returns null (rather than an NPE) if getRealPath returns null.

        2) Change to TemplateServlet so that if getScriptUriAsFile returns null it will read the template text using servletContext.getResource(name) instead.

        3) Change to TemplateServlet's caching, to allow for the fact that we might not have a File (when it checks file.dateLastModified).

        4) Minor change to AbstractHttpServlet's test class.

        Show
        Paul Cager added a comment - Patch attached for you to look at. Summary: 1) Minor change to AbstractHttpServlet.getScriptUriAsFile so that it returns null (rather than an NPE) if getRealPath returns null. 2) Change to TemplateServlet so that if getScriptUriAsFile returns null it will read the template text using servletContext.getResource(name) instead. 3) Change to TemplateServlet's caching, to allow for the fact that we might not have a File (when it checks file.dateLastModified). 4) Minor change to AbstractHttpServlet's test class.
        Hide
        Guillaume Laforge added a comment -

        The patch needed to be applied somehow manually, as it couldn't be applied automatically, making me wonder from what exact version you made the patch, but anyway, I've tried a groovlet and template in a war on Jetty, and it worked fine with the adapted patch.

        Show
        Guillaume Laforge added a comment - The patch needed to be applied somehow manually, as it couldn't be applied automatically, making me wonder from what exact version you made the patch, but anyway, I've tried a groovlet and template in a war on Jetty, and it worked fine with the adapted patch.

          People

          • Assignee:
            Guillaume Laforge
            Reporter:
            Paul Cager
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: