1 24 package org.riotfamily.common.web.util; 25 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.io.PrintWriter ; 29 import java.io.Writer ; 30 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpServletResponseWrapper ; 34 35 41 public class CapturingResponseWrapper extends HttpServletResponseWrapper { 42 43 private OutputStream targetStream; 44 45 private Writer targetWriter; 46 47 private ServletOutputStream outputStream; 48 49 private PrintWriter writer; 50 51 public CapturingResponseWrapper(HttpServletResponse response, 52 OutputStream targetStream) { 53 54 super(response); 55 this.targetStream = targetStream; 56 } 57 58 public CapturingResponseWrapper(HttpServletResponse response, 59 Writer targetWriter) { 60 61 super(response); 62 this.targetWriter = targetWriter; 63 } 64 65 public ServletOutputStream getOutputStream() throws IOException { 66 if (outputStream == null) { 67 if (targetStream == null) { 68 throw new java.lang.IllegalStateException ( 69 "The wrapper was set up to capture character data"); 70 } 71 outputStream = new DelegatingServletOutputStream(targetStream); 72 } 73 return outputStream; 74 } 75 76 public PrintWriter getWriter() throws IOException { 77 if (writer == null) { 78 if (targetWriter != null) { 79 writer = new PrintWriter (targetWriter); 80 } 81 else { 82 writer = new PrintWriter (targetStream); 83 } 84 } 85 return writer; 86 } 87 88 } 89 | Popular Tags |