001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.maven.cactus.sample.util;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.PrintWriter;
024:
025: import javax.servlet.ServletOutputStream;
026: import javax.servlet.http.HttpServletResponse;
027: import javax.servlet.http.HttpServletResponseWrapper;
028:
029: /**
030: * Wrapper around a <code>HttpServletResponse</code> that we use to easily
031: * write filters that manipulate the output stream. Indeed, we cannot pass
032: * the output stream of our filter direectly to the next filter in the chain
033: * because then we won't be able to write to it (the response will have been
034: * committed). Instead, we pass this wrapper class and then copy its data
035: * to our filter output stream.
036: *
037: * Note: This code was adapted from the Filter tutorial found
038: * {@link <a href="http://www.orionserver.com/tutorials/filters/lesson3/">
039: * here</a>}
040: *
041: * @version $Id: GenericResponseWrapper.java 238815 2004-02-29 16:34:44Z vmassol $
042: *
043: * @see FilterServletOutputStream
044: */
045: public class GenericResponseWrapper extends HttpServletResponseWrapper {
046: /**
047: * Holder for the output data
048: */
049: private ByteArrayOutputStream output;
050:
051: /**
052: * Save the content length so that we can query it at a later time
053: * (otherwise it would not be possible as
054: * <code>HttpServletResponseWrapper</code> does not have a method to get
055: * the content length).
056: */
057: private int contentLength;
058:
059: /**
060: * Save the content type so that we can query it at a later time
061: * (otherwise it would not be possible as
062: * <code>HttpServletResponseWrapper</code> does not have a method to get
063: * the content type).
064: */
065: private String contentType;
066:
067: // Constructors ----------------------------------------------------------
068:
069: /**
070: * @param theResponse the wrapped response object
071: */
072: public GenericResponseWrapper(HttpServletResponse theResponse) {
073: super (theResponse);
074: this .output = new ByteArrayOutputStream();
075: }
076:
077: // New methods -----------------------------------------------------------
078:
079: /**
080: * @return the data sent to the output stream
081: */
082: public byte[] getData() {
083: return output.toByteArray();
084: }
085:
086: // Overridden methods ----------------------------------------------------
087:
088: /**
089: * @see HttpServletResponseWrapper#getOutputStream()
090: */
091: public ServletOutputStream getOutputStream() {
092: return new FilterServletOutputStream(this .output);
093: }
094:
095: /**
096: * @see HttpServletResponseWrapper#setContentLength(int)
097: */
098: public void setContentLength(int theLength) {
099: this .contentLength = theLength;
100: super .setContentLength(theLength);
101: }
102:
103: /**
104: * @see HttpServletResponseWrapper#getContentLength()
105: */
106: public int getContentLength() {
107: return this .contentLength;
108: }
109:
110: /**
111: * @see HttpServletResponseWrapper#setContentType(String)
112: */
113: public void setContentType(String theType) {
114: this .contentType = theType;
115: super .setContentType(theType);
116: }
117:
118: /**
119: * @see HttpServletResponseWrapper#getContentType()
120: */
121: public String getContentType() {
122: return this .contentType;
123: }
124:
125: /**
126: * @see HttpServletResponseWrapper#getWriter()
127: */
128: public PrintWriter getWriter() {
129: return new PrintWriter(getOutputStream(), true);
130: }
131: }
|