4 Replies Latest reply on Aug 31, 2006 5:56 PM by Pete Muir

    How to use graphicImageDynamic with Seam?

    Mulder Mulder Newbie

      I want to use graphicImageDynamic for dynamic images in an application using Seam. For testing it, I tried to implement the sandbox GraphicImageDynamicText example using Seam and Facelets. This component uses imageRendererClass param which must point to ?reference or a fully qualified class name which implements org.apache.myfaces.custom.graphicimagedynamic.ImageRenderer?.

      I have compiled myfaces svn version (1.1.2) from 29.03.06. All myfaces .jars are placed to
      ?/jboss/server/default/deploy/jbossweb-tomcat55.sar/jsf-libs/. JBoss version is 4.0.4RC1.
      I have Login, LoggedIn and LoggedInInterceptor components from the old beta1 booking example.
      I have GraphicImageDynamicComponentHandler and sandbox.taglib.xml from facelets wiki. Facelets version is 1.1.5.
      The Seam version is CVS20060416.

      Also I have:
      Test.java:

      package test.seam;
      
      import java.io.IOException;
      
      import javax.ejb.Local;
      import javax.faces.context.FacesContext;
      import javax.faces.context.ResponseStream;
      
      import org.apache.myfaces.custom.dynamicResources.ResourceContext;
      
      @Local
      public interface Test {
      
       public void setContext(FacesContext facesContext, ResourceContext resourceContext) throws IOException;
       public String getText();
      
       public void setText(String text);
       public Class getImageRenderer();
       public String getContentType();
       public int getContentLength();
       public void renderResource(ResponseStream out) throws IOException;
      }
      



      TestAction.java:
      Imports
      <?>
      
      @Stateless
      @Name("test")
      @Interceptors(SeamInterceptor.class)
      @Intercept(InterceptionType.ALWAYS)
      @Scope(ScopeType.SESSION)
      @LoggedIn
      public class TestAction implements Test, ImageRenderer, Serializable {
      
       private static final long serialVersionUID = 500L;
      
       private static final Logger log = Logger.getLogger(Test.class);
       private String _text;
      
      
       private byte[] bytes = null;
      
       public String getText() {
       return _text;
       }
      
       public void setText(String text) {
       this._text = text;
       }
      
       @In
       private User user;
      
       public void setContext(FacesContext facesContext, ResourceContext resourceContext) throws IOException {
       ImageContext imageContext = (ImageContext) resourceContext;
       //Object text = imageContext.getParamters().get("text");
       Object text = (user== null ? "user=null" : user.getName());
       if (text == null) {
       text = "_null_";
       }
       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() {
       return this.getClass();
       }
      
       public String getContentType() {
       return "image/jpeg";
       }
      
       public int getContentLength() {
       return -1;
       }
      
       public void renderResource(ResponseStream out) throws IOException {
       out.write( bytes );
       }
      
      }
      


      web.xml:
      <context-param>
       <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
       <param-value>.xhtml</param-value>
       </context-param>
       <context-param>
       <param-name>facelets.DEVELOPMENT</param-name>
       <param-value>true</param-value>
       </context-param>
       <context-param>
       <param-name>facelets.LIBRARIES</param-name>
       <param-value>/WEB-INF/tomahawk.taglib.xml;/WEB-INF/sandbox.taglib.xml</param-value>
       </context-param>
       <context-param>
       <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
       <param-value>client</param-value>
       </context-param>
       <context-param>
       <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
       <param-value>true</param-value>
       </context-param>
       <context-param>
       <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
       <param-value>false</param-value>
       </context-param>
       <context-param>
       <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
       <param-value>true</param-value>
       </context-param>
       <context-param>
       <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
       <param-value>true</param-value>
       </context-param>
       <context-param>
       <param-name>org.jboss.seam.core.init.jndiPattern</param-name>
       <param-value>test/#{ejbName}/local</param-value>
       </context-param>
      
       <filter>
       <filter-name>extensionsFilter</filter-name>
       <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
      </filter>
      
       <filter>
       <filter-name>SeamRedirectFilter</filter-name>
       <filter-class>org.jboss.seam.servlet.SeamRedirectFilter</filter-class>
       </filter>
      
       <filter>
       <filter-name>SeamServletFilter</filter-name>
       <filter-class>org.jboss.seam.servlet.SeamServletFilter</filter-class>
       </filter>
      
       <filter-mapping>
       <filter-name>extensionsFilter</filter-name>
       <url-pattern>*.jsf</url-pattern>
       </filter-mapping>
      
       <filter-mapping>
       <filter-name>extensionsFilter</filter-name>
       <url-pattern>/faces/*</url-pattern>
       </filter-mapping>
      
       <filter-mapping>
       <filter-name>SeamRedirectFilter</filter-name>
       <url-pattern>*.jsf</url-pattern>
       </filter-mapping>
      
       <filter-mapping>
       <filter-name>SeamServletFilter</filter-name>
       <url-pattern>/faces/*</url-pattern>
       </filter-mapping>
      
      <listener>
       <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
       </listener>
       <listener>
       <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
       </listener>
      
       <servlet>
       <servlet-name>Faces Servlet</servlet-name>
       <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
       <servlet-name>Faces Servlet</servlet-name>
       <url-pattern>*.jsf</url-pattern>
       </servlet-mapping>
      </web-app>
      


      faces-config.xml:
      <faces-config>
      <navigation-rule>
       <navigation-case>
       <from-outcome>test</from-outcome>
       <to-view-id>/test.xhtml</to-view-id>
       <redirect />
       </navigation-case>
      </navigation-rule>
      
      <application>
       <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
       </application>
       <lifecycle>
       <phase-listener>org.jboss.seam.jsf.SeamPhaseListener</phase-listener>
       </lifecycle>
      </faces-config>
      


      Test.xhml:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:t="http://myfaces.apache.org/tomahawk"
       xmlns:s="http://myfaces.apache.org/sandbox">
      <head>
      <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=UTF-8" />
      </head>
      <body>
       <h:form enctype="multipart/form-data">
      
       <br/>
       <h:inputText maxlength="20" value="#{test.text}"/>
       <br/>
       <s:graphicImageDynamic id="imageDisplay3" width="300" height="22"
       imageRendererClass="#{test.imageRenderer}">
       <f:param name="text" value="#{test.text}" />
       </s:graphicImageDynamic>
      
       <br/>
       <t:messages showDetail="true"/>
       <h:commandButton value="Update"/>
       </h:form>
      </body>
      </html>
      


      When I use ?Object text = imageContext.getParamters().get("text");? the text is rendered properly.

      When I try to render User name, it is null, but in debug.xhml (taken from the examples), I see the User component in the session context. The same thing happens when I try to use ?@In private Context sessionContext;?.

      Why the injection components are null?

      Is it possible to use graphicImageDynamic and Seam? And if not, in what way can I use SeamServletFilter in pure servlet to interacts with Seam component? Unfortunately, there are no examples of using SeamServletFilter.

      Thanks.