View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.constructs.web;
18  
19  import net.sf.ehcache.constructs.web.filter.FilterServletOutputStream;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import javax.servlet.ServletOutputStream;
24  import javax.servlet.http.Cookie;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.servlet.http.HttpServletResponseWrapper;
27  import java.io.IOException;
28  import java.io.OutputStream;
29  import java.io.OutputStreamWriter;
30  import java.io.PrintWriter;
31  import java.io.Serializable;
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.List;
35  
36  /**
37   * Provides a wrapper for {@link javax.servlet.http.HttpServletResponseWrapper}.
38   * <p/>
39   * It is used to wrap the real Response so that we can modify it after
40   * that the target of the request has delivered its response.
41   * <p/>
42   * It uses the Wrapper pattern.
43   *
44   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
45   * @version $Id: GenericResponseWrapper.java 512 2007-07-10 09:18:45Z gregluck $
46   */
47  public class GenericResponseWrapper extends HttpServletResponseWrapper implements Serializable {
48  
49      private static final long serialVersionUID = -5976708169031065498L;
50  
51      private static final Log LOG = LogFactory.getLog(GenericResponseWrapper.class);
52      private int statusCode = SC_OK;
53      private int contentLength;
54      private String contentType;
55      private final List headers = new ArrayList();
56      private final List cookies = new ArrayList();
57      private ServletOutputStream outstr;
58      private PrintWriter writer;
59  
60      /**
61       * Creates a GenericResponseWrapper
62       */
63      public GenericResponseWrapper(final HttpServletResponse response, final OutputStream outstr) {
64          super(response);
65          this.outstr = new FilterServletOutputStream(outstr);
66      }
67  
68      /**
69       * Gets the outputstream.
70       */
71      public ServletOutputStream getOutputStream() {
72          return outstr;
73      }
74  
75      /**
76       * Sets the status code for this response.
77       */
78      public void setStatus(final int code) {
79          statusCode = code;
80          super.setStatus(code);
81      }
82  
83      /**
84       * Send the error. If the response is not ok, most of the logic is bypassed and the error is sent raw
85       * Also, the content is not cached.
86       *
87       * @param i      the status code
88       * @param string the error message
89       * @throws IOException
90       */
91      public void sendError(int i, String string) throws IOException {
92          statusCode = i;
93          super.sendError(i, string);
94      }
95  
96      /**
97       * Send the error. If the response is not ok, most of the logic is bypassed and the error is sent raw
98       * Also, the content is not cached.
99       *
100      * @param i the status code
101      * @throws IOException
102      */
103     public void sendError(int i) throws IOException {
104         statusCode = i;
105         super.sendError(i);
106     }
107 
108     /**
109      * Send the redirect. If the response is not ok, most of the logic is bypassed and the error is sent raw.
110      * Also, the content is not cached.
111      *
112      * @param string the URL to redirect to
113      * @throws IOException
114      */
115     public void sendRedirect(String string) throws IOException {
116         statusCode = HttpServletResponse.SC_MOVED_TEMPORARILY;
117         super.sendRedirect(string);
118     }
119 
120     /**
121      * Sets the status code for this response.
122      */
123     public void setStatus(final int code, final String msg) {
124         statusCode = code;
125         LOG.warn("Discarding message because this method is deprecated.");
126         super.setStatus(code);
127     }
128 
129     /**
130      * Returns the status code for this response.
131      */
132     public int getStatus() {
133         return statusCode;
134     }
135 
136     /**
137      * Sets the content length.
138      */
139     public void setContentLength(final int length) {
140         this.contentLength = length;
141         super.setContentLength(length);
142     }
143 
144     /**
145      * Gets the content length.
146      */
147     public int getContentLength() {
148         return contentLength;
149     }
150 
151     /**
152      * Sets the content type.
153      */
154     public void setContentType(final String type) {
155         this.contentType = type;
156         super.setContentType(type);
157     }
158 
159     /**
160      * Gets the content type.
161      */
162     public String getContentType() {
163         return contentType;
164     }
165 
166 
167     /**
168      * Gets the print writer.
169      */
170     public PrintWriter getWriter() throws IOException {
171         if (writer == null) {
172             writer = new PrintWriter(new OutputStreamWriter(outstr, getCharacterEncoding()), true);
173         }
174         return writer;
175     }
176 
177     /**
178      * Adds a header.
179      */
180     public void addHeader(final String name, final String value) {
181         final String[] header = new String[]{name, value};
182         headers.add(header);
183         super.addHeader(name, value);
184     }
185 
186     /**
187      * @see #addHeader
188      */
189     public void setHeader(final String name, final String value) {
190         addHeader(name, value);
191     }
192 
193     /**
194      * Gets the headers.
195      */
196     public Collection getHeaders() {
197         return headers;
198     }
199 
200     /**
201      * Adds a cookie.
202      */
203     public void addCookie(final Cookie cookie) {
204         cookies.add(cookie);
205         super.addCookie(cookie);
206     }
207 
208     /**
209      * Gets all the cookies.
210      */
211     public Collection getCookies() {
212         return cookies;
213     }
214 
215     /**
216      * Flushes buffer and commits response to client.
217      */
218     public void flushBuffer() throws IOException {
219         flush();
220         super.flushBuffer();
221     }
222 
223     /**
224      * Resets the response.
225      */
226     public void reset() {
227         super.reset();
228         cookies.clear();
229         headers.clear();
230         statusCode = SC_OK;
231         contentType = null;
232         contentLength = 0;
233     }
234 
235     /**
236      * Resets the buffers.
237      */
238     public void resetBuffer() {
239         super.resetBuffer();
240     }
241 
242     /**
243      * Flushes all the streams for this response.
244      */
245     public void flush() throws IOException {
246         if (writer != null) {
247             writer.flush();
248         }
249         outstr.flush();
250     }
251 
252     /**
253      * Override the deprecated method and call non-deprecated method
254      */
255     public String encodeRedirectUrl(String s) {
256         return super.encodeRedirectURL(s);
257     }
258 
259     /**
260      * Override the deprecated method and call non-deprecated method
261      */
262     public String encodeUrl(String s) {
263         return super.encodeURL(s);
264     }
265 
266 
267 
268 }
269