1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.servlet.mvc;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import javax.servlet.http.HttpSession;
  20. import org.springframework.web.servlet.ModelAndView;
  21. import org.springframework.web.servlet.support.WebContentGenerator;
  22. /**
  23. * <p>Convenient superclass for controller implementations, using the Template
  24. * Method design pattern.</p>
  25. *
  26. * <p>As stated in the {@link org.springframework.web.servlet.mvc.Controller Controller}
  27. * interface, a lot of functionality is already provided by certain abstract
  28. * base controllers. The AbstractController is one of the most important
  29. * abstract base controller providing basic features such as the generation
  30. * of caching headers and the enabling or disabling of
  31. * supported methods (GET/POST).</p>
  32. *
  33. * <p><b><a name="workflow">Workflow
  34. * (<a href="Controller.html#workflow">and that defined by interface</a>):</b><br>
  35. * <ol>
  36. * <li>{@link #handleRequest(HttpServletRequest,HttpServletResponse) handleRequest()}
  37. * will be called by the DispatcherServlet</li>
  38. * <li>Inspection of supported methods (ServletException if request method
  39. * is not support)</li>
  40. * <li>If session is required, try to get it (ServletException if not found)</li>
  41. * <li>Set caching headers if needed according to cacheSeconds propery</li>
  42. * <li>Call abstract method {@link #handleRequestInternal(HttpServletRequest,HttpServletResponse) handleRequestInternal()},
  43. * which should be implemented by extending classes to provide actual
  44. * functionality to return {@link org.springframework.web.servlet.ModelAndView ModelAndView} objects.</li>
  45. * </ol>
  46. * </p>
  47. *
  48. * <p><b><a name="config">Exposed configuration properties</a>
  49. * (<a href="Controller.html#config">and those defined by interface</a>):</b><br>
  50. * <table border="1">
  51. * <tr>
  52. * <td><b>name</b></th>
  53. * <td><b>default</b></td>
  54. * <td><b>description</b></td>
  55. * </tr>
  56. * <tr>
  57. * <td>supportedMethods</td>
  58. * <td>GET,POST</td>
  59. * <td>comma-separated (CSV) list of methods supported by this controller,
  60. * such as GET, POST and PUT</td>
  61. * </tr>
  62. * <tr>
  63. * <td>requiresSession</td>
  64. * <td>false</td>
  65. * <td>whether a session should be required for requests to be able to
  66. * be handled by this controller. This ensures, derived controller
  67. * can - without fear of Nullpointers - call request.getSession() to
  68. * retrieve a session. If no session can be found while processing
  69. * the request, a ServletException will be thrown</td>
  70. * </tr>
  71. * <tr>
  72. * <td>cacheSeconds</td>
  73. * <td>-1</td>
  74. * <td>indicates the amount of seconds to include in the cache header
  75. * for the response following on this request. 0 (zero) will include
  76. * headers for no caching at all, -1 (the default) will not generate
  77. * <i>any headers</i> and any positive number will generate headers
  78. * that state the amount indicated as seconds to cache the content</td>
  79. * </tr>
  80. * </table>
  81. *
  82. * @author Rod Johnson
  83. * @author Juergen Hoeller
  84. * @see WebContentInterceptor
  85. */
  86. public abstract class AbstractController extends WebContentGenerator implements Controller {
  87. private boolean synchronizeOnSession = false;
  88. /**
  89. * Set if controller execution should be synchronized on the session,
  90. * to serialize parallel invocations from the same client.
  91. * <p>More specifically, the execution of the handleRequestInternal
  92. * method will get synchronized if this flag is true.
  93. * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal
  94. */
  95. public final void setSynchronizeOnSession(boolean synchronizeOnSession) {
  96. this.synchronizeOnSession = synchronizeOnSession;
  97. }
  98. public final ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
  99. throws Exception {
  100. // delegate to WebContentGenerator for checking and preparing
  101. checkAndPrepare(request, response, this instanceof LastModified);
  102. // execute in synchronized block if required
  103. if (this.synchronizeOnSession) {
  104. HttpSession session = request.getSession(false);
  105. if (session != null) {
  106. synchronized (session) {
  107. return handleRequestInternal(request, response);
  108. }
  109. }
  110. }
  111. return handleRequestInternal(request, response);
  112. }
  113. /**
  114. * Template method. Subclasses must implement this.
  115. * The contract is the same as for handleRequest.
  116. * @see #handleRequest
  117. */
  118. protected abstract ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
  119. throws Exception;
  120. }