001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.cachius;
025:
026: import java.io.IOException;
027: import java.io.PrintWriter;
028:
029: import javax.servlet.ServletOutputStream;
030: import javax.servlet.http.Cookie;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.servlet.http.HttpServletResponseWrapper;
033:
034: import org.riotfamily.cachius.support.Cookies;
035: import org.riotfamily.cachius.support.Headers;
036: import org.riotfamily.cachius.support.SessionIdEncoder;
037: import org.riotfamily.common.web.util.DelegatingServletOutputStream;
038:
039: /**
040: * A HttpServletResponseWrapper that captures the response and updates
041: * the associated CacheItem.
042: *
043: * @author Felix Gnass
044: */
045: public class CachiusResponseWrapper extends HttpServletResponseWrapper {
046:
047: private CacheItem cacheItem;
048:
049: private SessionIdEncoder sessionIdEncoder;
050:
051: private ServletOutputStream outputStream;
052:
053: private PrintWriter writer;
054:
055: private int status = 0;
056:
057: private String contentType;
058:
059: private Headers headers = new Headers();
060:
061: private Cookies cookies = new Cookies();
062:
063: private boolean contentLengthSet;
064:
065: public CachiusResponseWrapper(HttpServletResponse response,
066: CacheItem cacheItem, SessionIdEncoder sessionIdEncoder) {
067:
068: super (response);
069: this .cacheItem = cacheItem;
070: this .sessionIdEncoder = sessionIdEncoder;
071: }
072:
073: public void setStatus(int status) {
074: super .setStatus(status);
075: this .status = status;
076: }
077:
078: public boolean isOk() {
079: return status == 0 || status == HttpServletResponse.SC_OK;
080: }
081:
082: public String getContentType() {
083: return contentType;
084: }
085:
086: public void setContentType(String contentType) {
087: this .contentType = contentType;
088: }
089:
090: public void setContentLength(int len) {
091: contentLengthSet = true;
092: }
093:
094: public void addDateHeader(String name, long date) {
095: headers.addDate(name, date);
096: }
097:
098: public void setDateHeader(String name, long date) {
099: headers.setDate(name, date);
100: }
101:
102: public void addIntHeader(String name, int value) {
103: headers.addInt(name, value);
104: }
105:
106: public void setIntHeader(String name, int value) {
107: headers.setInt(name, value);
108: }
109:
110: public void addHeader(String name, String value) {
111: headers.add(name, value);
112: }
113:
114: public void setHeader(String name, String value) {
115: headers.set(name, value);
116: }
117:
118: public void addCookie(Cookie cookie) {
119: cookies.add(cookie);
120: }
121:
122: /**
123: * Returns an ServletOutputStream that writes into the OutputStream
124: * provided by the CacheItem. All output is redirected so nothing will be
125: * sent to the client.
126: *
127: * @throws IllegalStateException If getWriter() has been called before
128: * @throws IOException
129: */
130: public ServletOutputStream getOutputStream() throws IOException {
131: if (writer != null) {
132: throw new IllegalStateException();
133: }
134: if (outputStream == null) {
135: outputStream = new DelegatingServletOutputStream(cacheItem
136: .getOutputStream());
137: }
138: return outputStream;
139: }
140:
141: /**
142: * Returns a PrintWriter that writes into the Writer provided by the
143: * CacheItem. All output is redirected so nothing will be
144: * sent to the client.
145: *
146: * @throws IllegalStateExcepion If getOutputStream() has been called before
147: * @throws IOException
148: */
149: public PrintWriter getWriter() throws IOException {
150: if (writer == null) {
151: if (outputStream != null) {
152: throw new IllegalStateException();
153: }
154: writer = new PrintWriter(cacheItem
155: .getWriter(sessionIdEncoder.getSessionId()));
156: }
157: return writer;
158: }
159:
160: public void flushBuffer() throws IOException {
161: if (writer != null) {
162: writer.flush();
163: } else if (outputStream != null) {
164: outputStream.flush();
165: }
166: }
167:
168: public void stopCapturing() throws IOException {
169: flushBuffer();
170: if (writer != null) {
171: writer.close();
172: } else if (outputStream != null) {
173: outputStream.close();
174: }
175: }
176:
177: /**
178: * Sets the captured headers on the CacheItem.
179: */
180: public void updateHeaders() {
181: cacheItem.setContentType(contentType);
182: cacheItem.setHeaders(headers);
183: cacheItem.setCookies(cookies);
184: cacheItem.setSetContentLength(contentLengthSet);
185: }
186:
187: /**
188: * Delegates the call to {@link SessionIdEncoder#encodeRedirectURL(String)}
189: * to ensure that the session state remains the same during processing.
190: */
191: public String encodeRedirectURL(String url) {
192: return sessionIdEncoder.encodeRedirectURL(url);
193: }
194:
195: /**
196: * Delegates the call to {@link #encodeRedirectURL(String)}.
197: */
198: public String encodeRedirectUrl(String url) {
199: return encodeRedirectURL(url);
200: }
201:
202: /**
203: * Delegates the call to {@link SessionIdEncoder#encodeURL(String)}
204: * to ensure that the session state remains the same during processing.
205: */
206: public String encodeURL(String url) {
207: return sessionIdEncoder.encodeURL(url);
208: }
209:
210: /**
211: * Delegates the call to {@link #encodeURL(String)}.
212: */
213: public String encodeUrl(String url) {
214: return encodeURL(url);
215: }
216:
217: }
|