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