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.portals.gems.util;
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:
28: public class HttpBufferedResponse extends
29: javax.servlet.http.HttpServletResponseWrapper {
30: private boolean usingWriter;
31: private boolean usingStream;
32:
33: /** Commons logging */
34: protected final static Log log = LogFactory
35: .getLog(HttpBufferedResponse.class);
36:
37: private ServletOutputStream wrappedStream;
38: private PrintWriter writer;
39:
40: public HttpBufferedResponse(HttpServletResponse servletResponse,
41: PrintWriter writer) {
42: super (servletResponse);
43: this .writer = writer;
44: }
45:
46: public ServletOutputStream getOutputStream()
47: throws IllegalStateException, IOException {
48: if (usingWriter) {
49: throw new IllegalStateException(
50: "getOutputStream can't be used after getWriter was invoked");
51: }
52:
53: if (wrappedStream == null) {
54: wrappedStream = new PrintWriterServletOutputStream(writer,
55: getResponse().getCharacterEncoding());
56: }
57:
58: usingStream = true;
59:
60: return wrappedStream;
61: }
62:
63: public PrintWriter getWriter() throws UnsupportedEncodingException,
64: IllegalStateException, IOException {
65:
66: if (usingStream) {
67: throw new IllegalStateException(
68: "getWriter can't be used after getOutputStream was invoked");
69: }
70:
71: usingWriter = true;
72:
73: return writer;
74: }
75:
76: public void setBufferSize(int size) {
77: // ignore
78: }
79:
80: public int getBufferSize() {
81: return 0;
82: }
83:
84: public void flushBuffer() throws IOException {
85: writer.flush();
86: }
87:
88: public boolean isCommitted() {
89: return false;
90: }
91:
92: public void reset() {
93: // ignore right now
94: }
95: }
|