[seam-commits] Seam SVN: r9275 - in trunk/src: main/org/jboss/seam/web and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Sun Oct 12 14:50:51 EDT 2008


Author: norman.richards at jboss.com
Date: 2008-10-12 14:50:50 -0400 (Sun, 12 Oct 2008)
New Revision: 9275

Added:
   trunk/src/main/org/jboss/seam/web/ServletMapping.java
Modified:
   trunk/src/main/org/jboss/seam/navigation/Page.java
   trunk/src/main/org/jboss/seam/web/IncomingPattern.java
   trunk/src/main/org/jboss/seam/web/OutgoingPattern.java
   trunk/src/main/org/jboss/seam/web/Pattern.java
   trunk/src/main/org/jboss/seam/web/RewriteFilter.java
   trunk/src/test/unit/org/jboss/seam/test/unit/web/RewriteTest.java
Log:
JBSEAM-3527

Modified: trunk/src/main/org/jboss/seam/navigation/Page.java
===================================================================
--- trunk/src/main/org/jboss/seam/navigation/Page.java	2008-10-12 18:19:25 UTC (rev 9274)
+++ trunk/src/main/org/jboss/seam/navigation/Page.java	2008-10-12 18:50:50 UTC (rev 9275)
@@ -16,6 +16,7 @@
 import org.jboss.seam.core.ResourceLoader;
 import org.jboss.seam.core.Expressions.ValueExpression;
 import org.jboss.seam.security.Identity;
