KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > internal > InternalExpressionUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.pageflow.internal;
19
20 import java.util.Map JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.jsp.el.ExpressionEvaluator JavaDoc;
26 import javax.servlet.jsp.el.VariableResolver JavaDoc;
27 import javax.servlet.jsp.el.ELException JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30 import javax.servlet.http.Cookie JavaDoc;
31
32 import org.apache.beehive.netui.util.logging.Logger;
33 import org.apache.commons.el.ExpressionEvaluatorImpl;
34 import org.apache.commons.el.EnumeratedMap;
35
36 /**
37  * Internal class used to evaluate simple action expressions.
38  *
39  * todo: need to merge this down into the expression language registration infrastructure
40  * todo: need to provdie an ImplicitObjectFactory that will create Maps for requestScope, sessionScope, etc.
41  */

42 public class InternalExpressionUtils
43 {
44     private static final Logger _logger = Logger.getInstance(InternalExpressionUtils.class);
45
46     public static final boolean evaluateCondition(String JavaDoc expression,
47                                                   Object JavaDoc actionForm,
48                                                   HttpServletRequest JavaDoc request,
49                                                   ServletContext JavaDoc servletContext)
50         throws ELException JavaDoc
51     {
52         return ((Boolean JavaDoc)evaluate(expression, Boolean JavaDoc.class, actionForm, request, servletContext)).booleanValue();
53     }
54
55     public static final String JavaDoc evaluateMessage(String JavaDoc expression, Object JavaDoc actionForm, HttpServletRequest JavaDoc request,
56                                                ServletContext JavaDoc servletContext)
57         throws ELException JavaDoc
58     {
59         return (String JavaDoc)evaluate(expression, String JavaDoc.class, actionForm, request, servletContext);
60     }
61
62     /* do not construct */
63     private InternalExpressionUtils() {}
64
65     private static final Object JavaDoc evaluate(String JavaDoc expression, Class JavaDoc expectedType, Object JavaDoc actionForm, HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext)
66         throws ELException JavaDoc
67     {
68         // todo: can this be static / final?
69
ExpressionEvaluator JavaDoc ee = getExpressionEvaluator();
70         return ee.evaluate(expression, expectedType, getVariableResolver(actionForm, request, servletContext), null);
71     }
72
73     private static final ExpressionEvaluator JavaDoc getExpressionEvaluator()
74     {
75         return new ExpressionEvaluatorImpl();
76     }
77
78     private static final VariableResolver JavaDoc getVariableResolver(Object JavaDoc actionForm, HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext)
79     {
80         return new SimpleActionVariableResolver(actionForm, request, servletContext);
81     }
82
83     private static class SimpleActionVariableResolver
84         implements VariableResolver JavaDoc
85     {
86         private Object JavaDoc _actionForm = null;
87         private HttpServletRequest JavaDoc _request = null;
88         private ServletContext JavaDoc _servletContext = null;
89
90         private SimpleActionVariableResolver(Object JavaDoc actionForm, HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext)
91         {
92             _actionForm = actionForm;
93             _request = request;
94             _servletContext = servletContext;
95         }
96
97         public Object JavaDoc resolveVariable(String JavaDoc name)
98         {
99             // requestScope, sessionScope, applicationScope, param, paramValues, header, headerValues, cookie, initParam, <default>
100
if(name.equals("actionForm"))
101                 return _actionForm;
102             else if(name.equals("requestScope"))
103                 return buildRequestScopeMap(_request);
104             else if(name.equals("sessionScope"))
105                 return buildSessionScopeMap(_request);
106             else if(name.equals("applicationScope"))
107                 return buildServletContextMap(_servletContext);
108             else if(name.equals("param"))
109                 return buildParamMap(_request);
110             else if(name.equals("paramValues"))
111                 return buildParamsMap(_request);
112             else if(name.equals("header"))
113                 return buildHeaderMap(_request);
114             else if(name.equals("headerValues"))
115                 return buildHeadersMap(_request);
116             else if(name.equals("cookie"))
117                 return buildCookieMap(_request);
118             else if(name.equals("initParam"))
119                 return buildInitParamMap(_servletContext);
120             // chain up the request > session (if exists) > application
121
// note, this should handle pageFlow, globalApp, sharedFlow, and bundle if they're in the request
122
// attribute map already
123
else if(_request.getAttribute(name) != null)
124                 return _request.getAttribute(name);
125             else if(_request.getSession(false) != null && _request.getSession(false).getAttribute(name) != null)
126                 return _request.getSession(false).getAttribute(name);
127             else return _servletContext.getAttribute(name);
128         }
129
130         private static final Map JavaDoc buildCookieMap(HttpServletRequest JavaDoc httpServletRequest)
131         {
132             HttpServletRequest JavaDoc servletRequest = httpServletRequest;
133             Map JavaDoc/*<String, Cookie>*/ cookieMap = new HashMap JavaDoc/*<String, Cookie>*/();
134             Cookie JavaDoc[] cookies = servletRequest.getCookies();
135             for(int i = 0; i < cookies.length; i++)
136             {
137                 if(!cookieMap.containsKey(cookies[i].getName()))
138                     cookieMap.put(cookies[i].getName(), cookies[i]);
139             }
140             return cookieMap;
141         }
142
143         private static final Map JavaDoc buildHeadersMap(HttpServletRequest JavaDoc httpServletRequest)
144         {
145             final HttpServletRequest JavaDoc _servletRequest = httpServletRequest;
146             return new EnumeratedMap()
147             {
148                 public Enumeration JavaDoc enumerateKeys()
149                 {
150                     return _servletRequest.getHeaderNames();
151                 }
152
153                 public Object JavaDoc getValue(Object JavaDoc key)
154                 {
155                     return (key instanceof String JavaDoc ? _servletRequest.getHeaders((String JavaDoc)key) : null);
156                 }
157
158                 public boolean isMutable() {return false;}
159             };
160         }
161
162         private static final Map JavaDoc buildHeaderMap(HttpServletRequest JavaDoc httpServletRequest)
163         {
164             final HttpServletRequest JavaDoc _servletRequest = httpServletRequest;
165             return new EnumeratedMap()
166             {
167                 public Enumeration JavaDoc enumerateKeys()
168                 {
169                     return _servletRequest.getHeaderNames();
170                 }
171
172                 public Object JavaDoc getValue(Object JavaDoc key)
173                 {
174                     return (key instanceof String JavaDoc ? _servletRequest.getHeader((String JavaDoc)key) : null);
175                 }
176
177                 public boolean isMutable() {return false;}
178             };
179         }
180
181         private static final Map JavaDoc buildInitParamMap(ServletContext JavaDoc servletContext)
182         {
183             final ServletContext JavaDoc _servletContext = servletContext;
184             return new EnumeratedMap()
185             {
186                 public Enumeration JavaDoc enumerateKeys()
187                 {
188                     return _servletContext.getInitParameterNames();
189                 }
190
191                 public Object JavaDoc getValue(Object JavaDoc key)
192                 {
193                     return (key instanceof String JavaDoc ? _servletContext.getInitParameter((String JavaDoc)key) : null);
194                 }
195
196                 public boolean isMutable() {return false;}
197             };
198         }
199
200         private static final Map JavaDoc buildParamsMap(HttpServletRequest JavaDoc servletRequest)
201         {
202             final HttpServletRequest JavaDoc _servletRequest = servletRequest;
203             return new EnumeratedMap()
204             {
205                 public Enumeration JavaDoc enumerateKeys()
206                 {
207                     return _servletRequest.getParameterNames();
208                 }
209
210                 public Object JavaDoc getValue(Object JavaDoc key)
211                 {
212                     return (key instanceof String JavaDoc ? _servletRequest.getParameterValues((String JavaDoc)key) : null);
213                 }
214
215                 public boolean isMutable() {return false;}
216             };
217         }
218
219         private static final Map JavaDoc buildParamMap(HttpServletRequest JavaDoc servletRequest)
220         {
221             final HttpServletRequest JavaDoc _servletRequest = servletRequest;
222             return new EnumeratedMap()
223             {
224                 public Enumeration JavaDoc enumerateKeys()
225                 {
226                     return _servletRequest.getParameterNames();
227                 }
228
229                 public Object JavaDoc getValue(Object JavaDoc key)
230                 {
231                     return (key instanceof String JavaDoc ? _servletRequest.getParameter((String JavaDoc)key) : null);
232                 }
233
234                 public boolean isMutable() {return false;}
235             };
236         }
237
238         private static final Map JavaDoc buildRequestScopeMap(HttpServletRequest JavaDoc servletRequest)
239         {
240             final HttpServletRequest JavaDoc _servletRequest = servletRequest;
241             return new EnumeratedMap()
242             {
243                 public Enumeration JavaDoc enumerateKeys()
244                 {
245                     return _servletRequest.getAttributeNames();
246                 }
247
248                 public Object JavaDoc getValue(Object JavaDoc key)
249                 {
250                     return (key instanceof String JavaDoc ? _servletRequest.getAttribute((String JavaDoc)key) : null);
251                 }
252
253                 public boolean isMutable() {return false;}
254             };
255         }
256
257         private static final Map JavaDoc buildSessionScopeMap(HttpServletRequest JavaDoc servletRequest)
258         {
259             if(servletRequest.getSession(false) == null)
260                 return null;
261
262             final HttpSession JavaDoc _session = servletRequest.getSession(false);
263             return new EnumeratedMap()
264             {
265                 public Enumeration JavaDoc enumerateKeys()
266                 {
267                     return _session.getAttributeNames();
268                 }
269
270                 public Object JavaDoc getValue(Object JavaDoc key)
271                 {
272                     return (key instanceof String JavaDoc ? _session.getAttribute((String JavaDoc)key) : null);
273                 }
274
275                 public boolean isMutable() {return false;}
276             };
277         }
278
279         private static final Map JavaDoc buildServletContextMap(ServletContext JavaDoc servletContext)
280         {
281             final ServletContext JavaDoc _servletContext = servletContext;
282             return new EnumeratedMap()
283             {
284                 public Enumeration JavaDoc enumerateKeys()
285                 {
286                     return _servletContext.getAttributeNames();
287                 }
288
289                 public Object JavaDoc getValue(Object JavaDoc key)
290                 {
291                     return (key instanceof String JavaDoc ? _servletContext.getAttribute((String JavaDoc)key) : null);
292                 }
293
294                 public boolean isMutable() {return false;}
295             };
296         }
297     }
298 }
299
Popular Tags