001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpResponseStream.java,v 1.14 2002/03/18 07:15:40 remm Exp $
003: * $Revision: 1.14 $
004: * $Date: 2002/03/18 07:15:40 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.connector.http;
065:
066: import java.io.IOException;
067: import javax.servlet.http.HttpServletResponse;
068: import javax.servlet.http.HttpServletRequest;
069: import org.apache.catalina.Response;
070: import org.apache.catalina.connector.ResponseStream;
071:
072: /**
073: * Response stream for the HTTP/1.1 connector. This stream will automatically
074: * chunk the answer if using HTTP/1.1 and no Content-Length has been properly
075: * set.
076: *
077: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
078: * @deprecated
079: */
080: public final class HttpResponseStream extends ResponseStream {
081:
082: // ----------------------------------------------------------- Constructors
083:
084: private static final int MAX_CHUNK_SIZE = 4096;
085:
086: private static final String CRLF = "\r\n";
087:
088: // ----------------------------------------------------------- Constructors
089:
090: /**
091: * Construct a servlet output stream associated with the specified Request.
092: *
093: * @param response The associated response
094: */
095: public HttpResponseStream(HttpResponseImpl response) {
096:
097: super (response);
098: checkChunking(response);
099: checkHead(response);
100:
101: }
102:
103: // ----------------------------------------------------- Instance Variables
104:
105: /**
106: * True if chunking is allowed.
107: */
108: private boolean useChunking;
109:
110: /**
111: * True if printing a chunk.
112: */
113: private boolean writingChunk;
114:
115: /**
116: * True if no content should be written.
117: */
118: private boolean writeContent;
119:
120: // -------------------------------------------- ServletOutputStream Methods
121:
122: /**
123: * Write the specified byte to our output stream.
124: *
125: * @param b The byte to be written
126: *
127: * @exception IOException if an input/output error occurs
128: */
129: public void write(int b) throws IOException {
130:
131: if (suspended)
132: return;
133:
134: if (!writeContent)
135: return;
136:
137: if (useChunking && !writingChunk) {
138: writingChunk = true;
139: try {
140: print("1\r\n");
141: super .write(b);
142: println();
143: } finally {
144: writingChunk = false;
145: }
146: } else {
147: super .write(b);
148: }
149:
150: }
151:
152: /**
153: * Write the specified byte array.
154: */
155: public void write(byte[] b, int off, int len) throws IOException {
156:
157: if (suspended)
158: return;
159:
160: if (!writeContent)
161: return;
162:
163: if (useChunking && !writingChunk) {
164: if (len > 0) {
165: writingChunk = true;
166: try {
167: println(Integer.toHexString(len));
168: super .write(b, off, len);
169: println();
170: } finally {
171: writingChunk = false;
172: }
173: }
174: } else {
175: super .write(b, off, len);
176: }
177:
178: }
179:
180: /**
181: * Close this output stream, causing any buffered data to be flushed and
182: * any further output data to throw an IOException.
183: */
184: public void close() throws IOException {
185:
186: if (suspended)
187: throw new IOException(sm
188: .getString("responseStream.suspended"));
189:
190: if (!writeContent)
191: return;
192:
193: if (useChunking) {
194: // Write the final chunk.
195: writingChunk = true;
196: try {
197: print("0\r\n\r\n");
198: } finally {
199: writingChunk = false;
200: }
201: }
202: super .close();
203:
204: }
205:
206: // -------------------------------------------------------- Package Methods
207:
208: void checkChunking(HttpResponseImpl response) {
209: // If any data has already been written to the stream, we must not
210: // change the chunking mode
211: if (count != 0)
212: return;
213: // Check the basic cases in which we chunk
214: useChunking = (!response.isCommitted()
215: && response.getContentLength() == -1 && response
216: .getStatus() != HttpServletResponse.SC_NOT_MODIFIED);
217: if (!response.isChunkingAllowed() && useChunking) {
218: // If we should chunk, but chunking is forbidden by the connector,
219: // we close the connection
220: response.setHeader("Connection", "close");
221: }
222: // Don't chunk is the connection will be closed
223: useChunking = (useChunking && !response.isCloseConnection());
224: if (useChunking) {
225: response.setHeader("Transfer-Encoding", "chunked");
226: } else if (response.isChunkingAllowed()) {
227: response.removeHeader("Transfer-Encoding", "chunked");
228: }
229: }
230:
231: protected void checkHead(HttpResponseImpl response) {
232: HttpServletRequest servletRequest = (HttpServletRequest) response
233: .getRequest();
234: if ("HEAD".equals(servletRequest.getMethod())) {
235: writeContent = false;
236: } else {
237: writeContent = true;
238: }
239: }
240:
241: }
|