Author: julien_viet
Date: 2010-09-01 09:54:55 -0400 (Wed, 01 Sep 2010)
New Revision: 4011
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/controller.xml
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/AbstractTestController.java
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java
Log:
update controller parsing
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/controller.xml
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/controller.xml 2010-09-01
13:40:23 UTC (rev 4010)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/controller.xml 2010-09-01
13:54:55 UTC (rev 4011)
@@ -1,24 +1,35 @@
<router>
- <route match="/private/{gtn:sitename}"
to="SiteController">
+ <route match="/private/{gtn:sitename}">
+ <parameter name="gtn:controller" value="site"/>
<parameter name="gtn:sitetype" value="portal"/>
</route>
- <route match="/groups/{gtn:sitename}" to="SiteController">
+ <route match="/groups/{gtn:sitename}">
+ <parameter name="gtn:controller" value="site"/>
<parameter name="gtn:sitetype" value="group"/>
</route>
- <route match="/users/{gtn:sitename}" to="SiteController">
+ <route match="/users/{gtn:sitename}">
+ <parameter name="gtn:controller" value="site"/>
<parameter name="gtn:sitetype" value="user"/>
</route>
+ <route match="/toto">
+ <route match="/titi"/>
+ </route>
+
<route match="/" to="foo"/>
<route match="/public" to="foo"/>
- <route match="/download" to="DownloadController"/>
+ <route match="/download">
+ <parameter name="gtn:controller" value="download"/>
+ </route>
- <route match="/upload" to="UploadController"/>
+ <route match="/upload">
+ <parameter name="gtn:controller" value="upload"/>
+ </route>
</router>
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-01
13:40:23 UTC (rev 4010)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java 2010-09-01
13:54:55 UTC (rev 4011)
@@ -146,89 +146,109 @@
{
String path = context.getPath();
- // Remove any leading '/'
- while (path.length() > 0 && path.charAt(0) == '/')
+ // Anything that does not begin with '/' returns null
+ if (path.length() > 0 && path.charAt(0) == '/')
{
- path = path.substring(1);
- }
-
- //
- if (path.isEmpty())
- {
- if (controllerRef != null)
+ // The '/' means the current controller if any, otherwise it may be
processed by the pattern matching
+ if (path.length() == 1)
{
- return new ProcessResponse(controllerRef, context.getPath(),
context.getParameters());
+ if (controllerRef != null)
+ {
+ return new ProcessResponse(controllerRef, context.getPath(),
context.getParameters());
+ }
}
- }
- else
- {
- int pos = path.indexOf('/');
- String segment;
- if (pos == -1)
- {
- segment = path;
- }
else
{
- segment = path.substring(0, pos);
- }
- Route route = simpleRoutes.get(segment);
- if (route != null)
- {
- Map<QualifiedName, String[]> parameters = context.getParameters();
- if (route.routeParameters.size() > 0)
+ // Find the next '/' for determining the segment and next path
+ int pos = path.indexOf('/', 1);
+ if (pos == -1)
{
- parameters = new HashMap<QualifiedName, String[]>(parameters);
- // julien : do a safe put all here on String[]
- parameters.putAll(route.routeParameters);
+ pos = path.length();
}
- ControllerContext nextContext = new ControllerContext(
- path.substring(segment.length()),
- parameters
- );
- ProcessResponse response = route.route(nextContext);
- if (response != null)
+
+ String segment = path.substring(1, pos);
+
+ // Try to find a route for the segment
+ Route route = simpleRoutes.get(segment);
+ if (route != null)
{
- return response;
- }
- }
- }
+ Map<QualifiedName, String[]> parameters = context.getParameters();
- // Try to find a pattern matching route
- for (PatternRoute route : patternRoutes)
- {
- Matcher matcher = route.pattern.matcher(path);
+ // Update parameters
+ if (route.routeParameters.size() > 0)
+ {
+ parameters = new HashMap<QualifiedName, String[]>(parameters);
+ // julien : do a safe put all here on String[]
+ parameters.putAll(route.routeParameters);
+ }
- boolean matched = false;
- if (matcher.find())
- {
- int end = matcher.end();
- if (end >= path.length())
- {
- matched = true;
+ // Determine next path
+ String nextPath;
+ if (pos == path.length())
+ {
+ nextPath = "/";
+ }
+ else
+ {
+ nextPath = path.substring(pos);
+ }
+
+ // Build next controller context
+ ControllerContext nextContext = new ControllerContext(nextPath,
parameters);
+
+ // Delegate the process to the next route
+ ProcessResponse response = route.route(nextContext);
+
+ // If we do have a response we return it
+ if (response != null)
+ {
+ return response;
+ }
}
- else if (path.charAt(end) == '/')
- {
- matched = true;
- }
}
- // We match
- if (matched)
+ // Try to find a pattern matching route
+ for (PatternRoute route : patternRoutes)
{
- Map<QualifiedName, String[]> parameters = new HashMap<QualifiedName,
String[]>(context.getParameters());
- // julien : do a safe put all here on String[]
- parameters.putAll(route.routeParameters);
- int group = 1;
- for (QualifiedName parameterName : route.parameterNames)
+ Matcher matcher = route.pattern.matcher(path.substring(1));
+
+ // We match
+ if (matcher.find())
{
- parameters.put(parameterName, new String[]{matcher.group(group++)});
+ // Update parameters
+ Map<QualifiedName, String[]> parameters = new
HashMap<QualifiedName, String[]>(context.getParameters());
+ // julien : do a safe put all here on String[]
+ parameters.putAll(route.routeParameters);
+ int group = 1;
+ for (QualifiedName parameterName : route.parameterNames)
+ {
+ parameters.put(parameterName, new String[]{matcher.group(group++)});
+ }
+
+ // Build next controller context
+ int nextPos = matcher.end() + 1;
+ String nextPath;
+ if (path.length() == nextPos)
+ {
+ nextPath = "/";
+ }
+ else
+ {
+ nextPath = path.substring(nextPos);
+ }
+
+ //
+ ControllerContext nextContext = new ControllerContext(nextPath,
parameters);
+
+ // Delegate to next route
+ ProcessResponse response = route.route(nextContext);
+
+ // If we do have a response we return it
+ if (response != null)
+ {
+ return response;
+ }
}
- ControllerContext nextContext = new ControllerContext(
- path.substring(matcher.end()),
- parameters
- );
- return route.route(nextContext);
}
}
Modified:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/AbstractTestController.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/AbstractTestController.java 2010-09-01
13:40:23 UTC (rev 4010)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/AbstractTestController.java 2010-09-01
13:54:55 UTC (rev 4011)
@@ -38,7 +38,7 @@
public void assertProcessResponse(String controllerId, String path,
Map<QualifiedName, String[]> parameters, ControllerResponse response)
{
- assertNotNull(response);
+ assertNotNull("Was not expecting a null response", response);
assertEquals(ProcessResponse.class, response.getClass());
assertEquals(controllerId, ((ProcessResponse)response).getControllerId());
// todo check path and define semantic
Modified:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java 2010-09-01
13:40:23 UTC (rev 4010)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java 2010-09-01
13:54:55 UTC (rev 4011)
@@ -40,7 +40,7 @@
Router router = new Router(routerMD);
//
- assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("")));
+ assertNull(router.process(new ControllerContext("")));
//
assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/")));
@@ -62,10 +62,10 @@
assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/a")));
//
- assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("a")));
+ assertNull(router.process(new ControllerContext("a")));
//
- assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("a/")));
+ assertNull(router.process(new ControllerContext("a/")));
//
assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/a/")));
@@ -93,7 +93,7 @@
Router router = new Router( routerMD);
//
- assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("a/b")));
+ assertNull(router.process(new ControllerContext("a/b")));
//
assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/a/b")));
@@ -102,7 +102,7 @@
assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/a/b/")));
//
- assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("a/b/")));
+ assertNull(router.process(new ControllerContext("a/b/")));
//
assertNull(router.process(new ControllerContext("")));
@@ -141,7 +141,7 @@
assertProcessResponse("ref1", Collections.singletonMap(new
QualifiedName("p"), new String[]{"a"}), router.process(new
ControllerContext("/a")));
//
- assertProcessResponse("ref1", Collections.singletonMap(new
QualifiedName("p"), new String[]{"a"}), router.process(new
ControllerContext("a")));
+ assertNull(router.process(new ControllerContext("a")));
//
assertProcessResponse("ref1", Collections.singletonMap(new
QualifiedName("p"), new String[]{"a/b"}), router.process(new
ControllerContext("/a/b")));
@@ -157,7 +157,7 @@
assertProcessResponse("ref1", Collections.singletonMap(new
QualifiedName("p"), new String[]{"a"}), router.process(new
ControllerContext("/a")));
//
- assertProcessResponse("ref1", Collections.singletonMap(new
QualifiedName("p"), new String[]{"a"}), router.process(new
ControllerContext("a")));
+ assertNull(router.process(new ControllerContext("a")));
//
assertNull(router.process(new ControllerContext("/ab")));
@@ -174,7 +174,7 @@
Router router = new Router(routerMD);
//
- assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("a")));
+ assertNull(router.process(new ControllerContext("a")));
//
assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/a")));