001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package javax.servlet;
021    
022    import java.io.IOException;
023    import java.io.PrintWriter;
024    import java.util.Locale;
025    
026    /**
027     * Provides a convenient implementation of the ServletResponse interface that
028     * can be subclassed by developers wishing to adapt the response from a Servlet.
029     * This class implements the Wrapper or Decorator pattern. Methods default to
030     * calling through to the wrapped response object.
031     *
032     * @version $Rev: 807027 $ $Date: 2009-08-23 18:52:08 -0400 (Sun, 23 Aug 2009) $
033     * @see javax.servlet.ServletResponse
034     * @since v 2.3
035     */
036    
037    public class ServletResponseWrapper implements ServletResponse {
038        private ServletResponse response;
039    
040        /**
041         * Creates a ServletResponse adaptor wrapping the given response object.
042         *
043         * @param response response to wrap
044         * @throws java.lang.IllegalArgumentException
045         *          if the response is null.
046         */
047        public ServletResponseWrapper(ServletResponse response) {
048            if (response == null) {
049                throw new IllegalArgumentException("Response cannot be null");
050            }
051            this.response = response;
052        }
053    
054        /**
055         * Return the wrapped ServletResponse object.
056         *
057         * @return wrapped response
058         */
059        public ServletResponse getResponse() {
060            return this.response;
061        }
062    
063        /**
064         * Sets the response being wrapped.
065         *
066         * @param response response to wrap
067         * @throws java.lang.IllegalArgumentException
068         *          if the response is null.
069         */
070        public void setResponse(ServletResponse response) {
071            if (response == null) {
072                throw new IllegalArgumentException("Response cannot be null");
073            }
074            this.response = response;
075        }
076    
077        /**
078         * The default behavior of this method is to call setCharacterEncoding(String charset)
079         * on the wrapped response object.
080         *
081         * @since 2.4
082         */
083        public void setCharacterEncoding(String charset) {
084            this.response.setCharacterEncoding(charset);
085        }
086    
087        /**
088         * The default behavior of this method is to return getCharacterEncoding()
089         * on the wrapped response object.
090         */
091        public String getCharacterEncoding() {
092            return this.response.getCharacterEncoding();
093        }
094    
095        /**
096         * The default behavior of this method is to return getOutputStream()
097         * on the wrapped response object.
098         */
099        public ServletOutputStream getOutputStream() throws IOException {
100            return this.response.getOutputStream();
101        }
102    
103        /**
104         * The default behavior of this method is to return getWriter()
105         * on the wrapped response object.
106         */
107        public PrintWriter getWriter() throws IOException {
108            return this.response.getWriter();
109        }
110    
111        /**
112         * The default behavior of this method is to call setContentLength(int len)
113         * on the wrapped response object.
114         */
115        public void setContentLength(int len) {
116            this.response.setContentLength(len);
117        }
118    
119        /**
120         * The default behavior of this method is to call setContentType(String type)
121         * on the wrapped response object.
122         */
123        public void setContentType(String type) {
124            this.response.setContentType(type);
125        }
126    
127        /**
128         * The default behavior of this method is to return getContentType()
129         * on the wrapped response object.
130         *
131         * @since 2.4
132         */
133        public String getContentType() {
134            return this.response.getContentType();
135        }
136    
137        /**
138         * The default behavior of this method is to call setBufferSize(int size)
139         * on the wrapped response object.
140         */
141        public void setBufferSize(int size) {
142            this.response.setBufferSize(size);
143        }
144    
145        /**
146         * The default behavior of this method is to return getBufferSize()
147         * on the wrapped response object.
148         */
149        public int getBufferSize() {
150            return this.response.getBufferSize();
151        }
152    
153        /**
154         * The default behavior of this method is to call flushBuffer()
155         * on the wrapped response object.
156         */
157        public void flushBuffer() throws IOException {
158            this.response.flushBuffer();
159        }
160    
161        /**
162         * The default behavior of this method is to return isCommitted()
163         * on the wrapped response object.
164         */
165        public boolean isCommitted() {
166            return this.response.isCommitted();
167        }
168    
169        /**
170         * The default behavior of this method is to call reset()
171         * on the wrapped response object.
172         */
173        public void reset() {
174            this.response.reset();
175        }
176    
177        /**
178         * The default behavior of this method is to call resetBuffer()
179         * on the wrapped response object.
180         */
181        public void resetBuffer() {
182            this.response.resetBuffer();
183        }
184    
185        /**
186         * The default behavior of this method is to call setLocale(Locale loc)
187         * on the wrapped response object.
188         */
189        public void setLocale(Locale loc) {
190            this.response.setLocale(loc);
191        }
192    
193        /**
194         * The default behavior of this method is to return getLocale()
195         * on the wrapped response object.
196         */
197        public Locale getLocale() {
198            return this.response.getLocale();
199        }
200    
201        public boolean isWrapperFor(Class wrappedType) {
202            if (this.response.getClass().isAssignableFrom(wrappedType)) {
203                return true;
204            }
205            if (this.response instanceof ServletResponseWrapper) {
206                return ((ServletResponseWrapper)this.response).isWrapperFor(wrappedType);
207            }
208            return false;
209        }
210    
211        public boolean isWrapperFor(ServletResponse instance) {
212            if (instance == this.response) {
213                return true;
214            }
215            if (this.response instanceof ServletResponseWrapper) {
216                return ((ServletResponseWrapper)this.response).isWrapperFor(instance);
217            }
218            return false;
219        }
220    
221    
222    }
223    
224    
225    
226    
227