001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.ext.servlet;
054:
055: import java.util.HashMap;
056: import java.util.Map;
057:
058: import javax.servlet.ServletContext;
059: import javax.servlet.http.HttpServletRequest;
060: import javax.servlet.http.HttpSession;
061:
062: import freemarker.template.ObjectWrapper;
063: import freemarker.template.SimpleHash;
064: import freemarker.template.TemplateModel;
065: import freemarker.template.TemplateModelException;
066:
067: /**
068: * An extension of SimpleHash that looks up keys in the hash, then in the
069: * request, session, and servlet context scopes. Makes "Application", "Session"
070: * and "Request" keys largely obsolete, however we keep them for backward
071: * compatibility (also, "Request" is required for proper operation of JSP
072: * taglibs).
073: * It is on purpose that we didn't override <tt>keys</tt> and <tt>values</tt>
074: * methods. That way, only those variables assigned into the hash directly by a
075: * subclass of <tt>FreemarkerServlet</tt> that overrides
076: * <tt>preTemplateProcess</tt>) are discovered as "page" variables by the FM
077: * JSP PageContext implementation.
078: * @author Attila Szegedi
079: * @version $Id: AllHttpScopesHashModel.java,v 1.5.4.1 2006/04/26 12:22:07 szegedia Exp $
080: */
081: public class AllHttpScopesHashModel extends SimpleHash {
082: private final ObjectWrapper wrapper;
083: private final ServletContext context;
084: private final HttpServletRequest request;
085: private final Map unlistedModels = new HashMap();
086:
087: AllHttpScopesHashModel(ObjectWrapper wrapper,
088: ServletContext context, HttpServletRequest request) {
089: this .wrapper = wrapper;
090: this .context = context;
091: this .request = request;
092: }
093:
094: /**
095: * Stores a model in the hash so that it doesn't show up in <tt>keys()</tt>
096: * and <tt>values()</tt> methods. Used to put the Application, Session,
097: * Request, RequestParameters and JspTaglibs objects.
098: * @param key the key under which the model is stored
099: * @param model the stored model
100: */
101: public void putUnlistedModel(String key, TemplateModel model) {
102: unlistedModels.put(key, model);
103: }
104:
105: public TemplateModel get(String key) throws TemplateModelException {
106: // Lookup in page scope
107: TemplateModel model = super .get(key);
108: if (model != null) {
109: return model;
110: }
111:
112: // Look in unlisted models
113: model = (TemplateModel) unlistedModels.get(key);
114: if (model != null) {
115: return model;
116: }
117:
118: // Lookup in request scope
119: Object obj = request.getAttribute(key);
120: if (obj != null) {
121: return wrapper.wrap(obj);
122: }
123:
124: // Lookup in session scope
125: HttpSession session = request.getSession(false);
126: if (session != null) {
127: obj = session.getAttribute(key);
128: if (obj != null) {
129: return wrapper.wrap(obj);
130: }
131: }
132:
133: // Lookup in application scope
134: obj = context.getAttribute(key);
135: if (obj != null) {
136: return wrapper.wrap(obj);
137: }
138:
139: // return wrapper's null object (probably null).
140: return wrapper.wrap(null);
141: }
142: }
|