KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > mapper > CookieDecoratorMapper


1 /*
2  * Title: CookieDecoratorMapper
3  * Description:
4  *
5  * This software is published under the terms of the OpenSymphony Software
6  * License version 1.1, of which a copy has been included with this
7  * distribution in the LICENSE.txt file.
8  */

9
10 package com.opensymphony.module.sitemesh.mapper;
11
12 import com.opensymphony.module.sitemesh.Config;
13 import com.opensymphony.module.sitemesh.Decorator;
14 import com.opensymphony.module.sitemesh.DecoratorMapper;
15 import com.opensymphony.module.sitemesh.Page;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.Cookie JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 /**
22  * The CookieDecoratorMapper will map a suitable decorator based on a cookie value.
23  *
24  * <p>The CookieDecoratorMapper is configured via one properties.
25  * <code>cookie.name</code> - the cookie which contains the name of the
26  * decorator which will be mapped.</p>
27  *
28  * @author Paul Hammant
29  * @version $Revision: 1.2 $
30  *
31  * @see com.opensymphony.module.sitemesh.DecoratorMapper
32  */

33 public class CookieDecoratorMapper extends AbstractDecoratorMapper {
34     private String JavaDoc cookieName;
35
36     public void init(Config config, Properties JavaDoc properties, DecoratorMapper parent) throws InstantiationException JavaDoc {
37         super.init(config, properties, parent);
38         cookieName = properties.getProperty("cookie.name", null);
39         if (cookieName == null) {
40             throw new InstantiationException JavaDoc("'cookie.name' name parameter not set for this decorator mapper");
41         }
42     }
43
44     public Decorator getDecorator(HttpServletRequest JavaDoc request, Page page) {
45         Decorator result = null;
46         Cookie JavaDoc[] cookies = request.getCookies();
47         if (cookies != null) {
48             for (int i = 0; i < cookies.length; i++) {
49                 Cookie JavaDoc cookie = cookies[i];
50                 if (cookie.getName().equals(cookieName)) {
51                     result = getNamedDecorator(request, cookie.getValue());
52                 }
53             }
54         }
55         return result == null ? super.getDecorator(request, page) : result;
56     }
57 }
Popular Tags