1 10 11 package mondrian.tui; 12 13 import java.io.PrintWriter ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.OutputStreamWriter ; 16 import java.io.IOException ; 17 import java.util.Locale ; 18 import java.util.Collections ; 19 import java.util.ArrayList ; 20 import java.util.Date ; 21 import java.util.List ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.text.DateFormat ; 25 import java.text.SimpleDateFormat ; 26 import javax.servlet.ServletOutputStream ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.Cookie ; 29 30 41 public class MockHttpServletResponse implements HttpServletResponse { 42 43 public final static String DATE_FORMAT_HEADER = "EEE, d MMM yyyy HH:mm:ss Z"; 44 45 46 static class MockServletOutputStream extends ServletOutputStream { 47 private ByteArrayOutputStream buffer; 48 private String encoding; 49 50 public MockServletOutputStream(int size) { 51 this(size, "ISO-8859-1"); 52 } 53 54 public MockServletOutputStream(int size, String encoding) { 55 buffer = new ByteArrayOutputStream (size); 56 this.encoding = encoding; 57 } 58 59 public void setEncoding(String encoding) { 60 this.encoding = encoding; 61 } 62 63 public void write(int value) throws IOException { 64 buffer.write(value); 65 } 66 67 public String getContent() throws IOException { 68 try { 69 buffer.flush(); 70 return buffer.toString(encoding); 71 } catch (IOException exc) { 72 throw exc; 73 } 74 } 75 76 public byte[] getBinaryContent() throws IOException { 77 try { 78 buffer.flush(); 79 return buffer.toByteArray(); 80 } catch (IOException exc) { 81 throw exc; 82 } 83 } 84 85 public void clearContent() { 86 buffer = new ByteArrayOutputStream (); 87 } 88 } 89 90 91 private PrintWriter writer; 92 private Locale locale; 93 private String charEncoding; 94 private List <Cookie > cookies; 95 private MockServletOutputStream outputStream; 96 private int statusCode; 97 private boolean isCommited; 98 private String errorMsg; 99 private int errorCode; 100 private boolean wasErrorSent; 101 private boolean wasRedirectSent; 102 private int bufferSize; 103 private final Map <String , List <String >> headers; 104 105 public MockHttpServletResponse() { 106 this.isCommited = false; 107 this.cookies = Collections.emptyList(); 108 this.bufferSize = 8192; 109 this.charEncoding = "ISO-8859-1"; 110 this.errorCode = SC_OK; 111 this.statusCode = SC_OK; 112 this.headers = new HashMap <String , List <String >>(); 113 this.outputStream = new MockServletOutputStream(bufferSize); 114 } 115 116 121 public String getCharacterEncoding() { 122 return charEncoding; 123 } 124 125 131 public ServletOutputStream getOutputStream() throws IOException { 132 return outputStream; 133 } 134 135 140 public PrintWriter getWriter() throws IOException { 141 if (writer == null) { 142 writer = new PrintWriter (new OutputStreamWriter ( 143 outputStream, charEncoding), true); 144 } 145 146 return writer; 147 } 148 149 public void setCharacterEncoding(String charEncoding) { 150 this.charEncoding = charEncoding; 151 this.outputStream.setEncoding(charEncoding); 152 } 153 154 159 public void setContentLength(int len) { 160 setIntHeader("Content-Length", len); 161 } 162 163 167 public void setContentType(String contentType) { 168 setHeader("Content-Type", contentType); 169 } 170 171 175 public void setBufferSize(int size) { 176 this.bufferSize = size; 177 } 178 179 183 public int getBufferSize() { 184 return this.bufferSize; 185 } 186 187 192 public void flushBuffer() throws IOException { 193 if (writer != null) { 194 writer.flush(); 195 } 196 outputStream.flush(); 197 } 198 199 public void resetBuffer() { 200 outputStream.clearContent(); 201 } 202 203 207 public boolean isCommitted() { 208 return isCommited; 209 } 210 211 215 public void reset() { 216 headers.clear(); 217 resetBuffer(); 218 } 219 220 225 public void setLocale(Locale locale) { 226 this.locale = locale; 227 } 228 229 233 public Locale getLocale() { 234 return locale; 235 } 236 237 241 public void addCookie(Cookie cookie) { 242 if (cookies.isEmpty()) { 243 cookies = new ArrayList <Cookie >(); 244 } 245 cookies.add(cookie); 246 } 247 248 253 public boolean containsHeader(String name) { 254 return headers.containsKey(name); 255 } 256 257 262 public String encodeURL(String url) { 263 return encode(url); 264 } 265 266 271 public String encodeRedirectURL(String url) { 272 return encode(url); 273 } 274 275 278 279 public String encodeUrl(String s) { 280 return encodeURL(s); 281 } 282 283 286 287 public String encodeRedirectUrl(String s) { 288 return encodeRedirectURL(s); 289 } 290 291 296 public void sendError(int code, String msg) throws IOException { 297 this.errorCode = code; 298 this.wasErrorSent = true; 299 this.errorMsg = msg; 300 } 301 302 306 public void sendError(int code) throws IOException { 307 this.errorCode = code; 308 this.wasErrorSent = true; 309 } 310 311 316 public void sendRedirect(String location) throws IOException { 317 setHeader("Location", location); 318 wasRedirectSent = true; 319 } 320 321 325 public void setDateHeader(String name, long date) { 326 Date dateValue = new Date (date); 327 String dateString = DateFormat.getDateInstance().format(dateValue); 328 setHeader(name, dateString); 329 } 330 331 335 public void addDateHeader(String name, long date) { 336 Date dateValue = new Date (date); 337 String dateString = new SimpleDateFormat (DATE_FORMAT_HEADER, Locale.US).format(dateValue); 338 addHeader(name, dateString); 339 } 340 341 345 public void setHeader(String name, String value) { 346 List <String > valueList = headers.get(name); 347 if (valueList == null) { 348 valueList = new ArrayList <String >(); 349 headers.put(name, valueList); 350 } 351 valueList.add(value); 352 353 } 354 355 359 public void addHeader(String name, String value) { 360 List <String > valueList = headers.get(name); 361 if (null == valueList) { 362 valueList = new ArrayList <String >(); 363 headers.put(name, valueList); 364 } 365 valueList.add(value); 366 } 367 368 372 public void setIntHeader(String name, int value) { 373 String stringValue = Integer.toString(value); 374 addHeader(name, stringValue); 375 } 376 377 381 public void addIntHeader(String name, int value) { 382 String stringValue = Integer.toString(value); 383 addHeader(name, stringValue); 384 } 385 386 390 public void setStatus(int status) { 391 this.statusCode = status; 392 } 393 394 401 public void setStatus(int status, String s) { 402 setStatus(status); 403 } 404 405 public byte[] toByteArray() throws IOException { 411 return outputStream.getBinaryContent(); 412 } 413 414 public String getHeader(String name) { 415 List <String > list = getHeaderList(name); 416 417 return ((list == null) || (list.size() == 0)) 418 ? null 419 : list.get(0); 420 421 } 422 423 public String getContentType() { 424 return getHeader("Content-Type"); 425 } 426 427 428 public List <String > getHeaderList(String name) { 434 return headers.get(name); 435 } 436 437 public int getStatusCode() { 438 return statusCode; 439 } 440 441 public int getErrorCode() { 442 return errorCode; 443 } 444 445 public List getCookies() { 446 return cookies; 447 } 448 449 public boolean wasErrorSent() { 450 return wasErrorSent; 451 } 452 453 public boolean wasRedirectSent() { 454 return wasRedirectSent; 455 } 456 457 462 463 protected String encode(String s) { 464 return s; 466 } 467 468 469 } 470 | Popular Tags |