01: /*
02: * Title: CookieDecoratorMapper
03: * Description:
04: *
05: * This software is published under the terms of the OpenSymphony Software
06: * License version 1.1, of which a copy has been included with this
07: * distribution in the LICENSE.txt file.
08: */
09:
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;
18: import javax.servlet.http.Cookie;
19: import java.util.Properties;
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 cookieName;
35:
36: public void init(Config config, Properties properties,
37: DecoratorMapper parent) throws InstantiationException {
38: super .init(config, properties, parent);
39: cookieName = properties.getProperty("cookie.name", null);
40: if (cookieName == null) {
41: throw new InstantiationException(
42: "'cookie.name' name parameter not set for this decorator mapper");
43: }
44: }
45:
46: public Decorator getDecorator(HttpServletRequest request, Page page) {
47: Decorator result = null;
48: Cookie[] cookies = request.getCookies();
49: if (cookies != null) {
50: for (int i = 0; i < cookies.length; i++) {
51: Cookie cookie = cookies[i];
52: if (cookie.getName().equals(cookieName)) {
53: result = getNamedDecorator(request, cookie
54: .getValue());
55: }
56: }
57: }
58: return result == null ? super.getDecorator(request, page)
59: : result;
60: }
61: }
|