You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2005/10/15 17:38:03 UTC

svn commit: r321355 - in /jakarta/tapestry/trunk/examples/Vlib/src: context/WEB-INF/hivemodule.xml context/WEB-INF/web.xml java/org/apache/tapestry/vlib/services/ViewPageEncoder.java java/org/apache/tapestry/vlib/services/ViewPageEncoderTest.java

Author: hlship
Date: Sat Oct 15 08:37:59 2005
New Revision: 321355

URL: http://svn.apache.org/viewcvs?rev=321355&view=rev
Log:
Continue revving Virtual Library for Tapestry 4.0: Customized friendly URLs for ViewBook and ViewPerson

Added:
    jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoder.java
    jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoderTest.java
Modified:
    jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/hivemodule.xml
    jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/web.xml

Modified: jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/hivemodule.xml
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/hivemodule.xml?rev=321355&r1=321354&r2=321355&view=diff
==============================================================================
--- jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/hivemodule.xml (original)
+++ jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/hivemodule.xml Sat Oct 15 08:37:59 2005
@@ -20,6 +20,8 @@
     <contribution configuration-id="tapestry.url.ServiceEncoders">
         <direct-service-encoder id="direct" stateless-extension="direct" stateful-extension="sdirect"/>
         <page-service-encoder id="page" extension="page" service="page"/>
+        <encoder id="viewbook" before="external" object="instance:ViewPageEncoder,pageName=ViewBook,url=/book"/>
+        <encoder id="viewperson" before="external" object="instance:ViewPageEncoder,pageName=ViewPerson,url=/person"/>
         <page-service-encoder id="external" extension="external" service="external"/>    
         <asset-encoder id="asset" path="/assets/"/>
         <extension-encoder id="ext" extension="svc" after="*"/>

Modified: jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/web.xml
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/web.xml?rev=321355&r1=321354&r2=321355&view=diff
==============================================================================
--- jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/web.xml (original)
+++ jakarta/tapestry/trunk/examples/Vlib/src/context/WEB-INF/web.xml Sat Oct 15 08:37:59 2005
@@ -62,6 +62,14 @@
     <servlet-name>vlib</servlet-name>
     <url-pattern>/assets/*</url-pattern>
   </servlet-mapping>
+  <servlet-mapping>
+    <servlet-name>vlib</servlet-name>
+    <url-pattern>/book/*</url-pattern>
+  </servlet-mapping>
+  <servlet-mapping>
+    <servlet-name>vlib</servlet-name>
+    <url-pattern>/person/*</url-pattern>
+  </servlet-mapping>
   <session-config>
     <session-timeout>15</session-timeout>
   </session-config>

Added: jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoder.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoder.java?rev=321355&view=auto
==============================================================================
--- jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoder.java (added)
+++ jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoder.java Sat Oct 15 08:37:59 2005
@@ -0,0 +1,102 @@
+// Copyright 2005 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.vlib.services;
+
+import org.apache.tapestry.Tapestry;
+import org.apache.tapestry.TapestryUtils;
+import org.apache.tapestry.engine.ServiceEncoder;
+import org.apache.tapestry.engine.ServiceEncoding;
+import org.apache.tapestry.services.ServiceConstants;
+
+/**
+ * Specialized encdoder for the {@link org.apache.tapestry.vlib.pages.ViewBook} and
+ * {@link org.apache.tapestry.vlib.pages.ViewPerson} pages.
+ * 
+ * @author Howard M. Lewis Ship
+ * @since 4.0
+ */
+public class ViewPageEncoder implements ServiceEncoder
+{
+    private String _pageName;
+
+    private String _url;
+
+    public void encode(ServiceEncoding encoding)
+    {
+        if (!isExternalService(encoding))
+            return;
+
+        String pageName = encoding.getParameterValue(ServiceConstants.PAGE);
+
+        if (!pageName.equals(_pageName))
+            return;
+
+        StringBuilder builder = new StringBuilder(_url);
+
+        String[] params = encoding.getParameterValues(ServiceConstants.PARAMETER);
+
+        // params will not be null; in fact, pretty sure it will consist
+        // of just one element (an integer).
+
+        for (String param : params)
+        {
+            builder.append("/");
+            builder.append(param);
+        }
+
+        encoding.setServletPath(builder.toString());
+
+        encoding.setParameterValue(ServiceConstants.SERVICE, null);
+        encoding.setParameterValue(ServiceConstants.PAGE, null);
+        encoding.setParameterValue(ServiceConstants.PARAMETER, null);
+    }
+
+    private boolean isExternalService(ServiceEncoding encoding)
+    {
+        String service = encoding.getParameterValue(ServiceConstants.SERVICE);
+
+        return service.equals(Tapestry.EXTERNAL_SERVICE);
+    }
+
+    public void decode(ServiceEncoding encoding)
+    {
+        String servletPath = encoding.getServletPath();
+
+        if (!servletPath.startsWith(_url))
+            return;
+
+        // Skip the URL and the path, end up with a "/" seperated series of
+        // strings.
+
+        String pathInfo = servletPath.substring(_url.length() + 1);
+
+        String[] params = TapestryUtils.split(pathInfo, '/');
+
+        encoding.setParameterValue(ServiceConstants.SERVICE, Tapestry.EXTERNAL_SERVICE);
+        encoding.setParameterValue(ServiceConstants.PAGE, _pageName);
+        encoding.setParameterValues(ServiceConstants.PARAMETER, params);
+    }
+
+    public void setPageName(String pageName)
+    {
+        _pageName = pageName;
+    }
+
+    public void setUrl(String url)
+    {
+        _url = url;
+    }
+
+}