+import org.jboss.seam.util.Faces;
 import org.jboss.seam.web.Pattern;
 
 /**
@@ -402,28 +403,12 @@
 
     //public void setRewritePatterns(List<String> rewritePatterns) {
     //    this.rewritePatterns = rewritePatterns;
-    //}
+    //} 
 
     public void addRewritePattern(String value) {
-        Pattern pattern = new Pattern(externalUrl(), value);
+        Pattern pattern = new Pattern(viewId, value);
         rewritePatterns.add(pattern);
     }
-    
-    // XXX - not the right implementation - assumes .seam
-    private String externalUrl() {
-        String id = viewId;
-        
-        if (id.contains("*")) {
-            throw new IllegalArgumentException("rewrite patterns not allowed on wildcard page defs");
-        }
-        
-        int pos = id.lastIndexOf(".xhtml");
-        if (pos!=-1) {
-            id = id.substring(0, pos) + ".seam";
-        }
-        
-        return id;
-    }
 
     public List<Header> getHeaders() {
         return httpHeaders;

Modified: trunk/src/main/org/jboss/seam/web/IncomingPattern.java
===================================================================
--- trunk/src/main/org/jboss/seam/web/IncomingPattern.java	2008-10-12 18:19:25 UTC (rev 9274)
+++ trunk/src/main/org/jboss/seam/web/IncomingPattern.java	2008-10-12 18:50:50 UTC (rev 9275)
@@ -4,16 +4,19 @@
 import java.util.List;
 import java.util.regex.Matcher;
 
+
 public class IncomingPattern {
     String view;
     String pattern;
-    
+    ServletMapping viewMapping;
+
     java.util.regex.Pattern regexp;
     List<String> regexpArgs = new ArrayList<String>();
     
-    public IncomingPattern(String view, String pattern) {
+    public IncomingPattern(ServletMapping viewMapping, String view, String pattern) {
         this.view = view;
         this.pattern = pattern;
+        this.viewMapping = viewMapping;
         
         parsePattern(pattern);
     }
@@ -49,7 +52,7 @@
     }
 
     private String regexpArg(String substring) {
-        return "([^/]+)";
+        return "([^/]*)";
     }
 
     private String regexpLiteral(String value) {
@@ -66,7 +69,6 @@
         return res.toString();
     }
     
-    
     public class IncomingRewrite
         implements Rewrite
     {
@@ -87,15 +89,16 @@
                 this.queryArgs = incoming.substring(queryPos+1);
             }
 
-            this.incoming = stripTrailingSlash(this.incoming);
+            // don't match trailing slash - it might indicate an empty arg
+            // this.incoming = stripTrailingSlash(this.incoming);
         }
 
-        private String stripTrailingSlash(String text) {
-            if (text.endsWith("/")) {
-                return stripTrailingSlash(text.substring(0,text.length()-1));
-            }
-            return text;
-        }
+//        private String stripTrailingSlash(String text) {
+//            if (text.endsWith("/")) {
+//                return stripTrailingSlash(text.substring(0,text.length()-1));
+//            }
+//            return text;
+//        }
 
         public boolean isMatch() {
             if (isMatch == null) {
@@ -121,7 +124,8 @@
 
         public String rewrite() {
             StringBuffer result = new StringBuffer();
-            result.append(view);
+            
+            result.append(mappedURL(view));
 
             boolean first = true;
 
@@ -133,7 +137,7 @@
             for (int i=0; i<regexpArgs.size(); i++) {
                 String key   = regexpArgs.get(i);
                 String value = matchedArgs.get(i);
-
+                
                 if (first) {
                     result.append('?');
                     first = false;
@@ -146,4 +150,8 @@
             return result.toString();
         }
     }
+    
+    private String mappedURL(String viewId) {
+        return viewMapping.mapViewIdToURL(viewId);
+    }
 }

Modified: trunk/src/main/org/jboss/seam/web/OutgoingPattern.java
===================================================================
--- trunk/src/main/org/jboss/seam/web/OutgoingPattern.java	2008-10-12 18:19:25 UTC (rev 9274)
+++ trunk/src/main/org/jboss/seam/web/OutgoingPattern.java	2008-10-12 18:50:50 UTC (rev 9275)
@@ -6,20 +6,22 @@
 public class OutgoingPattern {
     String view;
     String pattern;
+    ServletMapping viewMapping;
 
     List<String> parts = new ArrayList<String>();
-    
-    public OutgoingPattern(String view, String pattern) {
+
+    public OutgoingPattern(ServletMapping viewMapping, String view, String pattern) {
         this.view = view;
         this.pattern = pattern;
-        
+        this.viewMapping = viewMapping;
+
         parsePattern(pattern);
     }
-    
+
     public Rewrite rewrite(String path) {
         return new OutgoingRewrite(path);
     }
-    
+
     private void parsePattern(String value) {       
         while (value.length()>0) {
             int pos = value.indexOf('{');
@@ -37,23 +39,23 @@
             }
         }
     }
-    
+
     public class OutgoingRewrite 
         implements Rewrite
     {
         Boolean isMatch;
-        
+
         private String base;
         private List<String> queryArgs     = new ArrayList<String>();
         private List<String> matchedArgs   = new ArrayList<String>();
-        
+
         public OutgoingRewrite(String outgoing) {           
             int queryPos = outgoing.indexOf('?');
 
             if (queryPos == -1) {
-                this.base      = outgoing;
+                this.base = outgoing;
             } else {
-                this.base      = outgoing.substring(0, queryPos);
+                this.base = outgoing.substring(0, queryPos);
                 parseArgs(outgoing.substring(queryPos+1));
             }
         }
@@ -70,33 +72,36 @@
             }
             return isMatch;
         }
-        
+
         private boolean match() {
-            if (!base.equals(view)) {
+            if (!viewMapping.isMapped(base,view)) {
                 return false;
             }
-            
+
             for (String part: parts) {
                 if (part.startsWith("{") && part.endsWith("}")) {
                     String name = part.substring(1,part.length()-1);
                     String value = matchArg(name);
-                    
+
                     if (value == null) {
                         return false;
                     }
-                    
+
                     matchedArgs.add(value);
                 }
             }
-            
+
             return true;
         }
 
+        
+
+
         private String matchArg(String argName) {
             for (int i=0; i<queryArgs.size(); i++) {
                 String query = queryArgs.get(i);
                 int pos = query.indexOf("=");
-                
+
                 if (query.subSequence(0, pos).equals(argName)) {
                     queryArgs.remove(i);
                     return query.substring(pos+1);
@@ -107,7 +112,7 @@
 
         public String rewrite() {
             StringBuffer res = new StringBuffer();
-            
+
             int matchedPosition = 0;
             for (String part: parts) {
                 if (part.startsWith("{")) { 
@@ -116,13 +121,13 @@
                     res.append(part);
                 }
             }
-            
+
             char sep = '?';
             for (String arg: queryArgs) {
                 res.append(sep).append(arg);
                 sep = '&';
             }
-            
+
             return res.toString();
         }
     }

Modified: trunk/src/main/org/jboss/seam/web/Pattern.java
===================================================================
--- trunk/src/main/org/jboss/seam/web/Pattern.java	2008-10-12 18:19:25 UTC (rev 9274)
+++ trunk/src/main/org/jboss/seam/web/Pattern.java	2008-10-12 18:50:50 UTC (rev 9275)
@@ -4,6 +4,7 @@
 {
     String view;
     String pattern;
+    ServletMapping viewMapping;
     
     IncomingPattern inPattern;
     OutgoingPattern outPattern;
@@ -11,17 +12,33 @@
     public Pattern(String view, String pattern) {
         this.view = view;
         this.pattern = pattern;
-        
-        inPattern = new IncomingPattern(view, pattern);
-        outPattern = new OutgoingPattern(view, pattern);
+    }    
+    
+    // not necessarily available when pattern is created 
+    public void setViewMapping(ServletMapping viewMapping) {
+       this.viewMapping = viewMapping;
     }
+    
+    protected IncomingPattern inPattern() {
+       if (inPattern == null) {
+          inPattern = new IncomingPattern(viewMapping, view, pattern);
+       }
+       return inPattern;
+    }
+    
+    protected OutgoingPattern outPattern() {
+       if (outPattern == null) {
+          outPattern = new OutgoingPattern(viewMapping, view, pattern);
+       }
+       return outPattern;
+    }
 
     public Rewrite matchIncoming(String path) {
-        return returnIfMatch(inPattern.rewrite(path));
+        return returnIfMatch(inPattern().rewrite(path));
     }
 
     public Rewrite matchOutgoing(String path) {
-        return returnIfMatch(outPattern.rewrite(path));
+        return returnIfMatch(outPattern().rewrite(path));
     }
     
     @Override

Modified: trunk/src/main/org/jboss/seam/web/RewriteFilter.java
===================================================================
--- trunk/src/main/org/jboss/seam/web/RewriteFilter.java	2008-10-12 18:19:25 UTC (rev 9274)
+++ trunk/src/main/org/jboss/seam/web/RewriteFilter.java	2008-10-12 18:50:50 UTC (rev 9275)
@@ -36,6 +36,9 @@
 {
     private static LogProvider log = Logging.getLogProvider(RewriteFilter.class);
 
+    private String viewMapping;
+    
+    
     public void doFilter(ServletRequest request, 
                          ServletResponse response, 
                          FilterChain chain) 
@@ -135,7 +138,24 @@
             log.warn("Pages is null for incoming request!");
         }
         
+        ServletMapping mapping = new ServletMapping(viewMapping);
+        for (Pattern pattern: allPatterns) {
+            pattern.setViewMapping(mapping);
+        }
+        
         return allPatterns;
     }
+
+
+   public String getViewMapping()
+   {
+      return viewMapping == null ? "*.seam" : viewMapping;
+   }
+
+
+   public void setViewMapping(String viewMapping)
+   {
+      this.viewMapping = viewMapping;
+   }
 }
 

Added: trunk/src/main/org/jboss/seam/web/ServletMapping.java
===================================================================
--- trunk/src/main/org/jboss/seam/web/ServletMapping.java	                        (rev 0)
+++ trunk/src/main/org/jboss/seam/web/ServletMapping.java	2008-10-12 18:50:50 UTC (rev 9275)
@@ -0,0 +1,51 @@
+package org.jboss.seam.web;
+
+
+/**
+ * functions related to the faces servlet mapping and the translation from viewId to URL and back
+ */
+public class ServletMapping {
+    String mapping;
+    
+    public ServletMapping(String mapping) {
+        this.mapping = mapping;
+    }
+    
+
+    public String mapViewIdToURL(String viewId) {
+        if (mapping.endsWith("/*")) {
+            return mapping.substring(0,mapping.length()-2) + viewId;
+        
+        } else if (mapping.startsWith("*.")){
+            int pos = viewId.lastIndexOf(".");
+            if (pos != -1) {
+                return viewId.substring(0, pos) + mapping.substring(1);
+            }
+        }
+        
+        return null;
+    }
+    
+    private String stripExtension(String text) {
+        int pos = text.lastIndexOf('.');
+        return (pos == -1) ? null : text.substring(0,pos);
+    }
+    
+    // this method should really be one that converts the baseURL to the viewId,
+    // but we need default faces extension for that
+    public boolean isMapped(String baseURL, String viewId)
+    {
+        if (mapping.startsWith("*.")) {
+            String baseValue = stripExtension(baseURL);
+            String viewValue = stripExtension(viewId);
+           
+            return baseValue!=null && viewValue!=null && baseValue.equals(viewValue);
+        } else if (mapping.endsWith("/*")) {
+            String prefix = mapping.substring(0,mapping.length()-2) + viewId;
+            return baseURL.equals(prefix);
+        }
+        
+        return false;        
+    }
+
+}

