1 24 package org.riotfamily.website.interceptor.pushup; 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 import java.io.StringWriter ; 30 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpServletResponseWrapper ; 34 35 import org.riotfamily.common.web.util.DelegatingServletOutputStream; 36 import org.springframework.util.FileCopyUtils; 37 38 45 public class DeferredRenderingResponseWrapper extends HttpServletResponseWrapper { 46 47 private ByteArrayOutputStream outputStream; 48 49 private StringWriter writer; 50 51 private boolean redirectSent = false; 52 53 public DeferredRenderingResponseWrapper(HttpServletResponse response) { 54 super(response); 55 } 56 57 public void sendError(int sc) throws IOException { 58 redirectSent = true; 59 super.sendError(sc); 60 } 61 62 public void sendError(int sc, String msg) throws IOException { 63 redirectSent = true; 64 super.sendError(sc, msg); 65 } 66 67 public void sendRedirect(String location) throws IOException { 68 redirectSent = true; 69 super.sendRedirect(location); 70 } 71 72 public PrintWriter getWriter() throws IOException { 73 if (outputStream == null) { 74 if (writer == null) { 75 writer = new StringWriter (); 76 } 77 return new PrintWriter (writer); 78 } 79 else { 80 throw new IllegalStateException ( 81 "getOutputStream() has been called already"); 82 } 83 } 84 85 public ServletOutputStream getOutputStream() throws IOException { 86 if (writer == null) { 87 if (outputStream == null) { 88 outputStream = new ByteArrayOutputStream (); 89 } 90 return new DelegatingServletOutputStream(outputStream); 91 } 92 else { 93 throw new IllegalStateException ( 94 "getWriter() has been called already"); 95 } 96 } 97 98 public boolean isRedirectSent() { 99 return redirectSent; 100 } 101 102 public void renderResponse(HttpServletResponse response) throws IOException { 103 if (outputStream != null) { 104 FileCopyUtils.copy(outputStream.toByteArray(), 105 response.getOutputStream()); 106 } 107 else if (writer != null) { 108 response.getWriter().write(writer.toString()); 109 } 110 } 111 112 113 } 114 | Popular Tags |