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.connector;
018:
019: import java.io.IOException;
020: import java.io.PrintWriter;
021: import java.util.Locale;
022:
023: import javax.servlet.ServletOutputStream;
024: import javax.servlet.ServletResponse;
025:
026: import org.apache.catalina.Response;
027:
028: /**
029: * Facade class that wraps a Catalina-internal <b>Response</b>
030: * object. All methods are delegated to the wrapped response.
031: *
032: * @author Remy Maucherat
033: * @version $Revision: 1.5 $ $Date: 2004/02/27 14:58:41 $
034: */
035:
036: public class ResponseFacade implements ServletResponse {
037:
038: // ----------------------------------------------------------- Constructors
039:
040: /**
041: * Construct a wrapper for the specified response.
042: *
043: * @param response The response to be wrapped
044: */
045: public ResponseFacade(Response response) {
046: this .resp = response;
047: this .response = (ServletResponse) response;
048: }
049:
050: // ----------------------------------------------------- Instance Variables
051:
052: /**
053: * The wrapped response.
054: */
055: protected ServletResponse response = null;
056:
057: /**
058: * The wrapped response.
059: */
060: protected Response resp = null;
061:
062: // --------------------------------------------------------- Public Methods
063:
064: /**
065: * Clear facade.
066: */
067: public void clear() {
068: response = null;
069: resp = null;
070: }
071:
072: public void finish() {
073:
074: resp.setSuspended(true);
075:
076: }
077:
078: public boolean isFinished() {
079:
080: return resp.isSuspended();
081:
082: }
083:
084: // ------------------------------------------------ ServletResponse Methods
085:
086: public String getCharacterEncoding() {
087: return response.getCharacterEncoding();
088: }
089:
090: public ServletOutputStream getOutputStream() throws IOException {
091:
092: // if (isFinished())
093: // throw new IllegalStateException
094: // (/*sm.getString("responseFacade.finished")*/);
095:
096: ServletOutputStream sos = response.getOutputStream();
097: if (isFinished())
098: resp.setSuspended(true);
099: return (sos);
100:
101: }
102:
103: public PrintWriter getWriter() throws IOException {
104:
105: // if (isFinished())
106: // throw new IllegalStateException
107: // (/*sm.getString("responseFacade.finished")*/);
108:
109: PrintWriter writer = response.getWriter();
110: if (isFinished())
111: resp.setSuspended(true);
112: return (writer);
113:
114: }
115:
116: public void setContentLength(int len) {
117:
118: if (isCommitted())
119: return;
120:
121: response.setContentLength(len);
122:
123: }
124:
125: public void setCharacterEncoding(String charset) {
126:
127: if (isCommitted())
128: return;
129:
130: response.setCharacterEncoding(charset);
131:
132: }
133:
134: public void setContentType(String type) {
135:
136: if (isCommitted())
137: return;
138:
139: response.setContentType(type);
140:
141: }
142:
143: public String getContentType() {
144:
145: return response.getContentType();
146:
147: }
148:
149: public void setBufferSize(int size) {
150:
151: if (isCommitted())
152: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
153:
154: response.setBufferSize(size);
155:
156: }
157:
158: public int getBufferSize() {
159: return response.getBufferSize();
160: }
161:
162: public void flushBuffer() throws IOException {
163:
164: if (isFinished())
165: // throw new IllegalStateException
166: // (/*sm.getString("responseFacade.finished")*/);
167: return;
168:
169: resp.setAppCommitted(true);
170:
171: try {
172: response.flushBuffer();
173: } catch (IOException ioe) {
174: // An IOException on a write is almost always due to
175: // the remote client aborting the request. Wrap this
176: // so that it can be handled better by the error dispatcher.
177: throw new ClientAbortException(ioe);
178: }
179: }
180:
181: public void resetBuffer() {
182:
183: if (isCommitted())
184: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
185:
186: response.resetBuffer();
187:
188: }
189:
190: public boolean isCommitted() {
191: return (resp.isAppCommitted());
192: }
193:
194: public void reset() {
195:
196: if (isCommitted())
197: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
198:
199: response.reset();
200:
201: }
202:
203: public void setLocale(Locale loc) {
204:
205: if (isCommitted())
206: return;
207:
208: response.setLocale(loc);
209: }
210:
211: public Locale getLocale() {
212: return response.getLocale();
213: }
214:
215: }
|