001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package javax.servlet.jsp.el;
031:
032: import javax.el.ELContext;
033: import javax.el.ELResolver;
034: import javax.el.PropertyNotWritableException;
035: import javax.servlet.ServletContext;
036: import javax.servlet.ServletRequest;
037: import javax.servlet.http.Cookie;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpSession;
040: import javax.servlet.jsp.JspContext;
041: import javax.servlet.jsp.PageContext;
042: import java.beans.FeatureDescriptor;
043: import java.util.ArrayList;
044: import java.util.Enumeration;
045: import java.util.HashMap;
046: import java.util.Iterator;
047: import java.util.Map;
048:
049: /**
050: * Variable resolution for JSP variables
051: */
052: public class ImplicitObjectELResolver extends ELResolver {
053: private static final HashMap<Object, Prop> _propMap = new HashMap<Object, Prop>();
054:
055: private static final ArrayList<FeatureDescriptor> _featureDescriptors = new ArrayList<FeatureDescriptor>();
056:
057: @Override
058: public Class<String> getCommonPropertyType(ELContext context,
059: Object base) {
060: if (base == null)
061: return String.class;
062: else
063: return null;
064: }
065:
066: @Override
067: public Iterator<FeatureDescriptor> getFeatureDescriptors(
068: ELContext context, Object base) {
069: if (base != null)
070: return null;
071:
072: return _featureDescriptors.iterator();
073: }
074:
075: @Override
076: public Class getType(ELContext context, Object base, Object property) {
077: if (base != null)
078: return null;
079:
080: Prop prop = _propMap.get(property);
081: if (prop == null)
082: return null;
083:
084: context.setPropertyResolved(true);
085:
086: return null;
087: }
088:
089: @Override
090: public Object getValue(ELContext context, Object base,
091: Object property) {
092: if (base != null)
093: return null;
094:
095: Prop prop = _propMap.get(property);
096: if (prop == null)
097: return null;
098:
099: context.setPropertyResolved(true);
100:
101: PageContext jspContext = (PageContext) context
102: .getContext(JspContext.class);
103:
104: switch (prop) {
105: case PAGE_CONTEXT:
106: return jspContext;
107:
108: case PAGE_SCOPE: {
109: HashMap<String, Object> map = new HashMap<String, Object>();
110:
111: Enumeration e = jspContext.getAttributeNames();
112: while (e.hasMoreElements()) {
113: String name = (String) e.nextElement();
114:
115: map.put(name, jspContext.getAttribute(name));
116: }
117:
118: return map;
119: }
120:
121: case REQUEST_SCOPE: {
122: HashMap<String, Object> map = new HashMap<String, Object>();
123:
124: ServletRequest request = jspContext.getRequest();
125:
126: Enumeration e = request.getAttributeNames();
127: while (e.hasMoreElements()) {
128: String name = (String) e.nextElement();
129:
130: map.put(name, request.getAttribute(name));
131: }
132:
133: return map;
134: }
135:
136: case SESSION_SCOPE: {
137: HashMap<String, Object> map = new HashMap<String, Object>();
138:
139: HttpSession session = jspContext.getSession();
140:
141: if (session == null)
142: return null;
143:
144: Enumeration e = session.getAttributeNames();
145: while (e.hasMoreElements()) {
146: String name = (String) e.nextElement();
147:
148: map.put(name, session.getAttribute(name));
149: }
150:
151: return map;
152: }
153:
154: case APPLICATION_SCOPE: {
155: HashMap<String, Object> map = new HashMap<String, Object>();
156:
157: ServletContext app = jspContext.getServletContext();
158:
159: Enumeration e = app.getAttributeNames();
160: while (e.hasMoreElements()) {
161: String name = (String) e.nextElement();
162:
163: map.put(name, app.getAttribute(name));
164: }
165:
166: return map;
167: }
168:
169: case PARAM: {
170: HashMap<String, Object> map = new HashMap<String, Object>();
171:
172: ServletRequest request = jspContext.getRequest();
173:
174: Enumeration e = request.getParameterNames();
175: while (e.hasMoreElements()) {
176: String name = (String) e.nextElement();
177:
178: map.put(name, request.getParameter(name));
179: }
180:
181: return map;
182: }
183:
184: case PARAM_VALUES: {
185: HashMap<String, Object> map = new HashMap<String, Object>();
186:
187: ServletRequest request = jspContext.getRequest();
188:
189: Enumeration e = request.getParameterNames();
190: while (e.hasMoreElements()) {
191: String name = (String) e.nextElement();
192:
193: map.put(name, request.getParameterValues(name));
194: }
195:
196: return map;
197: }
198:
199: case HEADER: {
200: HashMap<String, Object> map = new HashMap<String, Object>();
201:
202: HttpServletRequest request = (HttpServletRequest) jspContext
203: .getRequest();
204:
205: Enumeration e = request.getHeaderNames();
206: while (e.hasMoreElements()) {
207: String name = (String) e.nextElement();
208:
209: map.put(name, request.getHeader(name));
210: }
211:
212: return map;
213: }
214:
215: case HEADER_VALUES: {
216: HashMap<String, Object> map = new HashMap<String, Object>();
217:
218: HttpServletRequest request = (HttpServletRequest) jspContext
219: .getRequest();
220:
221: Enumeration e = request.getHeaderNames();
222: while (e.hasMoreElements()) {
223: String name = (String) e.nextElement();
224:
225: map.put(name, request.getHeaders(name));
226: }
227:
228: return map;
229: }
230:
231: case COOKIE: {
232: HashMap<String, Object> map = new HashMap<String, Object>();
233:
234: HttpServletRequest request = (HttpServletRequest) jspContext
235: .getRequest();
236:
237: Cookie[] cookies = request.getCookies();
238:
239: if (cookies == null)
240: return map;
241:
242: for (int i = cookies.length - 1; i >= 0; i--) {
243: map.put(cookies[i].getName(), cookies[i].getValue());
244: }
245:
246: return map;
247: }
248:
249: case INIT_PARAM: {
250: HashMap<String, Object> map = new HashMap<String, Object>();
251:
252: ServletContext app = jspContext.getServletContext();
253:
254: Enumeration e = app.getInitParameterNames();
255: while (e.hasMoreElements()) {
256: String name = (String) e.nextElement();
257:
258: map.put(name, app.getInitParameter(name));
259: }
260:
261: return map;
262: }
263: }
264:
265: return null;
266: }
267:
268: @Override
269: public boolean isReadOnly(ELContext context, Object base,
270: Object property) {
271: if (base != null)
272: return true;
273:
274: Prop prop = _propMap.get(property);
275: if (prop == null)
276: return true;
277:
278: context.setPropertyResolved(true);
279: return true;
280: }
281:
282: @Override
283: public void setValue(ELContext context, Object base,
284: Object property, Object value) {
285: if (base != null)
286: return;
287:
288: Prop prop = _propMap.get(property);
289: if (prop == null)
290: return;
291:
292: context.setPropertyResolved(true);
293:
294: throw new PropertyNotWritableException(String.valueOf(value));
295: }
296:
297: private enum Prop {
298: PAGE_CONTEXT, PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, APPLICATION_SCOPE, PARAM, PARAM_VALUES, HEADER, HEADER_VALUES, COOKIE, INIT_PARAM
299: };
300:
301: static {
302: _propMap.put("pageContext", Prop.PAGE_CONTEXT);
303: _propMap.put("pageScope", Prop.PAGE_SCOPE);
304: _propMap.put("requestScope", Prop.REQUEST_SCOPE);
305: _propMap.put("sessionScope", Prop.SESSION_SCOPE);
306: _propMap.put("applicationScope", Prop.APPLICATION_SCOPE);
307: _propMap.put("param", Prop.PARAM);
308: _propMap.put("paramValues", Prop.PARAM_VALUES);
309: _propMap.put("header", Prop.HEADER);
310: _propMap.put("headerValues", Prop.HEADER_VALUES);
311: _propMap.put("cookie", Prop.COOKIE);
312: _propMap.put("initParam", Prop.INIT_PARAM);
313:
314: for (Object key : _propMap.keySet()) {
315: String name = String.valueOf(key);
316:
317: FeatureDescriptor desc = new FeatureDescriptor();
318: desc.setName(name);
319: desc.setDisplayName(name);
320: desc.setShortDescription("");
321: desc.setExpert(false);
322: desc.setHidden(false);
323: desc.setPreferred(true);
324:
325: desc.setValue(ELResolver.TYPE, Map.class);
326:
327: desc.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME,
328: Boolean.TRUE);
329:
330: _featureDescriptors.add(desc);
331: }
332: }
333: }
|