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