1 5 6 package org.exoplatform.test.mocks.servlet; 7 import javax.servlet.http.HttpServletResponseWrapper ; 8 import javax.servlet.http.HttpServletResponse ; 9 import javax.servlet.ServletOutputStream ; 10 import java.io.ByteArrayOutputStream ; 11 import java.io.PrintWriter ; 12 import java.io.IOException ; 13 import java.util.* ; 14 15 22 public class MockServletResponse extends HttpServletResponseWrapper { 23 24 private PrintWriter tmpWriter; 25 private ByteArrayOutputStream output; 26 private ByteArrayServletOutputStream servletOutput; 27 private Locale locale_ ; 28 29 public MockServletResponse(HttpServletResponse httpServletResponse) { 30 super(httpServletResponse); 31 output = new ByteArrayOutputStream (); 32 tmpWriter = new PrintWriter (output); 33 servletOutput = new ByteArrayServletOutputStream(output); 34 } 35 36 public void finalize() throws Throwable { 37 super.finalize(); 38 servletOutput.close(); 39 output.close(); 40 tmpWriter.close(); 41 } 42 43 public String getPortletContent() { 44 String s = output.toString(); 45 reset(); 46 return s; 47 } 48 49 59 60 public byte[] toByteArray() { 61 return output.toByteArray(); 62 } 63 64 public String getOutputContent() { 65 return new String (output.toByteArray()); 66 } 67 68 public void flushBuffer() throws IOException { 69 tmpWriter.flush(); 70 servletOutput.flush(); 71 } 72 73 public void reset() { 74 output.reset(); 75 } 76 77 public void close() throws IOException { 78 tmpWriter.close(); 79 } 80 81 private static class ByteArrayServletOutputStream extends ServletOutputStream { 82 ByteArrayOutputStream baos; 83 84 public ByteArrayServletOutputStream(ByteArrayOutputStream baos) { 85 this.baos = baos; 86 } 87 88 public void write(int i) throws IOException { 89 baos.write(i); 90 } 91 } 92 93 public Locale getLocale() { return locale_ ; } 94 public void setLocale(java.util.Locale loc) { locale_ = loc ; } 95 96 } 97 | Popular Tags |