Author: ddewolf
Date: Tue Aug 21 09:46:34 2007
New Revision: 568193

URL: http://svn.apache.org/viewvc?rev=568193&view=rev
Log:
TILES-203: Prevent infinate look of decoration filter when mapped with wild 
cards: Merging in from tunk

Modified:
    
tiles/framework/branches/TILES_2_0_X/tiles-core/src/main/java/org/apache/tiles/web/util/TilesDecorationFilter.java

Modified: 
tiles/framework/branches/TILES_2_0_X/tiles-core/src/main/java/org/apache/tiles/web/util/TilesDecorationFilter.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/branches/TILES_2_0_X/tiles-core/src/main/java/org/apache/tiles/web/util/TilesDecorationFilter.java?rev=568193&r1=568192&r2=568193&view=diff
==============================================================================
--- 
tiles/framework/branches/TILES_2_0_X/tiles-core/src/main/java/org/apache/tiles/web/util/TilesDecorationFilter.java
 (original)
+++ 
tiles/framework/branches/TILES_2_0_X/tiles-core/src/main/java/org/apache/tiles/web/util/TilesDecorationFilter.java
 Tue Aug 21 09:46:34 2007
@@ -59,6 +59,10 @@
  * <param-name>attribute-name</param-name>
  * <param-value>body</param-value>
  * </init-param>
+ * <init-param>
+ * <param-name>prevent-token</param-name>
+ * <param-value>layout</param-value>
+ * </init-param>
  * </filter>
  * <p/>
  * <filter-mapping>
@@ -69,7 +73,9 @@
  * </xmp>
  * The filter will intercept all requests to the indicated url pattern
  * store the initial request path as the "body"  attribute and then render the
- * "test.definition" definition.
+ * "test.definition" definition.  The filter will only redecorate those 
requests
+ * which do not contain the request attribute associated with the prevent token
+ * "layout".
  */
 public class TilesDecorationFilter implements Filter {
 
@@ -96,6 +102,13 @@
     private String definitionName = "layout";
 
     /**
+     * Token used to prevent re-decoration of requests.
+     * This token is used to prevent infinate loops on
+     * filters configured to match wildcards.
+     */
+    private String preventDecorationToken;
+
+    /**
      * Stores a map of the type "mask -> definition": when a definition name
      * mask is identified, it is substituted with the configured definition.
      */
@@ -138,6 +151,9 @@
             definitionName = temp;
         }
 
+        temp = config.getInitParameter("prevent-token");
+        preventDecorationToken = "org.apache.tiles.decoration.PREVENT:"+(temp 
== null ? definitionName : temp);
+
         alternateDefinitions = parseAlternateDefinitions();
 
         temp = config.getInitParameter("mutator");
@@ -183,17 +199,30 @@
     }
 
 
-    /** [EMAIL PROTECTED] */
+    /**
+     * [EMAIL PROTECTED]
+     */
     public void doFilter(ServletRequest req, ServletResponse res, FilterChain 
filterChain)
-        throws IOException, ServletException {
+            throws IOException, ServletException {
+        // If the request contains the prevent token, we must not reapply the 
definition.
+        // This is used to ensure that filters mapped to wild cards do not 
infinately
+        // loop.
+        if (isPreventTokenPresent(req)) {
+            filterChain.doFilter(req, res);
+            return;
+        }
+        
         TilesContainer container = 
TilesAccess.getContainer(getServletContext());
         mutator.mutate(container.getAttributeContext(req, res), req);
         try {
+            if(preventDecorationToken != null) {
+                req.setAttribute(preventDecorationToken, Boolean.TRUE);
+            }
             String definitionName = getDefinitionForRequest(req);
             container.render(definitionName, req, res);
-        } catch (TilesException e) {
-            throw new ServletException(
-                    "Error wrapping jsp with tile definition. "
+        }
+        catch (TilesException e) {
+            throw new ServletException("Error wrapping jsp with tile 
definition. "
                             + e.getMessage(), e);
         }
     }
@@ -251,4 +280,7 @@
         }
     }
 
+    private boolean isPreventTokenPresent(ServletRequest request) {
+        return preventDecorationToken != null && 
request.getAttribute(preventDecorationToken) != null;
+    }
 }


Reply via email to