KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > handler > PortletContentGenerator


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.portlet.handler;
18
19 import javax.portlet.PortletException;
20 import javax.portlet.PortletRequest;
21 import javax.portlet.PortletResponse;
22 import javax.portlet.RenderRequest;
23 import javax.portlet.RenderResponse;
24
25 import org.springframework.web.portlet.context.PortletApplicationObjectSupport;
26
27 /**
28  * Convenient superclass for any kind of web content generator,
29  * like AbstractController. Can also be used for custom handlers
30  * that have their own HandlerAdapter.
31  *
32  * <p>Supports portlet cache control options.
33  *
34  * @author Juergen Hoeller
35  * @author John A. Lewis
36  * @since 2.0
37  * @see org.springframework.web.portlet.mvc.AbstractController
38  */

39 public abstract class PortletContentGenerator extends PortletApplicationObjectSupport {
40
41     private boolean requireSession = false;
42
43     private int cacheSeconds = -1;
44
45
46     /**
47      * Set whether a session should be required to handle requests.
48      */

49     public final void setRequireSession(boolean requireSession) {
50         this.requireSession = requireSession;
51     }
52
53     /**
54      * Return whether a session is required to handle requests.
55      */

56     public final boolean isRequireSession() {
57         return requireSession;
58     }
59
60     /**
61      * Cache content for the given number of seconds. Default is -1,
62      * indicating no override of portlet content caching.
63      * <p>Only if this is set to 0 (no cache) or a positive value (cache for
64      * this many seconds) will this class override the portlet settings.
65      * <p>The cache setting can be overwritten by subclasses, before content is generated.
66      */

67     public final void setCacheSeconds(int seconds) {
68         this.cacheSeconds = seconds;
69     }
70
71     /**
72      * Return the number of seconds that content is cached.
73      */

74     public final int getCacheSeconds() {
75         return cacheSeconds;
76     }
77
78
79     /**
80      * Check and prepare the given request and response according to the settings
81      * of this generator. Checks for a required session, and applies the number of
82      * cache seconds configured for this generator (if it is a render request/response).
83      * @param request current portlet request
84      * @param response current portlet response
85      * @throws PortletException if the request cannot be handled because a check failed
86      */

87     protected final void check(PortletRequest request, PortletResponse response) throws PortletException {
88         if (this.requireSession) {
89             if (request.getPortletSession(false) == null) {
90                 throw new PortletSessionRequiredException("Pre-existing session required but none found");
91             }
92         }
93     }
94
95     /**
96      * Check and prepare the given request and response according to the settings
97      * of this generator. Checks for a required session, and applies the number of
98      * cache seconds configured for this generator (if it is a render request/response).
99      * @param request current portlet request
100      * @param response current portlet response
101      * @throws PortletException if the request cannot be handled because a check failed
102      */

103     protected final void checkAndPrepare(RenderRequest request, RenderResponse response)
104             throws PortletException {
105
106         checkAndPrepare(request, response, this.cacheSeconds);
107     }
108
109     /**
110      * Check and prepare the given request and response according to the settings
111      * of this generator. Checks for a required session, and applies the given
112      * number of cache seconds (if it is a render request/response).
113      * @param request current portlet request
114      * @param response current portlet response
115      * @param cacheSeconds positive number of seconds into the future that the
116      * response should be cacheable for, 0 to prevent caching
117      * @throws PortletException if the request cannot be handled because a check failed
118      */

119     protected final void checkAndPrepare(
120             RenderRequest request, RenderResponse response, int cacheSeconds)
121             throws PortletException {
122
123         check(request, response);
124         applyCacheSeconds(response, cacheSeconds);
125     }
126
127     /**
128      * Prevent the render response from being cached.
129      */

130     protected final void preventCaching(RenderResponse response) {
131         cacheForSeconds(response, 0);
132     }
133
134     /**
135      * Set portlet response to allow caching for the given number of seconds.
136      * @param response current portlet render response
137      * @param seconds number of seconds into the future that the response
138      * should be cacheable for
139      */

140     protected final void cacheForSeconds(RenderResponse response, int seconds) {
141         response.setProperty(RenderResponse.EXPIRATION_CACHE, Integer.toString(seconds));
142     }
143
144     /**
145      * Apply the given cache seconds to the render response
146      * @param response current portlet render response
147      * @param seconds positive number of seconds into the future that the
148      * response should be cacheable for, 0 to prevent caching
149      */

150     protected final void applyCacheSeconds(RenderResponse response, int seconds) {
151         if (seconds > 0) {
152             cacheForSeconds(response, seconds);
153         }
154         else if (seconds == 0) {
155             preventCaching(response);
156         }
157         // Leave caching to the portlet configuration otherwise.
158
}
159
160 }
161
Popular Tags