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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.el;
030:
031: import com.caucho.el.Expr;
032: import com.caucho.jsp.PageContextImpl;
033: import com.caucho.vfs.WriteStream;
034:
035: import javax.el.ELContext;
036: import javax.el.ELException;
037: import javax.servlet.ServletContext;
038: import javax.servlet.http.Cookie;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.jsp.PageContext;
041: import java.io.IOException;
042: import java.util.*;
043:
044: public class ImplicitObjectExpr extends Expr {
045: final static int PAGE_CONTEXT = 1;
046:
047: final static int APPLICATION_SCOPE = PAGE_CONTEXT + 1;
048: final static int SESSION_SCOPE = APPLICATION_SCOPE + 1;
049: final static int REQUEST_SCOPE = SESSION_SCOPE + 1;
050: final static int PAGE_SCOPE = REQUEST_SCOPE + 1;
051:
052: final static int PARAM = PAGE_SCOPE + 1;
053: final static int PARAM_VALUES = PARAM + 1;
054:
055: final static int INIT_PARAM = PARAM_VALUES + 1;
056:
057: final static int HEADER = INIT_PARAM + 1;
058: final static int HEADER_VALUES = HEADER + 1;
059:
060: final static int COOKIE = HEADER_VALUES + 1;
061:
062: private String _id;
063: private int _index;
064:
065: public ImplicitObjectExpr(String id) {
066: _id = id;
067:
068: if ("pageContext".equals(id))
069: _index = PAGE_CONTEXT;
070: else if ("applicationScope".equals(id))
071: _index = APPLICATION_SCOPE;
072: else if ("sessionScope".equals(id))
073: _index = SESSION_SCOPE;
074: else if ("requestScope".equals(id))
075: _index = REQUEST_SCOPE;
076: else if ("pageScope".equals(id))
077: _index = PAGE_SCOPE;
078: else if ("param".equals(id))
079: _index = PARAM;
080: else if ("paramValues".equals(id))
081: _index = PARAM_VALUES;
082: else if ("initParam".equals(id))
083: _index = INIT_PARAM;
084: else if ("header".equals(id))
085: _index = HEADER;
086: else if ("headerValues".equals(id))
087: _index = HEADER_VALUES;
088: else if ("cookie".equals(id))
089: _index = COOKIE;
090: else
091: throw new IllegalArgumentException();
092: }
093:
094: public Expr createField(Expr field) {
095: switch (_index) {
096: case APPLICATION_SCOPE:
097: case SESSION_SCOPE:
098: case REQUEST_SCOPE:
099: case PAGE_SCOPE:
100: case PARAM:
101: case PARAM_VALUES:
102: case HEADER:
103: case HEADER_VALUES:
104: case COOKIE:
105: case INIT_PARAM:
106: return new ImplicitFieldExpr(_index, field);
107:
108: default:
109: return super .createField(field);
110: }
111: }
112:
113: /**
114: * Evaluate the expr as an object.
115: *
116: * @param env the page context
117: */
118: @Override
119: public Object getValue(ELContext env) throws ELException {
120: if (!(env instanceof PageContextImpl.PageELContext))
121: return null;
122:
123: PageContextImpl page = ((PageContextImpl.PageELContext) env)
124: .getPageContext();
125:
126: switch (_index) {
127: case PAGE_CONTEXT:
128: return page;
129:
130: case APPLICATION_SCOPE:
131: return new AttributeMap(page, PageContext.APPLICATION_SCOPE);
132:
133: case SESSION_SCOPE:
134: return new AttributeMap(page, PageContext.SESSION_SCOPE);
135:
136: case REQUEST_SCOPE:
137: return new AttributeMap(page, PageContext.REQUEST_SCOPE);
138:
139: case PAGE_SCOPE:
140: return new AttributeMap(page, PageContext.PAGE_SCOPE);
141:
142: case PARAM_VALUES:
143: return page.getRequest().getParameterMap();
144:
145: case PARAM: {
146: HashMap<String, String> map = new HashMap<String, String>();
147: Map pMap = page.getRequest().getParameterMap();
148: Iterator iter = pMap.entrySet().iterator();
149:
150: while (iter.hasNext()) {
151: Map.Entry entry = (Map.Entry) iter.next();
152: String key = (String) entry.getKey();
153: String[] value = (String[]) entry.getValue();
154: map.put(key, value[0]);
155: }
156:
157: return map;
158: }
159:
160: case INIT_PARAM: {
161: ServletContext app = page.getServletContext();
162: HashMap<String, String> map = new HashMap<String, String>();
163: Enumeration e = app.getInitParameterNames();
164:
165: while (e.hasMoreElements()) {
166: String name = (String) e.nextElement();
167:
168: map.put(name, app.getInitParameter(name));
169: }
170:
171: return map;
172: }
173:
174: case HEADER: {
175: HttpServletRequest req = (HttpServletRequest) page
176: .getRequest();
177: HashMap<String, String> map = new HashMap<String, String>();
178: Enumeration e = req.getHeaderNames();
179:
180: while (e.hasMoreElements()) {
181: String name = (String) e.nextElement();
182:
183: map.put(name, req.getHeader(name));
184: }
185:
186: return map;
187: }
188:
189: case HEADER_VALUES: {
190: HttpServletRequest req = (HttpServletRequest) page
191: .getRequest();
192: HashMap<String, String[]> map = new HashMap<String, String[]>();
193: Enumeration e = req.getHeaderNames();
194:
195: while (e.hasMoreElements()) {
196: String name = (String) e.nextElement();
197: Enumeration values = req.getHeaders(name);
198:
199: ArrayList<String> list = new ArrayList<String>();
200:
201: while (values.hasMoreElements())
202: list.add((String) values.nextElement());
203:
204: map.put(name, list.toArray(new String[list.size()]));
205: }
206:
207: return map;
208: }
209:
210: case COOKIE: {
211: HashMap<String, Object> map = new HashMap<String, Object>();
212: Cookie[] cookies = ((HttpServletRequest) page.getRequest())
213: .getCookies();
214:
215: for (int i = 0; cookies != null && i < cookies.length; i++) {
216: if (map.get(cookies[i].getName()) == null)
217: map.put(cookies[i].getName(), cookies[i]);
218: }
219:
220: return map;
221: }
222: }
223:
224: throw new UnsupportedOperationException();
225: }
226:
227: public String toString() {
228: return _id;
229: }
230:
231: /**
232: * Prints the code to create an IdExpr.
233: */
234: public void printCreate(WriteStream os) throws IOException {
235: os.print("new com.caucho.jsp.el.ImplicitObjectExpr(\"");
236: printEscapedString(os, _id);
237: os.print("\")");
238: }
239:
240: public static class AttributeMap extends AbstractMap {
241: private PageContext _pageContext;
242: private int _scope;
243:
244: AttributeMap(PageContext pageContext, int scope) {
245: _pageContext = pageContext;
246: _scope = scope;
247: }
248:
249: public Object get(Object key) {
250: return _pageContext.getAttribute((String) key, _scope);
251: }
252:
253: public Object put(Object key, Object value) {
254: _pageContext.setAttribute((String) key, value, _scope);
255:
256: return null;
257: }
258:
259: private EntrySet _entrySet;
260:
261: public Set entrySet() {
262: if (_entrySet == null)
263: _entrySet = new EntrySet();
264:
265: return _entrySet;
266: }
267:
268: public class EntrySet extends AbstractSet {
269: public int size() {
270: Enumeration e = _pageContext
271: .getAttributeNamesInScope(_scope);
272: int i = 0;
273: while (e.hasMoreElements()) {
274: e.nextElement();
275: i++;
276: }
277:
278: return i;
279: }
280:
281: public Iterator iterator() {
282: return new EntryIterator();
283: }
284: }
285:
286: public class EntryIterator implements Iterator, Map.Entry {
287: Enumeration _e;
288: String _name;
289: Object _value;
290:
291: EntryIterator() {
292: _e = _pageContext.getAttributeNamesInScope(_scope);
293: }
294:
295: public boolean hasNext() {
296: return _e.hasMoreElements();
297: }
298:
299: public Object next() {
300: _name = (String) _e.nextElement();
301: _value = _pageContext.getAttribute(_name, _scope);
302:
303: return this ;
304: }
305:
306: public void remove() {
307: throw new UnsupportedOperationException();
308: }
309:
310: public Object getKey() {
311: return _name;
312: }
313:
314: public Object getValue() {
315: return _value;
316: }
317:
318: public Object setValue(Object value) {
319: _pageContext.setAttribute(_name, value, _scope);
320:
321: Object oldValue = _value;
322: _value = value;
323:
324: return oldValue;
325: }
326:
327: public int hashCode() {
328: return _name.hashCode();
329: }
330:
331: public boolean equals(Object obj) {
332: if (!(obj instanceof EntryIterator))
333: return false;
334:
335: EntryIterator entry = (EntryIterator) obj;
336:
337: return (_name.equals(entry._name) && (_value == null
338: && entry._value == null || _value != null
339: && _value.equals(entry._value)));
340: }
341: }
342: }
343: }
|