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.ssi;
018:
019: import java.io.IOException;
020: import java.io.PrintWriter;
021:
022: import javax.servlet.ServletOutputStream;
023: import javax.servlet.http.HttpServletResponse;
024: import javax.servlet.http.HttpServletResponseWrapper;
025:
026: /**
027: * A HttpServletResponseWrapper, used from <code>SSIServletExternalResolver</code>
028: *
029: * @author Bip Thelin
030: * @version $Revision: 1.4 $, $Date: 2004/05/26 16:14:49 $
031: */
032: public class ResponseIncludeWrapper extends HttpServletResponseWrapper {
033:
034: /**
035: * Our ServletOutputStream
036: */
037: protected ServletOutputStream originalServletOutputStream;
038: protected ServletOutputStream servletOutputStream;
039: protected PrintWriter printWriter;
040:
041: /**
042: * Initialize our wrapper with the current HttpServletResponse
043: * and ServletOutputStream.
044: *
045: * @param res The HttpServletResponse to use
046: * @param originalServletOutputStream The ServletOutputStream' to use
047: */
048: public ResponseIncludeWrapper(HttpServletResponse res,
049: ServletOutputStream originalServletOutputStream) {
050: super (res);
051: this .originalServletOutputStream = originalServletOutputStream;
052: }
053:
054: /**
055: * Flush the servletOutputStream or printWriter ( only one will be non-null )
056: *
057: * This must be called after a requestDispatcher.include, since we can't assume that
058: * the included servlet flushed its stream.
059: */
060: public void flushOutputStreamOrWriter() throws IOException {
061: if (servletOutputStream != null) {
062: servletOutputStream.flush();
063: }
064: if (printWriter != null) {
065: printWriter.flush();
066: }
067: }
068:
069: /**
070: * Return a printwriter, throws and exception if a
071: * OutputStream already been returned.
072: *
073: * @return a PrintWriter object
074: * @exception java.io.IOException if the outputstream already been called
075: */
076: public PrintWriter getWriter() throws java.io.IOException {
077: if (servletOutputStream == null) {
078: if (printWriter == null) {
079: printWriter = new PrintWriter(
080: originalServletOutputStream);
081: }
082: return printWriter;
083: }
084: throw new IllegalStateException();
085: }
086:
087: /**
088: * Return a OutputStream, throws and exception if a
089: * printwriter already been returned.
090: *
091: * @return a OutputStream object
092: * @exception java.io.IOException if the printwriter already been called
093: */
094: public ServletOutputStream getOutputStream()
095: throws java.io.IOException {
096: if (printWriter == null) {
097: if (servletOutputStream == null) {
098: servletOutputStream = originalServletOutputStream;
099: }
100: return servletOutputStream;
101: }
102: throw new IllegalStateException();
103: }
104: }
|