[gatein-commits] gatein SVN: r4168 - in portal/branches/navcontroller/component/web/controller/src: test/java/org/exoplatform/web/controller/router and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Sep 13 05:33:19 EDT 2010


Author: julien_viet
Date: 2010-09-13 05:33:19 -0400 (Mon, 13 Sep 2010)
New Revision: 4168

Added:
   portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RenderContext.java
   portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRenderContext.java
Modified:
   portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RequestParamDef.java
   portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java
   portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Router.java
   portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRequestParam.java
Log:
finished impl of request param support


Added: portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RenderContext.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RenderContext.java	                        (rev 0)
+++ portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RenderContext.java	2010-09-13 09:33:19 UTC (rev 4168)
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.web.controller.router;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public interface RenderContext
+{
+
+   void appendPath(char s);
+
+   void appendPath(String s);
+
+   void appendQueryParameter(String parameterName, String paramaterValue);
+
+}

Modified: portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RequestParamDef.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RequestParamDef.java	2010-09-13 09:30:45 UTC (rev 4167)
+++ portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RequestParamDef.java	2010-09-13 09:33:19 UTC (rev 4168)
@@ -50,33 +50,26 @@
       //
       PatternBuilder matchValue = new PatternBuilder();
       matchValue.expr("^");
-      boolean litteral = true;
+      int level = 0;
       for (char c : descriptor.getMatchValue().toCharArray())
       {
          switch (c)
          {
             case '{':
-               if (litteral)
+
+               if (level++ > 0)
                {
-                  litteral = false;
-               }
-               else
-               {
                   matchValue.expr('{');
                }
                break;
             case '}':
-               if (litteral)
+               if (--level > 0)
                {
-                  litteral = true;
-               }
-               else
-               {
                   matchValue.expr('}');
                }
                break;
             default:
-               if (litteral)
+               if (level == 0)
                {
                   matchValue.litteral(c);
                }

Modified: portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java	2010-09-13 09:30:45 UTC (rev 4167)
+++ portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java	2010-09-13 09:33:19 UTC (rev 4168)
@@ -74,55 +74,57 @@
     * Ok, so this is not the fastest way to do it, but for now it's OK, it's what is needed, we'll find
     * a way to optimize it later with some precompilation. 
     */
-   final String render(Map<QualifiedName, String> blah)
+   final void render(Map<QualifiedName, String> blah, RenderContext renderContext)
    {
       Route r = find(blah);
-      if (r == null)
+      if (r != null)
       {
-         return null;
+         r._render(blah, renderContext);
       }
-      else
-      {
-         if (r instanceof PatternRoute || r instanceof SegmentRoute)
-         {
-            StringBuilder sb = new StringBuilder();
-            r.render(blah, sb);
-            return sb.toString();
-         }
-         else
-         {
-            return "/";
-         }
-      }
    }
 
-   private void render(Map<QualifiedName, String> blah, StringBuilder sb)
+   private void _render(Map<QualifiedName, String> blah, RenderContext renderContext)
    {
-      if (parent != null)
+      if (parent != null && parent.parent != null)
       {
-         parent.render(blah, sb);
+         parent._render(blah, renderContext);
       }
 
       //
+      if (requestParamDefs.size() > 0)
+      {
+         for (RequestParamDef requestParamDef : requestParamDefs.values())
+         {
+            String s = blah.get(requestParamDef.getName());
+            renderContext.appendQueryParameter(requestParamDef.getMatchName(), s);
+         }
+      }
+
+      //
       if (this instanceof SegmentRoute)
       {
          SegmentRoute sr = (SegmentRoute)this;
-         sb.append('/').append(sr.name);
+         renderContext.appendPath('/');
+         renderContext.appendPath(sr.name);
       }
       else if (this instanceof PatternRoute)
       {
          PatternRoute pr = (PatternRoute)this;
-         sb.append('/');
+         renderContext.appendPath('/');
          int i = 0;
          while (i < pr.parameterNames.size())
          {
-            sb.append(pr.chunks.get(i));
+            renderContext.appendPath(pr.chunks.get(i));
             String value = blah.get(pr.parameterNames.get(i));
-            sb.append(value);
+            renderContext.appendPath(value);
             i++;
          }
-         sb.append(pr.chunks.get(i));
+         renderContext.appendPath(pr.chunks.get(i));
       }
+      else
+      {
+         renderContext.appendPath("/");
+      }
    }
 
    final Route find(Map<QualifiedName, String> blah)
@@ -145,6 +147,25 @@
          }
       }
 
