KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > AbstractController


1 /*
2  * Copyright 2002-2006 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
17 package org.springframework.web.servlet.mvc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22
23 import org.springframework.web.servlet.ModelAndView;
24 import org.springframework.web.servlet.support.WebContentGenerator;
25 import org.springframework.web.util.WebUtils;
26
27 /**
28  * <p>Convenient superclass for controller implementations, using the Template
29  * Method design pattern.</p>
30  *
31  * <p>As stated in the {@link org.springframework.web.servlet.mvc.Controller Controller}
32  * interface, a lot of functionality is already provided by certain abstract
33  * base controllers. The AbstractController is one of the most important
34  * abstract base controller providing basic features such as the generation
35  * of caching headers and the enabling or disabling of
36  * supported methods (GET/POST).</p>
37  *
38  * <p><b><a name="workflow">Workflow
39  * (<a HREF="Controller.html#workflow">and that defined by interface</a>):</b><br>
40  * <ol>
41  * <li>{@link #handleRequest(HttpServletRequest,HttpServletResponse) handleRequest()}
42  * will be called by the DispatcherServlet</li>
43  * <li>Inspection of supported methods (ServletException if request method
44  * is not support)</li>
45  * <li>If session is required, try to get it (ServletException if not found)</li>
46  * <li>Set caching headers if needed according to cacheSeconds propery</li>
47  * <li>Call abstract method {@link #handleRequestInternal(HttpServletRequest,HttpServletResponse) handleRequestInternal()}
48  * (optionally synchronizing around the call on the HttpSession),
49  * which should be implemented by extending classes to provide actual
50  * functionality to return {@link org.springframework.web.servlet.ModelAndView ModelAndView} objects.</li>
51  * </ol>
52  * </p>
53  *
54  * <p><b><a name="config">Exposed configuration properties</a>
55  * (<a HREF="Controller.html#config">and those defined by interface</a>):</b><br>
56  * <table border="1">
57  * <tr>
58  * <td><b>name</b></th>
59  * <td><b>default</b></td>
60  * <td><b>description</b></td>
61  * </tr>
62  * <tr>
63  * <td>supportedMethods</td>
64  * <td>GET,POST</td>
65  * <td>comma-separated (CSV) list of methods supported by this controller,
66  * such as GET, POST and PUT</td>
67  * </tr>
68  * <tr>
69  * <td>requireSession</td>
70  * <td>false</td>
71  * <td>whether a session should be required for requests to be able to
72  * be handled by this controller. This ensures that derived controller
73  * can - without fear of null pointers - call request.getSession() to
74  * retrieve a session. If no session can be found while processing
75  * the request, a ServletException will be thrown</td>
76  * </tr>
77  * <tr>
78  * <td>cacheSeconds</td>
79  * <td>-1</td>
80  * <td>indicates the amount of seconds to include in the cache header
81  * for the response following on this request. 0 (zero) will include
82  * headers for no caching at all, -1 (the default) will not generate
83  * <i>any headers</i> and any positive number will generate headers
84  * that state the amount indicated as seconds to cache the content</td>
85  * </tr>
86  * <tr>
87  * <td>synchronizeOnSession</td>
88  * <td>false</td>
89  * <td>whether the call to <code>handleRequestInternal</code> should be
90  * synchronized around the HttpSession, to serialize invocations
91  * from the same client. No effect if there is no HttpSession.
92  * </td>
93  * </tr>
94  * </table>
95  *
96  * @author Rod Johnson
97  * @author Juergen Hoeller
98  * @see WebContentInterceptor
99  */

100 public abstract class AbstractController extends WebContentGenerator implements Controller {
101
102     private boolean synchronizeOnSession = false;
103
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(boolean synchronizeOnSession) {
125         this.synchronizeOnSession = synchronizeOnSession;
126     }
127
128     /**
129      * Return whether controller execution should be synchronized on the session.
130      */

131     public final boolean isSynchronizeOnSession() {
132         return synchronizeOnSession;
133     }
134
135
136     public final ModelAndView handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
137             throws Exception JavaDoc {
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 JavaDoc session = request.getSession(false);
145             if (session != null) {
146                 Object JavaDoc 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(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
162         throws Exception JavaDoc;
163
164 }
165
Popular Tags