1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. *
  54. */
  55. package org.apache.commons.el;
  56. import java.util.ArrayList;
  57. import java.util.Collections;
  58. import java.util.Date;
  59. import java.util.Enumeration;
  60. import java.util.HashMap;
  61. import java.util.List;
  62. import java.util.Map;
  63. import javax.servlet.ServletContext;
  64. import javax.servlet.http.Cookie;
  65. import javax.servlet.http.HttpServletRequest;
  66. import javax.servlet.jsp.PageContext;
  67. /**
  68. *
  69. * <p>This class is used to generate the implicit Map and List objects
  70. * that wrap various elements of the PageContext. It also returns the
  71. * correct implicit object for a given implicit object name.
  72. *
  73. * @author Nathan Abramson - Art Technology Group
  74. * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
  75. **/
  76. public class ImplicitObjects
  77. {
  78. //-------------------------------------
  79. // Constants
  80. //-------------------------------------
  81. static final String sAttributeName =
  82. "org.apache.taglibs.standard.ImplicitObjects";
  83. //-------------------------------------
  84. // Member variables
  85. //-------------------------------------
  86. PageContext mContext;
  87. Map mPage;
  88. Map mRequest;
  89. Map mSession;
  90. Map mApplication;
  91. Map mParam;
  92. Map mParams;
  93. Map mHeader;
  94. Map mHeaders;
  95. Map mInitParam;
  96. Map mCookie;
  97. //-------------------------------------
  98. /**
  99. *
  100. * Constructor
  101. **/
  102. public ImplicitObjects (PageContext pContext)
  103. {
  104. mContext = pContext;
  105. }
  106. //-------------------------------------
  107. /**
  108. *
  109. * Finds the ImplicitObjects associated with the PageContext,
  110. * creating it if it doesn't yet exist.
  111. **/
  112. public static ImplicitObjects getImplicitObjects (PageContext pContext)
  113. {
  114. ImplicitObjects objs =
  115. (ImplicitObjects)
  116. pContext.getAttribute (sAttributeName,
  117. PageContext.PAGE_SCOPE);
  118. if (objs == null) {
  119. objs = new ImplicitObjects (pContext);
  120. pContext.setAttribute (sAttributeName,
  121. objs,
  122. PageContext.PAGE_SCOPE);
  123. }
  124. return objs;
  125. }
  126. //-------------------------------------
  127. /**
  128. *
  129. * Returns the Map that "wraps" page-scoped attributes
  130. **/
  131. public Map getPageScopeMap ()
  132. {
  133. if (mPage == null) {
  134. mPage = createPageScopeMap (mContext);
  135. }
  136. return mPage;
  137. }
  138. //-------------------------------------
  139. /**
  140. *
  141. * Returns the Map that "wraps" request-scoped attributes
  142. **/
  143. public Map getRequestScopeMap ()
  144. {
  145. if (mRequest == null) {
  146. mRequest = createRequestScopeMap (mContext);
  147. }
  148. return mRequest;
  149. }
  150. //-------------------------------------
  151. /**
  152. *
  153. * Returns the Map that "wraps" session-scoped attributes
  154. **/
  155. public Map getSessionScopeMap ()
  156. {
  157. if (mSession == null) {
  158. mSession = createSessionScopeMap (mContext);
  159. }
  160. return mSession;
  161. }
  162. //-------------------------------------
  163. /**
  164. *
  165. * Returns the Map that "wraps" application-scoped attributes
  166. **/
  167. public Map getApplicationScopeMap ()
  168. {
  169. if (mApplication == null) {
  170. mApplication = createApplicationScopeMap (mContext);
  171. }
  172. return mApplication;
  173. }
  174. //-------------------------------------
  175. /**
  176. *
  177. * Returns the Map that maps parameter name to a single parameter
  178. * values.
  179. **/
  180. public Map getParamMap ()
  181. {
  182. if (mParam == null) {
  183. mParam = createParamMap (mContext);
  184. }
  185. return mParam;
  186. }
  187. //-------------------------------------
  188. /**
  189. *
  190. * Returns the Map that maps parameter name to an array of parameter
  191. * values.
  192. **/
  193. public Map getParamsMap ()
  194. {
  195. if (mParams == null) {
  196. mParams = createParamsMap (mContext);
  197. }
  198. return mParams;
  199. }
  200. //-------------------------------------
  201. /**
  202. *
  203. * Returns the Map that maps header name to a single header
  204. * values.
  205. **/
  206. public Map getHeaderMap ()
  207. {
  208. if (mHeader == null) {
  209. mHeader = createHeaderMap (mContext);
  210. }
  211. return mHeader;
  212. }
  213. //-------------------------------------
  214. /**
  215. *
  216. * Returns the Map that maps header name to an array of header
  217. * values.
  218. **/
  219. public Map getHeadersMap ()
  220. {
  221. if (mHeaders == null) {
  222. mHeaders = createHeadersMap (mContext);
  223. }
  224. return mHeaders;
  225. }
  226. //-------------------------------------
  227. /**
  228. *
  229. * Returns the Map that maps init parameter name to a single init
  230. * parameter values.
  231. **/
  232. public Map getInitParamMap ()
  233. {
  234. if (mInitParam == null) {
  235. mInitParam = createInitParamMap (mContext);
  236. }
  237. return mInitParam;
  238. }
  239. //-------------------------------------
  240. /**
  241. *
  242. * Returns the Map that maps cookie name to the first matching
  243. * Cookie in request.getCookies().
  244. **/
  245. public Map getCookieMap ()
  246. {
  247. if (mCookie == null) {
  248. mCookie = createCookieMap (mContext);
  249. }
  250. return mCookie;
  251. }
  252. //-------------------------------------
  253. // Methods for generating wrapper maps
  254. //-------------------------------------
  255. /**
  256. *
  257. * Creates the Map that "wraps" page-scoped attributes
  258. **/
  259. public static Map createPageScopeMap (PageContext pContext)
  260. {
  261. final PageContext context = pContext;
  262. return new EnumeratedMap ()
  263. {
  264. public Enumeration enumerateKeys ()
  265. {
  266. return context.getAttributeNamesInScope
  267. (PageContext.PAGE_SCOPE);
  268. }
  269. public Object getValue (Object pKey)
  270. {
  271. if (pKey instanceof String) {
  272. return context.getAttribute
  273. ((String) pKey,
  274. PageContext.PAGE_SCOPE);
  275. }
  276. else {
  277. return null;
  278. }
  279. }
  280. public boolean isMutable ()
  281. {
  282. return true;
  283. }
  284. };
  285. }
  286. //-------------------------------------
  287. /**
  288. *
  289. * Creates the Map that "wraps" request-scoped attributes
  290. **/
  291. public static Map createRequestScopeMap (PageContext pContext)
  292. {
  293. final PageContext context = pContext;
  294. return new EnumeratedMap ()
  295. {
  296. public Enumeration enumerateKeys ()
  297. {
  298. return context.getAttributeNamesInScope
  299. (PageContext.REQUEST_SCOPE);
  300. }
  301. public Object getValue (Object pKey)
  302. {
  303. if (pKey instanceof String) {
  304. return context.getAttribute
  305. ((String) pKey,
  306. PageContext.REQUEST_SCOPE);
  307. }
  308. else {
  309. return null;
  310. }
  311. }
  312. public boolean isMutable ()
  313. {
  314. return true;
  315. }
  316. };
  317. }
  318. //-------------------------------------
  319. /**
  320. *
  321. * Creates the Map that "wraps" session-scoped attributes
  322. **/
  323. public static Map createSessionScopeMap (PageContext pContext)
  324. {
  325. final PageContext context = pContext;
  326. return new EnumeratedMap ()
  327. {
  328. public Enumeration enumerateKeys ()
  329. {
  330. return context.getAttributeNamesInScope
  331. (PageContext.SESSION_SCOPE);
  332. }
  333. public Object getValue (Object pKey)
  334. {
  335. if (pKey instanceof String) {
  336. return context.getAttribute
  337. ((String) pKey,
  338. PageContext.SESSION_SCOPE);
  339. }
  340. else {
  341. return null;
  342. }
  343. }
  344. public boolean isMutable ()
  345. {
  346. return true;
  347. }
  348. };
  349. }
  350. //-------------------------------------
  351. /**
  352. *
  353. * Creates the Map that "wraps" application-scoped attributes
  354. **/
  355. public static Map createApplicationScopeMap (PageContext pContext)
  356. {
  357. final PageContext context = pContext;
  358. return new EnumeratedMap ()
  359. {
  360. public Enumeration enumerateKeys ()
  361. {
  362. return context.getAttributeNamesInScope
  363. (PageContext.APPLICATION_SCOPE);
  364. }
  365. public Object getValue (Object pKey)
  366. {
  367. if (pKey instanceof String) {
  368. return context.getAttribute
  369. ((String) pKey,
  370. PageContext.APPLICATION_SCOPE);
  371. }
  372. else {
  373. return null;
  374. }
  375. }
  376. public boolean isMutable ()
  377. {
  378. return true;
  379. }
  380. };
  381. }
  382. //-------------------------------------
  383. /**
  384. *
  385. * Creates the Map that maps parameter name to single parameter
  386. * value.
  387. **/
  388. public static Map createParamMap (PageContext pContext)
  389. {
  390. final HttpServletRequest request =
  391. (HttpServletRequest) pContext.getRequest ();
  392. return new EnumeratedMap ()
  393. {
  394. public Enumeration enumerateKeys ()
  395. {
  396. return request.getParameterNames ();
  397. }
  398. public Object getValue (Object pKey)
  399. {
  400. if (pKey instanceof String) {
  401. return request.getParameter ((String) pKey);
  402. }
  403. else {
  404. return null;
  405. }
  406. }
  407. public boolean isMutable ()
  408. {
  409. return false;
  410. }
  411. };
  412. }
  413. //-------------------------------------
  414. /**
  415. *
  416. * Creates the Map that maps parameter name to an array of parameter
  417. * values.
  418. **/
  419. public static Map createParamsMap (PageContext pContext)
  420. {
  421. final HttpServletRequest request =
  422. (HttpServletRequest) pContext.getRequest ();
  423. return new EnumeratedMap ()
  424. {
  425. public Enumeration enumerateKeys ()
  426. {
  427. return request.getParameterNames ();
  428. }
  429. public Object getValue (Object pKey)
  430. {
  431. if (pKey instanceof String) {
  432. return request.getParameterValues ((String) pKey);
  433. }
  434. else {
  435. return null;
  436. }
  437. }
  438. public boolean isMutable ()
  439. {
  440. return false;
  441. }
  442. };
  443. }
  444. //-------------------------------------
  445. /**
  446. *
  447. * Creates the Map that maps header name to single header
  448. * value.
  449. **/
  450. public static Map createHeaderMap (PageContext pContext)
  451. {
  452. final HttpServletRequest request =
  453. (HttpServletRequest) pContext.getRequest ();
  454. return new EnumeratedMap ()
  455. {
  456. public Enumeration enumerateKeys ()
  457. {
  458. return request.getHeaderNames ();
  459. }
  460. public Object getValue (Object pKey)
  461. {
  462. if (pKey instanceof String) {
  463. return request.getHeader ((String) pKey);
  464. }
  465. else {
  466. return null;
  467. }
  468. }
  469. public boolean isMutable ()
  470. {
  471. return false;
  472. }
  473. };
  474. }
  475. //-------------------------------------
  476. /**
  477. *
  478. * Creates the Map that maps header name to an array of header
  479. * values.
  480. **/
  481. public static Map createHeadersMap (PageContext pContext)
  482. {
  483. final HttpServletRequest request =
  484. (HttpServletRequest) pContext.getRequest ();
  485. return new EnumeratedMap ()
  486. {
  487. public Enumeration enumerateKeys ()
  488. {
  489. return request.getHeaderNames ();
  490. }
  491. public Object getValue (Object pKey)
  492. {
  493. if (pKey instanceof String) {
  494. // Drain the header enumeration
  495. List l = new ArrayList ();
  496. Enumeration enum = request.getHeaders ((String) pKey);
  497. if (enum != null) {
  498. while (enum.hasMoreElements ()) {
  499. l.add (enum.nextElement ());
  500. }
  501. }
  502. String [] ret = (String []) l.toArray (new String [l.size ()]);
  503. return ret;
  504. }
  505. else {
  506. return null;
  507. }
  508. }
  509. public boolean isMutable ()
  510. {
  511. return false;
  512. }
  513. };
  514. }
  515. //-------------------------------------
  516. /**
  517. *
  518. * Creates the Map that maps init parameter name to single init
  519. * parameter value.
  520. **/
  521. public static Map createInitParamMap (PageContext pContext)
  522. {
  523. final ServletContext context = pContext.getServletContext ();
  524. return new EnumeratedMap ()
  525. {
  526. public Enumeration enumerateKeys ()
  527. {
  528. return context.getInitParameterNames ();
  529. }
  530. public Object getValue (Object pKey)
  531. {
  532. if (pKey instanceof String) {
  533. return context.getInitParameter ((String) pKey);
  534. }
  535. else {
  536. return null;
  537. }
  538. }
  539. public boolean isMutable ()
  540. {
  541. return false;
  542. }
  543. };
  544. }
  545. //-------------------------------------
  546. /**
  547. *
  548. * Creates the Map that maps cookie name to the first matching
  549. * Cookie in request.getCookies().
  550. **/
  551. public static Map createCookieMap (PageContext pContext)
  552. {
  553. // Read all the cookies and construct the entire map
  554. HttpServletRequest request = (HttpServletRequest) pContext.getRequest ();
  555. Cookie [] cookies = request.getCookies ();
  556. Map ret = new HashMap ();
  557. for (int i = 0; cookies != null && i < cookies.length; i++) {
  558. Cookie cookie = cookies [i];
  559. if (cookie != null) {
  560. String name = cookie.getName ();
  561. if (!ret.containsKey (name)) {
  562. ret.put (name, cookie);
  563. }
  564. }
  565. }
  566. return ret;
  567. }
  568. //-------------------------------------
  569. }