001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.mvc;
018:
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021: import javax.servlet.http.HttpSession;
022:
023: import org.springframework.web.servlet.ModelAndView;
024: import org.springframework.web.servlet.support.WebContentGenerator;
025: import org.springframework.web.util.WebUtils;
026:
027: /**
028: * <p>Convenient superclass for controller implementations, using the Template
029: * Method design pattern.</p>
030: *
031: * <p>As stated in the {@link org.springframework.web.servlet.mvc.Controller Controller}
032: * interface, a lot of functionality is already provided by certain abstract
033: * base controllers. The AbstractController is one of the most important
034: * abstract base controller providing basic features such as the generation
035: * of caching headers and the enabling or disabling of
036: * supported methods (GET/POST).</p>
037: *
038: * <p><b><a name="workflow">Workflow
039: * (<a href="Controller.html#workflow">and that defined by interface</a>):</b><br>
040: * <ol>
041: * <li>{@link #handleRequest(HttpServletRequest,HttpServletResponse) handleRequest()}
042: * will be called by the DispatcherServlet</li>
043: * <li>Inspection of supported methods (ServletException if request method
044: * is not support)</li>
045: * <li>If session is required, try to get it (ServletException if not found)</li>
046: * <li>Set caching headers if needed according to cacheSeconds propery</li>
047: * <li>Call abstract method {@link #handleRequestInternal(HttpServletRequest,HttpServletResponse) handleRequestInternal()}
048: * (optionally synchronizing around the call on the HttpSession),
049: * which should be implemented by extending classes to provide actual
050: * functionality to return {@link org.springframework.web.servlet.ModelAndView ModelAndView} objects.</li>
051: * </ol>
052: * </p>
053: *
054: * <p><b><a name="config">Exposed configuration properties</a>
055: * (<a href="Controller.html#config">and those defined by interface</a>):</b><br>
056: * <table border="1">
057: * <tr>
058: * <td><b>name</b></th>
059: * <td><b>default</b></td>
060: * <td><b>description</b></td>
061: * </tr>
062: * <tr>
063: * <td>supportedMethods</td>
064: * <td>GET,POST</td>
065: * <td>comma-separated (CSV) list of methods supported by this controller,
066: * such as GET, POST and PUT</td>
067: * </tr>
068: * <tr>
069: * <td>requireSession</td>
070: * <td>false</td>
071: * <td>whether a session should be required for requests to be able to
072: * be handled by this controller. This ensures that derived controller
073: * can - without fear of null pointers - call request.getSession() to
074: * retrieve a session. If no session can be found while processing
075: * the request, a ServletException will be thrown</td>
076: * </tr>
077: * <tr>
078: * <td>cacheSeconds</td>
079: * <td>-1</td>
080: * <td>indicates the amount of seconds to include in the cache header
081: * for the response following on this request. 0 (zero) will include
082: * headers for no caching at all, -1 (the default) will not generate
083: * <i>any headers</i> and any positive number will generate headers
084: * that state the amount indicated as seconds to cache the content</td>
085: * </tr>
086: * <tr>
087: * <td>synchronizeOnSession</td>
088: * <td>false</td>
089: * <td>whether the call to <code>handleRequestInternal</code> should be
090: * synchronized around the HttpSession, to serialize invocations
091: * from the same client. No effect if there is no HttpSession.
092: * </td>
093: * </tr>
094: * </table>
095: *
096: * @author Rod Johnson
097: * @author Juergen Hoeller
098: * @see WebContentInterceptor
099: */
100: public abstract class AbstractController extends WebContentGenerator
101: implements Controller {
102:
103: private boolean synchronizeOnSession = false;
104:
105: /**
106: * Set if controller execution should be synchronized on the session,
107: * to serialize parallel invocations from the same client.
108: * <p>More specifically, the execution of the <code>handleRequestInternal</code>
109: * method will get synchronized if this flag is "true". The best available
110: * session mutex will be used for the synchronization; ideally, this will
111: * be a mutex exposed by HttpSessionMutexListener.
112: * <p>The session mutex is guaranteed to be the same object during
113: * the entire lifetime of the session, available under the key defined
114: * by the <code>SESSION_MUTEX_ATTRIBUTE</code> constant. It serves as a
115: * safe reference to synchronize on for locking on the current session.
116: * <p>In many cases, the HttpSession reference itself is a safe mutex
117: * as well, since it will always be the same object reference for the
118: * same active logical session. However, this is not guaranteed across
119: * different servlet containers; the only 100% safe way is a session mutex.
120: * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal
121: * @see org.springframework.web.util.HttpSessionMutexListener
122: * @see org.springframework.web.util.WebUtils#getSessionMutex(javax.servlet.http.HttpSession)
123: */
124: public final void setSynchronizeOnSession(
125: boolean synchronizeOnSession) {
126: this .synchronizeOnSession = synchronizeOnSession;
127: }
128:
129: /**
130: * Return whether controller execution should be synchronized on the session.
131: */
132: public final boolean isSynchronizeOnSession() {
133: return synchronizeOnSession;
134: }
135:
136: public final ModelAndView handleRequest(HttpServletRequest request,
137: HttpServletResponse response) throws Exception {
138:
139: // Delegate to WebContentGenerator for checking and preparing.
140: checkAndPrepare(request, response, this instanceof LastModified);
141:
142: // Execute handleRequestInternal in synchronized block if required.
143: if (this .synchronizeOnSession) {
144: HttpSession session = request.getSession(false);
145: if (session != null) {
146: Object mutex = WebUtils.getSessionMutex(session);
147: synchronized (mutex) {
148: return handleRequestInternal(request, response);
149: }
150: }
151: }
152:
153: return handleRequestInternal(request, response);
154: }
155:
156: /**
157: * Template method. Subclasses must implement this.
158: * The contract is the same as for <code>handleRequest</code>.
159: * @see #handleRequest
160: */
161: protected abstract ModelAndView handleRequestInternal(
162: HttpServletRequest request, HttpServletResponse response)
163: throws Exception;
164:
165: }
|