Search code examples
urlservletsglassfishfilteringpayara

payara glassfish web xml filter url parsing


I have kind of below filter mapping my web xml. But the deployment fails. Is there an alternative ? thanks

<filter-mapping>
    <filter-name>TestFilter</filter-name>
    <url-pattern>*.js</url-pattern>
</filter-mapping> <!-- this works -->

<filter-mapping>
    <filter-name>TestFilter</filter-name>
    <url-pattern>/Application/*.html</url-pattern>
</filter-mapping> <!-- this doesn't work with parsing error as below-->

Error

java.lang.IllegalArgumentException: Invalid URL Pattern: [{0}]
at org.glassfish.web.deployment.node.WebResourceCollectionNode.setElementValue(WebResourceCollectionNode.java:136)
at com.sun.enterprise.deployment.node.SaxParserHandler.endElement(SaxParserHandler.java:583)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)

Solution

  • I'm afraid that mixing mapping by suffix and prefix, as in /Application/*.html isn't supported. You need to map with one of the following patterns:

    • /Application/* (everything with /Application prefix will be mapped)
    • /*.html (everything with the html suffix will be mapped)

    If you want to mix them together, you can map by the prefix (the first option) to a proxy servlet, which would parse the URL in the request and forward it to the appropriate servlet with ServletContext.html#getNamedDispatcher and forward(req, resp), like this for a servlet named application-html:

    if (request.getRequestURI().endsWith(".html")) {
      request.getServletContext()
        .getNamedDispatcher("application-html")
        .forward(request, response)
    }