1 28 29 package com.caucho.jsp.el; 30 31 import com.caucho.el.Expr; 32 import com.caucho.jsp.PageContextImpl; 33 import com.caucho.vfs.WriteStream; 34 35 import javax.el.ELContext; 36 import javax.el.ELException; 37 import javax.servlet.ServletContext ; 38 import javax.servlet.http.Cookie ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.jsp.PageContext ; 41 import java.io.IOException ; 42 import java.util.*; 43 44 public class ImplicitObjectExpr extends Expr { 45 final static int PAGE_CONTEXT = 1; 46 47 final static int APPLICATION_SCOPE = PAGE_CONTEXT + 1; 48 final static int SESSION_SCOPE = APPLICATION_SCOPE + 1; 49 final static int REQUEST_SCOPE = SESSION_SCOPE + 1; 50 final static int PAGE_SCOPE = REQUEST_SCOPE + 1; 51 52 final static int PARAM = PAGE_SCOPE + 1; 53 final static int PARAM_VALUES = PARAM + 1; 54 55 final static int INIT_PARAM = PARAM_VALUES + 1; 56 57 final static int HEADER = INIT_PARAM + 1; 58 final static int HEADER_VALUES = HEADER + 1; 59 60 final static int COOKIE = HEADER_VALUES + 1; 61 62 private String _id; 63 private int _index; 64 65 public ImplicitObjectExpr(String id) 66 { 67 _id = id; 68 69 if ("pageContext".equals(id)) 70 _index = PAGE_CONTEXT; 71 else if ("applicationScope".equals(id)) 72 _index = APPLICATION_SCOPE; 73 else if ("sessionScope".equals(id)) 74 _index = SESSION_SCOPE; 75 else if ("requestScope".equals(id)) 76 _index = REQUEST_SCOPE; 77 else if ("pageScope".equals(id)) 78 _index = PAGE_SCOPE; 79 else if ("param".equals(id)) 80 _index = PARAM; 81 else if ("paramValues".equals(id)) 82 _index = PARAM_VALUES; 83 else if ("initParam".equals(id)) 84 _index = INIT_PARAM; 85 else if ("header".equals(id)) 86 _index = HEADER; 87 else if ("headerValues".equals(id)) 88 _index = HEADER_VALUES; 89 else if ("cookie".equals(id)) 90 _index = COOKIE; 91 else 92 throw new IllegalArgumentException (); 93 } 94 95 public Expr createField(Expr field) 96 { 97 switch (_index) { 98 case APPLICATION_SCOPE: 99 case SESSION_SCOPE: 100 case REQUEST_SCOPE: 101 case PAGE_SCOPE: 102 case PARAM: 103 case PARAM_VALUES: 104 case HEADER: 105 case HEADER_VALUES: 106 case COOKIE: 107 case INIT_PARAM: 108 return new ImplicitFieldExpr(_index, field); 109 110 default: 111 return super.createField(field); 112 } 113 } 114 115 120 @Override 121 public Object getValue(ELContext env) 122 throws ELException 123 { 124 if (! (env instanceof PageContextImpl.PageELContext)) 125 return null; 126 127 PageContextImpl page 128 = ((PageContextImpl.PageELContext) env).getPageContext(); 129 130 switch (_index) { 131 case PAGE_CONTEXT: 132 return page; 133 134 case APPLICATION_SCOPE: 135 return new AttributeMap(page, PageContext.APPLICATION_SCOPE); 136 137 case SESSION_SCOPE: 138 return new AttributeMap(page, PageContext.SESSION_SCOPE); 139 140 case REQUEST_SCOPE: 141 return new AttributeMap(page, PageContext.REQUEST_SCOPE); 142 143 case PAGE_SCOPE: 144 return new AttributeMap(page, PageContext.PAGE_SCOPE); 145 146 case PARAM_VALUES: 147 return page.getRequest().getParameterMap(); 148 149 case PARAM: { 150 HashMap<String ,String > map = new HashMap<String ,String >(); 151 Map pMap = page.getRequest().getParameterMap(); 152 Iterator iter = pMap.entrySet().iterator(); 153 154 while (iter.hasNext()) { 155 Map.Entry entry = (Map.Entry) iter.next(); 156 String key = (String ) entry.getKey(); 157 String []value = (String []) entry.getValue(); 158 map.put(key, value[0]); 159 } 160 161 return map; 162 } 163 164 case INIT_PARAM: 165 { 166 ServletContext app = page.getServletContext(); 167 HashMap<String ,String > map = new HashMap<String ,String >(); 168 Enumeration e = app.getInitParameterNames(); 169 170 while (e.hasMoreElements()) { 171 String name = (String ) e.nextElement(); 172 173 map.put(name, app.getInitParameter(name)); 174 } 175 176 return map; 177 } 178 179 case HEADER: 180 { 181 HttpServletRequest req = (HttpServletRequest ) page.getRequest(); 182 HashMap<String ,String > map = new HashMap<String ,String >(); 183 Enumeration e = req.getHeaderNames(); 184 185 while (e.hasMoreElements()) { 186 String name = (String ) e.nextElement(); 187 188 map.put(name, req.getHeader(name)); 189 } 190 191 return map; 192 } 193 194 case HEADER_VALUES: 195 { 196 HttpServletRequest req = (HttpServletRequest ) page.getRequest(); 197 HashMap<String ,String []> map = new HashMap<String ,String []>(); 198 Enumeration e = req.getHeaderNames(); 199 200 while (e.hasMoreElements()) { 201 String name = (String ) e.nextElement(); 202 Enumeration values = req.getHeaders(name); 203 204 ArrayList<String > list = new ArrayList<String >(); 205 206 while (values.hasMoreElements()) 207 list.add((String ) values.nextElement()); 208 209 map.put(name, list.toArray(new String [list.size()])); 210 } 211 212 return map; 213 } 214 215 case COOKIE: 216 { 217 HashMap<String ,Object > map = new HashMap<String ,Object >(); 218 Cookie []cookies = ((HttpServletRequest ) page.getRequest()).getCookies(); 219 220 for (int i = 0; cookies != null && i < cookies.length; i++) { 221 if (map.get(cookies[i].getName()) == null) 222 map.put(cookies[i].getName(), cookies[i]); 223 } 224 225 return map; 226 } 227 } 228 229 throw new UnsupportedOperationException (); 230 } 231 232 public String toString() 233 { 234 return _id; 235 } 236 237 240 public void printCreate(WriteStream os) 241 throws IOException 242 { 243 os.print("new com.caucho.jsp.el.ImplicitObjectExpr(\""); 244 printEscapedString(os, _id); 245 os.print("\")"); 246 } 247 248 public static class AttributeMap extends AbstractMap { 249 private PageContext _pageContext; 250 private int _scope; 251 252 AttributeMap(PageContext pageContext, int scope) 253 { 254 _pageContext = pageContext; 255 _scope = scope; 256 } 257 258 public Object get(Object key) 259 { 260 return _pageContext.getAttribute((String ) key, _scope); 261 } 262 263 public Object put(Object key, Object value) 264 { 265 _pageContext.setAttribute((String ) key, value, _scope); 266 267 return null; 268 } 269 270 private EntrySet _entrySet; 271 272 public Set entrySet() 273 { 274 if (_entrySet == null) 275 _entrySet = new EntrySet(); 276 277 return _entrySet; 278 } 279 280 public class EntrySet extends AbstractSet { 281 public int size() 282 { 283 Enumeration e = _pageContext.getAttributeNamesInScope(_scope); 284 int i = 0; 285 while (e.hasMoreElements()) { 286 e.nextElement(); 287 i++; 288 } 289 290 return i; 291 } 292 293 public Iterator iterator() 294 { 295 return new EntryIterator(); 296 } 297 } 298 299 public class EntryIterator implements Iterator, Map.Entry { 300 Enumeration _e; 301 String _name; 302 Object _value; 303 304 EntryIterator() 305 { 306 _e = _pageContext.getAttributeNamesInScope(_scope); 307 } 308 309 public boolean hasNext() 310 { 311 return _e.hasMoreElements(); 312 } 313 314 public Object next() 315 { 316 _name = (String ) _e.nextElement(); 317 _value = _pageContext.getAttribute(_name, _scope); 318 319 return this; 320 } 321 322 public void remove() 323 { 324 throw new UnsupportedOperationException (); 325 } 326 327 public Object getKey() 328 { 329 return _name; 330 } 331 332 public Object getValue() 333 { 334 return _value; 335 } 336 337 public Object setValue(Object value) 338 { 339 _pageContext.setAttribute(_name, value, _scope); 340 341 Object oldValue = _value; 342 _value = value; 343 344 return oldValue; 345 } 346 347 public int hashCode() 348 { 349 return _name.hashCode(); 350 } 351 352 public boolean equals(Object obj) 353 { 354 if (! (obj instanceof EntryIterator)) 355 return false; 356 357 EntryIterator entry = (EntryIterator) obj; 358 359 return (_name.equals(entry._name) && 360 (_value == null && entry._value == null || 361 _value != null && _value.equals(entry._value))); 362 } 363 } 364 } 365 } 366 | Popular Tags |