You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by an...@apache.org on 2005/07/21 14:28:15 UTC

svn commit: r220045 - in /cocoon/branches/BRANCH_2_1_X: ./ src/documentation/xdocs/userdocs/concepts/ src/java/org/apache/cocoon/components/modules/input/ src/webapp/WEB-INF/ src/webapp/samples/modules/

Author: anathaniel
Date: Thu Jul 21 05:28:12 2005
New Revision: 220045

URL: http://svn.apache.org/viewcvs?rev=220045&view=rev
Log:
Added CookieModule as a more convinient way to access cookie values as {cookie:query}
in alternative to {request:cookies[name='query']/value}.

Code contributed by Jon Evans (jon.evans@misgl.com).  Bugzilla #28045

Added:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/CookieModule.java   (with props)
Modified:
    cocoon/branches/BRANCH_2_1_X/src/documentation/xdocs/userdocs/concepts/modules-ref.xml
    cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf
    cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/menu.xml
    cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/sitemap.xmap
    cocoon/branches/BRANCH_2_1_X/status.xml

Modified: cocoon/branches/BRANCH_2_1_X/src/documentation/xdocs/userdocs/concepts/modules-ref.xml
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/documentation/xdocs/userdocs/concepts/modules-ref.xml?rev=220045&r1=220044&r2=220045&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/documentation/xdocs/userdocs/concepts/modules-ref.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/src/documentation/xdocs/userdocs/concepts/modules-ref.xml Thu Jul 21 05:28:12 2005
@@ -100,6 +100,23 @@
         </section>
 
         <section>
+            <title>CookieModule</title>
+
+            <p>This module returns the value of the named HTTP cookie.</p>
+
+            <p><strong>Example Pipeline Fragment:</strong></p>
+
+            <source><![CDATA[
+<map:match pattern="foo.html">
+  <map:generate type="file" src="documents/{cookie:user-language}/foo.xml"/>
+  <map:transform src="stylesheets/foo2html.xsl"/>
+  <map:serialize/>
+</map:match>
+]]></source>
+
+        </section>
+
+        <section>
             <title>DateInputModule</title>
 
             <p>This module returns the current date, optionally formated 

Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/CookieModule.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/CookieModule.java?rev=220045&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/CookieModule.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/CookieModule.java Thu Jul 21 05:28:12 2005
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2004-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.cocoon.components.modules.input;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.components.modules.input.AbstractInputModule;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.http.HttpCookie;
+
+/**
+ * Input module for cookies. Retrieves the value of the requested cookie.
+ * 
+ * @author Jon Evans <jo...@misgl.com>
+ * @version CVS $Id:$
+ */
+public class CookieModule extends AbstractInputModule implements ThreadSafe {
+    
+    /**
+     * @return the value of the cookie whose name matches the one requested,
+     * or <code>null</code> if there is no match.
+     */
+    public Object getAttribute(String name, Configuration modeConf,
+            Map objectModel) throws ConfigurationException {
+        
+        HttpCookie cookie = (HttpCookie) getCookieMap(objectModel).get(name);
+        String value = (cookie == null ? null : cookie.getValue());
+        
+        if (getLogger().isDebugEnabled()) {
+            getLogger().debug("Cookie[" + name + "]=" + value);
+        }
+        return value;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
+     *      java.util.Map)
+     */
+    public Iterator getAttributeNames(Configuration modeConf, Map objectModel)
+            throws ConfigurationException {
+        
+        return getCookieMap(objectModel).keySet().iterator();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
+     *      org.apache.avalon.framework.configuration.Configuration,
+     *      java.util.Map)
+     */
+    public Object[] getAttributeValues(String name, Configuration modeConf,
+            Map objectModel) throws ConfigurationException {
+        
+        Map allCookies = getCookieMap(objectModel);
+        
+        Iterator it = allCookies.values().iterator();
+        List matched = new LinkedList();
+        while (it.hasNext()) {
+            HttpCookie cookie = (HttpCookie) it.next();
+            if (cookie.getName().matches(name)) {
+                matched.add(cookie.getValue());
+            }
+        }
+        return matched.toArray();
+    }
+
+    /**
+     * @param objectModel
+     *            Object Model for the current request
+     * @return a Map of {see: HttpCookie}s for the current request, keyed on
+     *         cookie name.
+     */
+    protected Map getCookieMap(Map objectModel) {
+        return ObjectModelHelper.getRequest(objectModel).getCookieMap();
+    }
+}

