/root/projects/surf/spring-surf/spring-surf/src/main/java/org/springframework/extensions/surf/util/FakeJspPageContext.java

https://github.com/deas/alfresco · Java · 470 lines · 260 code · 56 blank · 154 comment · 37 complexity · b7e111942da4f18431bdc2dc5edaa755 MD5 · raw file

  1. /**
  2. * Copyright (C) 2005-2009 Alfresco Software Limited.
  3. *
  4. * This file is part of the Spring Surf Extension project.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.springframework.extensions.surf.util;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.Enumeration;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.Map;
  25. import javax.el.ELContext;
  26. import javax.servlet.Servlet;
  27. import javax.servlet.ServletConfig;
  28. import javax.servlet.ServletContext;
  29. import javax.servlet.ServletException;
  30. import javax.servlet.ServletRequest;
  31. import javax.servlet.ServletResponse;
  32. import javax.servlet.http.HttpServletRequest;
  33. import javax.servlet.http.HttpServletResponse;
  34. import javax.servlet.http.HttpSession;
  35. import javax.servlet.jsp.JspWriter;
  36. import javax.servlet.jsp.PageContext;
  37. import javax.servlet.jsp.el.ExpressionEvaluator;
  38. import javax.servlet.jsp.el.VariableResolver;
  39. import org.springframework.extensions.surf.site.RequestUtil;
  40. /**
  41. * Fake Jsp PageContext implementation which wraps predescribed HTTP objects.
  42. *
  43. * @author muzquiano
  44. */
  45. public class FakeJspPageContext
  46. extends PageContext
  47. {
  48. /** The exception. */
  49. protected Exception exception;
  50. /** The values. */
  51. protected Map<String, Object> values;
  52. /** The context. */
  53. protected ServletContext context;
  54. /** The request. */
  55. protected HttpServletRequest request;
  56. /** The response. */
  57. protected HttpServletResponse response;
  58. /** The out. */
  59. protected JspWriter out;
  60. /**
  61. * Instantiates a new fake jsp page context
  62. *
  63. * @param context The ServletContext to wrap
  64. * @param request The HttpServletRequest instance to wrap
  65. * @param response The HttpServletResponse instance to wrap
  66. * @param out The JspWriter to wrap
  67. */
  68. public FakeJspPageContext(ServletContext context, HttpServletRequest request, HttpServletResponse response, JspWriter out)
  69. {
  70. this.context = context;
  71. this.request = request;
  72. this.response = response;
  73. this.out = out;
  74. }
  75. /* (non-Javadoc)
  76. * @see javax.servlet.jsp.PageContext#getRequest()
  77. */
  78. public ServletRequest getRequest()
  79. {
  80. return this.request;
  81. }
  82. /* (non-Javadoc)
  83. * @see javax.servlet.jsp.PageContext#getResponse()
  84. */
  85. public ServletResponse getResponse()
  86. {
  87. return this.response;
  88. }
  89. /* (non-Javadoc)
  90. * @see javax.servlet.jsp.PageContext#getServletContext()
  91. */
  92. public ServletContext getServletContext()
  93. {
  94. return this.context;
  95. }
  96. /* (non-Javadoc)
  97. * @see javax.servlet.jsp.PageContext#getServletConfig()
  98. */
  99. public ServletConfig getServletConfig()
  100. {
  101. return null;
  102. }
  103. /* (non-Javadoc)
  104. * @see javax.servlet.jsp.JspContext#getOut()
  105. */
  106. public JspWriter getOut()
  107. {
  108. return this.out;
  109. }
  110. /* (non-Javadoc)
  111. * @see javax.servlet.jsp.PageContext#getSession()
  112. */
  113. public HttpSession getSession()
  114. {
  115. return this.request.getSession();
  116. }
  117. /* (non-Javadoc)
  118. * @see javax.servlet.jsp.JspContext#findAttribute(java.lang.String)
  119. */
  120. public Object findAttribute(String name)
  121. {
  122. Object ret = getAttribute(name, PAGE_SCOPE);
  123. if (ret != null)
  124. return ret;
  125. ret = getAttribute(name, REQUEST_SCOPE);
  126. if (ret != null)
  127. return ret;
  128. ret = getAttribute(name, SESSION_SCOPE);
  129. if (ret != null)
  130. return ret;
  131. ret = getAttribute(name, APPLICATION_SCOPE);
  132. if (ret != null)
  133. return ret;
  134. return null;
  135. }
  136. /* (non-Javadoc)
  137. * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String)
  138. */
  139. public Object getAttribute(String name)
  140. {
  141. return findAttribute(name);
  142. }
  143. /* (non-Javadoc)
  144. * @see javax.servlet.jsp.JspContext#getAttribute(java.lang.String, int)
  145. */
  146. public Object getAttribute(String name, int scope)
  147. {
  148. switch (scope)
  149. {
  150. case APPLICATION_SCOPE:
  151. return getServletContext().getAttribute(name);
  152. case REQUEST_SCOPE:
  153. Object ret = getRequest().getAttribute(name);
  154. if (ret == null)
  155. ret = getRequest().getParameter(name);
  156. return ret;
  157. case SESSION_SCOPE:
  158. if (getSession() != null)
  159. return getSession().getAttribute(name);
  160. else
  161. return null;
  162. case PAGE_SCOPE:
  163. return getValue(name);
  164. }
  165. return null;
  166. }
  167. /* (non-Javadoc)
  168. * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object)
  169. */
  170. public void setAttribute(String name, Object obj)
  171. {
  172. setValue(name, obj);
  173. }
  174. /* (non-Javadoc)
  175. * @see javax.servlet.jsp.JspContext#setAttribute(java.lang.String, java.lang.Object, int)
  176. */
  177. public void setAttribute(String name, Object obj, int scope)
  178. {
  179. switch (scope)
  180. {
  181. case APPLICATION_SCOPE:
  182. getServletContext().setAttribute(name, obj);
  183. break;
  184. case REQUEST_SCOPE:
  185. getRequest().setAttribute(name, obj);
  186. break;
  187. case SESSION_SCOPE:
  188. if (getSession() != null)
  189. getSession().setAttribute(name, obj);
  190. break;
  191. case PAGE_SCOPE:
  192. setValue(name, obj);
  193. break;
  194. }
  195. }
  196. /* (non-Javadoc)
  197. * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String)
  198. */
  199. public void removeAttribute(String name)
  200. {
  201. removeValue(name);
  202. }
  203. /* (non-Javadoc)
  204. * @see javax.servlet.jsp.JspContext#removeAttribute(java.lang.String, int)
  205. */
  206. public void removeAttribute(String name, int scope)
  207. {
  208. switch (scope)
  209. {
  210. case APPLICATION_SCOPE:
  211. getServletContext().removeAttribute(name);
  212. break;
  213. case REQUEST_SCOPE:
  214. getRequest().removeAttribute(name);
  215. break;
  216. case SESSION_SCOPE:
  217. if (getSession() != null)
  218. getSession().removeAttribute(name);
  219. break;
  220. case PAGE_SCOPE:
  221. removeValue(name);
  222. break;
  223. }
  224. }
  225. /* (non-Javadoc)
  226. * @see javax.servlet.jsp.JspContext#getAttributeNamesInScope(int)
  227. */
  228. public Enumeration getAttributeNamesInScope(int scope)
  229. {
  230. switch (scope)
  231. {
  232. case APPLICATION_SCOPE:
  233. return getServletContext().getAttributeNames();
  234. case REQUEST_SCOPE:
  235. return getRequest().getAttributeNames();
  236. case SESSION_SCOPE:
  237. return getSession().getAttributeNames();
  238. case PAGE_SCOPE:
  239. return getValueNames();
  240. }
  241. return null;
  242. }
  243. /* (non-Javadoc)
  244. * @see javax.servlet.jsp.JspContext#getAttributesScope(java.lang.String)
  245. */
  246. public int getAttributesScope(String name)
  247. {
  248. if (getValue(name) != null)
  249. return PAGE_SCOPE;
  250. // allow request attributes to override request parameters
  251. if (getRequest().getAttribute(name) != null)
  252. return REQUEST_SCOPE;
  253. if (getRequest().getParameter(name) != null)
  254. return REQUEST_SCOPE;
  255. if (getSession().getAttribute(name) != null)
  256. return SESSION_SCOPE;
  257. if (getServletContext().getAttribute(name) != null)
  258. return APPLICATION_SCOPE;
  259. return 0;
  260. }
  261. /* (non-Javadoc)
  262. * @see javax.servlet.jsp.PageContext#forward(java.lang.String)
  263. */
  264. public void forward(String url) throws ServletException, IOException
  265. {
  266. RequestUtil.forward(getServletContext(), getRequest(), getResponse(), url);
  267. }
  268. /* (non-Javadoc)
  269. * @see javax.servlet.jsp.PageContext#include(java.lang.String)
  270. */
  271. public void include(String url) throws ServletException, IOException
  272. {
  273. include(url, true);
  274. }
  275. /* (non-Javadoc)
  276. * @see javax.servlet.jsp.PageContext#include(java.lang.String, boolean)
  277. */
  278. public void include(String url, boolean b) throws ServletException,
  279. IOException
  280. {
  281. RequestUtil.include(getServletContext(), getRequest(), getResponse(), url);
  282. // make sure everything is flushed before doing an include -- important for included JSP files
  283. flushOut();
  284. }
  285. /**
  286. * Flush out.
  287. *
  288. * @throws IOException
  289. * Signals that an I/O exception has occurred.
  290. */
  291. public void flushOut() throws java.io.IOException
  292. {
  293. out.flush();
  294. }
  295. /* (non-Javadoc)
  296. * @see javax.servlet.jsp.PageContext#release()
  297. */
  298. public void release()
  299. {
  300. }
  301. /* (non-Javadoc)
  302. * @see javax.servlet.jsp.JspContext#getExpressionEvaluator()
  303. */
  304. public ExpressionEvaluator getExpressionEvaluator()
  305. {
  306. return null;
  307. }
  308. /* (non-Javadoc)
  309. * @see javax.servlet.jsp.JspContext#getVariableResolver()
  310. */
  311. public VariableResolver getVariableResolver()
  312. {
  313. return null;
  314. }
  315. /* (non-Javadoc)
  316. * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Throwable)
  317. */
  318. public void handlePageException(Throwable t)
  319. {
  320. // TODO?
  321. }
  322. /* (non-Javadoc)
  323. * @see javax.servlet.jsp.PageContext#handlePageException(java.lang.Exception)
  324. */
  325. public void handlePageException(Exception e)
  326. {
  327. exception = e;
  328. }
  329. /* (non-Javadoc)
  330. * @see javax.servlet.jsp.PageContext#getException()
  331. */
  332. public Exception getException()
  333. {
  334. return exception;
  335. }
  336. /* (non-Javadoc)
  337. * @see javax.servlet.jsp.PageContext#getPage()
  338. */
  339. public Object getPage()
  340. {
  341. return null;
  342. }
  343. /* (non-Javadoc)
  344. * @see javax.servlet.jsp.PageContext#getELContext()
  345. */
  346. @Override
  347. public ELContext getELContext()
  348. {
  349. return null;
  350. }
  351. /* (non-Javadoc)
  352. * @see javax.servlet.jsp.PageContext#initialize(javax.servlet.Servlet, javax.servlet.ServletRequest, javax.servlet.ServletResponse, java.lang.String, boolean, int, boolean)
  353. */
  354. public void initialize(Servlet srv, ServletRequest req,
  355. ServletResponse res, String s1, boolean b1, int i1, boolean b2)
  356. {
  357. }
  358. // local page context helper methods
  359. /**
  360. * Gets the value.
  361. *
  362. * @param key
  363. * the key
  364. *
  365. * @return the value
  366. */
  367. protected Object getValue(String key)
  368. {
  369. if (values == null)
  370. values = new HashMap<String, Object>();
  371. return values.get(key);
  372. }
  373. /**
  374. * Sets the value.
  375. *
  376. * @param key
  377. * the key
  378. * @param value
  379. * the value
  380. */
  381. protected void setValue(String key, Object value)
  382. {
  383. if (values == null)
  384. values = new HashMap<String, Object>();
  385. values.put(key, value);
  386. }
  387. /**
  388. * Removes the value.
  389. *
  390. * @param key
  391. * the key
  392. */
  393. protected void removeValue(String key)
  394. {
  395. if (values == null)
  396. values = new HashMap<String, Object>();
  397. values.remove(key);
  398. }
  399. /**
  400. * Gets the value names.
  401. *
  402. * @return the value names
  403. */
  404. protected Enumeration getValueNames()
  405. {
  406. ArrayList<Object> array = new ArrayList<Object>();
  407. Iterator it = values.keySet().iterator();
  408. while (it.hasNext())
  409. {
  410. array.add(it.next());
  411. }
  412. return java.util.Collections.enumeration(array);
  413. }
  414. }