[ 
https://issues.apache.org/struts/browse/WW-3077?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=46270#action_46270
 ] 

Alex Siman commented on WW-3077:
--------------------------------

Hi.

I have solved this issue for me. Simply I copied StrutsExecuteFilter and made 
little modifications to it. Maybe it has potential bugs, but it seems like 
working well.

Such as I did not check out Struts from SVN, I paste my StrutsExecuteFilter 
here. Hope somebody will made patch from it.

web.xml
--------------------------------------------------
...
        <filter>
                <filter-name>UrlRewriteFilter</filter-name>
                
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <filter>
        <filter>
                <filter-name>struts-prepare</filter-name>
                
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
        </filter>
        <filter>
                <filter-name>sitemesh</filter-name>
                
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
        </filter>
        <filter>
                <filter-name>struts-execute</filter-name>
                <filter-class>custom.StrutsExecuteFilter</filter-class>
        </filter>
...
        <filter-mapping>
                <filter-name>UrlRewriteFilter</filter-name>
                <url-pattern>/*</url-pattern>
                <dispatcher>REQUEST</dispatcher>
                <dispatcher>FORWARD</dispatcher>
        </filter-mapping>
        <filter-mapping>
                <filter-name>struts-prepare</filter-name>
                <url-pattern>/*</url-pattern>
                <dispatcher>REQUEST</dispatcher>
                <dispatcher>FORWARD</dispatcher>
        </filter-mapping>
        <filter-mapping>
                <filter-name>sitemesh</filter-name>
                <url-pattern>/*</url-pattern>
                <dispatcher>REQUEST</dispatcher>
                <dispatcher>FORWARD</dispatcher>
        </filter-mapping>
        <filter-mapping>
                <filter-name>struts-execute</filter-name>
                <url-pattern>/*</url-pattern>
                <dispatcher>REQUEST</dispatcher>
                <dispatcher>FORWARD</dispatcher>
        </filter-mapping>
...

custom.StrutsExecuteFilter.java
--------------------------------------------------
package custom;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.dispatcher.ng.ExecuteOperations;
import org.apache.struts2.dispatcher.ng.InitOperations;
import org.apache.struts2.dispatcher.ng.PrepareOperations;
import org.apache.struts2.dispatcher.ng.filter.FilterHostConfig;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter;

/**
 * Executes the discovered request information.  This filter requires the 
{...@link StrutsPrepareFilter} to have already
 * been executed in the current chain.
 */
public class StrutsExecuteFilter implements StrutsStatics, Filter {

        public static final String EXECUTING_ACTION = 
"struts.filter.executingAction";

    private PrepareOperations prepare;
    private ExecuteOperations execute;

    private FilterConfig filterConfig;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    protected synchronized void lazyInit() {
        if (execute == null) {
            InitOperations init = new InitOperations();
            Dispatcher dispatcher = init.findDispatcherOnThread();
            init.initStaticContentLoader(new FilterHostConfig(filterConfig), 
dispatcher);

            prepare = new PrepareOperations(filterConfig.getServletContext(), 
dispatcher);
            execute = new ExecuteOperations(filterConfig.getServletContext(), 
dispatcher);
        }

    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain 
chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

                if (excludeUrl(request)) {
                        chain.doFilter(request, response);
                        return;
                }

        // This is necessary since we need the dispatcher instance, which was 
created by the prepare filter
                if (execute == null) {
                        lazyInit();
                }

                ActionMapping mapping = null;
                Object execAttr = req.getAttribute(EXECUTING_ACTION);
                if (execAttr == null) {
                        mapping = prepare.findActionMapping(request, response);
                }

        if (mapping == null) {
            boolean handled = execute.executeStaticResourceRequest(request, 
response);
            if (!handled) {
                chain.doFilter(request, response);
            }
        } else {
                request.setAttribute(EXECUTING_ACTION, true);
            execute.executeAction(request, response, mapping);
            request.removeAttribute(EXECUTING_ACTION);
        }
    }

        private boolean excludeUrl(HttpServletRequest request) {
                return 
request.getAttribute(StrutsPrepareFilter.REQUEST_EXCLUDED_FROM_ACTION_MAPPING) 
!= null;
        }

    public void destroy() {
        prepare = null;
        execute = null;
        filterConfig = null;
    }
}


> StrutsPrepareAndExecuteFilter: infinite loop in filter chain with dispatcher 
> FORWARD
> ------------------------------------------------------------------------------------
>
>                 Key: WW-3077
>                 URL: https://issues.apache.org/struts/browse/WW-3077
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Dispatch Filter
>    Affects Versions: 2.1.6
>         Environment: Windows XP SP2, Java 1.6.0_10. Tomcat 6.0.18
>  
>            Reporter: Marco Rolando
>             Fix For: Future
>
>
> Using the struts 2 filter StrutsPrepareAndExecuteFilter with UrlRewrite 
> (org.tuckey.web.filters.urlrewrite.UrlRewriteFilter) the filter evaluation 
> sequence enters an infinite loop. The same problem arise with other 
> forwarding filters.
> org.apache.struts2.dispatcher.FilterDispatcher does not suffer from this 
> problem.
> Below the filter configuration in web.xml
>       <filter-mapping>
>               <filter-name>rewriteUrl</filter-name>
>               <url-pattern>/*</url-pattern>
>               <dispatcher>REQUEST</dispatcher>
>               <dispatcher>FORWARD</dispatcher>
>       </filter-mapping>
>       <filter-mapping>
>               <filter-name>struts2</filter-name>
>               <url-pattern>/*</url-pattern>
>               <dispatcher>REQUEST</dispatcher>
>               <dispatcher>FORWARD</dispatcher>
>       </filter-mapping>

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to