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.coyote.tomcat5;
018:
019: import java.io.IOException;
020: import java.io.PrintWriter;
021: import java.security.AccessController;
022: import java.security.PrivilegedAction;
023: import java.security.PrivilegedActionException;
024: import java.security.PrivilegedExceptionAction;
025: import java.util.Locale;
026:
027: import javax.servlet.ServletOutputStream;
028: import javax.servlet.http.Cookie;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.apache.catalina.connector.ResponseFacade;
032:
033: /**
034: * Facade class that wraps a Coyote response object.
035: * All methods are delegated to the wrapped response.
036: *
037: * @author Remy Maucherat
038: * @author Jean-Francois Arcand
039: * @version $Revision: 1.4 $ $Date: 2004/02/27 14:58:53 $
040: */
041:
042: public class CoyoteResponseFacade extends ResponseFacade implements
043: HttpServletResponse {
044:
045: // ----------------------------------------------------------- DoPrivileged
046:
047: private final class SetContentTypePrivilegedAction implements
048: PrivilegedAction {
049: private String contentType;
050:
051: public SetContentTypePrivilegedAction(String contentType) {
052: this .contentType = contentType;
053: }
054:
055: public Object run() {
056: response.setContentType(contentType);
057: return null;
058: }
059: }
060:
061: private final class DateHeaderPrivilegedAction implements
062: PrivilegedAction {
063: private String name;
064: private long value;
065: private boolean add;
066:
067: DateHeaderPrivilegedAction(String name, long value, boolean add) {
068: this .name = name;
069: this .value = value;
070: this .add = add;
071: }
072:
073: public Object run() {
074: if (add) {
075: response.addDateHeader(name, value);
076: } else {
077: response.setDateHeader(name, value);
078: }
079: return null;
080: }
081: }
082:
083: // ----------------------------------------------------------- Constructors
084:
085: /**
086: * Construct a wrapper for the specified response.
087: *
088: * @param response The response to be wrapped
089: */
090: public CoyoteResponseFacade(CoyoteResponse response) {
091:
092: super (response);
093: this .response = response;
094:
095: }
096:
097: // ----------------------------------------------------- Instance Variables
098:
099: /**
100: * The wrapped response.
101: */
102: protected CoyoteResponse response = null;
103:
104: // --------------------------------------------------------- Public Methods
105:
106: /**
107: * Clear facade.
108: */
109: public void clear() {
110: response = null;
111: }
112:
113: public void finish() {
114:
115: response.setSuspended(true);
116:
117: }
118:
119: public boolean isFinished() {
120:
121: return response.isSuspended();
122:
123: }
124:
125: // ------------------------------------------------ ServletResponse Methods
126:
127: public String getCharacterEncoding() {
128: return response.getCharacterEncoding();
129: }
130:
131: public ServletOutputStream getOutputStream() throws IOException {
132:
133: // if (isFinished())
134: // throw new IllegalStateException
135: // (/*sm.getString("responseFacade.finished")*/);
136:
137: ServletOutputStream sos = response.getOutputStream();
138: if (isFinished())
139: response.setSuspended(true);
140: return (sos);
141:
142: }
143:
144: public PrintWriter getWriter() throws IOException {
145:
146: // if (isFinished())
147: // throw new IllegalStateException
148: // (/*sm.getString("responseFacade.finished")*/);
149:
150: PrintWriter writer = response.getWriter();
151: if (isFinished())
152: response.setSuspended(true);
153: return (writer);
154:
155: }
156:
157: public void setContentLength(int len) {
158:
159: if (isCommitted())
160: return;
161:
162: response.setContentLength(len);
163:
164: }
165:
166: public void setContentType(String type) {
167:
168: if (isCommitted())
169: return;
170:
171: if (System.getSecurityManager() != null) {
172: AccessController
173: .doPrivileged(new SetContentTypePrivilegedAction(
174: type));
175: } else {
176: response.setContentType(type);
177: }
178: }
179:
180: public void setBufferSize(int size) {
181:
182: if (isCommitted())
183: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
184:
185: response.setBufferSize(size);
186:
187: }
188:
189: public int getBufferSize() {
190: return response.getBufferSize();
191: }
192:
193: public void flushBuffer() throws IOException {
194:
195: if (isFinished())
196: // throw new IllegalStateException
197: // (/*sm.getString("responseFacade.finished")*/);
198: return;
199:
200: if (System.getSecurityManager() != null) {
201: try {
202: AccessController
203: .doPrivileged(new PrivilegedExceptionAction() {
204:
205: public Object run() throws IOException {
206: response.setAppCommitted(true);
207:
208: response.flushBuffer();
209: return null;
210: }
211: });
212: } catch (PrivilegedActionException e) {
213: Exception ex = e.getException();
214: if (ex instanceof IOException) {
215: throw (IOException) ex;
216: }
217: }
218: } else {
219: response.setAppCommitted(true);
220:
221: response.flushBuffer();
222: }
223:
224: }
225:
226: public void resetBuffer() {
227:
228: if (isCommitted())
229: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
230:
231: response.resetBuffer();
232:
233: }
234:
235: public boolean isCommitted() {
236: return (response.isAppCommitted());
237: }
238:
239: public void reset() {
240:
241: if (isCommitted())
242: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
243:
244: response.reset();
245:
246: }
247:
248: public void setLocale(Locale loc) {
249:
250: if (isCommitted())
251: return;
252:
253: response.setLocale(loc);
254: }
255:
256: public Locale getLocale() {
257: return response.getLocale();
258: }
259:
260: public void addCookie(Cookie cookie) {
261:
262: if (isCommitted())
263: return;
264:
265: response.addCookie(cookie);
266:
267: }
268:
269: public boolean containsHeader(String name) {
270: return response.containsHeader(name);
271: }
272:
273: public String encodeURL(String url) {
274: return response.encodeURL(url);
275: }
276:
277: public String encodeRedirectURL(String url) {
278: return response.encodeRedirectURL(url);
279: }
280:
281: public String encodeUrl(String url) {
282: return response.encodeURL(url);
283: }
284:
285: public String encodeRedirectUrl(String url) {
286: return response.encodeRedirectURL(url);
287: }
288:
289: public void sendError(int sc, String msg) throws IOException {
290:
291: if (isCommitted())
292: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
293:
294: response.setAppCommitted(true);
295:
296: response.sendError(sc, msg);
297:
298: }
299:
300: public void sendError(int sc) throws IOException {
301:
302: if (isCommitted())
303: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
304:
305: response.setAppCommitted(true);
306:
307: response.sendError(sc);
308:
309: }
310:
311: public void sendRedirect(String location) throws IOException {
312:
313: if (isCommitted())
314: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
315:
316: response.setAppCommitted(true);
317:
318: response.sendRedirect(location);
319:
320: }
321:
322: public void setDateHeader(String name, long date) {
323:
324: if (isCommitted())
325: return;
326:
327: if (System.getSecurityManager() != null) {
328: AccessController
329: .doPrivileged(new DateHeaderPrivilegedAction(name,
330: date, false));
331: } else {
332: response.setDateHeader(name, date);
333: }
334:
335: }
336:
337: public void addDateHeader(String name, long date) {
338:
339: if (isCommitted())
340: return;
341:
342: if (System.getSecurityManager() != null) {
343: AccessController
344: .doPrivileged(new DateHeaderPrivilegedAction(name,
345: date, true));
346: } else {
347: response.addDateHeader(name, date);
348: }
349:
350: }
351:
352: public void setHeader(String name, String value) {
353:
354: if (isCommitted())
355: return;
356:
357: response.setHeader(name, value);
358:
359: }
360:
361: public void addHeader(String name, String value) {
362:
363: if (isCommitted())
364: return;
365:
366: response.addHeader(name, value);
367:
368: }
369:
370: public void setIntHeader(String name, int value) {
371:
372: if (isCommitted())
373: return;
374:
375: response.setIntHeader(name, value);
376:
377: }
378:
379: public void addIntHeader(String name, int value) {
380:
381: if (isCommitted())
382: return;
383:
384: response.addIntHeader(name, value);
385:
386: }
387:
388: public void setStatus(int sc) {
389:
390: if (isCommitted())
391: return;
392:
393: response.setStatus(sc);
394:
395: }
396:
397: public void setStatus(int sc, String sm) {
398:
399: if (isCommitted())
400: return;
401:
402: response.setStatus(sc, sm);
403:
404: }
405:
406: }
|