Hi.
I have modified StrutsPrepareAndExecuteFilter, StrutsPrepareFilter and 
StrutsExecuteFilter to enable excluding of uri-patters. The filter-config is as 
follows:
        <filter>
                <filter-name>struts2-prepare</filter-name>
                
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
                <init-param>
                        <description>Comma separated list of patterns 
(java.util.regex.Pattern) to be excluded from Struts2-processing</description>
                        <param-name>excludedPatterns</param-name>
                        <param-value>/lang/.*,/pages/.*</param-value>
                </init-param>
        </filter>

Attached is a patch against trunk(revision 747785).

My original plan was to be able to configure this by setting a property 
specifying the pattern(s) in struts.properties or struts.xml, but nobody 
proposed a solution for how I could use those properties from the 
servlet-filters when I asked...

-- 
Andreas Joseph Krogh <andr...@officenet.no>
Senior Software Developer / CEO
------------------------+---------------------------------------------+
OfficeNet AS            | The most difficult thing in the world is to |
Rosenholmveien 25       | know how to do a thing and to watch         |
1414 TrollÄsen          | somebody else doing it wrong, without       |
NORWAY                  | comment.                                    |
                        |                                             |
Tlf:    +47 24 15 38 90 |                                             |
Fax:    +47 24 15 38 91 |                                             |
Mobile: +47 909  56 963 |                                             |
------------------------+---------------------------------------------+
Index: src/main/java/org/apache/struts2/dispatcher/ng/filter/StrutsPrepareAndExecuteFilter.java
===================================================================
--- src/main/java/org/apache/struts2/dispatcher/ng/filter/StrutsPrepareAndExecuteFilter.java	(revision 747785)
+++ src/main/java/org/apache/struts2/dispatcher/ng/filter/StrutsPrepareAndExecuteFilter.java	(working copy)
@@ -21,6 +21,7 @@
 package org.apache.struts2.dispatcher.ng.filter;
 
 import org.apache.struts2.StrutsStatics;
+import org.apache.struts2.RequestUtils;
 import org.apache.struts2.dispatcher.Dispatcher;
 import org.apache.struts2.dispatcher.ng.PrepareOperations;
 import org.apache.struts2.dispatcher.ng.ExecuteOperations;
@@ -31,14 +32,31 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.util.regex.Pattern;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
 
 /**
  * Handles both the preparation and execution phases of the Struts dispatching process.  This filter is better to use
  * when you don't have another filter that needs access to action context information, such as Sitemesh.
+ * <pre>
+	&lt;filter&gt;
+		&lt;filter-name&gt;struts2-prepare&lt;/filter-name&gt;
+		&lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/filter-class&gt;
+		&lt;init-param&gt;
+			&lt;description&gt;Comma separated list of patterns (java.util.regex.Pattern) to be excluded from Struts2-processing&lt;/description&gt;
+			&lt;param-name&gt;excludedPatterns&lt;/param-name&gt;
+			&lt;param-value&gt;/lang/.*,/pages/.*&lt;/param-value&gt;
+		&lt;/init-param&gt;
+	&lt;/filter&gt;
+ * </pre>
+ *
  */
 public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {
     private PrepareOperations prepare;
     private ExecuteOperations execute;
+	private List<Pattern> excludedPatterns = null;
 
     public void init(FilterConfig filterConfig) throws ServletException {
         InitOperations init = new InitOperations();
@@ -50,6 +68,7 @@
 
             prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
             execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
+			setExcludedPatterns(config.getInitParameter("excludedPatterns"));
         } finally {
             init.cleanup();
         }
@@ -61,6 +80,11 @@
         HttpServletRequest request = (HttpServletRequest) req;
         HttpServletResponse response = (HttpServletResponse) res;
 
+		if (excludeUrl(request)) {
+			chain.doFilter(request, response);
+			return;
+		}
+
         try {
             prepare.createActionContext(request, response);
             prepare.assignDispatcherToThread();
@@ -80,6 +104,54 @@
         }
     }
 
