KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > matching > CookieMatcher


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.matching;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.framework.parameters.Parameters;
20 import org.apache.avalon.framework.thread.ThreadSafe;
21
22 import org.apache.cocoon.environment.Cookie;
23 import org.apache.cocoon.environment.Request;
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.sitemap.PatternException;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Matches cookies agains given name. Returns value of the matched cookie.
32  *
33  * @author <a HREF="mailto:maciejka@tiger.com.pl">Maciek Kaminski</a>
34  * @version CVS $Id: CookieMatcher.java 30932 2004-07-29 17:35:38Z vgritsenko $
35  */

36 public class CookieMatcher extends AbstractLogEnabled implements Matcher, ThreadSafe {
37
38     public Map JavaDoc match(String JavaDoc pattern, Map JavaDoc objectModel, Parameters parameters)
39             throws PatternException {
40
41         if (pattern == null) {
42             throw new PatternException("No cookie name given.");
43         }
44
45         Request request = ObjectModelHelper.getRequest(objectModel);
46         Cookie[] cookies = request.getCookies();
47         HashMap JavaDoc result = null;
48
49         if (cookies != null) {
50             for (int i = 0; i < cookies.length; i++) {
51                 Cookie cookie = cookies[i];
52                 if (cookie.getName().equals(pattern)) {
53                     result = new HashMap JavaDoc();
54                     result.put("1", cookie.getValue());
55                     break;
56                 }
57             }
58         }
59
60         return result;
61     }
62 }
63
Popular Tags