]> WPIA git - gigi.git/blob - lib/servlet-api/javax/servlet/ServletResponseWrapper.java
adding servlet api (from tomcat)
[gigi.git] / lib / servlet-api / javax / servlet / ServletResponseWrapper.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     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 javax.servlet;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.util.Locale;
22
23 /**
24  * Provides a convenient implementation of the ServletResponse interface that
25  * can be subclassed by developers wishing to adapt the response from a Servlet.
26  * This class implements the Wrapper or Decorator pattern. Methods default to
27  * calling through to the wrapped response object.
28  *
29  * @since v 2.3
30  * @see javax.servlet.ServletResponse
31  */
32 public class ServletResponseWrapper implements ServletResponse {
33     private ServletResponse response;
34
35     /**
36      * Creates a ServletResponse adaptor wrapping the given response object.
37      *
38      * @throws java.lang.IllegalArgumentException
39      *             if the response is null.
40      */
41     public ServletResponseWrapper(ServletResponse response) {
42         if (response == null) {
43             throw new IllegalArgumentException("Response cannot be null");
44         }
45         this.response = response;
46     }
47
48     /**
49      * Return the wrapped ServletResponse object.
50      */
51     public ServletResponse getResponse() {
52         return this.response;
53     }
54
55     /**
56      * Sets the response being wrapped.
57      *
58      * @throws java.lang.IllegalArgumentException
59      *             if the response is null.
60      */
61     public void setResponse(ServletResponse response) {
62         if (response == null) {
63             throw new IllegalArgumentException("Response cannot be null");
64         }
65         this.response = response;
66     }
67
68     /**
69      * The default behavior of this method is to call
70      * setCharacterEncoding(String charset) on the wrapped response object.
71      *
72      * @since 2.4
73      */
74     @Override
75     public void setCharacterEncoding(String charset) {
76         this.response.setCharacterEncoding(charset);
77     }
78
79     /**
80      * The default behavior of this method is to return getCharacterEncoding()
81      * on the wrapped response object.
82      */
83     @Override
84     public String getCharacterEncoding() {
85         return this.response.getCharacterEncoding();
86     }
87
88     /**
89      * The default behavior of this method is to return getOutputStream() on the
90      * wrapped response object.
91      */
92     @Override
93     public ServletOutputStream getOutputStream() throws IOException {
94         return this.response.getOutputStream();
95     }
96
97     /**
98      * The default behavior of this method is to return getWriter() on the
99      * wrapped response object.
100      */
101     @Override
102     public PrintWriter getWriter() throws IOException {
103         return this.response.getWriter();
104     }
105
106     /**
107      * The default behavior of this method is to call setContentLength(int len)
108      * on the wrapped response object.
109      */
110     @Override
111     public void setContentLength(int len) {
112         this.response.setContentLength(len);
113     }
114
115     /**
116      * The default behavior of this method is to call setContentLength(long len)
117      * on the wrapped response object.
118      */
119     @Override
120     public void setContentLengthLong(long length) {
121         this.response.setContentLengthLong(length);
122     }
123
124     /**
125      * The default behavior of this method is to call setContentType(String
126      * type) on the wrapped response object.
127      */
128     @Override
129     public void setContentType(String type) {
130         this.response.setContentType(type);
131     }
132
133     /**
134      * The default behavior of this method is to return getContentType() on the
135      * wrapped response object.
136      *
137      * @since 2.4
138      */
139     @Override
140     public String getContentType() {
141         return this.response.getContentType();
142     }
143
144     /**
145      * The default behavior of this method is to call setBufferSize(int size) on
146      * the wrapped response object.
147      */
148     @Override
149     public void setBufferSize(int size) {
150         this.response.setBufferSize(size);
151     }
152
153     /**
154      * The default behavior of this method is to return getBufferSize() on the
155      * wrapped response object.
156      */
157     @Override
158     public int getBufferSize() {
159         return this.response.getBufferSize();
160     }
161
162     /**
163      * The default behavior of this method is to call flushBuffer() on the
164      * wrapped response object.
165      */
166     @Override
167     public void flushBuffer() throws IOException {
168         this.response.flushBuffer();
169     }
170
171     /**
172      * The default behavior of this method is to return isCommitted() on the
173      * wrapped response object.
174      */
175     @Override
176     public boolean isCommitted() {
177         return this.response.isCommitted();
178     }
179
180     /**
181      * The default behavior of this method is to call reset() on the wrapped
182      * response object.
183      */
184     @Override
185     public void reset() {
186         this.response.reset();
187     }
188
189     /**
190      * The default behavior of this method is to call resetBuffer() on the
191      * wrapped response object.
192      */
193     @Override
194     public void resetBuffer() {
195         this.response.resetBuffer();
196     }
197
198     /**
199      * The default behavior of this method is to call setLocale(Locale loc) on
200      * the wrapped response object.
201      */
202     @Override
203     public void setLocale(Locale loc) {
204         this.response.setLocale(loc);
205     }
206
207     /**
208      * The default behavior of this method is to return getLocale() on the
209      * wrapped response object.
210      */
211     @Override
212     public Locale getLocale() {
213         return this.response.getLocale();
214     }
215
216     /**
217      * @param wrapped
218      * @since Servlet 3.0 TODO SERVLET3 - Add comments
219      */
220     public boolean isWrapperFor(ServletResponse wrapped) {
221         if (response == wrapped) {
222             return true;
223         }
224         if (response instanceof ServletResponseWrapper) {
225             return ((ServletResponseWrapper) response).isWrapperFor(wrapped);
226         }
227         return false;
228     }
229
230     /**
231      * @param wrappedType
232      * @since Servlet 3.0 TODO SERVLET3 - Add comments
233      */
234     public boolean isWrapperFor(Class<?> wrappedType) {
235         if (wrappedType.isAssignableFrom(response.getClass())) {
236             return true;
237         }
238         if (response instanceof ServletResponseWrapper) {
239             return ((ServletResponseWrapper) response).isWrapperFor(wrappedType);
240         }
241         return false;
242     }
243
244 }