+	private void setExcludedPatterns(String patterns) {
+		if (null != patterns && patterns.trim().length() != 0) {
+			List<Pattern> list = new ArrayList<Pattern>();
+			String[] tokens = patterns.split(",");
+			for (String token : tokens) {
+				list.add(Pattern.compile(token.trim()));
+			}
+			this.excludedPatterns = Collections.unmodifiableList(list);
+		} else {
+			this.excludedPatterns = null;
+		}
+	}
+
+	/**
+	 * Gets the uri from the request
+	 *
+	 * @param request
+	 *            The request
+	 * @return The uri
+	 */
+	protected String getUri(HttpServletRequest request) {
+		// handle http dispatcher includes.
+		String uri = (String) request.getAttribute("javax.servlet.include.servlet_path");
+		if (uri != null) {
+			return uri;
+		}
+
+		uri = RequestUtils.getServletPath(request);
+		if (uri != null && !"".equals(uri)) {
+			return uri;
+		}
+
+		uri = request.getRequestURI();
+		return uri.substring(request.getContextPath().length());
+	}
+
+	private boolean excludeUrl(HttpServletRequest request) {
+		if (excludedPatterns != null) {
+			String uri = getUri(request);
+			for (Pattern pattern : excludedPatterns) {
+				if (pattern.matcher(uri).matches()) {
+					return true;
+				}
+			}
+		}
+		return false;
+	}
+
     public void destroy() {
         prepare.cleanupDispatcher();
     }
Index: src/main/java/org/apache/struts2/dispatcher/ng/filter/StrutsPrepareFilter.java
===================================================================
--- src/main/java/org/apache/struts2/dispatcher/ng/filter/StrutsPrepareFilter.java	(revision 747785)
+++ src/main/java/org/apache/struts2/dispatcher/ng/filter/StrutsPrepareFilter.java	(working copy)
@@ -21,6 +21,7 @@
 package org.apache.struts2.dispatcher.ng.filter;
 
 import org.apache.struts2.StrutsStatics;
+import org.apache.struts2.RequestUtils;
 import org.apache.struts2.dispatcher.Dispatcher;
 import org.apache.struts2.dispatcher.ng.PrepareOperations;
 import org.apache.struts2.dispatcher.ng.InitOperations;
