1 /*
2 * Title: ParameterDecoratorMapper
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;
18 import java.util.Properties;
19
20 /**
21 * The ParameterDecoratorMapper will map a suitable decorator based on request
22 * parameters.
23 *
24 * <p>The ParameterDecoratorMapper is configured via three properties.
25 * <code>decorator.parameter</code> - the parameter which contains the name of the decorator which will be mapped.
26 * The default is "decorator".</p>
27 *
28 * <p>For example if <code>decorator.parameter</code> is "foobar" then
29 * myurl.jsp?foobar=mydecorator will map to the decorator named "mydecorator".</p>
30 *
31 * <p>You can also supply an optional 'confirmation parameter'.
32 * The decorator will only be mapped if the parameter named <code>parameter.name</code> is
33 * in the request URI and the value of that parameter is equal to the
34 * <code>parameter.value</code> property.</p>
35 *
36 * <p>For example assuming parameter.name=confirm and parameter.value=true
37 * the URI myurl.jsp?decorator=mydecorator&confirm=true will map the decorator mydecorator.
38 * where as the URIs myurl.jsp?decorator=mydecorator and myurl.jsp?decorator=mydecorator&confirm=false will
39 * not return any decorator.</p>
40 *
41 * @author <a href="mailto:mcannon@internet.com">Mike Cannon-Brookes</a>
42 * @version $Revision: 1.2 $
43 *
44 * @see com.opensymphony.module.sitemesh.DecoratorMapper
45 */
46 public class ParameterDecoratorMapper extends AbstractDecoratorMapper {
47 private String decoratorParameter = null, paramName = null, paramValue = null;
48
49 public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException {
50 super.init(config, properties, parent);
51 decoratorParameter = properties.getProperty("decorator.parameter", "decorator");
52 paramName = properties.getProperty("parameter.name", null);
53 paramValue = properties.getProperty("parameter.value", null);
54 }
55
56 public Decorator getDecorator(HttpServletRequest request, Page page) {
57 Decorator result = null;
58 String decoratorParamValue = request.getParameter(decoratorParameter);
59
60 if ((paramName == null || paramValue.equals(request.getParameter(paramName)))
61 && decoratorParamValue != null && !decoratorParamValue.trim().equals("")) {
62 result = getNamedDecorator(request, decoratorParamValue);
63 }
64 return result == null ? super.getDecorator(request, page) : result;
65 }
66 }