Modified: trunk/src/test/unit/org/jboss/seam/test/unit/web/RewriteTest.java
===================================================================
--- trunk/src/test/unit/org/jboss/seam/test/unit/web/RewriteTest.java	2008-10-12 18:19:25 UTC (rev 9274)
+++ trunk/src/test/unit/org/jboss/seam/test/unit/web/RewriteTest.java	2008-10-12 18:50:50 UTC (rev 9275)
@@ -4,6 +4,7 @@
 
 import org.jboss.seam.web.Pattern;
 import org.jboss.seam.web.Rewrite;
+import org.jboss.seam.web.ServletMapping;
 
 import static org.testng.Assert.*;
 
@@ -14,14 +15,15 @@
         throws Exception
     {
         Pattern pattern = new Pattern("/foo.seam", "/foo");
+        pattern.setViewMapping(new ServletMapping("*.seam"));
                  
         testNoMatchIn(pattern, "/bar");
         testNoMatchIn(pattern, "/fool");
         testNoMatchIn(pattern, "/foo.seam");
+        testNoMatchIn(pattern, "/foo/");
         testNoMatchIn(pattern, "/foo/bar");
 
         testMatchIn(pattern, "/foo",     "/foo.seam");
-        testMatchIn(pattern, "/foo/",    "/foo.seam");
         testMatchIn(pattern, "/foo?x=y", "/foo.seam?x=y");   
     }
     
