You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sv...@apache.org on 2006/02/16 19:35:16 UTC

svn commit: r378332 - in /myfaces/tomahawk/trunk/sandbox: core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/ examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/ examples/src/main/webapp/

Author: svieujot
Date: Thu Feb 16 10:35:14 2006
New Revision: 378332

URL: http://svn.apache.org/viewcvs?rev=378332&view=rev
Log:
Small refactor on GraphicImageDynamic to better handle big files (avoid a copy of the file in memory & misc).

Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/ImageRenderer.java
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/GraphicImageDynamicTextBean.java
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/UploadedImageRenderer.java
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/graphicImageDynamic.jsp

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java?rev=378332&r1=378331&r2=378332&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/GraphicImageDynamicRenderer.java Thu Feb 16 10:35:14 2006
@@ -16,27 +16,12 @@
 
 package org.apache.myfaces.custom.graphicimagedynamic;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.myfaces.renderkit.html.util.AddResource;
-import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
-import org.apache.myfaces.component.html.util.ParameterResourceHandler;
-import org.apache.myfaces.renderkit.html.util.ResourceLoader;
-import org.apache.myfaces.renderkit.RendererUtils;
-import org.apache.myfaces.renderkit.html.HTML;
-import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
-import org.apache.myfaces.renderkit.html.ext.HtmlImageRenderer;
-import org.apache.myfaces.util.ClassUtils;
-
 import javax.faces.FacesException;
 import javax.faces.FactoryFinder;
 import javax.faces.component.UIComponent;
@@ -53,6 +38,18 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.component.html.util.ParameterResourceHandler;
+import org.apache.myfaces.renderkit.RendererUtils;
+import org.apache.myfaces.renderkit.html.HTML;
+import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
+import org.apache.myfaces.renderkit.html.ext.HtmlImageRenderer;
+import org.apache.myfaces.renderkit.html.util.AddResource;
+import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
+import org.apache.myfaces.renderkit.html.util.ResourceLoader;
+import org.apache.myfaces.util.ClassUtils;
+
 /**
  * @author Sylvain Vieujot
  * @version $Revision$ $Date: 2005-05-23 19:39:37 +0200 (Mon, 23 May 2005) $
@@ -197,6 +194,7 @@
     }
 
     /**
+     * @throws IOException 
      * @see org.apache.myfaces.renderkit.html.util.ResourceLoader#serveResource(javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
      */
     public void serveResource(ServletContext context, HttpServletRequest request,
@@ -241,6 +239,11 @@
                     throw new FacesException("could not instantiate image renderer class "
                             + rendererValue + " : " + e.getMessage(), e);
                 }
+                catch (Exception e)
+                {
+                    throw new FacesException("could not renderer image "
+                            + rendererValue + " : " + e.getMessage(), e);
+                }
             }
             catch (ClassNotFoundException e)
             {
@@ -255,31 +258,32 @@
     }
 
     protected void renderImage(ImageRenderer imageRenderer, FacesContext facesContext)