Added: jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoderTest.java
URL: http://svn.apache.org/viewcvs/jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoderTest.java?rev=321355&view=auto
==============================================================================
--- jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoderTest.java (added)
+++ jakarta/tapestry/trunk/examples/Vlib/src/java/org/apache/tapestry/vlib/services/ViewPageEncoderTest.java Sat Oct 15 08:37:59 2005
@@ -0,0 +1,141 @@
+// Copyright 2005 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.vlib.services;
+
+import org.apache.hivemind.test.HiveMindTestCase;
+import org.apache.tapestry.Tapestry;
+import org.apache.tapestry.engine.ServiceEncoding;
+import org.apache.tapestry.services.ServiceConstants;
+import org.easymock.internal.ArrayMatcher;
+
+/**
+ * Tests for {@link org.apache.tapestry.vlib.services.ViewPageEncoder}.
+ * 
+ * @author Howard M. Lewis Ship
+ * @since 4.0
+ */
+public class ViewPageEncoderTest extends HiveMindTestCase
+{
+    private ViewPageEncoder _encoder;
+
+    {
+        _encoder = new ViewPageEncoder();
+        _encoder.setPageName("ViewBook");
+        _encoder.setUrl("/book");
+    }
+
+    public void testEncodeWrongService()
+    {
+        ServiceEncoding encoding = newEncoding();
+
+        trainGetParameterValue(encoding, ServiceConstants.SERVICE, "wrong");
+
+        replayControls();
+
+        _encoder.encode(encoding);
+
+        verifyControls();
+    }
+
+    public void testEncodeWrongPage()
+    {
+        ServiceEncoding encoding = newEncoding();
+
+        trainGetParameterValue(encoding, ServiceConstants.SERVICE, Tapestry.EXTERNAL_SERVICE);
+        trainGetParameterValue(encoding, ServiceConstants.PAGE, "Foo");
+
+        replayControls();
+
+        _encoder.encode(encoding);
+
+        verifyControls();
+    }
+
+    public void testEncodeMatch()
+    {
+        ServiceEncoding encoding = newEncoding();
+
+        trainGetParameterValue(encoding, ServiceConstants.SERVICE, Tapestry.EXTERNAL_SERVICE);
+        trainGetParameterValue(encoding, ServiceConstants.PAGE, "ViewBook");
+        trainGetParameterValues(encoding, ServiceConstants.PARAMETER, new String[]
+        { "1234" });
+
+        encoding.setServletPath("/book/1234");
+        encoding.setParameterValue(ServiceConstants.SERVICE, null);
+        encoding.setParameterValue(ServiceConstants.PAGE, null);
+        encoding.setParameterValue(ServiceConstants.PARAMETER, null);
+
+        replayControls();
+
+        _encoder.encode(encoding);
+
+        verifyControls();
+    }
+
+    public void testDecodeWrongURL()
+    {
+        ServiceEncoding encoding = newEncoding();
+
+        trainGetServletPath(encoding, "/Home.html");
+
+        replayControls();
+
+        _encoder.decode(encoding);
+
+        verifyControls();
+    }
+
+    public void testDecodeMatch()
+    {
+        ServiceEncoding encoding = newEncoding();
+
+        trainGetServletPath(encoding, "/book/2001");
+
+        encoding.setParameterValue(ServiceConstants.SERVICE, Tapestry.EXTERNAL_SERVICE);
+        encoding.setParameterValue(ServiceConstants.PAGE, "ViewBook");
+        encoding.setParameterValues(ServiceConstants.PARAMETER, new String[]
+        { "2001" });
+        getControl(encoding).setMatcher(new ArrayMatcher());
+
+        replayControls();
+
+        _encoder.decode(encoding);
+
+        verifyControls();
+    }
+
+    private void trainGetServletPath(ServiceEncoding encoding, String servletPath)
+    {
+        encoding.getServletPath();
+        setReturnValue(encoding, servletPath);
+    }
+
+    private void trainGetParameterValues(ServiceEncoding encoding, String name, String[] values)
+    {
+        encoding.getParameterValues(name);
+        setReturnValue(encoding, values);
+    }
+
+    private ServiceEncoding newEncoding()
+    {
+        return (ServiceEncoding) newMock(ServiceEncoding.class);
+    }
+
+    private void trainGetParameterValue(ServiceEncoding encoding, String name, String value)
+    {
+        encoding.getParameterValue(name);
+        setReturnValue(encoding, value);
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org