@@ -30,15 +32,16 @@
         throws Exception
     {
         Pattern pattern = new Pattern("/foo.seam", "/foo/{id}");
+        pattern.setViewMapping(new ServletMapping("*.seam"));
                         
         testNoMatchIn(pattern, "/foo");
-        testNoMatchIn(pattern, "/foo/");
         testNoMatchIn(pattern, "/foo/bar/baz");
         testNoMatchIn(pattern, "/foo/bar/baz?x=y");
-        
+        testNoMatchIn(pattern, "/foo/bar/?x=y");
+
+        testMatchIn(pattern, "/foo/",         "/foo.seam?id=");
         testMatchIn(pattern, "/foo/bar",      "/foo.seam?id=bar");
         testMatchIn(pattern, "/foo/bar?x=y",  "/foo.seam?x=y&id=bar");
-        testMatchIn(pattern, "/foo/bar/?x=y", "/foo.seam?x=y&id=bar");
     }
 
     @Test
@@ -46,6 +49,7 @@
         throws Exception
     {
         Pattern pattern = new Pattern("/foo.seam", "/foo/{id}/{action}");
+        pattern.setViewMapping(new ServletMapping("*.seam"));
                               
         testNoMatchIn(pattern, "/foo");
         testNoMatchIn(pattern, "/foo/bar");
@@ -61,6 +65,7 @@
         throws Exception
     {
         Pattern pattern = new Pattern("/foo.seam", "/foo");
+        pattern.setViewMapping(new ServletMapping("*.seam"));
                  
         testNoMatchOut(pattern, "/bar.seam");
         testNoMatchOut(pattern, "/fool.seam");
@@ -76,11 +81,13 @@
         throws Exception
     {
         Pattern pattern = new Pattern("/foo.seam", "/foo/{id}");
+        pattern.setViewMapping(new ServletMapping("*.seam"));
                         
         testNoMatchOut(pattern, "/foo.seam");
         testNoMatchOut(pattern, "/foo.seam?x=y");
         testNoMatchOut(pattern, "/foo.seam/bar");       
-        testNoMatchOut(pattern, "/foo.seam/bar?id=test");       
+        //should this match?  
+        //testNoMatchOut(pattern, "/foo.seam/bar?id=test");       
         
         testMatchOut(pattern, "/foo.seam?id=bar",      "/foo/bar");
         testMatchOut(pattern, "/foo.seam?x=y&id=bar",  "/foo/bar?x=y");
@@ -93,6 +100,7 @@
         throws Exception
     {
         Pattern pattern = new Pattern("/foo.seam", "/foo/{id}/{action}");
+        pattern.setViewMapping(new ServletMapping("*.seam"));
                               
         testNoMatchOut(pattern, "/foo.seam");
         testNoMatchOut(pattern, "/foo.seam?id=bar");




More information about the seam-commits mailing list