1 /* Copyright 2004-2005 the original author or authors.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 package org.codehaus.groovy.grails.web.util;
16 
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.io.PrintWriter;
20 import java.nio.charset.CharacterCodingException;
21 import java.util.Locale;
22 
23 import javax.servlet.ServletOutputStream;
24 import javax.servlet.http.HttpServletResponse;
25 import javax.servlet.http.HttpServletResponseWrapper;
26 
27 /**
28  * Response wrapper used to capture the content of a response (such as within in an include).
29  *
30  * @author Graeme Rocher
31  * @since 1.2.1
32  */
33 public class IncludeResponseWrapper extends HttpServletResponseWrapper {
34 
35     private StreamCharBuffer charBuffer;
36     private PrintWriter pw;
37     private StreamByteBuffer byteBuffer;
38     private OutputStream os;
39     private ServletOutputStream sos;
40     private boolean usingStream;
41     private boolean usingWriter;
42     private int status;
43     private String contentType;
44     private boolean committed;
45     private String redirectURL;
46 
IncludeResponseWrapper(HttpServletResponse httpServletResponse)47     public IncludeResponseWrapper(HttpServletResponse httpServletResponse) {
48         super(httpServletResponse);
49     }
50 
getRedirectURL()51     public String getRedirectURL() {
52         return redirectURL;
53     }
54 
55     @Override
getContentType()56     public String getContentType() {
57         return contentType;
58     }
59 
60     @Override
setStatus(int i)61     public void setStatus(int i) {
62         status = i;
63     }
64 
65     @Override
isCommitted()66     public boolean isCommitted() {
67         return committed;
68     }
69 
70     @Override
sendRedirect(String s)71     public void sendRedirect(String s) throws IOException {
72         committed = true;
73         redirectURL = s;
74         super.sendRedirect(s);
75     }
76 
getStatus()77     public int getStatus() {
78         return status;
79     }
80 
81     @Override
setContentType(String s)82     public void setContentType(String s) {
83         contentType = s;
84     }
85 
86     @Override
setLocale(Locale locale)87     public void setLocale(Locale locale) {
88         // do nothing
89     }
90 
91     @Override
sendError(int i, String s)92     public void sendError(int i, String s) throws IOException {
93         setStatus(i);
94     }
95 
96     @Override
sendError(int i)97     public void sendError(int i) throws IOException {
98         setStatus(i);
99     }
100 
101     @Override
getOutputStream()102     public ServletOutputStream getOutputStream() throws IOException {
103         if (usingWriter) throw new IllegalStateException("Method getWriter() already called");
104 
105         if (!usingStream) {
106             usingStream = true;
107             byteBuffer = new StreamByteBuffer();
108             os = byteBuffer.getOutputStream();
109             sos = new ServletOutputStream() {
110                 @Override
111                 public void write(byte[] b, int off, int len) throws IOException {
112                     os.write(b, off, len);
113                 }
114 
115                 @Override
116                 public void write(byte[] b) throws IOException {
117                     os.write(b);
118                 }
119 
120                 @Override
121                 public void write(int b) throws IOException {
122                     os.write(b);
123                 }
124             };
125         }
126 
127         return sos;
128     }
129 
130     @Override
getWriter()131     public PrintWriter getWriter() throws IOException {
132         if (usingStream) throw new IllegalStateException("Method getOutputStream() already called");
133 
134         if (!usingWriter) {
135             usingWriter = true;
136             charBuffer = new StreamCharBuffer();
137             pw = new GrailsPrintWriter(charBuffer.getWriter());
138         }
139         return pw;
140     }
141 
getContent()142     public Object getContent() throws CharacterCodingException {
143         return getContent("UTF-8");
144     }
145 
getContent(String encoding)146     public Object getContent(String encoding) throws CharacterCodingException {
147         if (usingWriter) {
148             return charBuffer;
149         }
150 
151         if (usingStream) {
152             return byteBuffer.readAsString(encoding);
153         }
154 
155         return "";
156     }
157 }
158