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