+      // Match any request parameter
+      if (requestParamDefs.size() > 0)
+      {
+         for (RequestParamDef requestParamDef : requestParamDefs.values())
+         {
+            String a = blah.get(requestParamDef.name);
+            if (a != null)
+            {
+               if (requestParamDef.matchValue.matcher(a).matches())
+               {
+                  //
+                  abc.remove(requestParamDef.name);
+                  continue;
+               }
+            }
+            return null;
+         }
+      }
+
       // Match any pattern parameter
       if (this instanceof PatternRoute)
       {

Modified: portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Router.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Router.java	2010-09-13 09:30:45 UTC (rev 4167)
+++ portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Router.java	2010-09-13 09:33:19 UTC (rev 4168)
@@ -53,9 +53,16 @@
       root.append(routeMetaData);
    }
 
+   public void render(Map<QualifiedName, String> parameters, RenderContext renderContext)
+   {
+      root.render(parameters, renderContext);
+   }
+
    public String render(Map<QualifiedName, String> parameters)
    {
-      return root.render(parameters);
+      SimpleRenderContext renderContext = new SimpleRenderContext();
+      render(parameters, renderContext);
+      return renderContext.getPath();
    }
 
    public Map<QualifiedName, String> route(String path) throws IOException

Added: portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRenderContext.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRenderContext.java	                        (rev 0)
+++ portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRenderContext.java	2010-09-13 09:33:19 UTC (rev 4168)
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.web.controller.router;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class SimpleRenderContext implements RenderContext
+{
+
+   /** . */
+   private StringBuilder sb = null;
+
+   /** . */
+   private Map<String, String> queryParams = Collections.emptyMap();
+
+   public String getPath()
+   {
+      return sb != null ? sb.toString() : null;
+   }
+
+   public Map<String, String> getQueryParams()
+   {
+      return queryParams;
+   }
+
+   public void appendPath(char c)
+   {
+      if (sb == null)
+      {
+         sb = new StringBuilder();
+      }
+      sb.append(c);
+   }
+
+   public void appendPath(String s)
+   {
+      if (sb == null)
+      {
+         sb = new StringBuilder();
+      }
+      sb.append(s);
+   }
+
+   public void appendQueryParameter(String parameterName, String paramaterValue)
+   {
+      if (queryParams.isEmpty())
+      {
+         queryParams = new HashMap<String, String>();
+      }
+      queryParams.put(parameterName, paramaterValue);
+   }
+
+}

Modified: portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRequestParam.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRequestParam.java	2010-09-13 09:30:45 UTC (rev 4167)
+++ portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRequestParam.java	2010-09-13 09:33:19 UTC (rev 4168)
@@ -39,8 +39,17 @@
       RouterDescriptor descriptor = new RouterDescriptor();
       descriptor.addRoute(new RouteDescriptor("/").addRequestParam("foo", "a", "a"));
       Router router = new Router(descriptor);
+
+      //
       assertNull(router.route("/"));
       assertEquals(Collections.singletonMap(QualifiedName.parse("foo"), "a"), router.route("/", Collections.singletonMap("a", new String[]{"a"})));
+
+      //
+      assertNull(router.render(Collections.<QualifiedName, String>emptyMap()));
+      SimpleRenderContext renderContext = new SimpleRenderContext();
+      router.render(Collections.singletonMap(QualifiedName.parse("foo"), "a"), renderContext);
+      assertEquals("/", renderContext.getPath());
+      assertEquals(Collections.singletonMap("a", "a"), renderContext.getQueryParams());
    }
 
    public void testSegment() throws Exception
@@ -48,19 +57,61 @@
       RouterDescriptor descriptor = new RouterDescriptor();
       descriptor.addRoute(new RouteDescriptor("/a").addRequestParam("foo", "a", "a"));
       Router router = new Router(descriptor);
+
+      //
       assertNull(router.route("/a"));
       assertEquals(Collections.singletonMap(QualifiedName.parse("foo"), "a"), router.route("/a", Collections.singletonMap("a", new String[]{"a"})));
+
+      //
+      assertNull(router.render(Collections.<QualifiedName, String>emptyMap()));
+      SimpleRenderContext renderContext = new SimpleRenderContext();
+      router.render(Collections.singletonMap(QualifiedName.parse("foo"), "a"), renderContext);
+      assertEquals("/a", renderContext.getPath());
+      assertEquals(Collections.singletonMap("a", "a"), renderContext.getQueryParams());
    }
 
