1 16 package org.apache.myfaces.custom.buffer; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.io.UnsupportedEncodingException ; 22 import java.lang.reflect.Method ; 23 import java.util.Iterator ; 24 25 import javax.faces.component.UIComponent; 26 import javax.faces.context.FacesContext; 27 import javax.faces.context.ResponseWriter; 28 import javax.faces.render.Renderer; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.myfaces.renderkit.RendererUtils; 33 import org.apache.myfaces.renderkit.html.HtmlResponseWriterImpl; 34 35 49 public class BufferRenderer extends Renderer { 50 private static final Log log = LogFactory.getLog(BufferRenderer.class); 51 52 public static final String RENDERER_TYPE = "org.apache.myfaces.Buffer"; 53 54 private ResponseWriter initialWriter; 55 private HtmlBufferResponseWriterWrapper bufferWriter; 56 57 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) { 58 RendererUtils.checkParamValidity(facesContext, uiComponent, Buffer.class); 59 60 initialWriter = facesContext.getResponseWriter(); 61 bufferWriter = HtmlBufferResponseWriterWrapper.getInstance( initialWriter ); 62 facesContext.setResponseWriter( bufferWriter ); 63 } 64 65 public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException { 66 RendererUtils.checkParamValidity(facesContext, component, Buffer.class); 67 RendererUtils.renderChildren(facesContext, component); 68 } 69 70 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) { 71 Buffer buffer = (Buffer)uiComponent; 72 buffer.fill(bufferWriter, facesContext); 73 74 if( bufferWriter.getDummyFormParams() != null ){ 75 try { 77 Method add = initialWriter.getClass().getDeclaredMethod("addDummyFormParameter", new Class [] {String .class}); 78 for(Iterator i = bufferWriter.getDummyFormParams().iterator() ; i.hasNext() ;){ 79 add.invoke(initialWriter, new Object [] {i.next()}); 80 } 81 } catch (Exception e) { 82 log.warn("Dummy form parameters are not supported by this JSF implementation."); 83 } 84 } 85 86 facesContext.setResponseWriter( initialWriter ); 87 } 88 89 private static class HtmlBufferResponseWriterWrapper extends HtmlResponseWriterImpl { 90 91 private ByteArrayOutputStream stream; 92 private PrintWriter writer; 93 94 static public HtmlBufferResponseWriterWrapper getInstance(ResponseWriter initialWriter){ 95 ByteArrayOutputStream instanceStream = new ByteArrayOutputStream (); 96 PrintWriter instanceWriter = new PrintWriter (instanceStream, true); 97 98 return new HtmlBufferResponseWriterWrapper(initialWriter, instanceStream, instanceWriter); 99 } 100 101 private HtmlBufferResponseWriterWrapper(ResponseWriter initialWriter, ByteArrayOutputStream stream, PrintWriter writer){ 102 super(writer, initialWriter.getContentType(), initialWriter.getCharacterEncoding()); 103 this.stream = stream; 104 this.writer = writer; 105 } 106 107 public String toString(){ 108 try{ 109 stream.flush(); 110 writer.close(); 111 112 return stream.toString( getCharacterEncoding() ); 113 }catch(UnsupportedEncodingException e){ 114 throw new RuntimeException ("ResponseWriter accepted invalid character encoding " + getCharacterEncoding()); 116 } catch (IOException e) { 117 throw new RuntimeException ( e ); 118 } 119 } 120 } 121 } | Popular Tags |