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.portlet.handler;
018:
019: import javax.portlet.PortletException;
020: import javax.portlet.PortletRequest;
021: import javax.portlet.PortletResponse;
022: import javax.portlet.RenderRequest;
023: import javax.portlet.RenderResponse;
024:
025: import org.springframework.web.portlet.context.PortletApplicationObjectSupport;
026:
027: /**
028: * Convenient superclass for any kind of web content generator,
029: * like AbstractController. Can also be used for custom handlers
030: * that have their own HandlerAdapter.
031: *
032: * <p>Supports portlet cache control options.
033: *
034: * @author Juergen Hoeller
035: * @author John A. Lewis
036: * @since 2.0
037: * @see org.springframework.web.portlet.mvc.AbstractController
038: */
039: public abstract class PortletContentGenerator extends
040: PortletApplicationObjectSupport {
041:
042: private boolean requireSession = false;
043:
044: private int cacheSeconds = -1;
045:
046: /**
047: * Set whether a session should be required to handle requests.
048: */
049: public final void setRequireSession(boolean requireSession) {
050: this .requireSession = requireSession;
051: }
052:
053: /**
054: * Return whether a session is required to handle requests.
055: */
056: public final boolean isRequireSession() {
057: return requireSession;
058: }
059:
060: /**
061: * Cache content for the given number of seconds. Default is -1,
062: * indicating no override of portlet content caching.
063: * <p>Only if this is set to 0 (no cache) or a positive value (cache for
064: * this many seconds) will this class override the portlet settings.
065: * <p>The cache setting can be overwritten by subclasses, before content is generated.
066: */
067: public final void setCacheSeconds(int seconds) {
068: this .cacheSeconds = seconds;
069: }
070:
071: /**
072: * Return the number of seconds that content is cached.
073: */
074: public final int getCacheSeconds() {
075: return cacheSeconds;
076: }
077:
078: /**
079: * Check and prepare the given request and response according to the settings
080: * of this generator. Checks for a required session, and applies the number of
081: * cache seconds configured for this generator (if it is a render request/response).
082: * @param request current portlet request
083: * @param response current portlet response
084: * @throws PortletException if the request cannot be handled because a check failed
085: */
086: protected final void check(PortletRequest request,
087: PortletResponse response) throws PortletException {
088: if (this .requireSession) {
089: if (request.getPortletSession(false) == null) {
090: throw new PortletSessionRequiredException(
091: "Pre-existing session required but none found");
092: }
093: }
094: }
095:
096: /**
097: * Check and prepare the given request and response according to the settings
098: * of this generator. Checks for a required session, and applies the number of
099: * cache seconds configured for this generator (if it is a render request/response).
100: * @param request current portlet request
101: * @param response current portlet response
102: * @throws PortletException if the request cannot be handled because a check failed
103: */
104: protected final void checkAndPrepare(RenderRequest request,
105: RenderResponse response) throws PortletException {
106:
107: checkAndPrepare(request, response, this .cacheSeconds);
108: }
109:
110: /**
111: * Check and prepare the given request and response according to the settings
112: * of this generator. Checks for a required session, and applies the given
113: * number of cache seconds (if it is a render request/response).
114: * @param request current portlet request
115: * @param response current portlet response
116: * @param cacheSeconds positive number of seconds into the future that the
117: * response should be cacheable for, 0 to prevent caching
118: * @throws PortletException if the request cannot be handled because a check failed
119: */
120: protected final void checkAndPrepare(RenderRequest request,
121: RenderResponse response, int cacheSeconds)
122: throws PortletException {
123:
124: check(request, response);
125: applyCacheSeconds(response, cacheSeconds);
126: }
127:
128: /**
129: * Prevent the render response from being cached.
130: */
131: protected final void preventCaching(RenderResponse response) {
132: cacheForSeconds(response, 0);
133: }
134:
135: /**
136: * Set portlet response to allow caching for the given number of seconds.
137: * @param response current portlet render response
138: * @param seconds number of seconds into the future that the response
139: * should be cacheable for
140: */
141: protected final void cacheForSeconds(RenderResponse response,
142: int seconds) {
143: response.setProperty(RenderResponse.EXPIRATION_CACHE, Integer
144: .toString(seconds));
145: }
146:
147: /**
148: * Apply the given cache seconds to the render response
149: * @param response current portlet render response
150: * @param seconds positive number of seconds into the future that the
151: * response should be cacheable for, 0 to prevent caching
152: */
153: protected final void applyCacheSeconds(RenderResponse response,
154: int seconds) {
155: if (seconds > 0) {
156: cacheForSeconds(response, seconds);
157: } else if (seconds == 0) {
158: preventCaching(response);
159: }
160: // Leave caching to the portlet configuration otherwise.
161: }
162:
163: }
|