Author: julien_viet
Date: 2010-08-19 08:53:40 -0400 (Thu, 19 Aug 2010)
New Revision: 3869
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/Controller.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/ControllerContext.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/QualifiedName.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/ControllerRefMetaData.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouterMetaData.java
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/protocol/
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/ControllerResponse.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/ProcessResponse.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/RedirectResponse.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternRoute.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/main/java/org/exoplatform/web/controller/router/SimpleRoute.java
portal/branches/navcontroller/component/web/controller/src/test/
portal/branches/navcontroller/component/web/controller/src/test/java/
portal/branches/navcontroller/component/web/controller/src/test/java/org/
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/
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/TestBuildRoute.java
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/TestPortalConfiguration.java
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRender.java
Modified:
portal/branches/navcontroller/webui/framework/pom.xml
Log:
front controller prototype commit
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/Controller.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/Controller.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/Controller.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,45 @@
+/*
+ * 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;
+
+import org.exoplatform.web.controller.protocol.ControllerResponse;
+
+import java.io.IOException;
+
+/**
+ * The controller is responsible for processing a controller context.
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public interface Controller
+{
+
+ /**
+ * Process an incoming context and returns a response.
+ *
+ * @param controllerContext the context to process
+ * @return the response
+ * @throws IOException any io exception
+ */
+ ControllerResponse process(ControllerContext controllerContext) throws IOException;
+
+
+}
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/ControllerContext.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/ControllerContext.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/ControllerContext.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,66 @@
+/*
+ * 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;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * <p>The controller context captures the state of a stage in the decoding of a
request. The controller context
+ * should not be mutated, if a new controller with modified state is needed, a new
controller should be created
+ * with the new state.</p>
+ *
+ * <p>The context augments the http request and response with additional state.
Usually this state is obtained
+ * from a processing context and is used by a controller to create a response.</p>
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class ControllerContext
+{
+
+ /** The current path. */
+ final String path;
+
+ /** The current parameters. */
+ final Map<QualifiedName, String[]> parameters;
+
+ public ControllerContext(String path)
+ {
+ this.path = path;
+ this.parameters = new HashMap<QualifiedName, String[]>();
+ }
+
+ public ControllerContext(String path, Map<QualifiedName, String[]> parameters)
+ {
+ this.path = path;
+ this.parameters = parameters;
+ }
+
+ public Map<QualifiedName, String[]> getParameters()
+ {
+ return parameters;
+ }
+
+ public String getPath()
+ {
+ return path;
+ }
+}
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/QualifiedName.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/QualifiedName.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/QualifiedName.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,105 @@
+/*
+ * 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;
+
+/**
+ * A qualified name that is a qualifier and a name.
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class QualifiedName
+{
+
+ /** . */
+ private final String qualifier;
+
+ /** . */
+ private final String name;
+
+ /**
+ * Creates a qualified name with an empty qualifier.
+ *
+ * @param name the name
+ */
+ public QualifiedName(String name)
+ {
+ this("", name);
+ }
+
+ /**
+ * Creates a qualified name.
+ *
+ * @param qualifier the qualifier
+ * @param name the name
+ */
+ public QualifiedName(String qualifier, String name)
+ {
+ if (qualifier == null)
+ {
+ throw new NullPointerException("No null prefix accepted");
+ }
+ if (name == null)
+ {
+ throw new NullPointerException("No null prefix accepted");
+ }
+
+ //
+ this.qualifier = qualifier;
+ this.name = name;
+ }
+
+ public String getQualifier()
+ {
+ return qualifier;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return qualifier.hashCode() ^ name.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj == this)
+ {
+ return true;
+ }
+ if (obj instanceof QualifiedName)
+ {
+ QualifiedName that = (QualifiedName)obj;
+ return qualifier.equals(that.qualifier) && name.equals(that.name);
+ }
+ return false;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "QName[prefix=" + qualifier + ",name=" + name +
"]";
+ }
+}
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/ControllerRefMetaData.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/ControllerRefMetaData.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/ControllerRefMetaData.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,70 @@
+/*
+ * 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.metadata;
+
+import org.exoplatform.web.controller.QualifiedName;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class ControllerRefMetaData
+{
+
+ /** . */
+ private String ref;
+
+ /** . */
+ private final Map<QualifiedName, String[]> parameters;
+
+ public ControllerRefMetaData()
+ {
+ this(null);
+ }
+
+ public ControllerRefMetaData(String ref)
+ {
+ this.ref = ref;
+ this.parameters = new HashMap<QualifiedName, String[]>();
+ }
+
+ public String getRef()
+ {
+ return ref;
+ }
+
+ public void setRef(String ref)
+ {
+ this.ref = ref;
+ }
+
+ public void setParameter(QualifiedName name, String... value)
+ {
+ parameters.put(name, value);
+ }
+
+ public Map<QualifiedName, String[]> getParameters()
+ {
+ return parameters;
+ }
+}
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouterMetaData.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouterMetaData.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/RouterMetaData.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,59 @@
+/*
+ * 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.metadata;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class RouterMetaData
+{
+
+ /** . */
+ private final Map<String, ControllerRefMetaData> routes;
+
+ public RouterMetaData()
+ {
+ this.routes = new HashMap<String, ControllerRefMetaData>();
+ }
+
+ public void addRoute(String pathTemplate, ControllerRefMetaData controller)
+ {
+ if (pathTemplate == null)
+ {
+ throw new NullPointerException();
+ }
+ if (controller == null)
+ {
+ throw new NullPointerException();
+ }
+
+ //
+ routes.put(pathTemplate, controller);
+ }
+
+ public Map<String, ControllerRefMetaData> getRoutes()
+ {
+ return routes;
+ }
+}
Added:
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
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/metadata/controller.xml 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,24 @@
+<router>
+
+ <route match="/private/{gtn:sitename}"
to="SiteController">
+ <parameter name="gtn:sitetype" value="portal"/>
+ </route>
+
+ <route match="/groups/{gtn:sitename}" to="SiteController">
+ <parameter name="gtn:sitetype" value="group"/>
+ </route>
+
+ <route match="/users/{gtn:sitename}" to="SiteController">
+ <parameter name="gtn:sitetype" value="user"/>
+ </route>
+
+ <route match="/" to="foo"/>
+
+ <route match="/public" to="foo"/>
+
+ <route match="/download" to="DownloadController"/>
+
+ <route match="/upload" to="UploadController"/>
+
+</router>
+
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/ControllerResponse.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/ControllerResponse.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/ControllerResponse.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,33 @@
+/*
+ * 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.protocol;
+
+/**
+ * The base response returned by a controller processing.
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public abstract class ControllerResponse
+{
+
+
+
+}
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/ProcessResponse.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/ProcessResponse.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/ProcessResponse.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,87 @@
+/*
+ * 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.protocol;
+
+import org.exoplatform.web.controller.QualifiedName;
+
+import java.util.Collections;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public final class ProcessResponse extends ControllerResponse
+{
+
+ /** . */
+ private final String controllerId;
+
+ /** . */
+ private final String path;
+
+ /** . */
+ private final Map<QualifiedName, String[]> parameters;
+
+ public ProcessResponse(String controllerId)
+ {
+ this(controllerId, null, Collections.<QualifiedName, String[]>emptyMap());
+ }
+
+ public ProcessResponse(String controllerId, String path)
+ {
+ this(controllerId, path, Collections.<QualifiedName, String[]>emptyMap());
+ }
+
+ public ProcessResponse(String controllerId, Map<QualifiedName, String[]>
parameters)
+ {
+ this(controllerId, null, parameters);
+ }
+
+ public ProcessResponse(String controllerId, String path, Map<QualifiedName,
String[]> parameters)
+ {
+ if (controllerId == null)
+ {
+ throw new NullPointerException();
+ }
+ if (parameters == null)
+ {
+ throw new NullPointerException();
+ }
+ this.controllerId = controllerId;
+ this.path = path;
+ this.parameters = parameters;
+ }
+
+ public String getPath()
+ {
+ return path;
+ }
+
+ public String getControllerId()
+ {
+ return controllerId;
+ }
+
+ public Map<QualifiedName, String[]> getParameters()
+ {
+ return parameters;
+ }
+}
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/RedirectResponse.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/RedirectResponse.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/protocol/RedirectResponse.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,47 @@
+/*
+ * 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.protocol;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class RedirectResponse extends ControllerResponse
+{
+
+ /** . */
+ private final String url;
+
+ public RedirectResponse(String url)
+ {
+ if (url == null)
+ {
+ throw new NullPointerException("No null URL accepted");
+ }
+
+ //
+ this.url = url;
+ }
+
+ public String getURL()
+ {
+ return url;
+ }
+}
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,70 @@
+/*
+ * 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.regex.Pattern;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class PatternBuilder
+{
+
+ /** . */
+ private static final char[] TABLE = "0123456789ABCDEF".toCharArray();
+
+ /** . */
+ private final StringBuilder buffer = new StringBuilder();
+
+ public void appendExpression(String s)
+ {
+ buffer.append(s);
+ }
+
+ public void append(String s, int from, int to)
+ {
+ for (int i = from;i < to;i++)
+ {
+ char c = s.charAt(i);
+ append(c);
+ }
+ }
+
+ public void append(String s, int from)
+ {
+ append(s, from, s.length());
+ }
+
+ public void append(char c)
+ {
+ buffer.append("\\u");
+ buffer.append(TABLE[(c & 0xF000) >> 12]);
+ buffer.append(TABLE[(c & 0x0F00) >> 8]);
+ buffer.append(TABLE[(c & 0x00F0) >> 4]);
+ buffer.append(TABLE[c & 0x000F]);
+ }
+
+ public Pattern build()
+ {
+ return Pattern.compile(buffer.toString());
+ }
+
+}
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternRoute.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternRoute.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternRoute.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,63 @@
+/*
+ * 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 org.exoplatform.web.controller.QualifiedName;
+
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class PatternRoute extends Route
+{
+
+ /** . */
+ final Pattern pattern;
+
+ /** . */
+ final List<QualifiedName> parameterNames;
+
+ /** . */
+ final List<Pattern> parameterPatterns;
+
+ /** . */
+ final List<String> chunks;
+
+ public PatternRoute(
+ Pattern pattern,
+ List<QualifiedName> parameterNames,
+ List<Pattern> parameterPatterns,
+ List<String> chunks)
+ {
+ if (chunks.size() != parameterNames.size() + 1)
+ {
+ throw new AssertionError("Was expecting chunk size " + chunks.size() +
" to be equals to " + parameterNames.size() + 1);
+ }
+
+ //
+ this.pattern = pattern;
+ this.parameterNames = parameterNames;
+ this.parameterPatterns = parameterPatterns;
+ this.chunks = chunks;
+ }
+}
Added:
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
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,365 @@
+/*
+ * 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 org.exoplatform.web.controller.ControllerContext;
+import org.exoplatform.web.controller.QualifiedName;
+import org.exoplatform.web.controller.protocol.ProcessResponse;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * The implementation of the routing algorithm.
+ * It should but does not yet implement:
+ * - check JAX-RS algorithm (compare)
+ * - should somehow validate regular expression
+ * - nice to have '*' equivalent to {path:.*}
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+class Route
+{
+
+ /** . */
+ String controllerRef;
+
+ /** . */
+ final Map<String, SimpleRoute> simpleRoutes;
+
+ /** . */
+ final List<PatternRoute> patternRoutes;
+
+ /** . */
+ final Map<QualifiedName, String[]> routeParameters;
+
+ Route()
+ {
+ this.simpleRoutes = new HashMap<String, SimpleRoute>();
+ this.patternRoutes = new ArrayList<PatternRoute>();
+ this.routeParameters = new HashMap<QualifiedName, String[]>();
+ }
+
+ String render(String controllerId, Map<QualifiedName, String[]> blah)
+ {
+ if (controllerRef != null && controllerRef.equals(controllerId))
+ {
+ for (Map.Entry<QualifiedName, String[]> entry :
routeParameters.entrySet())
+ {
+ String[] a = blah.get(entry.getKey());
+ if (a == null || !Arrays.equals(entry.getValue(), a))
+ {
+ return null;
+ }
+ }
+ return "";
+ }
+ else
+ {
+ for (Map.Entry<String, SimpleRoute> a : simpleRoutes.entrySet())
+ {
+ String b = a.getValue().render(controllerId, blah);
+ if (b != null)
+ {
+ if (b.length() > 0)
+ {
+ return a.getKey() + "/" + b;
+ }
+ else
+ {
+ return a.getKey();
+ }
+ }
+ }
+ there:
+ for (PatternRoute a : patternRoutes)
+ {
+ int i = 0;
+ while (i < a.parameterNames.size())
+ {
+ String[] value = blah.get(a.parameterNames.get(i));
+ if (value == null || value.length < 1)
+ {
+ continue there;
+ }
+ if (!a.parameterPatterns.get(i).matcher(value[0]).matches())
+ {
+ continue there;
+ }
+ i++;
+ }
+
+ //
+ i = 0;
+ StringBuilder sb = new StringBuilder();
+ while (i < a.parameterNames.size())
+ {
+ sb.append(a.chunks.get(i));
+ String[] value = blah.get(a.parameterNames.get(i));
+ sb.append(value[0]);
+ i++;
+ }
+ sb.append(a.chunks.get(i));
+
+ //
+ String bilto = a.render(controllerId, blah);
+ if (bilto != null)
+ {
+ if (bilto.length() > 0)
+ {
+ return sb + "/" + bilto;
+ }
+ else
+ {
+ return sb.toString();
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ ProcessResponse route(ControllerContext context)
+ {
+ String path = context.getPath();
+
+ // Remove any leading '/'
+ while (path.length() > 0 && path.charAt(0) == '/')
+ {
+ path = path.substring(1);
+ }
+
+ //
+ if (path.isEmpty())
+ {
+ 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)
+ {
+ parameters = new HashMap<QualifiedName, String[]>(parameters);
+ // julien : do a safe put all here on String[]
+ parameters.putAll(route.routeParameters);
+ }
+ ControllerContext nextContext = new ControllerContext(
+ path.substring(segment.length()),
+ parameters
+ );
+ ProcessResponse response = route.route(nextContext);
+ if (response != null)
+ {
+ return response;
+ }
+ }
+ }
+
+ // Try to find a pattern matching route
+ for (PatternRoute route : patternRoutes)
+ {
+ Matcher matcher = route.pattern.matcher(path);
+
+ boolean matched = false;
+ if (matcher.find())
+ {
+ int end = matcher.end();
+ if (end >= path.length())
+ {
+ matched = true;
+ }
+ else if (path.charAt(end) == '/')
+ {
+ matched = true;
+ }
+ }
+
+ // We match
+ if (matched)
+ {
+ 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++)});
+ }
+ ControllerContext nextContext = new ControllerContext(
+ path.substring(matcher.end()),
+ parameters
+ );
+ return route.route(nextContext);
+ }
+ }
+
+ //
+ return null;
+ }
+
+ /** . */
+ private static final Pattern PARAMETER_REGEX =
Pattern.compile("^(?:\\{([^\\}]*)\\})?(.*)$");
+
+ Route append(String path)
+ {
+ int pos = path.length();
+ int level = 0;
+ List<Integer> start = new ArrayList<Integer>();
+ List<Integer> end = new ArrayList<Integer>();
+ for (int i = 0;i < path.length();i++)
+ {
+ char c = path.charAt(i);
+ if (c == '{')
+ {
+ if (level++ == 0)
+ {
+ start.add(i);
+ }
+ }
+ else if (c == '}')
+ {
+ if (--level == 0)
+ {
+ end.add(i);
+ }
+ }
+ else if (c == '/')
+ {
+ if (level == 0)
+ {
+ pos = i;
+ break;
+ }
+ }
+ }
+
+ //
+ Route next;
+ if (start.isEmpty())
+ {
+ if (pos == 0)
+ {
+ next = this;
+ }
+ else
+ {
+ SimpleRoute route = new SimpleRoute(path.substring(0, pos));
+ simpleRoutes.put(route.value, route);
+ next = route;
+ }
+ }
+ else
+ {
+ if (start.size() == end.size())
+ {
+ List<QualifiedName> parameterNames = new
ArrayList<QualifiedName>();
+ PatternBuilder builder = new PatternBuilder();
+ builder.appendExpression("^");
+ List<String> chunks = new ArrayList<String>();
+ List<Pattern> parameterPatterns = new ArrayList<Pattern>();
+ int previous = 0;
+ for (int i = 0;i < start.size();i++)
+ {
+ builder.append(path, previous, start.get(i));
+ chunks.add(path.substring(previous, start.get(i)));
+ String parameterDef = path.substring(start.get(i) + 1, end.get(i));
+ int colon = parameterDef.indexOf(':');
+ String regex;
+ String parameterName;
+ if (colon == -1)
+ {
+ regex = "([^/]+)";
+ parameterName = parameterDef;
+ }
+ else
+ {
+ regex = "(" + parameterDef.substring(colon + 1) +
")";
+ parameterName = parameterDef.substring(0, colon);
+ }
+
+ //
+ QualifiedName parameterQName;
+ Matcher parameterMatcher = PARAMETER_REGEX.matcher(parameterName);
+ if (parameterMatcher.matches())
+ {
+ String qualifier = parameterMatcher.group(1);
+ String name = parameterMatcher.group(2);
+ parameterQName = new QualifiedName(qualifier == null ? "" :
qualifier, name);
+ }
+ else
+ {
+ throw new AssertionError();
+ }
+
+
+ //
+ builder.appendExpression(regex);
+ parameterNames.add(parameterQName);
+ parameterPatterns.add(Pattern.compile("^" + regex +
"$"));
+ previous = end.get(i) + 1;
+ }
+ builder.append(path, previous, pos);
+ chunks.add(path.substring(previous, pos));
+ // Julien : should the pattern end with a $ ?????? I don't see that for
now
+ // we need to figure out clearly
+ Pattern pattern = builder.build();
+ PatternRoute route = new PatternRoute(pattern, parameterNames,
parameterPatterns, chunks);
+ patternRoutes.add(route);
+ next = route;
+ }
+ else
+ {
+ throw new UnsupportedOperationException("Report error");
+ }
+ }
+
+ //
+ if (pos < path.length())
+ {
+ return next.append(path.substring(pos + 1));
+ }
+ else
+ {
+ return next;
+ }
+ }
+}
Added:
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
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Router.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,69 @@
+/*
+ * 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 org.exoplatform.web.controller.Controller;
+import org.exoplatform.web.controller.ControllerContext;
+import org.exoplatform.web.controller.QualifiedName;
+import org.exoplatform.web.controller.metadata.ControllerRefMetaData;
+import org.exoplatform.web.controller.metadata.RouterMetaData;
+import org.exoplatform.web.controller.protocol.ControllerResponse;
+import org.exoplatform.web.controller.protocol.ProcessResponse;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class Router implements Controller
+{
+
+ /** . */
+ final Route root;
+
+ public Router(RouterMetaData metaData)
+ {
+ Route root = new Route();
+
+ //
+ for (Map.Entry<String, ControllerRefMetaData> routeMetaData :
metaData.getRoutes().entrySet())
+ {
+ String path = routeMetaData.getKey();
+ Route route = root.append(path);
+ route.controllerRef = routeMetaData.getValue().getRef();
+ route.routeParameters.putAll(routeMetaData.getValue().getParameters());
+ }
+
+ //
+ this.root = root;
+ }
+
+ public String render(String controllerId, Map<QualifiedName, String[]> blah)
+ {
+ return root.render(controllerId, blah);
+ }
+
+ public ControllerResponse process(ControllerContext controllerContext) throws
IOException
+ {
+ return root.route(controllerContext);
+ }
+}
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRoute.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRoute.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/SimpleRoute.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,36 @@
+/*
+ * 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@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+class SimpleRoute extends Route
+{
+
+ /** . */
+ final String value;
+
+ public SimpleRoute(String value)
+ {
+ this.value = value;
+ }
+}
Added:
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
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/AbstractTestController.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,72 @@
+/*
+ * 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 junit.framework.Assert;
+import junit.framework.TestCase;
+import org.exoplatform.web.controller.QualifiedName;
+import org.exoplatform.web.controller.protocol.ControllerResponse;
+import org.exoplatform.web.controller.protocol.ProcessResponse;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public abstract class AbstractTestController extends TestCase
+{
+
+ public void assertProcessResponse(String controllerId, String path,
Map<QualifiedName, String[]> parameters, ControllerResponse response)
+ {
+ assertNotNull(response);
+ assertEquals(ProcessResponse.class, response.getClass());
+ assertEquals(controllerId, ((ProcessResponse)response).getControllerId());
+ // todo check path and define semantic
+ // assertEquals(path, ((ProcessResponse)response).getPath());
+ assertEquals(parameters, ((ProcessResponse)response).getParameters());
+ }
+
+ public void assertProcessResponse(String controllerId, ControllerResponse response)
+ {
+ assertProcessResponse(controllerId, null, Collections.<QualifiedName,
String[]>emptyMap(), response);
+ }
+
+ public void assertProcessResponse(String controllerId, String path, ControllerResponse
response)
+ {
+ assertProcessResponse(controllerId, path, Collections.<QualifiedName,
String[]>emptyMap(), response);
+ }
+
+ public void assertProcessResponse(String controllerId, Map<QualifiedName,
String[]> parameters, ControllerResponse response)
+ {
+ assertProcessResponse(controllerId, null, parameters, response);
+ }
+
+ public void assertEquals(Map<QualifiedName, String[]> expectedParameters,
Map<QualifiedName, String[]> parameters)
+ {
+ Assert.assertEquals(expectedParameters.keySet(), parameters.keySet());
+ for (Map.Entry<QualifiedName, String[]> expectedEntry :
expectedParameters.entrySet())
+ {
+ Assert.assertEquals(Arrays.asList(expectedEntry.getValue()),
Arrays.asList(parameters.get(expectedEntry.getKey())));
+ }
+ }
+}
Added:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestBuildRoute.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestBuildRoute.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestBuildRoute.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,144 @@
+/*
+ * 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 junit.framework.TestCase;
+import org.exoplatform.web.controller.QualifiedName;
+import org.exoplatform.web.controller.metadata.ControllerRefMetaData;
+import org.exoplatform.web.controller.metadata.RouterMetaData;
+
+import java.util.Arrays;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestBuildRoute extends TestCase
+{
+
+ private ControllerRefMetaData ref1 = new ControllerRefMetaData("ref1");
+
+ public void testRoot()
+ {
+ String[] paths = {"/",""};
+ for (String path : paths)
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute(path, ref1);
+ Router router = new Router(routerMD);
+ Route expectedRoute = new Route();
+ expectedRoute.controllerRef = "ref1";
+ assertEquals(expectedRoute, router.root);
+ }
+ }
+
+ public void testSimpleSegment()
+ {
+ String[] paths = {"/a","a"};
+ for (String path : paths)
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute(path, ref1);
+ Router router = new Router(routerMD);
+ Route expectedRoute = new Route();
+ SimpleRoute a = new SimpleRoute("a");
+ a.controllerRef = "ref1";
+ expectedRoute.simpleRoutes.put("a", a);
+ assertEquals(expectedRoute, router.root);
+ }
+ }
+
+/*
+ public void testParameterSegment()
+ {
+ String[] paths = {"/{a}","{a}"};
+ for (String path : paths)
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute(path, ref1);
+ Router router = new Router(routerMD);
+ Route expectedRoute = new Route();
+ PatternRoute a = new PatternRoute(Pattern.compile("^([^/]+)"),
Arrays.asList(new QualifiedName("a")));
+ a.controllerRef = "ref1";
+ expectedRoute.patternRoutes.add(a);
+ assertEquals(expectedRoute, router.root);
+ }
+ }
+
+ public void testQualifiedParameterSegment()
+ {
+ String[] paths = {"/{{q}a}","{{q}a}"};
+ for (String path : paths)
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute(path, ref1);
+ Router router = new Router(routerMD);
+ Route expectedRoute = new Route();
+ PatternRoute a = new PatternRoute(Pattern.compile("^([^/]+)"),
Arrays.asList(new QualifiedName("q", "a")));
+ a.controllerRef = "ref1";
+ expectedRoute.patternRoutes.add(a);
+ assertEquals(expectedRoute, router.root);
+ }
+ }
+
+ public void testPatternSegment()
+ {
+ String[] paths = {"/{a:.*}","{a:.*}"};
+ for (String path : paths)
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute(path, ref1);
+ Router router = new Router(routerMD);
+ Route expectedRoute = new Route();
+ PatternRoute a = new PatternRoute(Pattern.compile("^(.*)"),
Arrays.asList(new QualifiedName("a")));
+ a.controllerRef = "ref1";
+ expectedRoute.patternRoutes.add(a);
+ assertEquals(expectedRoute, router.root);
+ }
+ }
+*/
+
+ private void assertEquals(Route expectedRoute, Route route)
+ {
+ assertEquals(expectedRoute.getClass(), route.getClass());
+ assertEquals(expectedRoute.controllerRef, route.controllerRef);
+ assertEquals(expectedRoute.simpleRoutes.keySet(), route.simpleRoutes.keySet());
+ for (Map.Entry<String, SimpleRoute> entry :
expectedRoute.simpleRoutes.entrySet())
+ {
+ assertEquals(entry.getValue(), expectedRoute.simpleRoutes.get(entry.getKey()));
+ }
+ assertEquals(expectedRoute.patternRoutes.size(), route.patternRoutes.size());
+ for (int i = 0;i < expectedRoute.patternRoutes.size();i++)
+ {
+ assertEquals(expectedRoute.patternRoutes.get(i), route.patternRoutes.get(i));
+ }
+ if (route instanceof PatternRoute)
+ {
+ assertEquals(((PatternRoute)expectedRoute).pattern.toString(),
((PatternRoute)route).pattern.toString());
+ assertEquals(((PatternRoute)expectedRoute).parameterNames,
((PatternRoute)route).parameterNames);
+ }
+ else if (route instanceof SimpleRoute)
+ {
+ assertEquals(((SimpleRoute)expectedRoute).value, ((SimpleRoute)route).value);
+ }
+ }
+}
Added:
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
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,188 @@
+/*
+ * 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 org.exoplatform.web.controller.ControllerContext;
+import org.exoplatform.web.controller.QualifiedName;
+import org.exoplatform.web.controller.metadata.ControllerRefMetaData;
+import org.exoplatform.web.controller.metadata.RouterMetaData;
+
+import java.util.Collections;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestMatch extends AbstractTestController
+{
+
+ public void testRoot() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/", new ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+
+ //
+ assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("")));
+
+ //
+ assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/")));
+
+ //
+ assertNull(router.process(new ControllerContext("/a")));
+
+ //
+ assertNull(router.process(new ControllerContext("a")));
+ }
+
+ public void testA() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/a", new ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+
+ //
+ assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/a")));
+
+ //
+ assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("a")));
+
+ //
+ 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("")));
+
+ //
+ assertNull(router.process(new ControllerContext("/")));
+
+ //
+ assertNull(router.process(new ControllerContext("/b")));
+
+ //
+ assertNull(router.process(new ControllerContext("b")));
+
+ //
+ assertNull(router.process(new ControllerContext("/a/b")));
+ }
+
+ public void testAB() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/a/b", new ControllerRefMetaData("ref1"));
+ Router router = new Router( routerMD);
+
+ //
+ assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("a/b")));
+
+ //
+ assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/a/b")));
+
+ //
+ 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("")));
+
+ //
+ assertNull(router.process(new ControllerContext("/")));
+
+ //
+ assertNull(router.process(new ControllerContext("/b")));
+
+ //
+ assertNull(router.process(new ControllerContext("b")));
+
+ //
+ assertNull(router.process(new ControllerContext("/a/b/c")));
+ }
+
+ public void testParameter() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/{p}", new ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+ assertProcessResponse("ref1", Collections.singletonMap(new
QualifiedName("p"), new String[]{"a"}), router.process(new
ControllerContext("/a")));
+ }
+
+ public void testWildcardPattern() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/{p:.*}", new
ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+
+ //
+ assertProcessResponse("ref1", Collections.singletonMap(new
QualifiedName("p"), new String[]{""}), router.process(new
ControllerContext("/")));
+
+ //
+ 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")));
+
+ //
+ assertProcessResponse("ref1", Collections.singletonMap(new
QualifiedName("p"), new String[]{"a/b"}), router.process(new
ControllerContext("/a/b")));
+ }
+
+ public void testSimplePattern() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/{p:a}", new
ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+
+ //
+ 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("/ab")));
+
+ //
+ assertNull(router.process(new ControllerContext("ab")));
+ }
+
+ public void testPrecedence() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/a", new ControllerRefMetaData("ref1"));
+ routerMD.addRoute("/{p:a}/b", new
ControllerRefMetaData("ref2"));
+ Router router = new Router(routerMD);
+
+ //
+ assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("a")));
+
+ //
+ assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/a")));
+
+ //
+ assertProcessResponse("ref1", Collections.<QualifiedName,
String[]>emptyMap(), router.process(new ControllerContext("/a/")));
+
+ //
+ assertProcessResponse("ref2", Collections.singletonMap(new
QualifiedName("p"), new String[]{"a"}), router.process(new
ControllerContext("/a/b")));
+ }
+}
Added:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestPortalConfiguration.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestPortalConfiguration.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestPortalConfiguration.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,81 @@
+/*
+ * 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 org.exoplatform.web.controller.ControllerContext;
+import org.exoplatform.web.controller.QualifiedName;
+import org.exoplatform.web.controller.metadata.ControllerRefMetaData;
+import org.exoplatform.web.controller.metadata.RouterMetaData;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestPortalConfiguration extends AbstractTestController
+{
+
+ /** . */
+ private Router router;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ ControllerRefMetaData portalControllerRef = new
ControllerRefMetaData("site");
+ portalControllerRef.setParameter(new QualifiedName("gtn",
"sitetype"), "portal");
+ routerMD.addRoute("/private/{{gtn}sitename}/{{gtn}path:.*}",
portalControllerRef);
+ routerMD.addRoute("/groups/{{gtn}sitename}/{{gtn}path:.*}", new
ControllerRefMetaData("site"));
+ routerMD.addRoute("/users/{{gtn}sitename}/{{gtn}path:.*}", new
ControllerRefMetaData("site"));
+
+ //
+ this.router = new Router(routerMD);
+ }
+
+ public void testPrivateClassic() throws Exception
+ {
+ Map<QualifiedName, String[]> expectedParameters = new
HashMap<QualifiedName, String[]>();
+ expectedParameters.put(new QualifiedName("gtn", "sitename"),
new String[]{"classic"});
+ expectedParameters.put(new QualifiedName("gtn", "sitetype"),
new String[]{"portal"});
+ expectedParameters.put(new QualifiedName("gtn", "path"), new
String[]{""});
+ assertProcessResponse("site", expectedParameters, router.process(new
ControllerContext("/private/classic")));
+ }
+
+ public void testPrivateClassicSlash() throws Exception
+ {
+ router.process(new ControllerContext("/private/classic/"));
+ Map<QualifiedName, String[]> expectedParameters = new
HashMap<QualifiedName, String[]>();
+ expectedParameters.put(new QualifiedName("gtn", "sitename"),
new String[]{"classic"});
+ expectedParameters.put(new QualifiedName("gtn", "sitetype"),
new String[]{"portal"});
+ expectedParameters.put(new QualifiedName("gtn", "path"), new
String[]{""});
+ assertProcessResponse("site", expectedParameters, router.process(new
ControllerContext("/private/classic/")));
+ }
+
+ public void testPrivateClassicHome() throws Exception
+ {
+ Map<QualifiedName, String[]> expectedParameters = new
HashMap<QualifiedName, String[]>();
+ expectedParameters.put(new QualifiedName("gtn", "sitename"),
new String[]{"classic"});
+ expectedParameters.put(new QualifiedName("gtn", "sitetype"),
new String[]{"portal"});
+ expectedParameters.put(new QualifiedName("gtn", "path"), new
String[]{"home"});
+ assertProcessResponse("site", expectedParameters, router.process(new
ControllerContext("/private/classic/home")));
+ }
+}
Added:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRender.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRender.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRender.java 2010-08-19
12:53:40 UTC (rev 3869)
@@ -0,0 +1,117 @@
+/*
+ * 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 org.exoplatform.web.controller.QualifiedName;
+import org.exoplatform.web.controller.metadata.ControllerRefMetaData;
+import org.exoplatform.web.controller.metadata.RouterMetaData;
+
+import java.util.Collections;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestRender extends AbstractTestController
+{
+
+ public void testRoot() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/", new ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+
+ //
+ assertEquals("", router.render("ref1",
Collections.<QualifiedName, String[]>emptyMap()));
+ }
+
+ public void testA() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/a", new ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+
+ //
+ assertEquals("a", router.render("ref1",
Collections.<QualifiedName, String[]>emptyMap()));
+ }
+
+ public void testAB() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/a/b", new ControllerRefMetaData("ref1"));
+ Router router = new Router( routerMD);
+
+ //
+ assertEquals("a/b", router.render("ref1",
Collections.<QualifiedName, String[]>emptyMap()));
+ }
+
+ public void testParameter() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/{p}", new ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+
+ //
+ assertEquals("a", router.render("ref1",
Collections.singletonMap(new QualifiedName("p"), new
String[]{"a"})));
+ }
+
+ public void testWildcardPattern() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/{p:.*}", new
ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+
+ //
+ assertEquals("", router.render("ref1",
Collections.singletonMap(new QualifiedName("p"), new String[]{""})));
+
+ //
+ assertEquals("a", router.render("ref1",
Collections.singletonMap(new QualifiedName("p"), new
String[]{"a"})));
+
+ //
+ assertEquals("a/b", router.render("ref1",
Collections.singletonMap(new QualifiedName("p"), new
String[]{"a/b"})));
+ }
+
+ public void testSimplePattern() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/{p:a}", new
ControllerRefMetaData("ref1"));
+ Router router = new Router(routerMD);
+
+ //
+ assertEquals("a", router.render("ref1",
Collections.singletonMap(new QualifiedName("p"), new
String[]{"a"})));
+
+ //
+ assertNull(router.render("ref1", Collections.singletonMap(new
QualifiedName("p"), new String[]{"ab"})));
+ }
+
+ public void testPrecedence() throws Exception
+ {
+ RouterMetaData routerMD = new RouterMetaData();
+ routerMD.addRoute("/a", new ControllerRefMetaData("ref1"));
+ routerMD.addRoute("/{p:a}/b", new
ControllerRefMetaData("ref2"));
+ Router router = new Router(routerMD);
+
+ //
+ assertEquals("a", router.render("ref1",
Collections.<QualifiedName, String[]>emptyMap()));
+
+ //
+ assertEquals("a/b", router.render("ref2",
Collections.singletonMap(new QualifiedName("p"), new
String[]{"a"})));
+ }
+}
Modified: portal/branches/navcontroller/webui/framework/pom.xml
===================================================================
--- portal/branches/navcontroller/webui/framework/pom.xml 2010-08-19 09:17:49 UTC (rev
3868)
+++ portal/branches/navcontroller/webui/framework/pom.xml 2010-08-19 12:53:40 UTC (rev
3869)
@@ -33,6 +33,10 @@
<dependencies>
<dependency>
+ <groupId>org.gatein.common</groupId>
+ <artifactId>common-logging</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.exoplatform.portal</groupId>
<artifactId>exo.portal.component.web.controller</artifactId>
</dependency>
@@ -44,6 +48,11 @@
<groupId>org.exoplatform.portal</groupId>
<artifactId>exo.portal.component.scripting</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.exoplatform.portal</groupId>
+ <artifactId>exo.portal.component.test.core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>