@@ -29,12 +30,28 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.regex.Pattern;
 
 /**
  * Prepares the request for execution by a later {...@link org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter} filter instance.
+ * <pre>
+	&lt;filter&gt;
+		&lt;filter-name&gt;struts2-prepare&lt;/filter-name&gt;
+		&lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter&lt;/filter-class&gt;
+		&lt;init-param&gt;
+			&lt;description&gt;Comma separated list of patterns (java.util.regex.Pattern) to be excluded from Struts2-processing&lt;/description&gt;
+			&lt;param-name&gt;excludedPatterns&lt;/param-name&gt;
+			&lt;param-value&gt;/lang/.*,/pages/.*&lt;/param-value&gt;
+		&lt;/init-param&gt;
+	&lt;/filter&gt;
+ * </pre>
  */
 public class StrutsPrepareFilter implements StrutsStatics, Filter {
     private PrepareOperations prepare;
+	private List<Pattern> excludedPatterns = null;
 
     public void init(FilterConfig filterConfig) throws ServletException {
         InitOperations init = new InitOperations();
@@ -43,8 +60,9 @@
             init.initLogging(config);
             Dispatcher dispatcher = init.initDispatcher(config);
 
-            prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
-        } finally {
+			prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
+			setExcludedPatterns(config.getInitParameter("excludedPatterns"));
+		} finally {
             init.cleanup();
         }
 
@@ -55,7 +73,12 @@
         HttpServletRequest request = (HttpServletRequest) req;
         HttpServletResponse response = (HttpServletResponse) res;
 
-        try {
+		if (excludeUrl(request)) {
+			chain.doFilter(request, response);
+			return;
+		}
+
+		try {
             prepare.createActionContext(request, response);
             prepare.assignDispatcherToThread();
             prepare.setEncodingAndLocale(request, response);
@@ -68,7 +91,55 @@
         }
     }
 
-    public void destroy() {
+	private void setExcludedPatterns(String patterns) {
+		if (null != patterns && patterns.trim().length() != 0) {
+			List<Pattern> list = new ArrayList<Pattern>();
+			String[] tokens = patterns.split(",");
+			for (String token : tokens) {
+				list.add(Pattern.compile(token.trim()));
+			}
+			this.excludedPatterns = Collections.unmodifiableList(list);
+		} else {
+			this.excludedPatterns = null;
+		}
+	}
+
+	/**
+	 * Gets the uri from the request
+	 *
+	 * @param request
+	 *            The request
+	 * @return The uri
+	 */
+	protected String getUri(HttpServletRequest request) {
+		// handle http dispatcher includes.
+		String uri = (String) request.getAttribute("javax.servlet.include.servlet_path");
+		if (uri != null) {
+			return uri;
+		}
+
+		uri = RequestUtils.getServletPath(request);
+		if (uri != null && !"".equals(uri)) {
+			return uri;
+		}
+
+		uri = request.getRequestURI();
+		return uri.substring(request.getContextPath().length());
+	}
+
+	private boolean excludeUrl(HttpServletRequest request) {
+		if (excludedPatterns != null) {
+			String uri = getUri(request);
+			for (Pattern pattern : excludedPatterns) {
+				if (pattern.matcher(uri).matches()) {
+					return true;
+				}
+			}
+		}
+		return false;
+	}
+
+	public void destroy() {
         prepare.cleanupDispatcher();
     }
 }
\ No newline at end of file
Index: src/main/java/org/apache/struts2/dispatcher/ng/filter/StrutsExecuteFilter.java
===================================================================
--- src/main/java/org/apache/struts2/dispatcher/ng/filter/StrutsExecuteFilter.java	(revision 747785)
+++ src/main/java/org/apache/struts2/dispatcher/ng/filter/StrutsExecuteFilter.java	(working copy)
@@ -21,6 +21,7 @@
 package org.apache.struts2.dispatcher.ng.filter;
 
 import org.apache.struts2.StrutsStatics;
+import org.apache.struts2.RequestUtils;
 import org.apache.struts2.dispatcher.Dispatcher;
 import org.apache.struts2.dispatcher.ng.PrepareOperations;
 import org.apache.struts2.dispatcher.ng.ExecuteOperations;
@@ -31,19 +32,36 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.util.regex.Pattern;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
 
 /**
  * Executes the discovered request information.  This filter requires the {...@link StrutsPrepareFilter} to have already
  * been executed in the current chain.
+ * <pre>
+	&lt;filter&gt;
+		&lt;filter-name&gt;struts2-prepare&lt;/filter-name&gt;
+		&lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter&lt;/filter-class&gt;
+		&lt;init-param&gt;
+			&lt;description&gt;Comma separated list of patterns (java.util.regex.Pattern) to be excluded from Struts2-processing&lt;/description&gt;
+			&lt;param-name&gt;excludedPatterns&lt;/param-name&gt;
+			&lt;param-value&gt;/lang/.*,/pages/.*&lt;/param-value&gt;
+		&lt;/init-param&gt;
+	&lt;/filter&gt;
+ * </pre>
  */
 public class StrutsExecuteFilter implements StrutsStatics, Filter {
     private PrepareOperations prepare;
     private ExecuteOperations execute;
+	private List<Pattern> excludedPatterns = null;
 
     private FilterConfig filterConfig;
 
     public void init(FilterConfig filterConfig) throws ServletException {
         this.filterConfig = filterConfig;
+		setExcludedPatterns(filterConfig.getInitParameter("excludedPatterns"));
     }
 
     protected synchronized void lazyInit() {
@@ -63,6 +81,11 @@
         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
         lazyInit();
 
@@ -77,6 +100,54 @@
         }
     }
 
+	private void setExcludedPatterns(String patterns) {
+		if (null != patterns && patterns.trim().length() != 0) {
+			List<Pattern> list = new ArrayList<Pattern>();
+			String[] tokens = patterns.split(",");
+			for (String token : tokens) {
+				list.add(Pattern.compile(token.trim()));
+			}
+			this.excludedPatterns = Collections.unmodifiableList(list);
+		} else {
+			this.excludedPatterns = null;
+		}
+	}
+
+	/**
+	 * Gets the uri from the request
+	 *
+	 * @param request
+	 *            The request
+	 * @return The uri
+	 */
+	protected String getUri(HttpServletRequest request) {
+		// handle http dispatcher includes.
+		String uri = (String) request.getAttribute("javax.servlet.include.servlet_path");
+		if (uri != null) {
+			return uri;
+		}
+
+		uri = RequestUtils.getServletPath(request);
+		if (uri != null && !"".equals(uri)) {
+			return uri;
+		}
+
+		uri = request.getRequestURI();
+		return uri.substring(request.getContextPath().length());
+	}
+
+	private boolean excludeUrl(HttpServletRequest request) {
+		if (excludedPatterns != null) {
+			String uri = getUri(request);
+			for (Pattern pattern : excludedPatterns) {
+				if (pattern.matcher(uri).matches()) {
+					return true;
+				}
+			}
+		}
+		return false;
+	}
+
     public void destroy() {
         prepare = null;
         execute = null;

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@struts.apache.org
For additional commands, e-mail: dev-h...@struts.apache.org

Reply via email to