001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.internal;
020:
021: import java.util.Map;
022: import java.util.Enumeration;
023: import java.util.HashMap;
024:
025: import javax.servlet.ServletContext;
026: import javax.servlet.jsp.el.ExpressionEvaluator;
027: import javax.servlet.jsp.el.VariableResolver;
028: import javax.servlet.jsp.el.ELException;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpSession;
031: import javax.servlet.http.Cookie;
032:
033: import org.apache.beehive.netui.util.logging.Logger;
034: import org.apache.commons.el.ExpressionEvaluatorImpl;
035: import org.apache.commons.el.EnumeratedMap;
036:
037: /**
038: * Internal class used to evaluate simple action expressions.
039: *
040: * todo: need to merge this down into the expression language registration infrastructure
041: * todo: need to provdie an ImplicitObjectFactory that will create Maps for requestScope, sessionScope, etc.
042: */
043: public class InternalExpressionUtils {
044: private static final Logger _logger = Logger
045: .getInstance(InternalExpressionUtils.class);
046:
047: public static final boolean evaluateCondition(String expression,
048: Object actionForm, HttpServletRequest request,
049: ServletContext servletContext) throws ELException {
050: return ((Boolean) evaluate(expression, Boolean.class,
051: actionForm, request, servletContext)).booleanValue();
052: }
053:
054: public static final String evaluateMessage(String expression,
055: Object actionForm, HttpServletRequest request,
056: ServletContext servletContext) throws ELException {
057: return (String) evaluate(expression, String.class, actionForm,
058: request, servletContext);
059: }
060:
061: /* do not construct */
062: private InternalExpressionUtils() {
063: }
064:
065: private static final Object evaluate(String expression,
066: Class expectedType, Object actionForm,
067: HttpServletRequest request, ServletContext servletContext)
068: throws ELException {
069: // todo: can this be static / final?
070: ExpressionEvaluator ee = getExpressionEvaluator();
071: return ee
072: .evaluate(expression, expectedType,
073: getVariableResolver(actionForm, request,
074: servletContext), null);
075: }
076:
077: private static final ExpressionEvaluator getExpressionEvaluator() {
078: return new ExpressionEvaluatorImpl();
079: }
080:
081: private static final VariableResolver getVariableResolver(
082: Object actionForm, HttpServletRequest request,
083: ServletContext servletContext) {
084: return new SimpleActionVariableResolver(actionForm, request,
085: servletContext);
086: }
087:
088: private static class SimpleActionVariableResolver implements
089: VariableResolver {
090: private Object _actionForm = null;
091: private HttpServletRequest _request = null;
092: private ServletContext _servletContext = null;
093:
094: private SimpleActionVariableResolver(Object actionForm,
095: HttpServletRequest request,
096: ServletContext servletContext) {
097: _actionForm = actionForm;
098: _request = request;
099: _servletContext = servletContext;
100: }
101:
102: public Object resolveVariable(String name) {
103: // requestScope, sessionScope, applicationScope, param, paramValues, header, headerValues, cookie, initParam, <default>
104: if (name.equals("actionForm"))
105: return _actionForm;
106: else if (name.equals("requestScope"))
107: return buildRequestScopeMap(_request);
108: else if (name.equals("sessionScope"))
109: return buildSessionScopeMap(_request);
110: else if (name.equals("applicationScope"))
111: return buildServletContextMap(_servletContext);
112: else if (name.equals("param"))
113: return buildParamMap(_request);
114: else if (name.equals("paramValues"))
115: return buildParamsMap(_request);
116: else if (name.equals("header"))
117: return buildHeaderMap(_request);
118: else if (name.equals("headerValues"))
119: return buildHeadersMap(_request);
120: else if (name.equals("cookie"))
121: return buildCookieMap(_request);
122: else if (name.equals("initParam"))
123: return buildInitParamMap(_servletContext);
124: // chain up the request > session (if exists) > application
125: // note, this should handle pageFlow, globalApp, sharedFlow, and bundle if they're in the request
126: // attribute map already
127: else if (_request.getAttribute(name) != null)
128: return _request.getAttribute(name);
129: else if (_request.getSession(false) != null
130: && _request.getSession(false).getAttribute(name) != null)
131: return _request.getSession(false).getAttribute(name);
132: else
133: return _servletContext.getAttribute(name);
134: }
135:
136: private static final Map buildCookieMap(
137: HttpServletRequest httpServletRequest) {
138: HttpServletRequest servletRequest = httpServletRequest;
139: Map/*<String, Cookie>*/cookieMap = new HashMap/*<String, Cookie>*/();
140: Cookie[] cookies = servletRequest.getCookies();
141: for (int i = 0; i < cookies.length; i++) {
142: if (!cookieMap.containsKey(cookies[i].getName()))
143: cookieMap.put(cookies[i].getName(), cookies[i]);
144: }
145: return cookieMap;
146: }
147:
148: private static final Map buildHeadersMap(
149: HttpServletRequest httpServletRequest) {
150: final HttpServletRequest _servletRequest = httpServletRequest;
151: return new EnumeratedMap() {
152: public Enumeration enumerateKeys() {
153: return _servletRequest.getHeaderNames();
154: }
155:
156: public Object getValue(Object key) {
157: return (key instanceof String ? _servletRequest
158: .getHeaders((String) key) : null);
159: }
160:
161: public boolean isMutable() {
162: return false;
163: }
164: };
165: }
166:
167: private static final Map buildHeaderMap(
168: HttpServletRequest httpServletRequest) {
169: final HttpServletRequest _servletRequest = httpServletRequest;
170: return new EnumeratedMap() {
171: public Enumeration enumerateKeys() {
172: return _servletRequest.getHeaderNames();
173: }
174:
175: public Object getValue(Object key) {
176: return (key instanceof String ? _servletRequest
177: .getHeader((String) key) : null);
178: }
179:
180: public boolean isMutable() {
181: return false;
182: }
183: };
184: }
185:
186: private static final Map buildInitParamMap(
187: ServletContext servletContext) {
188: final ServletContext _servletContext = servletContext;
189: return new EnumeratedMap() {
190: public Enumeration enumerateKeys() {
191: return _servletContext.getInitParameterNames();
192: }
193:
194: public Object getValue(Object key) {
195: return (key instanceof String ? _servletContext
196: .getInitParameter((String) key) : null);
197: }
198:
199: public boolean isMutable() {
200: return false;
201: }
202: };
203: }
204:
205: private static final Map buildParamsMap(
206: HttpServletRequest servletRequest) {
207: final HttpServletRequest _servletRequest = servletRequest;
208: return new EnumeratedMap() {
209: public Enumeration enumerateKeys() {
210: return _servletRequest.getParameterNames();
211: }
212:
213: public Object getValue(Object key) {
214: return (key instanceof String ? _servletRequest
215: .getParameterValues((String) key) : null);
216: }
217:
218: public boolean isMutable() {
219: return false;
220: }
221: };
222: }
223:
224: private static final Map buildParamMap(
225: HttpServletRequest servletRequest) {
226: final HttpServletRequest _servletRequest = servletRequest;
227: return new EnumeratedMap() {
228: public Enumeration enumerateKeys() {
229: return _servletRequest.getParameterNames();
230: }
231:
232: public Object getValue(Object key) {
233: return (key instanceof String ? _servletRequest
234: .getParameter((String) key) : null);
235: }
236:
237: public boolean isMutable() {
238: return false;
239: }
240: };
241: }
242:
243: private static final Map buildRequestScopeMap(
244: HttpServletRequest servletRequest) {
245: final HttpServletRequest _servletRequest = servletRequest;
246: return new EnumeratedMap() {
247: public Enumeration enumerateKeys() {
248: return _servletRequest.getAttributeNames();
249: }
250:
251: public Object getValue(Object key) {
252: return (key instanceof String ? _servletRequest
253: .getAttribute((String) key) : null);
254: }
255:
256: public boolean isMutable() {
257: return false;
258: }
259: };
260: }
261:
262: private static final Map buildSessionScopeMap(
263: HttpServletRequest servletRequest) {
264: if (servletRequest.getSession(false) == null)
265: return null;
266:
267: final HttpSession _session = servletRequest
268: .getSession(false);
269: return new EnumeratedMap() {
270: public Enumeration enumerateKeys() {
271: return _session.getAttributeNames();
272: }
273:
274: public Object getValue(Object key) {
275: return (key instanceof String ? _session
276: .getAttribute((String) key) : null);
277: }
278:
279: public boolean isMutable() {
280: return false;
281: }
282: };
283: }
284:
285: private static final Map buildServletContextMap(
286: ServletContext servletContext) {
287: final ServletContext _servletContext = servletContext;
288: return new EnumeratedMap() {
289: public Enumeration enumerateKeys() {
290: return _servletContext.getAttributeNames();
291: }
292:
293: public Object getValue(Object key) {
294: return (key instanceof String ? _servletContext
295: .getAttribute((String) key) : null);
296: }
297:
298: public boolean isMutable() {
299: return false;
300: }
301: };
302: }
303: }
304: }
|