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 * 028 * Provides a convenient implementation of the ServletResponse interface that 029 * can be subclassed by developers wishing to adapt the response from a Servlet. 030 * This class implements the Wrapper or Decorator pattern. Methods default to 031 * calling through to the wrapped response object. 032 * 033 * @author Various 034 * @version $Version$ 035 * @since v 2.3 036 * 037 * @see javax.servlet.ServletResponse 038 * 039 */ 040 041 042 public class ServletResponseWrapper implements ServletResponse { 043 private ServletResponse response; 044 /** 045 * Creates a ServletResponse adaptor wrapping the given response object. 046 * @throws java.lang.IllegalArgumentException if the response is null. 047 */ 048 049 050 public ServletResponseWrapper(ServletResponse response) { 051 if (response == null) { 052 throw new IllegalArgumentException("Response cannot be null"); 053 } 054 this.response = response; 055 } 056 057 /** 058 * Return the wrapped ServletResponse object. 059 */ 060 061 public ServletResponse getResponse() { 062 return this.response; 063 } 064 065 066 /** 067 * Sets the response being wrapped. 068 * @throws java.lang.IllegalArgumentException if the response is null. 069 */ 070 071 public void setResponse(ServletResponse response) { 072 if (response == null) { 073 throw new IllegalArgumentException("Response cannot be null"); 074 } 075 this.response = response; 076 } 077 078 /** 079 * The default behavior of this method is to call setCharacterEncoding(String charset) 080 * on the wrapped response object. 081 * 082 * @since 2.4 083 */ 084 085 public void setCharacterEncoding(String charset) { 086 this.response.setCharacterEncoding(charset); 087 } 088 089 /** 090 * The default behavior of this method is to return getCharacterEncoding() 091 * on the wrapped response object. 092 */ 093 094 public String getCharacterEncoding() { 095 return this.response.getCharacterEncoding(); 096 } 097 098 099 /** 100 * The default behavior of this method is to return getOutputStream() 101 * on the wrapped response object. 102 */ 103 104 public ServletOutputStream getOutputStream() throws IOException { 105 return this.response.getOutputStream(); 106 } 107 108 /** 109 * The default behavior of this method is to return getWriter() 110 * on the wrapped response object. 111 */ 112 113 114 public PrintWriter getWriter() throws IOException { 115 return this.response.getWriter(); 116 } 117 118 /** 119 * The default behavior of this method is to call setContentLength(int len) 120 * on the wrapped response object. 121 */ 122 123 public void setContentLength(int len) { 124 this.response.setContentLength(len); 125 } 126 127 /** 128 * The default behavior of this method is to call setContentType(String type) 129 * on the wrapped response object. 130 */ 131 132 public void setContentType(String type) { 133 this.response.setContentType(type); 134 } 135 136 /** 137 * The default behavior of this method is to return getContentType() 138 * on the wrapped response object. 139 * 140 * @since 2.4 141 */ 142 143 public String getContentType() { 144 return this.response.getContentType(); 145 } 146 147 /** 148 * The default behavior of this method is to call setBufferSize(int size) 149 * on the wrapped response object. 150 */ 151 public void setBufferSize(int size) { 152 this.response.setBufferSize(size); 153 } 154 155 /** 156 * The default behavior of this method is to return getBufferSize() 157 * on the wrapped response object. 158 */ 159 public int getBufferSize() { 160 return this.response.getBufferSize(); 161 } 162 163 /** 164 * The default behavior of this method is to call flushBuffer() 165 * on the wrapped response object. 166 */ 167 168 public void flushBuffer() throws IOException { 169 this.response.flushBuffer(); 170 } 171 172 /** 173 * The default behavior of this method is to return isCommitted() 174 * on the wrapped response object. 175 */ 176 public boolean isCommitted() { 177 return this.response.isCommitted(); 178 } 179 180 /** 181 * The default behavior of this method is to call reset() 182 * on the wrapped response object. 183 */ 184 185 public void reset() { 186 this.response.reset(); 187 } 188 189 /** 190 * The default behavior of this method is to call resetBuffer() 191 * on the wrapped response object. 192 */ 193 194 public void resetBuffer() { 195 this.response.resetBuffer(); 196 } 197 198 /** 199 * The default behavior of this method is to call setLocale(Locale loc) 200 * on the wrapped response object. 201 */ 202 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() 209 * on the wrapped response object. 210 */ 211 public Locale getLocale() { 212 return this.response.getLocale(); 213 } 214 215 216 } 217 218 219 220 221