01: /*
02: * Title: ConfigDecoratorMapper
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.ServletException;
18: import javax.servlet.http.HttpServletRequest;
19: import java.util.Properties;
20:
21: /**
22: * Default implementation of DecoratorMapper. Reads decorators and
23: * mappings from the <code>config</code> property (default '/WEB-INF/decorators.xml').
24: *
25: * @author <a href="joe@truemesh.com">Joe Walnes</a>
26: * @author <a href="mcannon@internet.com">Mike Cannon-Brookes</a>
27: * @version $Revision: 1.2 $
28: *
29: * @see com.opensymphony.module.sitemesh.DecoratorMapper
30: * @see com.opensymphony.module.sitemesh.mapper.DefaultDecorator
31: * @see com.opensymphony.module.sitemesh.mapper.ConfigLoader
32: */
33: public class ConfigDecoratorMapper extends AbstractDecoratorMapper {
34: private ConfigLoader configLoader = null;
35:
36: /** Create new ConfigLoader using '/WEB-INF/decorators.xml' file. */
37: public void init(Config config, Properties properties,
38: DecoratorMapper parent) throws InstantiationException {
39: super .init(config, properties, parent);
40: try {
41: String fileName = properties.getProperty("config",
42: "/WEB-INF/decorators.xml");
43: configLoader = new ConfigLoader(fileName, config);
44: } catch (Exception e) {
45: throw new InstantiationException(e.toString());
46: }
47: }
48:
49: /** Retrieve {@link com.opensymphony.module.sitemesh.Decorator} based on 'pattern' tag. */
50: public Decorator getDecorator(HttpServletRequest request, Page page) {
51: String this Path = request.getServletPath();
52:
53: // getServletPath() returns null unless the mapping corresponds to a servlet
54: if (this Path == null) {
55: String requestURI = request.getRequestURI();
56: if (request.getPathInfo() != null) {
57: // strip the pathInfo from the requestURI
58: this Path = requestURI.substring(0, requestURI
59: .indexOf(request.getPathInfo()));
60: } else {
61: this Path = requestURI;
62: }
63: } else if ("".equals(this Path)) {
64: // in servlet 2.4, if a request is mapped to '/*', getServletPath returns null (SIM-130)
65: this Path = request.getPathInfo();
66: }
67:
68: String name = null;
69: try {
70: name = configLoader.getMappedName(this Path);
71: } catch (ServletException e) {
72: e.printStackTrace();
73: }
74:
75: Decorator result = getNamedDecorator(request, name);
76: return result == null ? super .getDecorator(request, page)
77: : result;
78: }
79:
80: /** Retrieve Decorator named in 'name' attribute. Checks the role if specified. */
81: public Decorator getNamedDecorator(HttpServletRequest request,
82: String name) {
83: Decorator result = null;
84: try {
85: result = configLoader.getDecoratorByName(name);
86: } catch (ServletException e) {
87: e.printStackTrace();
88: }
89:
90: if (result == null
91: || (result.getRole() != null && !request
92: .isUserInRole(result.getRole()))) {
93: // if the result is null or the user is not in the role
94: return super.getNamedDecorator(request, name);
95: } else {
96: return result;
97: }
98: }
99: }
|