001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
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.apache.catalina.core;
018:
019: import java.io.IOException;
020: import java.util.Locale;
021:
022: import javax.servlet.http.Cookie;
023: import javax.servlet.http.HttpServletResponse;
024: import javax.servlet.http.HttpServletResponseWrapper;
025:
026: import org.apache.catalina.util.StringManager;
027:
028: /**
029: * Wrapper around a <code>javax.servlet.http.HttpServletResponse</code>
030: * that transforms an application response object (which might be the original
031: * one passed to a servlet, or might be based on the 2.3
032: * <code>javax.servlet.http.HttpServletResponseWrapper</code> class)
033: * back into an internal <code>org.apache.catalina.HttpResponse</code>.
034: * <p>
035: * <strong>WARNING</strong>: Due to Java's lack of support for multiple
036: * inheritance, all of the logic in <code>ApplicationResponse</code> is
037: * duplicated in <code>ApplicationHttpResponse</code>. Make sure that you
038: * keep these two classes in synchronization when making changes!
039: *
040: * @author Craig R. McClanahan
041: * @version $Revision: 1.5 $ $Date: 2004/02/27 14:58:42 $
042: */
043:
044: class ApplicationHttpResponse extends HttpServletResponseWrapper {
045:
046: // ----------------------------------------------------------- Constructors
047:
048: /**
049: * Construct a new wrapped response around the specified servlet response.
050: *
051: * @param response The servlet response being wrapped
052: */
053: public ApplicationHttpResponse(HttpServletResponse response) {
054:
055: this (response, false);
056:
057: }
058:
059: /**
060: * Construct a new wrapped response around the specified servlet response.
061: *
062: * @param response The servlet response being wrapped
063: * @param included <code>true</code> if this response is being processed
064: * by a <code>RequestDispatcher.include()</code> call
065: */
066: public ApplicationHttpResponse(HttpServletResponse response,
067: boolean included) {
068:
069: super (response);
070: setIncluded(included);
071:
072: }
073:
074: // ----------------------------------------------------- Instance Variables
075:
076: /**
077: * Is this wrapped response the subject of an <code>include()</code>
078: * call?
079: */
080: protected boolean included = false;
081:
082: /**
083: * Descriptive information about this implementation.
084: */
085: protected static final String info = "org.apache.catalina.core.ApplicationHttpResponse/1.0";
086:
087: /**
088: * The string manager for this package.
089: */
090: protected static StringManager sm = StringManager
091: .getManager(Constants.Package);
092:
093: // ------------------------------------------------ ServletResponse Methods
094:
095: /**
096: * Disallow <code>reset()</code> calls on a included response.
097: *
098: * @exception IllegalStateException if the response has already
099: * been committed
100: */
101: public void reset() {
102:
103: // If already committed, the wrapped response will throw ISE
104: if (!included || getResponse().isCommitted())
105: getResponse().reset();
106:
107: }
108:
109: /**
110: * Disallow <code>setContentLength()</code> calls on an included response.
111: *
112: * @param len The new content length
113: */
114: public void setContentLength(int len) {
115:
116: if (!included)
117: getResponse().setContentLength(len);
118:
119: }
120:
121: /**
122: * Disallow <code>setContentType()</code> calls on an included response.
123: *
124: * @param type The new content type
125: */
126: public void setContentType(String type) {
127:
128: if (!included)
129: getResponse().setContentType(type);
130:
131: }
132:
133: /**
134: * Disallow <code>setLocale()</code> calls on an included response.
135: *
136: * @param loc The new locale
137: */
138: public void setLocale(Locale loc) {
139:
140: if (!included)
141: getResponse().setLocale(loc);
142:
143: }
144:
145: /**
146: * Ignore <code>setBufferSize()</code> calls on an included response.
147: *
148: * @param size The buffer size
149: */
150: public void setBufferSize(int size) {
151: if (!included)
152: getResponse().setBufferSize(size);
153: }
154:
155: // -------------------------------------------- HttpServletResponse Methods
156:
157: /**
158: * Disallow <code>addCookie()</code> calls on an included response.
159: *
160: * @param cookie The new cookie
161: */
162: public void addCookie(Cookie cookie) {
163:
164: if (!included)
165: ((HttpServletResponse) getResponse()).addCookie(cookie);
166:
167: }
168:
169: /**
170: * Disallow <code>addDateHeader()</code> calls on an included response.
171: *
172: * @param name The new header name
173: * @param value The new header value
174: */
175: public void addDateHeader(String name, long value) {
176:
177: if (!included)
178: ((HttpServletResponse) getResponse()).addDateHeader(name,
179: value);
180:
181: }
182:
183: /**
184: * Disallow <code>addHeader()</code> calls on an included response.
185: *
186: * @param name The new header name
187: * @param value The new header value
188: */
189: public void addHeader(String name, String value) {
190:
191: if (!included)
192: ((HttpServletResponse) getResponse())
193: .addHeader(name, value);
194:
195: }
196:
197: /**
198: * Disallow <code>addIntHeader()</code> calls on an included response.
199: *
200: * @param name The new header name
201: * @param value The new header value
202: */
203: public void addIntHeader(String name, int value) {
204:
205: if (!included)
206: ((HttpServletResponse) getResponse()).addIntHeader(name,
207: value);
208:
209: }
210:
211: /**
212: * Disallow <code>sendError()</code> calls on an included response.
213: *
214: * @param sc The new status code
215: *
216: * @exception IOException if an input/output error occurs
217: */
218: public void sendError(int sc) throws IOException {
219:
220: if (!included)
221: ((HttpServletResponse) getResponse()).sendError(sc);
222:
223: }
224:
225: /**
226: * Disallow <code>sendError()</code> calls on an included response.
227: *
228: * @param sc The new status code
229: * @param msg The new message
230: *
231: * @exception IOException if an input/output error occurs
232: */
233: public void sendError(int sc, String msg) throws IOException {
234:
235: if (!included)
236: ((HttpServletResponse) getResponse()).sendError(sc, msg);
237:
238: }
239:
240: /**
241: * Disallow <code>sendRedirect()</code> calls on an included response.
242: *
243: * @param location The new location
244: *
245: * @exception IOException if an input/output error occurs
246: */
247: public void sendRedirect(String location) throws IOException {
248:
249: if (!included)
250: ((HttpServletResponse) getResponse())
251: .sendRedirect(location);
252:
253: }
254:
255: /**
256: * Disallow <code>setDateHeader()</code> calls on an included response.
257: *
258: * @param name The new header name
259: * @param value The new header value
260: */
261: public void setDateHeader(String name, long value) {
262:
263: if (!included)
264: ((HttpServletResponse) getResponse()).setDateHeader(name,
265: value);
266:
267: }
268:
269: /**
270: * Disallow <code>setHeader()</code> calls on an included response.
271: *
272: * @param name The new header name
273: * @param value The new header value
274: */
275: public void setHeader(String name, String value) {
276:
277: if (!included)
278: ((HttpServletResponse) getResponse())
279: .setHeader(name, value);
280:
281: }
282:
283: /**
284: * Disallow <code>setIntHeader()</code> calls on an included response.
285: *
286: * @param name The new header name
287: * @param value The new header value
288: */
289: public void setIntHeader(String name, int value) {
290:
291: if (!included)
292: ((HttpServletResponse) getResponse()).setIntHeader(name,
293: value);
294:
295: }
296:
297: /**
298: * Disallow <code>setStatus()</code> calls on an included response.
299: *
300: * @param sc The new status code
301: */
302: public void setStatus(int sc) {
303:
304: if (!included)
305: ((HttpServletResponse) getResponse()).setStatus(sc);
306:
307: }
308:
309: /**
310: * Disallow <code>setStatus()</code> calls on an included response.
311: *
312: * @param sc The new status code
313: * @param msg The new message
314: */
315: public void setStatus(int sc, String msg) {
316:
317: if (!included)
318: ((HttpServletResponse) getResponse()).setStatus(sc, msg);
319:
320: }
321:
322: // -------------------------------------------------------- Package Methods
323:
324: /**
325: * Return descriptive information about this implementation.
326: */
327: public String getInfo() {
328:
329: return (info);
330:
331: }
332:
333: /**
334: * Return the included flag for this response.
335: */
336: boolean isIncluded() {
337:
338: return (this .included);
339:
340: }
341:
342: /**
343: * Set the included flag for this response.
344: *
345: * @param included The new included flag
346: */
347: void setIncluded(boolean included) {
348:
349: this .included = included;
350:
351: }
352:
353: /**
354: * Set the response that we are wrapping.
355: *
356: * @param response The new wrapped response
357: */
358: void setResponse(HttpServletResponse response) {
359:
360: super.setResponse(response);
361:
362: }
363:
364: }
|