-            throws IOException
+            throws Exception
     {
+    		ImageContext imageContext = createImageContext(facesContext);
+    		
+		imageRenderer.setContext(facesContext, imageContext);
+    		
         HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext()
                 .getResponse();
-        ImageContext imageContext = createImageContext(facesContext);
-        ByteArrayOutputStream baout = new ByteArrayOutputStream();
-        imageRenderer.renderImage(facesContext, imageContext, baout);
-        baout.flush();
-        response.setContentLength(baout.size());
+        
+        
+        int contentLength = imageRenderer.getContentLength();
+        if( contentLength >0 )
+        {
+            response.setContentLength(contentLength);
+        }
+        
         String contentType = imageRenderer.getContentType();
-        if (contentType != null)
+        if (contentType != null && contentType.length() > 0 )
         {
             response.setContentType(contentType);
         }
+        
         ResponseStream out = facesContext.getResponseStream();
         try
         {
-            InputStream is = new ByteArrayInputStream(baout.toByteArray());
-            byte[] buffer = new byte[1024];
-            int len = is.read(buffer);
-            while (len != -1)
-            {
-                out.write(buffer, 0, len);
-                len = is.read(buffer);
-            }
+        		imageRenderer.renderImage( out );
         }
         finally
         {

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/ImageRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/ImageRenderer.java?rev=378332&r1=378331&r2=378332&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/ImageRenderer.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/graphicimagedynamic/ImageRenderer.java Thu Feb 16 10:35:14 2006
@@ -16,9 +16,9 @@
 package org.apache.myfaces.custom.graphicimagedynamic;
 
 import java.io.IOException;
-import java.io.OutputStream;
 
 import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseStream;
 
 /**
  * The ImageRenderer is used to render the binary data for a html img tag
@@ -29,23 +29,37 @@
  */
 public interface ImageRenderer
 {
+	/**
+	 * This method will be called first, to set the contexts.
+	 * 
+     * @param facesContext the faces context
+     * @param imageContext the image context width aditional image parameters
+	 */
+	void setContext(FacesContext facesContext, ImageContext imageContext) throws Exception;
+	
+    /**
+     * The content length of the image to render.
+     * Set to a negative value if you don't want to set the response content length.
+     * 
+     * @return the content length of the rendered image
+     */
+    int getContentLength();
+    
     /**
      * The MimeType of the image. This is usally a value of 
      * image/jpeg, image/gif, image/png or anything else which can be 
      * used as an image for html
-     *   
+     *  
      * @return the Mime-Type, not null
      */
     String getContentType();
 
     /**
-     * Called to render the image to the given outputstream
+     * Called to render the image to the given outputstream.
      * 
-     * @param facesContext the faces context
-     * @param imageContext the image context width aditional image parameters
      * @param out the outputstream which is used to write the binary data of the image
      *  
      * @throws IOException
      */
-    void renderImage(FacesContext facesContext, ImageContext imageContext, OutputStream out) throws IOException;
+    void renderImage(ResponseStream out) throws IOException;
 }

Modified: myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/GraphicImageDynamicTextBean.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/GraphicImageDynamicTextBean.java?rev=378332&r1=378331&r2=378332&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/GraphicImageDynamicTextBean.java (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/GraphicImageDynamicTextBean.java Thu Feb 16 10:35:14 2006
@@ -18,14 +18,15 @@
 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
+
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseStream;
 
 import org.apache.myfaces.custom.graphicimagedynamic.ImageContext;
 import org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer;
 
-import javax.faces.context.FacesContext;
-
 import com.sun.image.codec.jpeg.JPEGCodec;
 import com.sun.image.codec.jpeg.JPEGImageEncoder;
 
@@ -35,16 +36,56 @@
  */
 public class GraphicImageDynamicTextBean implements ImageRenderer
 {
-    private String _text;
+	private String _text;
+	
+    private byte[] bytes = null;
+    
+    public String getText() {
+		return _text;
+	}
+
+	public void setText(String text) {
+		this._text = text;
+	}
 
-    public String getText()
+	public void setContext(FacesContext facesContext, ImageContext imageContext) throws IOException
     {
-        return _text;
-    }
-
-    public void setText(String text)
-    {
-        _text = text;
+	    Object text = imageContext.getParamters().get("text");
+	    if (text == null)
+	    {
+	        text = "";
+	    }
+	    int width = 300;
+	    int height = 30;
+	    Integer widthObj = imageContext.getWidth();
+	    if (widthObj != null)
+	    {
+	        width = widthObj.intValue();
+	    }
+	    Integer heightObj = imageContext.getHeight();
+	    if (heightObj != null)
+	    {
+	        height = heightObj.intValue();
+	    }
+	    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+	    Graphics graphics = img.getGraphics();
+	    try
+	    {
+	        graphics.setColor(Color.WHITE);
+	        graphics.fillRect(0, 0, width, height);
+	        graphics.setColor(Color.BLUE);
+	        graphics.drawString(text.toString(), 10, 20);
+	        
+	        ByteArrayOutputStream baout = new ByteArrayOutputStream();
+	        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baout);
+	        encoder.encode(img);
+	        baout.flush();
+	        bytes = baout.toByteArray();
+	    }
+	    finally
+	    {
+	        graphics.dispose();
+	    }	
     }
 
     public Class getImageRenderer()
@@ -59,45 +100,22 @@
     {
         return "image/jpeg";
     }
+    
+    /**
+     * @see org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer#getContentLength()
+     */
+    public int getContentLength()
+    {
+        return -1;
+    }
 
     /**
      * @see org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer#renderImage(javax.faces.context.FacesContext, org.apache.myfaces.custom.graphicimagedynamic.ImageContext, java.io.OutputStream)
      */
-    public void renderImage(FacesContext facesContext, ImageContext imageContext, OutputStream out)
+    public void renderImage(ResponseStream out)
             throws IOException
     {
-        Object text = imageContext.getParamters().get("text");
-        if (text == null)
-        {
-            text = "";
-        }
-        int width = 300;
-        int height = 30;
-        Integer widthObj = imageContext.getWidth();
-        if (widthObj != null)
-        {
-            width = widthObj.intValue();
-        }
-        Integer heightObj = imageContext.getHeight();
-        if (heightObj != null)
-        {
-            height = heightObj.intValue();
-        }
-        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-        Graphics graphics = img.getGraphics();
-        try
-        {
-            graphics.setColor(Color.WHITE);
-            graphics.fillRect(0, 0, width, height);
-            graphics.setColor(Color.BLUE);
-            graphics.drawString(text.toString(), 10, 20);
-            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
-            encoder.encode(img);
-        }
-        finally
-        {
-            graphics.dispose();
-        }
+        out.write( bytes );
     }
 
 }

Modified: myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/UploadedImageRenderer.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/UploadedImageRenderer.java?rev=378332&r1=378331&r2=378332&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/UploadedImageRenderer.java (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/graphicImageDynamic/UploadedImageRenderer.java Thu Feb 16 10:35:14 2006
@@ -17,34 +17,40 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
-
-import org.apache.myfaces.custom.graphicimagedynamic.ImageContext;
-import org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer;
 
 import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseStream;
 import javax.faces.el.ValueBinding;
 
+import org.apache.myfaces.custom.graphicimagedynamic.ImageContext;
+import org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer;
+
 /**
  * @author Mathias Broekelmann
  *
  */
 public class UploadedImageRenderer implements ImageRenderer
 {
-    private final GraphicImageDynamicBean _graphicImageDynamicBean;
+    private GraphicImageDynamicBean _graphicImageDynamicBean;
 
-    public UploadedImageRenderer()
+    public void setContext(FacesContext facesContext, ImageContext imageContext) throws IOException
     {
-        FacesContext currentInstance = FacesContext.getCurrentInstance();
-        ValueBinding vb = currentInstance.getApplication().createValueBinding(
+        ValueBinding vb = facesContext.getApplication().createValueBinding(
                 "#{graphicImageDynamicBean}");
-        GraphicImageDynamicBean value = (GraphicImageDynamicBean) vb.getValue(currentInstance);
+        GraphicImageDynamicBean value = (GraphicImageDynamicBean) vb.getValue(facesContext);
         if (value == null)
         {
             throw new IllegalStateException("managed bean graphicImageDynamicBean not found");
         }
         _graphicImageDynamicBean = value;
     }
+    
+    /**
+     * @see org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer#getContentLength()
+     */
+    public int getContentLength() {
+		return _graphicImageDynamicBean.isUploaded() ? (int)_graphicImageDynamicBean.getUpImage().getSize() : -1;
+	}
 
     /**
      * @see org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer#getContentType()
@@ -58,7 +64,7 @@
     /**
      * @see org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer#renderImage(javax.faces.context.FacesContext, org.apache.myfaces.custom.graphicimagedynamic.ImageContext, java.io.OutputStream)
      */
-    public void renderImage(FacesContext facesContext, ImageContext imageContext, OutputStream out)
+    public void renderImage(ResponseStream out)
             throws IOException
     {
         if (_graphicImageDynamicBean.isUploaded())

Modified: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/graphicImageDynamic.jsp
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/graphicImageDynamic.jsp?rev=378332&r1=378331&r2=378332&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/graphicImageDynamic.jsp (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/graphicImageDynamic.jsp Thu Feb 16 10:35:14 2006
@@ -1,7 +1,7 @@
 <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
 <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
-<%@ taglib uri="http://myfaces.apache.org/sandbox" prefix="x"%>
+<%@ taglib uri="http://myfaces.apache.org/sandbox" prefix="s"%>
 
 <!--
 /*
@@ -35,7 +35,7 @@
 
 			<br/>
 			Here it is :<br/>
-			<x:graphicImageDynamic
+			<s:graphicImageDynamic
         rendered="#{graphicImageDynamicBean.uploaded}"
 				id="imageDisplay"
         imageRendererClass="#{graphicImageDynamicBean.imageRenderer}"/>