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

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Mar 3 16:59:04 EST 2008


Author: norman.richards at jboss.com
Date: 2008-03-03 16:59:04 -0500 (Mon, 03 Mar 2008)
New Revision: 7500

Modified:
   trunk/src/main/org/jboss/seam/navigation/Page.java
   trunk/src/main/org/jboss/seam/navigation/Pages.java
   trunk/src/main/org/jboss/seam/web/RewriteFilter.java
Log:
better rewrite config

Modified: trunk/src/main/org/jboss/seam/navigation/Page.java
===================================================================
--- trunk/src/main/org/jboss/seam/navigation/Page.java	2008-03-03 21:58:23 UTC (rev 7499)
+++ trunk/src/main/org/jboss/seam/navigation/Page.java	2008-03-03 21:59:04 UTC (rev 7500)
@@ -12,6 +12,7 @@
 import org.jboss.seam.core.Interpolator;
 import org.jboss.seam.core.ResourceLoader;
 import org.jboss.seam.security.Identity;
+import org.jboss.seam.web.Pattern;
 
 /**
  * Metadata about page actions, page parameters, action navigation,
@@ -37,6 +38,7 @@
    private ProcessControl processControl = new ProcessControl();
    private ConversationIdParameter conversationIdParameter;
    private String eventType;
+   private List<Pattern> rewritePatterns = new ArrayList<Pattern>();
    
    /**
     * The scheme (http/https) required by this page.
@@ -345,5 +347,35 @@
    {
       this.eventType = eventType;
    }
-      
+
+   
+    public List<Pattern> getRewritePatterns() {
+        return rewritePatterns;
+    }
+
+    //public void setRewritePatterns(List<String> rewritePatterns) {
+    //    this.rewritePatterns = rewritePatterns;
+    //}
+
+    public void addRewritePattern(String value) {
+        Pattern pattern = new Pattern(externalUrl(), 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;
+    }
+
 }

Modified: trunk/src/main/org/jboss/seam/navigation/Pages.java
===================================================================
--- trunk/src/main/org/jboss/seam/navigation/Pages.java	2008-03-03 21:58:23 UTC (rev 7499)
+++ trunk/src/main/org/jboss/seam/navigation/Pages.java	2008-03-03 21:59:04 UTC (rev 7500)
@@ -6,6 +6,7 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -35,6 +36,7 @@
 import org.jboss.seam.annotations.Install;
 import org.jboss.seam.annotations.Name;
 import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Startup;
 import org.jboss.seam.annotations.intercept.BypassInterceptors;
 import org.jboss.seam.contexts.Contexts;
 import org.jboss.seam.core.Events;
@@ -65,19 +67,21 @@
 @BypassInterceptors
 @Name("org.jboss.seam.navigation.pages")
 @Install(precedence=BUILT_IN, classDependencies="javax.faces.context.FacesContext")
+ at Startup
 public class Pages
 {   
    private static final LogProvider log = Logging.getLogProvider(Pages.class);
-   
-   private Map<String, Page> pagesByViewId = Collections.synchronizedMap( new HashMap<String, Page>() );   
-   private Map<String, List<Page>> pageStacksByViewId = Collections.synchronizedMap( new HashMap<String, List<Page>>() );   
+
    private String noConversationViewId;
    private String loginViewId;
-   private Map<String, ConversationIdParameter> conversations = Collections.synchronizedMap( new HashMap<String, ConversationIdParameter>() );
      
    private Integer httpPort;
    private Integer httpsPort;
    
+   private Map<String, Page> pagesByViewId;  
+   private Map<String, List<Page>> pageStacksByViewId;   
+   private Map<String, ConversationIdParameter> conversations; 
+   
    private String[] resources = { "/WEB-INF/pages.xml" };
  
    private SortedSet<String> wildcardViewIds = new TreeSet<String>( 
@@ -95,19 +99,19 @@
    @Create
    public void initialize()
    {
-      for (String resource: resources)
-      {
-         InputStream stream = ResourceLoader.instance().getResourceAsStream(resource);      
-         if (stream==null)
-         {
-            log.info("no pages.xml file found: " + resource);
-         }
-         else
-         {
-            log.debug("reading pages.xml file: " + resource);
-            parse(stream);
-         }
-      }
+       pagesByViewId = Collections.synchronizedMap(new HashMap<String, Page>());   
+       pageStacksByViewId = Collections.synchronizedMap(new HashMap<String, List<Page>>());   
+       conversations = Collections.synchronizedMap(new HashMap<String, ConversationIdParameter>());
+
+       for (String resource: resources) {
+           InputStream stream = ResourceLoader.instance().getResourceAsStream(resource);      
+           if (stream==null) {
+               log.info("no pages.xml file found: " + resource);
+           } else {
+               log.debug("reading pages.xml file: " + resource);
+               parse(stream);
+           }
+       }
    }
    /**
     * Run any navigation rule defined in pages.xml
@@ -572,7 +576,7 @@
 
    protected void notLoggedIn()
    {
-//    TODO - Deprecated, remove for next major release
+      //    TODO - Deprecated, remove for next major release
       Events.instance().raiseEvent("org.jboss.seam.notLoggedIn");
       Events.instance().raiseEvent(Identity.EVENT_NOT_LOGGED_IN);
    }
@@ -1047,6 +1051,12 @@
       ConversationIdParameter param = conversations.get( element.attributeValue("conversation") );
       if (param != null) page.setConversationIdParameter(param);
       
+
+      List<Element> patterns = element.elements("rewrite");
+      for (Element pattern: patterns) {
+           page.addRewritePattern(pattern.attributeValue("pattern"));
+      }
+      
       Element eventElement = element.element("raise-event");
       if (eventElement!=null)
       {
@@ -1513,4 +1523,8 @@
             getCurrentViewId().startsWith("/debug.");
    }
    
+   
+   public Collection<String> getKnownViewIds() {
+       return pagesByViewId.keySet();
+   }
 }

Modified: trunk/src/main/org/jboss/seam/web/RewriteFilter.java
===================================================================
--- trunk/src/main/org/jboss/seam/web/RewriteFilter.java	2008-03-03 21:58:23 UTC (rev 7499)
+++ trunk/src/main/org/jboss/seam/web/RewriteFilter.java	2008-03-03 21:59:04 UTC (rev 7500)
@@ -4,11 +4,9 @@
 import static org.jboss.seam.annotations.Install.BUILT_IN;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Comparator;
-import java.util.Map;
-import java.util.TreeSet;
-import java.util.Map.Entry;
+import java.util.List;
 
 import javax.servlet.FilterChain;
 import javax.servlet.RequestDispatcher;
@@ -18,6 +16,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.jboss.seam.Seam;
 import org.jboss.seam.annotations.Install;
 import org.jboss.seam.annotations.Name;
 import org.jboss.seam.annotations.Scope;
@@ -25,6 +24,8 @@
 import org.jboss.seam.annotations.web.Filter;
 import org.jboss.seam.log.LogProvider;
 import org.jboss.seam.log.Logging;
+import org.jboss.seam.navigation.Page;
+import org.jboss.seam.navigation.Pages;
 
 @Scope(APPLICATION)
 @Name("org.jboss.seam.web.rewriteFilter")
@@ -36,38 +37,24 @@
 {
     private static LogProvider log = Logging.getLogProvider(RewriteFilter.class);
 
-    Collection<Pattern> patterns = null; 
-
-    // need to extract this from Pages!
-    public void setPatterns(Map<String,String> patternMap) {        
-        patterns = new TreeSet<Pattern>(new Comparator<Pattern>() {
-            public int compare(Pattern p1, Pattern p2) {
-                return p2.pattern.compareTo(p1.pattern);
-            }
-        });
-                
-        for(Entry<String, String> entry: patternMap.entrySet()) {
-            patterns.add(new Pattern(entry.getValue(), entry.getKey()));
-        }
-        
-        log.info("Rewrite patterns: " + patterns);
-    }
-
     public void doFilter(ServletRequest request, 
                          ServletResponse response, 
                          FilterChain chain) 
         throws IOException, 
                ServletException 
     {
-        if (patterns != null) {
-            if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
-                response = new RewritingResponse((HttpServletRequest) request,
-                        (HttpServletResponse)response,
-                        patterns);
-                process((HttpServletRequest) request, (HttpServletResponse) response);
-            }
+        List<Pattern> allPatterns = getAllPatterns();
+        
+        if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
+            response = new RewritingResponse((HttpServletRequest) request,
+                    (HttpServletResponse)response,
+                    allPatterns);
+            process((HttpServletRequest) request, 
+                    (HttpServletResponse) response,
+                    allPatterns);
         }
         
+        
         if (!response.isCommitted()) {
             chain.doFilter(request, response);
         }
@@ -76,16 +63,17 @@
     
     @SuppressWarnings("unchecked")
     public void process(HttpServletRequest request, 
-                        HttpServletResponse response)
+                        HttpServletResponse response, List<Pattern> patterns)
         throws IOException, 
                ServletException 
     {
         String fullPath = request.getRequestURI();
         log.info("incoming URL is " + fullPath);
+        log.info("known patterns are " + patterns);
 
         String localPath = strip(fullPath, request.getContextPath());
        
-        Rewrite rewrite = matchPatterns(localPath);
+        Rewrite rewrite = matchPatterns(localPath, patterns);
         if (rewrite!=null) {
             String newPath = rewrite.rewrite();
             
@@ -99,7 +87,7 @@
     }
 
 
-    private Rewrite matchPatterns(String localPath) {
+    private Rewrite matchPatterns(String localPath, List<Pattern> patterns) {
         for (Pattern pattern: patterns) {
             Rewrite rewrite = pattern.matchIncoming(localPath);
             if (rewrite!=null && rewrite.isMatch()) {
@@ -116,5 +104,24 @@
             return fullPath;
         }
     }
+    
+    
+    private List<Pattern> getAllPatterns() {
+        List<Pattern> allPatterns = new ArrayList<Pattern>();
+        
+        Pages pages = (Pages) getServletContext().getAttribute(Seam.getComponentName(Pages.class));
+        if (pages != null) {
+            Collection<String> ids = pages.getKnownViewIds();
+
+            for (String id: ids) {
+                 Page page = pages.getPage(id);
+                 allPatterns.addAll(page.getRewritePatterns());
+            }
+        } else {
+            log.warn("Pages is null for incoming request!");
+        }
+        
+        return allPatterns;
+    }
 }
 




More information about the seam-commits mailing list