01: /*
02: * $Id: SetRequestAttributeMethod.java,v 1.2 2003/09/14 05:36:47 jonesde Exp $
03: *
04: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
05: *
06: * Permission is hereby granted, free of charge, to any person obtaining a
07: * copy of this software and associated documentation files (the "Software"),
08: * to deal in the Software without restriction, including without limitation
09: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10: * and/or sell copies of the Software, and to permit persons to whom the
11: * Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included
14: * in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23: *
24: */
25: package org.ofbiz.content.webapp.ftl;
26:
27: import java.util.List;
28:
29: import javax.servlet.http.HttpServletRequest;
30:
31: import freemarker.ext.beans.BeanModel;
32: import freemarker.template.Environment;
33: import freemarker.template.SimpleScalar;
34: import freemarker.template.TemplateMethodModelEx;
35: import freemarker.template.TemplateModel;
36: import freemarker.template.TemplateModelException;
37: import freemarker.template.TemplateNumberModel;
38: import freemarker.template.TemplateScalarModel;
39:
40: /**
41: * SetRequestAttributeMethod - Freemarker Method for setting request attributes
42: *
43: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44: * @version $Revision: 1.2 $
45: * @since 2.1
46: */
47: public class SetRequestAttributeMethod implements TemplateMethodModelEx {
48:
49: public static final String module = SetRequestAttributeMethod.class
50: .getName();
51:
52: /*
53: * @see freemarker.template.TemplateMethodModel#exec(java.util.List)
54: */
55: public TemplateModel exec(List args) throws TemplateModelException {
56: if (args == null || args.size() != 2)
57: throw new TemplateModelException(
58: "Invalid number of arguements");
59: if (!(args.get(0) instanceof TemplateScalarModel))
60: throw new TemplateModelException(
61: "First argument not an instance of TemplateScalarModel");
62: if (!(args.get(1) instanceof BeanModel)
63: && !(args.get(1) instanceof TemplateNumberModel)
64: && !(args.get(1) instanceof TemplateScalarModel))
65: throw new TemplateModelException(
66: "Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel");
67:
68: Environment env = Environment.getCurrentEnvironment();
69: BeanModel req = (BeanModel) env.getVariable("request");
70: HttpServletRequest request = (HttpServletRequest) req
71: .getWrappedObject();
72:
73: String name = ((TemplateScalarModel) args.get(0)).getAsString();
74: Object value = null;
75: if (args.get(1) instanceof TemplateScalarModel)
76: value = ((TemplateScalarModel) args.get(1)).getAsString();
77: if (args.get(1) instanceof TemplateNumberModel)
78: value = ((TemplateNumberModel) args.get(1)).getAsNumber();
79: if (args.get(1) instanceof BeanModel)
80: value = ((BeanModel) args.get(1)).getWrappedObject();
81:
82: request.setAttribute(name, value);
83: return new SimpleScalar("");
84: }
85:
86: }
|