001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.connector;
019:
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.security.AccessController;
023: import java.security.PrivilegedAction;
024: import java.security.PrivilegedActionException;
025: import java.security.PrivilegedExceptionAction;
026: import java.util.Locale;
027:
028: import javax.servlet.ServletOutputStream;
029: import javax.servlet.http.Cookie;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.apache.catalina.Globals;
033: import org.apache.catalina.util.StringManager;
034: import org.apache.catalina.security.SecurityUtil;
035:
036: /**
037: * Facade class that wraps a Coyote response object.
038: * All methods are delegated to the wrapped response.
039: *
040: * @author Remy Maucherat
041: * @author Jean-Francois Arcand
042: * @version $Revision: 505593 $ $Date: 2007-02-10 01:54:56 +0100 (sam., 10 févr. 2007) $
043: */
044: @SuppressWarnings("deprecation")
045: public class ResponseFacade implements HttpServletResponse {
046:
047: // ----------------------------------------------------------- DoPrivileged
048:
049: private final class SetContentTypePrivilegedAction implements
050: PrivilegedAction {
051:
052: private String contentType;
053:
054: public SetContentTypePrivilegedAction(String contentType) {
055: this .contentType = contentType;
056: }
057:
058: public Object run() {
059: response.setContentType(contentType);
060: return null;
061: }
062: }
063:
064: private final class DateHeaderPrivilegedAction implements
065: PrivilegedAction {
066:
067: private String name;
068: private long value;
069: private boolean add;
070:
071: DateHeaderPrivilegedAction(String name, long value, boolean add) {
072: this .name = name;
073: this .value = value;
074: this .add = add;
075: }
076:
077: public Object run() {
078: if (add) {
079: response.addDateHeader(name, value);
080: } else {
081: response.setDateHeader(name, value);
082: }
083: return null;
084: }
085: }
086:
087: // ----------------------------------------------------------- Constructors
088:
089: /**
090: * Construct a wrapper for the specified response.
091: *
092: * @param response The response to be wrapped
093: */
094: public ResponseFacade(Response response) {
095:
096: this .response = response;
097: }
098:
099: // ----------------------------------------------- Class/Instance Variables
100:
101: /**
102: * The string manager for this package.
103: */
104: protected static StringManager sm = StringManager
105: .getManager(Constants.Package);
106:
107: /**
108: * The wrapped response.
109: */
110: protected Response response = null;
111:
112: // --------------------------------------------------------- Public Methods
113:
114: /**
115: * Clear facade.
116: */
117: public void clear() {
118: response = null;
119: }
120:
121: /**
122: * Prevent cloning the facade.
123: */
124: protected Object clone() throws CloneNotSupportedException {
125: throw new CloneNotSupportedException();
126: }
127:
128: public void finish() {
129:
130: if (response == null) {
131: throw new IllegalStateException(sm
132: .getString("responseFacade.nullResponse"));
133: }
134:
135: response.setSuspended(true);
136: }
137:
138: public boolean isFinished() {
139:
140: if (response == null) {
141: throw new IllegalStateException(sm
142: .getString("responseFacade.nullResponse"));
143: }
144:
145: return response.isSuspended();
146: }
147:
148: // ------------------------------------------------ ServletResponse Methods
149:
150: public String getCharacterEncoding() {
151:
152: if (response == null) {
153: throw new IllegalStateException(sm
154: .getString("responseFacade.nullResponse"));
155: }
156:
157: return response.getCharacterEncoding();
158: }
159:
160: public ServletOutputStream getOutputStream() throws IOException {
161:
162: // if (isFinished())
163: // throw new IllegalStateException
164: // (/*sm.getString("responseFacade.finished")*/);
165:
166: ServletOutputStream sos = response.getOutputStream();
167: if (isFinished())
168: response.setSuspended(true);
169: return (sos);
170:
171: }
172:
173: public PrintWriter getWriter() throws IOException {
174:
175: // if (isFinished())
176: // throw new IllegalStateException
177: // (/*sm.getString("responseFacade.finished")*/);
178:
179: PrintWriter writer = response.getWriter();
180: if (isFinished())
181: response.setSuspended(true);
182: return (writer);
183:
184: }
185:
186: public void setContentLength(int len) {
187:
188: if (isCommitted())
189: return;
190:
191: response.setContentLength(len);
192:
193: }
194:
195: public void setContentType(String type) {
196:
197: if (isCommitted())
198: return;
199:
200: if (SecurityUtil.isPackageProtectionEnabled()) {
201: AccessController
202: .doPrivileged(new SetContentTypePrivilegedAction(
203: type));
204: } else {
205: response.setContentType(type);
206: }
207: }
208:
209: public void setBufferSize(int size) {
210:
211: if (isCommitted())
212: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
213:
214: response.setBufferSize(size);
215:
216: }
217:
218: public int getBufferSize() {
219:
220: if (response == null) {
221: throw new IllegalStateException(sm
222: .getString("responseFacade.nullResponse"));
223: }
224:
225: return response.getBufferSize();
226: }
227:
228: public void flushBuffer() throws IOException {
229:
230: if (isFinished())
231: // throw new IllegalStateException
232: // (/*sm.getString("responseFacade.finished")*/);
233: return;
234:
235: if (SecurityUtil.isPackageProtectionEnabled()) {
236: try {
237: AccessController
238: .doPrivileged(new PrivilegedExceptionAction() {
239:
240: public Object run() throws IOException {
241: response.setAppCommitted(true);
242:
243: response.flushBuffer();
244: return null;
245: }
246: });
247: } catch (PrivilegedActionException e) {
248: Exception ex = e.getException();
249: if (ex instanceof IOException) {
250: throw (IOException) ex;
251: }
252: }
253: } else {
254: response.setAppCommitted(true);
255:
256: response.flushBuffer();
257: }
258:
259: }
260:
261: public void resetBuffer() {
262:
263: if (isCommitted())
264: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
265:
266: response.resetBuffer();
267:
268: }
269:
270: public boolean isCommitted() {
271:
272: if (response == null) {
273: throw new IllegalStateException(sm
274: .getString("responseFacade.nullResponse"));
275: }
276:
277: return (response.isAppCommitted());
278: }
279:
280: public void reset() {
281:
282: if (isCommitted())
283: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
284:
285: response.reset();
286:
287: }
288:
289: public void setLocale(Locale loc) {
290:
291: if (isCommitted())
292: return;
293:
294: response.setLocale(loc);
295: }
296:
297: public Locale getLocale() {
298:
299: if (response == null) {
300: throw new IllegalStateException(sm
301: .getString("responseFacade.nullResponse"));
302: }
303:
304: return response.getLocale();
305: }
306:
307: public void addCookie(Cookie cookie) {
308:
309: if (isCommitted())
310: return;
311:
312: response.addCookie(cookie);
313:
314: }
315:
316: public boolean containsHeader(String name) {
317:
318: if (response == null) {
319: throw new IllegalStateException(sm
320: .getString("responseFacade.nullResponse"));
321: }
322:
323: return response.containsHeader(name);
324: }
325:
326: public String encodeURL(String url) {
327:
328: if (response == null) {
329: throw new IllegalStateException(sm
330: .getString("responseFacade.nullResponse"));
331: }
332:
333: return response.encodeURL(url);
334: }
335:
336: public String encodeRedirectURL(String url) {
337:
338: if (response == null) {
339: throw new IllegalStateException(sm
340: .getString("responseFacade.nullResponse"));
341: }
342:
343: return response.encodeRedirectURL(url);
344: }
345:
346: public String encodeUrl(String url) {
347:
348: if (response == null) {
349: throw new IllegalStateException(sm
350: .getString("responseFacade.nullResponse"));
351: }
352:
353: return response.encodeURL(url);
354: }
355:
356: public String encodeRedirectUrl(String url) {
357:
358: if (response == null) {
359: throw new IllegalStateException(sm
360: .getString("responseFacade.nullResponse"));
361: }
362:
363: return response.encodeRedirectURL(url);
364: }
365:
366: public void sendError(int sc, String msg) throws IOException {
367:
368: if (isCommitted())
369: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
370:
371: response.setAppCommitted(true);
372:
373: response.sendError(sc, msg);
374:
375: }
376:
377: public void sendError(int sc) throws IOException {
378:
379: if (isCommitted())
380: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
381:
382: response.setAppCommitted(true);
383:
384: response.sendError(sc);
385:
386: }
387:
388: public void sendRedirect(String location) throws IOException {
389:
390: if (isCommitted())
391: throw new IllegalStateException(/*sm.getString("responseBase.reset.ise")*/);
392:
393: response.setAppCommitted(true);
394:
395: response.sendRedirect(location);
396:
397: }
398:
399: public void setDateHeader(String name, long date) {
400:
401: if (isCommitted())
402: return;
403:
404: if (Globals.IS_SECURITY_ENABLED) {
405: AccessController
406: .doPrivileged(new DateHeaderPrivilegedAction(name,
407: date, false));
408: } else {
409: response.setDateHeader(name, date);
410: }
411:
412: }
413:
414: public void addDateHeader(String name, long date) {
415:
416: if (isCommitted())
417: return;
418:
419: if (Globals.IS_SECURITY_ENABLED) {
420: AccessController
421: .doPrivileged(new DateHeaderPrivilegedAction(name,
422: date, true));
423: } else {
424: response.addDateHeader(name, date);
425: }
426:
427: }
428:
429: public void setHeader(String name, String value) {
430:
431: if (isCommitted())
432: return;
433:
434: response.setHeader(name, value);
435:
436: }
437:
438: public void addHeader(String name, String value) {
439:
440: if (isCommitted())
441: return;
442:
443: response.addHeader(name, value);
444:
445: }
446:
447: public void setIntHeader(String name, int value) {
448:
449: if (isCommitted())
450: return;
451:
452: response.setIntHeader(name, value);
453:
454: }
455:
456: public void addIntHeader(String name, int value) {
457:
458: if (isCommitted())
459: return;
460:
461: response.addIntHeader(name, value);
462:
463: }
464:
465: public void setStatus(int sc) {
466:
467: if (isCommitted())
468: return;
469:
470: response.setStatus(sc);
471:
472: }
473:
474: public void setStatus(int sc, String sm) {
475:
476: if (isCommitted())
477: return;
478:
479: response.setStatus(sc, sm);
480: }
481:
482: public String getContentType() {
483:
484: if (response == null) {
485: throw new IllegalStateException(sm
486: .getString("responseFacade.nullResponse"));
487: }
488:
489: return response.getContentType();
490: }
491:
492: public void setCharacterEncoding(String arg0) {
493:
494: if (response == null) {
495: throw new IllegalStateException(sm
496: .getString("responseFacade.nullResponse"));
497: }
498:
499: response.setCharacterEncoding(arg0);
500: }
501:
502: }
|