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 com.caucho.jsp.el;
031:
032: import com.caucho.el.ELParser;
033: import com.caucho.el.Expr;
034: import com.caucho.util.*;
035:
036: import javax.el.ELContext;
037: import javax.servlet.*;
038: import javax.servlet.http.*;
039: import java.util.*;
040:
041: /**
042: * Parses the expression.
043: */
044: abstract public class ServletELContext extends ELContext {
045: abstract public ServletContext getApplication();
046:
047: abstract public Object getApplicationScope();
048:
049: public Object getCookie() {
050: HttpServletRequest request = getRequest();
051:
052: if (request == null)
053: return null;
054:
055: HashMap<String, Object> map = new HashMap<String, Object>();
056: Cookie[] cookies = getRequest().getCookies();
057:
058: for (int i = 0; cookies != null && i < cookies.length; i++) {
059: if (map.get(cookies[i].getName()) == null)
060: map.put(cookies[i].getName(), cookies[i]);
061: }
062:
063: return map;
064: }
065:
066: public Object getHeader() {
067: HttpServletRequest request = getRequest();
068:
069: if (request == null)
070: return null;
071:
072: HashMap<String, String> map = new CaseInsensitiveHashMap<String>();
073: Enumeration e = request.getHeaderNames();
074:
075: while (e.hasMoreElements()) {
076: String name = (String) e.nextElement();
077: String value = request.getHeader(name);
078:
079: map.put(name, value);
080: }
081:
082: return map;
083: }
084:
085: public Object getHeader(String field) {
086: HttpServletRequest request = getRequest();
087:
088: if (request != null)
089: return request.getHeader(field);
090: else
091: return null;
092: }
093:
094: public Object getHeaderValues() {
095: HttpServletRequest request = getRequest();
096:
097: if (request == null)
098: return null;
099:
100: HashMap<String, String[]> map = new CaseInsensitiveHashMap<String[]>();
101: Enumeration e = request.getHeaderNames();
102:
103: while (e.hasMoreElements()) {
104: String name = (String) e.nextElement();
105: Enumeration values = request.getHeaders(name);
106:
107: ArrayList<String> list = new ArrayList<String>();
108:
109: while (values.hasMoreElements())
110: list.add((String) values.nextElement());
111:
112: map.put(name, list.toArray(new String[list.size()]));
113: }
114:
115: return map;
116: }
117:
118: public Object getHeaderValues(String field) {
119: HttpServletRequest request = getRequest();
120:
121: if (request != null) {
122: Enumeration values = request.getHeaders(field);
123:
124: ArrayList<String> list = new ArrayList<String>();
125:
126: while (values != null && values.hasMoreElements())
127: list.add((String) values.nextElement());
128:
129: return list.toArray(new String[list.size()]);
130: } else
131: return null;
132: }
133:
134: public Object getParameter() {
135: HttpServletRequest request = getRequest();
136:
137: if (request == null)
138: return null;
139:
140: HashMap<String, String> map = new CaseInsensitiveHashMap<String>();
141: Enumeration e = request.getParameterNames();
142:
143: while (e.hasMoreElements()) {
144: String name = (String) e.nextElement();
145: String value = request.getParameter(name);
146:
147: map.put(name, value);
148: }
149:
150: return map;
151: }
152:
153: public Object getParameter(String field) {
154: HttpServletRequest request = getRequest();
155:
156: if (request != null)
157: return request.getParameter(field);
158: else
159: return null;
160: }
161:
162: public Object getParameterValues() {
163: HttpServletRequest request = getRequest();
164:
165: if (request == null)
166: return null;
167:
168: HashMap<String, String[]> map = new CaseInsensitiveHashMap<String[]>();
169: Enumeration e = request.getParameterNames();
170:
171: while (e.hasMoreElements()) {
172: String name = (String) e.nextElement();
173: String[] values = request.getParameterValues(name);
174:
175: map.put(name, values);
176: }
177:
178: return map;
179: }
180:
181: public Object getParameterValues(String field) {
182: HttpServletRequest request = getRequest();
183:
184: if (request != null) {
185: String[] values = request.getParameterValues(field);
186:
187: return values;
188: } else
189: return null;
190: }
191:
192: abstract public HttpServletRequest getRequest();
193:
194: abstract public Object getRequestScope();
195:
196: public Object getSessionScope() {
197: HttpServletRequest request = getRequest();
198:
199: if (request == null)
200: return null;
201:
202: HttpSession session = request.getSession(false);
203:
204: if (session == null)
205: return null;
206:
207: HashMap<String, Object> map = new HashMap<String, Object>();
208:
209: Enumeration e = session.getAttributeNames();
210:
211: while (e.hasMoreElements()) {
212: String name = (String) e.nextElement();
213: Object value = request.getAttribute(name);
214:
215: map.put(name, value);
216: }
217:
218: return map;
219: }
220: }
|