01: /*
02: * Title: OSDecoratorMapper
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 java.util.Enumeration;
19: import java.util.Properties;
20:
21: /**
22: * The OSDecoratorMapper will map a suitable decorator based on the operating system
23: * of the remote client.
24: *
25: * <p>OSDecoratorMapper works by checking to see if the "UA-OS" header
26: * was sent with the HTTP request. If it was, the class will check the
27: * value of the header with all the different os's the user has configured
28: * the Decorator Mapper to identify and, if a match is found, routes the
29: * request accordingly. Configuration is done using the sitemesh.xml file.
30: * The param name is a string literal (operating system name) you would like
31: * to match in the UA-OS header, and the value is what will be appended to the
32: * decorator name if the user is using that operating system</p>
33: *
34: * @author <a href="mailto:schepdawg@yahoo.com">Adam P. Schepis</a>
35: * @version $Revision: 1.3 $
36: *
37: * @see com.opensymphony.module.sitemesh.mapper.AbstractDecoratorMapper
38: */
39: public class OSDecoratorMapper extends AbstractDecoratorMapper {
40: /**
41: * Properties holds the parameters that the object was initialized with.
42: */
43: protected Properties properties;
44:
45: /**
46: * Init initializes the OSDecoratorMapper object by setting the parent
47: * DecoratorMapper, and loading the initialization properties.
48: *
49: * @param config The config file
50: * @param properties An object containing intialization parameters
51: * @param parent The parent DecoratorMapper object
52: */
53: public void init(Config config, Properties properties,
54: DecoratorMapper parent)
55: throws java.lang.InstantiationException {
56: this .properties = properties;
57: this .parent = parent;
58: }
59:
60: /**
61: * Attempts to find the correct decorator for Page page based on
62: * the UA-OS HTTP header in the request.
63: *
64: * @param request The HTTP request sent to the server
65: * @param page The page SiteMesh is trying to find a decorator for
66: *
67: * @return A Decorator object that is either the decorator for the identified
68: * OS, or the parent DecoratorMapper's decorator
69: */
70: public Decorator getDecorator(HttpServletRequest request, Page page) {
71: String osHeader = request.getHeader("UA-OS").toLowerCase();
72: if (osHeader == null)
73: return parent.getDecorator(request, page);
74:
75: // run through the list of operating systems the application developer listed
76: // in sitemesh.xml to see if we have a match to the user's current OS
77: for (Enumeration e = properties.propertyNames(); e
78: .hasMoreElements();) {
79: String os = (String) e.nextElement();
80:
81: // see if the name matches the user's operating system name
82: if (osHeader.indexOf(os.toLowerCase()) != -1) {
83: String decoratorName = parent.getDecorator(request,
84: page).getName();
85: if (decoratorName != null) {
86: decoratorName += '-' + properties.getProperty(os);
87: }
88: return getNamedDecorator(request, decoratorName);
89: }
90: }
91:
92: return parent.getDecorator(request, page);
93: }
94: }
|