/servlet/src/main/java/com/twelvemonkeys/servlet/jsp/droplet/taglib/IncludeTag.java

https://github.com/conceptboard/TwelveMonkeys · Java · 214 lines · 76 code · 27 blank · 111 comment · 5 complexity · d961aa4f88106f55d13b3b4f76e859da MD5 · raw file

  1. /*
  2. * Copyright (c) 2002 TwelveMonkeys.
  3. * All rights reserved.
  4. *
  5. * $Log: IncludeTag.java,v $
  6. * Revision 1.2 2003/10/06 14:25:36 WMHAKUR
  7. * Code clean-up only.
  8. *
  9. * Revision 1.1 2002/10/18 14:03:09 WMHAKUR
  10. * Moved to com.twelvemonkeys.servlet.jsp.droplet.taglib
  11. *
  12. *
  13. */
  14. package com.twelvemonkeys.servlet.jsp.droplet.taglib;
  15. import com.twelvemonkeys.servlet.jsp.taglib.ExTagSupport;
  16. import javax.servlet.ServletException;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import javax.servlet.jsp.JspException;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. /**
  25. * Include tag tag that emulates ATG Dynamo Droplet tag JHTML behaviour for
  26. * JSP.
  27. *
  28. * @author Thomas Purcell (CSC Australia)
  29. * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
  30. * @author last modified by $Author: haku $
  31. *
  32. * @version $Revision: #1 $, ($Date: 2008/05/05 $)
  33. *
  34. */
  35. public class IncludeTag extends ExTagSupport {
  36. /**
  37. * This will contain the names of all the parameters that have been
  38. * added to the PageContext.REQUEST_SCOPE scope by this tag.
  39. */
  40. private ArrayList<String> parameterNames = null;
  41. /**
  42. * If any of the parameters we insert for this tag already exist, then
  43. * we back up the older parameter in this {@code HashMap} and
  44. * restore them when the tag is finished.
  45. */
  46. private HashMap<String, Object> oldParameters = null;
  47. /**
  48. * This is the URL for the JSP page that the parameters contained in this
  49. * tag are to be inserted into.
  50. */
  51. private String page;
  52. /**
  53. * The name of the PageContext attribute
  54. */
  55. public final static String PAGE_CONTEXT = "com.twelvemonkeys.servlet.jsp.PageContext";
  56. /**
  57. * Sets the value for the JSP page to insert the parameters into. This
  58. * will be set by the tag attribute within the original JSP page.
  59. *
  60. * @param pPage The URL for the JSP page to insert parameters into.
  61. */
  62. public void setPage(String pPage) {
  63. page = pPage;
  64. }
  65. /**
  66. * Adds a parameter to the {@code PageContext.REQUEST_SCOPE} scope.
  67. * If a parameter with the same name as {@code pName} already exists,
  68. * then the old parameter is first placed in the {@code OldParameters}
  69. * member variable. When this tag is finished, the old value will be
  70. * restored.
  71. *
  72. * @param pName The name of the new parameter to be stored in the
  73. * {@code PageContext.REQUEST_SCOPE} scope.
  74. * @param pValue The value for the parmeter to be stored in the {@code
  75. * PageContext.REQUEST_SCOPE} scope.
  76. */
  77. public void addParameter(String pName, Object pValue) {
  78. // Check that we haven't already saved this parameter
  79. if (!parameterNames.contains(pName)) {
  80. parameterNames.add(pName);
  81. // Now check if this parameter already exists in the page.
  82. Object obj = getRequest().getAttribute(pName);
  83. if (obj != null) {
  84. oldParameters.put(pName, obj);
  85. }
  86. }
  87. // Finally, insert the parameter in the request scope.
  88. getRequest().setAttribute(pName, pValue);
  89. }
  90. /**
  91. * This is the method called when the JSP interpreter first hits the tag
  92. * associated with this class. This method will firstly determine whether
  93. * the page referenced by the {@code page} attribute exists. If the
  94. * page doesn't exist, this method will throw a {@code JspException}.
  95. * If the page does exist, this method will hand control over to that JSP
  96. * page.
  97. *
  98. * @exception JspException
  99. */
  100. public int doStartTag() throws JspException {
  101. oldParameters = new HashMap<String, Object>();
  102. parameterNames = new ArrayList<String>();
  103. return EVAL_BODY_INCLUDE;
  104. }
  105. /**
  106. * This method is called when the JSP page compiler hits the end tag. By
  107. * now all the data should have been passed and parameters entered into
  108. * the {@code PageContext.REQUEST_SCOPE} scope. This method includes
  109. * the JSP page whose URL is stored in the {@code mPage} member
  110. * variable.
  111. *
  112. * @exception JspException
  113. */
  114. public int doEndTag() throws JspException {
  115. String msg;
  116. try {
  117. Iterator<String> iterator;
  118. String parameterName;
  119. // -- Harald K 20020726
  120. // Include the page, in place
  121. //getDispatcher().include(getRequest(), getResponse());
  122. addParameter(PAGE_CONTEXT, pageContext); // Will be cleared later
  123. pageContext.include(page);
  124. // Remove all the parameters that were added to the request scope
  125. // for this insert tag.
  126. iterator = parameterNames.iterator();
  127. while (iterator.hasNext()) {
  128. parameterName = iterator.next();
  129. getRequest().removeAttribute(parameterName);
  130. }
  131. iterator = oldParameters.keySet().iterator();
  132. // Restore the parameters we temporarily replaced (if any).
  133. while (iterator.hasNext()) {
  134. parameterName = iterator.next();
  135. getRequest().setAttribute(parameterName, oldParameters.get(parameterName));
  136. }
  137. return super.doEndTag();
  138. }
  139. catch (IOException ioe) {
  140. msg = "Caught an IOException while including " + page
  141. + "\n" + ioe.toString();
  142. log(msg, ioe);
  143. throw new JspException(msg);
  144. }
  145. catch (ServletException se) {
  146. msg = "Caught a ServletException while including " + page
  147. + "\n" + se.toString();
  148. log(msg, se);
  149. throw new JspException(msg);
  150. }
  151. }
  152. /**
  153. * Free up the member variables that we've used throughout this tag.
  154. */
  155. protected void clearServiceState() {
  156. oldParameters = null;
  157. parameterNames = null;
  158. }
  159. /**
  160. * Returns the request dispatcher for the JSP page whose URL is stored in
  161. * the {@code mPage} member variable.
  162. *
  163. * @return The RequestDispatcher for the JSP page whose URL is stored in
  164. * the {@code mPage} member variable.
  165. */
  166. /*
  167. private RequestDispatcher getDispatcher() {
  168. return getRequest().getRequestDispatcher(page);
  169. }
  170. */
  171. /**
  172. * Returns the HttpServletRequest object for the current user request.
  173. *
  174. * @return The HttpServletRequest object for the current user request.
  175. */
  176. private HttpServletRequest getRequest() {
  177. return (HttpServletRequest) pageContext.getRequest();
  178. }
  179. /**
  180. * Returns the HttpServletResponse object for the current user request.
  181. *
  182. * @return The HttpServletResponse object for the current user request.
  183. */
  184. private HttpServletResponse getResponse() {
  185. return (HttpServletResponse) pageContext.getResponse();
  186. }
  187. }