01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.jetspeed.aggregator.impl;
18:
19: import java.io.IOException;
20: import java.io.PrintWriter;
21: import java.io.UnsupportedEncodingException;
22:
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25: import javax.servlet.ServletOutputStream;
26: import javax.servlet.http.HttpServletResponse;
27: import org.apache.pluto.util.PrintWriterServletOutputStream;
28:
29: public class HttpBufferedResponse extends
30: javax.servlet.http.HttpServletResponseWrapper {
31: private boolean usingWriter;
32: private boolean usingStream;
33:
34: /** Commons logging */
35: protected final static Log log = LogFactory
36: .getLog(HttpBufferedResponse.class);
37:
38: private ServletOutputStream wrappedStream;
39: private PrintWriter writer;
40:
41: public HttpBufferedResponse(HttpServletResponse servletResponse,
42: PrintWriter writer) {
43: super (servletResponse);
44: this .writer = writer;
45: }
46:
47: public ServletOutputStream getOutputStream()
48: throws IllegalStateException, IOException {
49: if (usingWriter) {
50: throw new IllegalStateException(
51: "getOutputStream can't be used after getWriter was invoked");
52: }
53:
54: if (wrappedStream == null) {
55: wrappedStream = new PrintWriterServletOutputStream(writer,
56: getResponse().getCharacterEncoding());
57: }
58:
59: usingStream = true;
60:
61: return wrappedStream;
62: }
63:
64: public PrintWriter getWriter() throws UnsupportedEncodingException,
65: IllegalStateException, IOException {
66:
67: if (usingStream) {
68: throw new IllegalStateException(
69: "getWriter can't be used after getOutputStream was invoked");
70: }
71:
72: usingWriter = true;
73:
74: return writer;
75: }
76:
77: public void setBufferSize(int size) {
78: // ignore
79: }
80:
81: public int getBufferSize() {
82: return 0;
83: }
84:
85: public void flushBuffer() throws IOException {
86: writer.flush();
87: }
88:
89: public boolean isCommitted() {
90: return false;
91: }
92:
93: public void reset() {
94: // ignore right now
95: }
96: }
|