Propchange: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/CookieModule.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/input/CookieModule.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf?rev=220045&r1=220044&r2=220045&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf (original)
+++ cocoon/branches/BRANCH_2_1_X/src/webapp/WEB-INF/cocoon.xconf Thu Jul 21 05:28:12 2005
@@ -176,6 +176,7 @@
     <component-instance logger="core.modules.input" name="request-attr"     class="org.apache.cocoon.components.modules.input.RequestAttributeModule"/>
     <component-instance logger="core.modules.input" name="request-header"   class="org.apache.cocoon.components.modules.input.HeaderAttributeModule"/>
     <component-instance logger="core.modules.input" name="session-attr"     class="org.apache.cocoon.components.modules.input.SessionAttributeModule"/>
+    <component-instance logger="core.modules.input" name="cookie"           class="org.apache.cocoon.components.modules.input.CookieModule"/>
     <component-instance logger="core.modules.input" name="system-property"  class="org.apache.cocoon.components.modules.input.SystemPropertyModule"/>
     <component-instance logger="core.modules.input" name="constant"         class="org.apache.cocoon.components.modules.input.StringConstantModule"/>
     <component-instance logger="core.modules.input" name="random"           class="org.apache.cocoon.components.modules.input.RandomNumberModule"/>

Modified: cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/menu.xml
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/menu.xml?rev=220045&r1=220044&r2=220045&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/menu.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/menu.xml Thu Jul 21 05:28:12 2005
@@ -31,6 +31,7 @@
   <menu label="Modules">
     <menu-item label="BaseLinkModule" href="baselink.html" desc="BaseLink properties"/>
     <menu-item label="ChainMetaModule" href="chain.html" desc="Chained values"/>
+    <menu-item label="CookieModule" href="cookie.html" desc="Cookie values"/>
     <menu-item label="DateInputModule" href="date.html" desc="Date"/>
     <menu-item label="DefaultsModule" href="defaults.html" desc="Default (static) values"/>
     <menu-item label="GlobalInputModule" href="global.html" desc="Access to the global variables defined in the sitemap."/>

Modified: cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/sitemap.xmap
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/sitemap.xmap?rev=220045&r1=220044&r2=220045&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/sitemap.xmap (original)
+++ cocoon/branches/BRANCH_2_1_X/src/webapp/samples/modules/sitemap.xmap Thu Jul 21 05:28:12 2005
@@ -23,6 +23,16 @@
     +-->
 <map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
 
+  <!-- =========================== Components ============================== -->
+  <map:components>
+    <map:actions>
+      <!-- Action to store locale name as cookie. -->
+      <map:action name="cookie-locale" src="org.apache.cocoon.acting.LocaleAction">
+        <store-in-cookie>true</store-in-cookie>
+      </map:action>
+    </map:actions>
+  </map:components>
+
   <!-- =========================== Resources =============================== -->
   <map:resources>
     <!-- This resource is used to create a composite page from menu and content -->
@@ -256,6 +266,23 @@
             first that returns a non-null value, usually 'defaults'.
             Try adding '?skin=myskin' to the URL, and it should
             change."/>
+        </map:transform>
+        <map:serialize/>
+      </map:match>
+
+      <map:match pattern="content/cookie.xml">
+        <map:act type="cookie-locale"/>
+        <map:generate type="jx" src="properties.xml">
+          <map:parameter name="locale" value="{cookie:locale}"/>
+        </map:generate>
+        <map:transform src="properties2html.xsl">
+          <map:parameter name="title" value="Cookie module (CookieModule)"/>
+          <map:parameter name="description" value="Returns the value
+            of the named HTTP cookie.
+            The example used LocaleAction to store the current locale name as cookie.
+            If the value is empty, use the browser's reload button.
+            (Cookies just created cannot be read out. Only when the browser presents it
+            on the next request, it will be available.)"/>
         </map:transform>
         <map:serialize/>
       </map:match>

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/status.xml?rev=220045&r1=220044&r2=220045&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Thu Jul 21 05:28:12 2005
@@ -196,6 +196,10 @@
 
   <changes>
   <release version="@version@" date="@date@">
+    <action dev="AN" type="add" fixes-bug="28045" due-to="Jon Evans" due-to-email="jon.evans@misgl.com">
+      Added CookieModule as a more convinient way to access cookie values as {cookie:query}
+      in alternative to {request:cookies[name='query']/value}.
+    </action>
     <action dev="AN" type="add" fixes-bug="33388" due-to="Andrew Stevens" due-to-email="ats37@hotmail.com">
       Added parameter "show-cocoon-version" to web.xml for configuring whether X-Cocoon-Version
       response header should be sent.  Default is true.