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:
018: package javax.servlet.jsp.el;
019:
020: import java.beans.FeatureDescriptor;
021: import java.util.AbstractMap;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Enumeration;
025: import java.util.HashSet;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Set;
030: import java.util.Vector;
031:
032: import javax.el.ELContext;
033: import javax.el.ELException;
034: import javax.el.ELResolver;
035: import javax.el.PropertyNotFoundException;
036: import javax.el.PropertyNotWritableException;
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:
043: /**
044: *
045: * @since 2.1
046: */
047: public class ImplicitObjectELResolver extends ELResolver {
048:
049: private final static String[] SCOPE_NAMES = new String[] {
050: "applicationScope", "cookie", "header", "headerValues",
051: "initParam", "pageContext", "pageScope", "param",
052: "paramValues", "requestScope", "sessionScope" };
053:
054: private final static int APPLICATIONSCOPE = 0;
055:
056: private final static int COOKIE = 1;
057:
058: private final static int HEADER = 2;
059:
060: private final static int HEADERVALUES = 3;
061:
062: private final static int INITPARAM = 4;
063:
064: private final static int PAGECONTEXT = 5;
065:
066: private final static int PAGESCOPE = 6;
067:
068: private final static int PARAM = 7;
069:
070: private final static int PARAM_VALUES = 8;
071:
072: private final static int REQUEST_SCOPE = 9;
073:
074: private final static int SESSION_SCOPE = 10;
075:
076: public ImplicitObjectELResolver() {
077: super ();
078: }
079:
080: public Object getValue(ELContext context, Object base,
081: Object property) throws NullPointerException,
082: PropertyNotFoundException, ELException {
083: if (context == null) {
084: throw new NullPointerException();
085: }
086:
087: if (base == null && property != null) {
088: int idx = Arrays.binarySearch(SCOPE_NAMES, property
089: .toString());
090:
091: if (idx >= 0) {
092: PageContext page = (PageContext) context
093: .getContext(JspContext.class);
094: context.setPropertyResolved(true);
095: switch (idx) {
096: case APPLICATIONSCOPE:
097: return ScopeManager.get(page).getApplicationScope();
098: case COOKIE:
099: return ScopeManager.get(page).getCookie();
100: case HEADER:
101: return ScopeManager.get(page).getHeader();
102: case HEADERVALUES:
103: return ScopeManager.get(page).getHeaderValues();
104: case INITPARAM:
105: return ScopeManager.get(page).getInitParam();
106: case PAGECONTEXT:
107: return ScopeManager.get(page).getPageContext();
108: case PAGESCOPE:
109: return ScopeManager.get(page).getPageScope();
110: case PARAM:
111: return ScopeManager.get(page).getParam();
112: case PARAM_VALUES:
113: return ScopeManager.get(page).getParamValues();
114: case REQUEST_SCOPE:
115: return ScopeManager.get(page).getRequestScope();
116: case SESSION_SCOPE:
117: return ScopeManager.get(page).getSessionScope();
118: }
119: }
120: }
121: return null;
122: }
123:
124: public Class getType(ELContext context, Object base, Object property)
125: throws NullPointerException, PropertyNotFoundException,
126: ELException {
127: if (context == null) {
128: throw new NullPointerException();
129: }
130:
131: if (base == null && property != null) {
132: int idx = Arrays.binarySearch(SCOPE_NAMES, property
133: .toString());
134: if (idx >= 0) {
135: context.setPropertyResolved(true);
136: }
137: }
138: return null;
139: }
140:
141: public void setValue(ELContext context, Object base,
142: Object property, Object value) throws NullPointerException,
143: PropertyNotFoundException, PropertyNotWritableException,
144: ELException {
145: if (context == null) {
146: throw new NullPointerException();
147: }
148:
149: if (base == null && property != null) {
150: int idx = Arrays.binarySearch(SCOPE_NAMES, property
151: .toString());
152: if (idx >= 0) {
153: context.setPropertyResolved(true);
154: throw new PropertyNotWritableException();
155: }
156: }
157: }
158:
159: public boolean isReadOnly(ELContext context, Object base,
160: Object property) throws NullPointerException,
161: PropertyNotFoundException, ELException {
162: if (context == null) {
163: throw new NullPointerException();
164: }
165:
166: if (base == null && property != null) {
167: int idx = Arrays.binarySearch(SCOPE_NAMES, property
168: .toString());
169: if (idx >= 0) {
170: context.setPropertyResolved(true);
171: return true;
172: }
173: }
174: return false;
175: }
176:
177: public Iterator<FeatureDescriptor> getFeatureDescriptors(
178: ELContext context, Object base) {
179: List<FeatureDescriptor> feats = new ArrayList<FeatureDescriptor>(
180: SCOPE_NAMES.length);
181: FeatureDescriptor feat;
182: for (int i = 0; i < SCOPE_NAMES.length; i++) {
183: feat = new FeatureDescriptor();
184: feat.setDisplayName(SCOPE_NAMES[i]);
185: feat.setExpert(false);
186: feat.setHidden(false);
187: feat.setName(SCOPE_NAMES[i]);
188: feat.setPreferred(true);
189: feat.setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
190: feat.setValue(TYPE, String.class);
191: feats.add(feat);
192: }
193: return feats.iterator();
194: }
195:
196: public Class<String> getCommonPropertyType(ELContext context,
197: Object base) {
198: if (base == null) {
199: return String.class;
200: }
201: return null;
202: }
203:
204: private static class ScopeManager {
205: private final static String MNGR_KEY = ScopeManager.class
206: .getName();
207:
208: private final PageContext page;
209:
210: private Map applicationScope;
211:
212: private Map cookie;
213:
214: private Map header;
215:
216: private Map headerValues;
217:
218: private Map initParam;
219:
220: private Map pageScope;
221:
222: private Map param;
223:
224: private Map paramValues;
225:
226: private Map requestScope;
227:
228: private Map sessionScope;
229:
230: public ScopeManager(PageContext page) {
231: this .page = page;
232: }
233:
234: public static ScopeManager get(PageContext page) {
235: ScopeManager mngr = (ScopeManager) page
236: .getAttribute(MNGR_KEY);
237: if (mngr == null) {
238: mngr = new ScopeManager(page);
239: page.setAttribute(MNGR_KEY, mngr);
240: }
241: return mngr;
242: }
243:
244: public Map getApplicationScope() {
245: if (this .applicationScope == null) {
246: this .applicationScope = new ScopeMap() {
247: protected void setAttribute(String name,
248: Object value) {
249: page.getServletContext().setAttribute(name,
250: value);
251: }
252:
253: protected void removeAttribute(String name) {
254: page.getServletContext().removeAttribute(name);
255: }
256:
257: protected Enumeration getAttributeNames() {
258: return page.getServletContext()
259: .getAttributeNames();
260: }
261:
262: protected Object getAttribute(String name) {
263: return page.getServletContext().getAttribute(
264: name);
265: }
266: };
267: }
268: return this .applicationScope;
269: }
270:
271: public Map getCookie() {
272: if (this .cookie == null) {
273: this .cookie = new ScopeMap() {
274: protected Enumeration getAttributeNames() {
275: Cookie[] c = ((HttpServletRequest) page
276: .getRequest()).getCookies();
277: if (c != null) {
278: Vector v = new Vector();
279: for (int i = 0; i < c.length; i++) {
280: v.add(c[i].getName());
281: }
282: return v.elements();
283: }
284: return null;
285: }
286:
287: protected Object getAttribute(String name) {
288: Cookie[] c = ((HttpServletRequest) page
289: .getRequest()).getCookies();
290: if (c != null) {
291: for (int i = 0; i < c.length; i++) {
292: if (name.equals(c[i].getName())) {
293: return c[i];
294: }
295: }
296: }
297: return null;
298: }
299:
300: };
301: }
302: return this .cookie;
303: }
304:
305: public Map getHeader() {
306: if (this .header == null) {
307: this .header = new ScopeMap() {
308: protected Enumeration getAttributeNames() {
309: return ((HttpServletRequest) page.getRequest())
310: .getHeaderNames();
311: }
312:
313: protected Object getAttribute(String name) {
314: return ((HttpServletRequest) page.getRequest())
315: .getHeader(name);
316: }
317: };
318: }
319: return this .header;
320: }
321:
322: public Map getHeaderValues() {
323: if (this .headerValues == null) {
324: this .headerValues = new ScopeMap() {
325: protected Enumeration getAttributeNames() {
326: return ((HttpServletRequest) page.getRequest())
327: .getHeaderNames();
328: }
329:
330: protected Object getAttribute(String name) {
331: Enumeration e = ((HttpServletRequest) page
332: .getRequest()).getHeaders(name);
333: if (e != null) {
334: List list = new ArrayList();
335: while (e.hasMoreElements()) {
336: list.add(e.nextElement().toString());
337: }
338: return (String[]) list
339: .toArray(new String[list.size()]);
340: }
341: return null;
342: }
343:
344: };
345: }
346: return this .headerValues;
347: }
348:
349: public Map getInitParam() {
350: if (this .initParam == null) {
351: this .initParam = new ScopeMap() {
352: protected Enumeration getAttributeNames() {
353: return page.getServletContext()
354: .getInitParameterNames();
355: }
356:
357: protected Object getAttribute(String name) {
358: return page.getServletContext()
359: .getInitParameter(name);
360: }
361: };
362: }
363: return this .initParam;
364: }
365:
366: public PageContext getPageContext() {
367: return this .page;
368: }
369:
370: public Map getPageScope() {
371: if (this .pageScope == null) {
372: this .pageScope = new ScopeMap() {
373: protected void setAttribute(String name,
374: Object value) {
375: page.setAttribute(name, value);
376: }
377:
378: protected void removeAttribute(String name) {
379: page.removeAttribute(name);
380: }
381:
382: protected Enumeration getAttributeNames() {
383: return page
384: .getAttributeNamesInScope(PageContext.PAGE_SCOPE);
385: }
386:
387: protected Object getAttribute(String name) {
388: return page.getAttribute(name);
389: }
390: };
391: }
392: return this .pageScope;
393: }
394:
395: public Map getParam() {
396: if (this .param == null) {
397: this .param = new ScopeMap() {
398: protected Enumeration getAttributeNames() {
399: return page.getRequest().getParameterNames();
400: }
401:
402: protected Object getAttribute(String name) {
403: return page.getRequest().getParameter(name);
404: }
405: };
406: }
407: return this .param;
408: }
409:
410: public Map getParamValues() {
411: if (this .paramValues == null) {
412: this .paramValues = new ScopeMap() {
413: protected Object getAttribute(String name) {
414: return page.getRequest().getParameterValues(
415: name);
416: }
417:
418: protected Enumeration getAttributeNames() {
419: return page.getRequest().getParameterNames();
420: }
421: };
422: }
423: return this .paramValues;
424: }
425:
426: public Map getRequestScope() {
427: if (this .requestScope == null) {
428: this .requestScope = new ScopeMap() {
429: protected void setAttribute(String name,
430: Object value) {
431: page.getRequest().setAttribute(name, value);
432: }
433:
434: protected void removeAttribute(String name) {
435: page.getRequest().removeAttribute(name);
436: }
437:
438: protected Enumeration getAttributeNames() {
439: return page.getRequest().getAttributeNames();
440: }
441:
442: protected Object getAttribute(String name) {
443: return page.getRequest().getAttribute(name);
444: }
445: };
446: }
447: return this .requestScope;
448: }
449:
450: public Map getSessionScope() {
451: if (this .sessionScope == null) {
452: this .sessionScope = new ScopeMap() {
453: protected void setAttribute(String name,
454: Object value) {
455: ((HttpServletRequest) page.getRequest())
456: .getSession().setAttribute(name, value);
457: }
458:
459: protected void removeAttribute(String name) {
460: HttpSession session = page.getSession();
461: if (session != null) {
462: session.removeAttribute(name);
463: }
464: }
465:
466: protected Enumeration getAttributeNames() {
467: HttpSession session = page.getSession();
468: if (session != null) {
469: return session.getAttributeNames();
470: }
471: return null;
472: }
473:
474: protected Object getAttribute(String name) {
475: HttpSession session = page.getSession();
476: if (session != null) {
477: return session.getAttribute(name);
478: }
479: return null;
480: }
481: };
482: }
483: return this .sessionScope;
484: }
485: }
486:
487: private abstract static class ScopeMap extends AbstractMap {
488:
489: protected abstract Enumeration getAttributeNames();
490:
491: protected abstract Object getAttribute(String name);
492:
493: protected void removeAttribute(String name) {
494: throw new UnsupportedOperationException();
495: }
496:
497: protected void setAttribute(String name, Object value) {
498: throw new UnsupportedOperationException();
499: }
500:
501: public final Set entrySet() {
502: Enumeration e = getAttributeNames();
503: Set set = new HashSet();
504: if (e != null) {
505: while (e.hasMoreElements()) {
506: set.add(new ScopeEntry((String) e.nextElement()));
507: }
508: }
509: return set;
510: }
511:
512: private class ScopeEntry implements Map.Entry {
513:
514: private final String key;
515:
516: public ScopeEntry(String key) {
517: this .key = key;
518: }
519:
520: public Object getKey() {
521: return (Object) this .key;
522: }
523:
524: public Object getValue() {
525: return getAttribute(this .key);
526: }
527:
528: public Object setValue(Object value) {
529: if (value == null) {
530: removeAttribute(this .key);
531: } else {
532: setAttribute(this .key, value);
533: }
534: return null;
535: }
536:
537: public boolean equals(Object obj) {
538: return (obj != null && this .hashCode() == obj
539: .hashCode());
540: }
541:
542: public int hashCode() {
543: return this .key.hashCode();
544: }
545:
546: }
547:
548: public final Object get(Object key) {
549: if (key != null) {
550: return getAttribute(key.toString());
551: }
552: return null;
553: }
554:
555: public final Object put(Object key, Object value) {
556: if (key == null) {
557: throw new NullPointerException();
558: }
559: if (value == null) {
560: this .removeAttribute(key.toString());
561: } else {
562: this .setAttribute(key.toString(), value);
563: }
564: return null;
565: }
566:
567: public final Object remove(Object key) {
568: if (key == null) {
569: throw new NullPointerException();
570: }
571: this.removeAttribute(key.toString());
572: return null;
573: }
574:
575: }
576:
577: }
|