+   public void testValuePattern() throws Exception
+   {
+      RouterDescriptor descriptor = new RouterDescriptor();
+      descriptor.addRoute(new RouteDescriptor("/a").addRequestParam("foo", "a", "{[0-9]+}"));
+      Router router = new Router(descriptor);
+
+      //
+      assertNull(router.route("/a"));
+      assertNull(router.route("/a", Collections.singletonMap("a", new String[]{"a"})));
+      assertEquals(Collections.singletonMap(QualifiedName.parse("foo"), "0123"), router.route("/a", Collections.singletonMap("a", new String[]{"0123"})));
+
+      //
+      assertNull(router.render(Collections.<QualifiedName, String>emptyMap()));
+      assertNull(router.render(Collections.singletonMap(QualifiedName.parse("foo"), "a")));
+      SimpleRenderContext renderContext = new SimpleRenderContext();
+      router.render(Collections.singletonMap(QualifiedName.parse("foo"), "12"), renderContext);
+      assertEquals("/a", renderContext.getPath());
+      assertEquals(Collections.singletonMap("a", "12"), renderContext.getQueryParams());
+   }
+
    public void testPrecedence() throws Exception
    {
       RouterDescriptor descriptor = new RouterDescriptor();
       descriptor.addRoute(new RouteDescriptor("/a").addRequestParam("foo", "a", "a"));
       descriptor.addRoute(new RouteDescriptor("/a").addRequestParam("bar", "b", "b"));
       Router router = new Router(descriptor);
+
+      //
       assertNull(router.route("/a"));
       assertEquals(Collections.singletonMap(QualifiedName.parse("foo"), "a"), router.route("/a", Collections.singletonMap("a", new String[]{"a"})));
       assertEquals(Collections.singletonMap(QualifiedName.parse("bar"), "b"), router.route("/a", Collections.singletonMap("b", new String[]{"b"})));
+
+      //
+      assertNull(router.render(Collections.<QualifiedName, String>emptyMap()));
+      SimpleRenderContext renderContext1 = new SimpleRenderContext();
+      router.render(Collections.singletonMap(QualifiedName.parse("foo"), "a"), renderContext1);
+      assertEquals("/a", renderContext1.getPath());
+      assertEquals(Collections.singletonMap("a", "a"), renderContext1.getQueryParams());
+      SimpleRenderContext renderContext2 = new SimpleRenderContext();
+      router.render(Collections.singletonMap(QualifiedName.parse("bar"), "b"), renderContext2);
+      assertEquals("/a", renderContext2.getPath());
+      assertEquals(Collections.singletonMap("b", "b"), renderContext2.getQueryParams());
    }
 
    public void testInheritance() throws Exception
@@ -68,6 +119,8 @@
       RouterDescriptor descriptor = new RouterDescriptor();
       descriptor.addRoute(new RouteDescriptor("/a").addRequestParam("foo", "a", "a").addChild(new RouteDescriptor("/b").addRequestParam("bar", "b", "b")));
       Router router = new Router(descriptor);
+
+      //
       assertNull(router.route("/a"));
       assertEquals(Collections.singletonMap(QualifiedName.parse("foo"), "a"), router.route("/a", Collections.singletonMap("a", new String[]{"a"})));
       assertNull(router.route("/a/b"));
@@ -78,6 +131,21 @@
       expectedParameters.put(QualifiedName.parse("foo"), "a");
       expectedParameters.put(QualifiedName.parse("bar"), "b");
       assertEquals(expectedParameters, router.route("/a/b", requestParameters));
+
+      //
+      assertNull(router.render(Collections.<QualifiedName, String>emptyMap()));
+      SimpleRenderContext renderContext1 = new SimpleRenderContext();
+      router.render(Collections.singletonMap(QualifiedName.parse("foo"), "a"), renderContext1);
+      assertEquals("/a", renderContext1.getPath());
+      assertEquals(Collections.singletonMap("a", "a"), renderContext1.getQueryParams());
+
+      SimpleRenderContext renderContext2 = new SimpleRenderContext();
+      router.render(expectedParameters, renderContext2);
+      assertEquals("/a/b", renderContext2.getPath());
+      Map<String, String> expectedRequestParameters = new HashMap<String, String>();
+      expectedRequestParameters.put("a", "a");
+      expectedRequestParameters.put("b", "b");
+      assertEquals(expectedRequestParameters, renderContext2.getQueryParams());
    }
 
 }



More information about the gatein-commits mailing list