cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simone Gianni <sim...@discoone.com>
Subject Cookie matcher
Date Sat, 13 Oct 2001 01:32:33 GMT
Hi all ..

	I've not found a simple way to match against a cookie, it should be 
possible with matching the headers, but since it's a bit confusing and 
error prone, I've cloned the RequestParamMatcher and modified it in the 
RequestCookieMatcher .. I've not tested it too much .. but it seems to work 
.. and since it's 99% copied from the RequestParamMatcher class it should 
be ok ... here is the source :

/*****************************************************************************
  * Copyright (C) The Apache Software Foundation. All rights reserved.        *
  * ------------------------------------------------------------------------- *
  * This software is published under the terms of the Apache Software License *
  * version 1.1, a copy of which has been included  with this distribution in *
  * the LICENSE file.                                                         *
  *****************************************************************************/

/*
  * Package definition
  */
package org.apache.cocoon.matching;

/*
  * Standard imports
  */
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.Constants;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Cookie;

import java.util.HashMap;
import java.util.Map;

/**
  * This class allows for matching based on a request cookie.
  * If the specified request cookie exists, its value is retrieved for later
  * xpath substitution.
  *
  * <p><b>Example:</b></p>
  * <pre>
  * &lt;map:match type="cookie" pattern="skin"&gt;
  *     &lt;map:redirect-to uri="{1}"/&gt;
  * &lt;/map:match&gt;
  * </pre>
  *
  * @author <a href="mailto:simone@discoone.com">Simone Gianni</a>
  * @version CVS $Revision: 1.3.2.3 $
  */
public class RequestCookieMatcher implements Matcher, ThreadSafe {
     /**
       * Match method to see if the request cookie exists. If it does
       * have a value the parameter is added to the array list for later
       * substitution with a xpath attribute.
       *
       * @param pattern name of request cookie to find
       * @param objectModel environment passed through via cocoon
       * @returns null or map containing value of request cookie 'pattern'
       */
     public Map match(String pattern, Map objectModel, Parameters parameters) {
         Request request =
           (Request) objectModel.get(Constants.REQUEST_OBJECT);
         Cookie cook = (Cookie)request.getCookieMap().get(pattern);
         String param = null;
         if (cook != null) {
			param = cook.getValue();
		}

         if (param == null)
             return null; // no parameter defined
         else {
             Map map = new HashMap();
             map.put(Integer.toString(1), param);
             return map; // parameter defined, return map
         }
     }
}

	Hope it will be useful ...

	Congratulations from this great piece of software !

	Simone Gianni


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


Mime
View raw message