Why Doesn't Tuckey UrlRewrite Filter call chain.doFilter after a rule is matched?

泄露秘密 提交于 2019-12-18 17:01:15

问题


Using Spring Framework here...

I created a filter to change the response body of css files and if I call a url directly it runs. However, if a urlrewrite rule is matched the filter is skipped.

Example: In web.xml:

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    <!-- skipping init params here for brevity -->
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>cssFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
    <filter-mapping>
    <filter-name>cssFilter</filter-name>
    <url-pattern>*css</url-pattern>
</filter-mapping>

There is a mapping set up like this in urlrewrite.xml:

<rule>
    <from>/styles-special/(.*)$</from>
    <to last="true">/styles/$1</to>
</rule>

(we need this for a number of reasons)

so, any *.css file whose path starts w/ "/styles-special/" will be rewritten to "/styles/" and the cssFilter won't be called, but any *.css file whose path starts w/ "/styles/" will run through the cssFilter as expected.

I've tried changing the url-pattern for cssFilter to a number of different options, but same result. It seems to me like the tuckey urlrewrite filter just doesn't call chain.doFilter() after a rewrite, but maybe it's more complicated than that?

Any idea what the issue might be here? Is this expected functionality? Any workarounds? ...maybe an interceptor or controller is the way to go here?

Thanks in advance for any advice on this!!


Note: Using the following (as suggested by axtavt):

<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>

Fixes the issue w/ chaining and the Filter is run. However, I get the following error:

java.lang.IllegalStateException: NO CONTENT
at org.mortbay.jetty.HttpGenerator.addContent(HttpGenerator.java:106)
at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:644)
at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:579)

Here's code snippet from Filter:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    OutputStream out = httpResponse.getOutputStream();
    GenericResponseWrapper wrapper = new GenericResponseWrapper(httpResponse);

    chain.doFilter(request, wrapper);

    if(log.isDebugEnabled()) log.debug("doFilter: chain");

    String respBody = new String(wrapper.getData()); // this throws error
...

回答1:


When Tuckey UrlRewrite Filter rewrites a URL, it forwards request to the new URL instead of passing it down the filter chain. By default filters are not applied to the forwarded requests, so you need to configure it:

<filter-mapping>
     <filter-name>cssFilter</filter-name>
     <url-pattern>*css</url-pattern>
     <dispatcher>REQUEST</dispatcher>
     <dispatcher>FORWARD</dispatcher>
</filter-mapping>


来源:https://stackoverflow.com/questions/5043593/why-doesnt-tuckey-urlrewrite-filter-call-chain-dofilter-after-a-rule-is-matched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!