1 22 23 package org.cofax; 24 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpServletResponseWrapper ; 27 import javax.servlet.http.Cookie ; 28 import javax.servlet.ServletOutputStream ; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.StringWriter ; 31 import java.io.IOException ; 32 import java.io.PrintWriter ; 33 import java.io.UnsupportedEncodingException ; 34 import java.text.SimpleDateFormat ; 35 import java.util.Date ; 36 import java.util.Locale ; 37 import org.cofax.CofaxPage; 38 39 51 52 public class CofaxJspResponse extends HttpServletResponseWrapper implements HttpServletResponse { 53 54 private final SimpleDateFormat format = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 55 56 private String encoding = null; 57 58 private CofaxJspResponseStream stream = null; 59 60 private PrintWriter printWriter = null; 61 62 private StringWriter stringWriter = null; 63 64 private CofaxPage page = null; 65 66 private String redirect = null; 67 68 public CofaxJspResponse(HttpServletResponse response, CofaxPage page) { 69 super(response); 70 this.page = page; 71 } 72 73 public String getRedirect() { 74 return this.redirect; 75 } 76 77 public void addCookie(Cookie cookie) { 78 super.addCookie(cookie); 79 } 80 81 public boolean containsHeader(String name) { 82 return super.containsHeader(name) || this.page.getHeaders().containsKey(name); 83 } 84 85 90 91 public String encodeURL(String url) { 92 return super.encodeURL(url); 93 } 94 95 public String encodeRedirectURL(String url) { 96 return super.encodeRedirectURL(url); 97 } 98 99 public String encodeUrl(String url) { 100 return super.encodeUrl(url); 101 } 102 103 public String encodeRedirectUrl(String url) { 104 return super.encodeRedirectUrl(url); 105 } 106 107 public void sendError(int sc, String msg) throws IOException { 108 this.page.setStatus(sc); 109 this.page.setErrorMsg(msg); 110 } 111 112 public void sendError(int sc) throws IOException { 113 this.page.setStatus(sc); 114 } 115 116 public void sendRedirect(String location) throws java.io.IOException { 117 this.page.setStatus(SC_MOVED_TEMPORARILY); 118 this.redirect = location; 119 } 120 121 public void setDateHeader(String name, long date) { 122 this.page.putHeader(name, format.format(new Date (date))); 123 } 124 125 public void addDateHeader(String name, long date) { 126 this.page.putHeader(name, format.format(new Date (date))); 127 } 128 129 public void setHeader(String name, String value) { 130 this.page.putHeader(name, value); 131 } 132 133 public void addHeader(String name, String value) { 134 this.page.putHeader(name, value); 135 } 136 137 public void setIntHeader(String name, int value) { 138 this.page.putHeader(name, String.valueOf(value)); 139 } 140 141 public void addIntHeader(String name, int value) { 142 this.page.putHeader(name, String.valueOf(value)); 143 } 144 145 public void setStatus(int sc) { 146 this.page.setStatus(sc); 147 } 148 149 public void setStatus(int sc, String sm) { 150 this.page.setStatus(sc); 151 this.page.setErrorMsg(sm); 152 } 153 154 public String getCharacterEncoding() { 155 return super.getCharacterEncoding(); 156 } 157 158 public ServletOutputStream getOutputStream() throws IOException { 159 if (this.printWriter != null) { 160 throw new IOException ("getWriter() is already called"); 161 } 162 if (this.stream == null) { 163 this.stream = new CofaxJspResponseStream(encoding); 164 } 165 return this.stream; 166 } 167 168 public PrintWriter getWriter() throws IOException { 169 if (this.stream != null) { 170 throw new IOException ("getOutputStream() is already called"); 171 } 172 if (this.printWriter == null) { 173 this.stringWriter = new StringWriter (CofaxUtil.COMMON_DATA_SIZE); 174 this.printWriter = new PrintWriter (this.stringWriter); 175 } 176 return this.printWriter; 177 } 178 179 public void setContentLength(int len) { 180 } 181 182 public void setContentType(String type) { 183 int semicolon = type.lastIndexOf(';'); 184 if (semicolon >= 0 && semicolon != (type.length() - 1)) { 185 String charset = type.substring(semicolon + 1); 186 charset = charset.trim(); 187 if (charset.toLowerCase().startsWith("charset")) { 188 charset = charset.substring(charset.indexOf("=") + 1).trim(); 189 if (charset.length() > 0) { 190 this.encoding = charset; 191 } 192 } 193 } 194 super.setContentType(type); 195 } 196 197 public void setBufferSize(int size) { 198 } 199 200 public int getBufferSize() { 201 return this.stringWriter.getBuffer().length(); 202 } 203 204 public void flushBuffer() throws IOException { 205 this.stringWriter.flush(); 206 } 207 208 public void resetBuffer() { 209 this.stringWriter.getBuffer().setLength(0); 210 } 211 212 public boolean isCommitted() { 213 return super.isCommitted(); 214 } 215 216 public void reset() { 217 super.reset(); 218 this.resetBuffer(); 219 } 220 221 public void setLocale(java.util.Locale loc) { 222 super.setLocale(loc); 223 } 224 225 public Locale getLocale() { 226 return super.getLocale(); 227 } 228 229 public String toString() { 230 if (this.stringWriter != null) { 231 return this.stringWriter.getBuffer().toString(); 232 } else if (this.stream != null) { 233 return this.stream.toString(); 234 } else { 235 return ""; 236 } 237 } 238 239 class CofaxJspResponseStream extends ServletOutputStream { 240 protected String encoding = null; 241 242 protected ByteArrayOutputStream stream = new ByteArrayOutputStream (); 243 244 public CofaxJspResponseStream(String encoding) { 245 this.encoding = encoding; 246 } 247 248 public void close() throws IOException { 249 this.stream.close(); 250 } 251 252 public void flush() throws IOException { 253 this.stream.flush(); 254 } 255 256 public void reset() { 257 this.stream.reset(); 258 } 259 260 public int size() { 261 return this.stream.size(); 262 } 263 264 public void write(int b) throws IOException { 265 this.stream.write(b); 266 } 267 268 public void write(byte[] b) throws IOException { 269 this.stream.write(b); 270 } 271 272 public void write(byte[] b, int off, int len) throws IOException { 273 this.stream.write(b, off, len); 274 } 275 276 public String toString() { 277 try { 278 return encoding == null ? stream.toString() : stream.toString(encoding); 279 } catch (UnsupportedEncodingException e) { 280 return stream.toString(); 281 } 282 } 283 } 284 285 } | Popular Tags |