[seam-commits] Seam SVN: r9855 - in trunk: examples/ui/resources/WEB-INF and 7 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Jan 2 17:56:08 EST 2009


Author: danielc.roth
Date: 2009-01-02 17:56:08 -0500 (Fri, 02 Jan 2009)
New Revision: 9855

Added:
   trunk/examples/ui/src/org/jboss/seam/example/ui/Resources.java
   trunk/examples/ui/view/resource.xhtml
   trunk/examples/ui/view/resources.xhtml
   trunk/ui/src/main/config/component/download.xml
   trunk/ui/src/main/config/component/resource.xml
   trunk/ui/src/main/java/org/jboss/seam/ui/DocumentStoreUtils.java
   trunk/ui/src/main/java/org/jboss/seam/ui/component/UIDownload.java
   trunk/ui/src/main/java/org/jboss/seam/ui/component/UIResource.java
   trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/DownloadRendererBase.java
   trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/ResourceRendererBase.java
Modified:
   trunk/examples/mail/src/org/jboss/seam/example/mail/test/MailTest.java
   trunk/examples/ui/resources/WEB-INF/web.xml
   trunk/examples/ui/view/template.xhtml
   trunk/src/main/org/jboss/seam/document/package-info.java
Log:
JBSEAM-1350 pt2/3

Modified: trunk/examples/mail/src/org/jboss/seam/example/mail/test/MailTest.java
===================================================================
--- trunk/examples/mail/src/org/jboss/seam/example/mail/test/MailTest.java	2009-01-02 22:13:54 UTC (rev 9854)
+++ trunk/examples/mail/src/org/jboss/seam/example/mail/test/MailTest.java	2009-01-02 22:56:08 UTC (rev 9855)
@@ -4,7 +4,6 @@
 import static javax.mail.Message.RecipientType.CC;
 
 import java.io.InputStream;
-import java.util.Enumeration;
 
 import javax.faces.FacesException;
 import javax.faces.context.FacesContext;
@@ -14,13 +13,13 @@
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
 
-import org.jboss.seam.mail.ui.UIAttachment;
-import org.jboss.seam.mail.ui.UIMessage;
+import org.jboss.seam.contexts.Contexts;
 import org.jboss.seam.document.ByteArrayDocumentData;
 import org.jboss.seam.document.DocumentData;
-import org.jboss.seam.contexts.Contexts;
 import org.jboss.seam.example.mail.Person;
 import org.jboss.seam.mail.MailSession;
+import org.jboss.seam.mail.ui.UIAttachment;
+import org.jboss.seam.mail.ui.UIMessage;
 import org.jboss.seam.mock.MockTransport;
 import org.jboss.seam.mock.SeamTest;
 import org.testng.annotations.Test;

Modified: trunk/examples/ui/resources/WEB-INF/web.xml
===================================================================
--- trunk/examples/ui/resources/WEB-INF/web.xml	2009-01-02 22:13:54 UTC (rev 9854)
+++ trunk/examples/ui/resources/WEB-INF/web.xml	2009-01-02 22:56:08 UTC (rev 9855)
@@ -20,6 +20,15 @@
         <url-pattern>/seam/resource/*</url-pattern>
     </servlet-mapping>
     
+	<servlet>
+		<servlet-name>Document Store Servlet</servlet-name>
+		<servlet-class>org.jboss.seam.document.DocumentStoreServlet</servlet-class>
+	</servlet>
+	<servlet-mapping>
+		<servlet-name>Document Store Servlet</servlet-name>
+		<url-pattern>/seam/docstore/*</url-pattern>
+	</servlet-mapping>
+    
     <filter>
         <filter-name>Seam Filter</filter-name>
         <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>

Added: trunk/examples/ui/src/org/jboss/seam/example/ui/Resources.java
===================================================================
--- trunk/examples/ui/src/org/jboss/seam/example/ui/Resources.java	                        (rev 0)
+++ trunk/examples/ui/src/org/jboss/seam/example/ui/Resources.java	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,101 @@
+package org.jboss.seam.example.ui;
+
+import java.io.ByteArrayInputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.web.RequestParameter;
+
+ at Name("resources")
+ at Scope(ScopeType.EVENT)
+public class Resources
+{
+
+   private static Map<String, ResourceItem> resources = new HashMap<String, ResourceItem>();
+
+   static
+   {
+      resources.put("1", new ResourceItem("text.txt", new byte[] { 'a', 'b', 'c' }, null, "text/plain"));
+      ByteArrayInputStream str = new ByteArrayInputStream(new byte[] { '1', '2', '3' });
+      resources.put("2", new ResourceItem("numbers.txt", str, null, "text/plain"));
+   }
+
+   @RequestParameter
+   private String id;
+
+   private ResourceItem item;
+
+   @Create
+   public void create()
+   {
+      item = resources.get(id);
+   }
+
+   public static class ResourceItem
+   {
+
+      public ResourceItem(String fileName, Object data, String disposition, String contentType)
+      {
+         this.fileName = fileName;
+         this.data = data;
+         this.disposition = disposition;
+         this.contentType = contentType;
+      }
+
+      public String fileName;
+      public Object data;
+      public String disposition;
+      public String contentType;
+
+      public String getFileName()
+      {
+         return fileName;
+      }
+
+      public Object getData()
+      {
+         return data;
+      }
+
+      public String getDisposition()
+      {
+         return disposition;
+      }
+
+      public String getContentType()
+      {
+         return contentType;
+      }
+
+   }
+
+   public String getId()
+   {
+      return id;
+   }
+
+   public void setId(String id)
+   {
+      this.id = id;
+   }
+
+   public ResourceItem getItem()
+   {
+      return item;
+   }
+
+   public void setItem(ResourceItem item)
+   {
+      this.item = item;
+   }
+
+   public ResourceItem getTextItem()
+   {
+      return resources.get("1");
+   }
+
+}

Added: trunk/examples/ui/view/resource.xhtml
===================================================================
--- trunk/examples/ui/view/resource.xhtml	                        (rev 0)
+++ trunk/examples/ui/view/resource.xhtml	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,26 @@
+<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<ui:composition xmlns="http://www.w3.org/1999/xhtml"
+	xmlns:ui="http://java.sun.com/jsf/facelets"
+	xmlns:f="http://java.sun.com/jsf/core"
+	xmlns:h="http://java.sun.com/jsf/html"
+	xmlns:s="http://jboss.com/products/seam/taglib"
+	template="template.xhtml">
+	<ui:param name="tagName" value="s:resource, s:download" />
+	<ui:define name="body">
+		<p>Download a file.</p>
+		<h:panelGrid columns="3">
+			<h:outputText value="Totally restful" />
+			<h:outputLink value="resources.seam?id=1">Text</h:outputLink>
+			<h:outputLink value="resources.seam?id=2">Numbers</h:outputLink>
+			<h:outputText value="Restful with s:download" />
+			<s:download src="/resources.xhtml">
+				<h:outputText value="Text" />
+				<f:param name="id" value="1" />
+			</s:download>
+			<s:download src="/resources.xhtml">
+				<h:outputText value="Numbers" />
+				<f:param name="id" value="2" />
+			</s:download>
+		</h:panelGrid>
+	</ui:define>
+</ui:composition>

Added: trunk/examples/ui/view/resources.xhtml
===================================================================
--- trunk/examples/ui/view/resources.xhtml	                        (rev 0)
+++ trunk/examples/ui/view/resources.xhtml	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,5 @@
+<s:resource xmlns="http://www.w3.org/1999/xhtml"
+	xmlns:s="http://jboss.com/products/seam/taglib"
+	data="#{resources.item.data}"
+	contentType="#{resources.item.contentType}"
+	fileName="#{resources.item.fileName}" />

Modified: trunk/examples/ui/view/template.xhtml
===================================================================
--- trunk/examples/ui/view/template.xhtml	2009-01-02 22:13:54 UTC (rev 9854)
+++ trunk/examples/ui/view/template.xhtml	2009-01-02 22:56:08 UTC (rev 9855)
@@ -105,7 +105,12 @@
 		<li><s:link view="/equalityValidatorWConvert.xhtml">
 			<code>s:validateEquality(2)</code>
 		</s:link></li>		
+	
+		<li><s:link view="/resource.xhtml">
+			<code>s:resource/download</code>
+		</s:link></li>		
 		
+		
 		<li><s:span title="#{spanTitle}" style="font-weight:bold">
 		    <code>s:span</code>
 		  </s:span>

Modified: trunk/src/main/org/jboss/seam/document/package-info.java
===================================================================
--- trunk/src/main/org/jboss/seam/document/package-info.java	2009-01-02 22:13:54 UTC (rev 9854)
+++ trunk/src/main/org/jboss/seam/document/package-info.java	2009-01-02 22:56:08 UTC (rev 9855)
@@ -2,3 +2,4 @@
 package org.jboss.seam.document;
 
 import org.jboss.seam.annotations.Namespace;
+

Added: trunk/ui/src/main/config/component/download.xml
===================================================================
--- trunk/ui/src/main/config/component/download.xml	                        (rev 0)
+++ trunk/ui/src/main/config/component/download.xml	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE components PUBLIC "-//AJAX4JSF//CDK Generator config/EN"  "http://jboss.org/jbossrichfaces/component-config.dtd" >
+<components>
+	<component>
+		<name>org.jboss.seam.ui.Download</name>
+		<family>org.jboss.seam.ui.Download</family>
+		<classname>org.jboss.seam.ui.component.html.HtmlDownload</classname>
+		<superclass>org.jboss.seam.ui.component.UIDownload</superclass>
+		<description>
+			<![CDATA[Link for downloading a file]]>
+		</description>
+		<renderer generate="false" override="false">
+			<name>org.jboss.seam.ui.DownloadRenderer</name>
+			<classname>org.jboss.seam.ui.renderkit.DownloadRendererBase</classname>
+		</renderer>		
+		<tag>
+			<name>download</name>
+			<classname>org.jboss.seam.ui.taglib.DownloadTag</classname>
+			<superclass>
+				org.jboss.seam.ui.util.cdk.UIComponentTagBase
+			</superclass>
+		</tag>
+		&html_style_attributes;
+		<property>
+			<name>src</name>
+			<classname>java.lang.String</classname>
+			<description>Source xhtml file that acts as resource holder</description>
+		</property>
+		<property>
+			<name>rendered</name>
+			<classname>boolean</classname>
+			<description>true iff this component should be rendered</description>
+			<defaultvalue>true</defaultvalue>
+		</property>		
+	</component>
+</components>

Added: trunk/ui/src/main/config/component/resource.xml
===================================================================
--- trunk/ui/src/main/config/component/resource.xml	                        (rev 0)
+++ trunk/ui/src/main/config/component/resource.xml	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE components PUBLIC "-//AJAX4JSF//CDK Generator config/EN"  "http://jboss.org/jbossrichfaces/component-config.dtd" >
+<components>
+	<component>
+		<name>org.jboss.seam.ui.Resource</name>
+		<family>org.jboss.seam.ui.Resource</family>
+		<classname>org.jboss.seam.ui.component.html.HtmlResource</classname>
+		<superclass>org.jboss.seam.ui.component.UIResource</superclass>
+		<description>
+			<![CDATA[Given a data in form of an inputstream, java.util.File or byte[] and a content-type, this tag sends the data to the browser]]>
+		</description>
+		<renderer generate="false" override="false">
+			<name>org.jboss.seam.ui.ResourceRenderer</name>
+			<classname>org.jboss.seam.ui.renderkit.ResourceRendererBase</classname>
+		</renderer>		
+		<tag>
+			<name>resource</name>
+			<classname>org.jboss.seam.ui.taglib.ResourceTag</classname>
+			<superclass>
+				org.jboss.seam.ui.util.cdk.UIComponentTagBase
+			</superclass>
+		</tag>
+		<property>
+			<name>data</name>
+			<classname>java.lang.Object</classname>
+			<description>Inputstream, File or byte[].</description>
+		</property>
+		<property>
+			<name>contentType</name>
+			<classname>java.lang.String</classname>
+			<description>Content-type of given data</description>
+		</property>
+		<property>
+			<name>fileName</name>
+			<classname>java.lang.String</classname>
+			<description>file name to send to browser (default: name of view)</description>
+		</property>
+		<property>
+			<name>disposition</name>
+			<classname>java.lang.String</classname>
+			<description>Content-Disposition for file (default: inline)</description>
+		</property>				
+	</component>
+</components>

Added: trunk/ui/src/main/java/org/jboss/seam/ui/DocumentStoreUtils.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/DocumentStoreUtils.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/DocumentStoreUtils.java	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,102 @@
+package org.jboss.seam.ui;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.faces.context.FacesContext;
+
+import org.jboss.seam.core.Manager;
+import org.jboss.seam.document.DocumentData;
+import org.jboss.seam.document.DocumentStore;
+import org.jboss.seam.document.DocumentData.DocumentType;
+import org.jboss.seam.navigation.Pages;
+import org.jboss.seam.ui.component.UIResource;
+
+public class DocumentStoreUtils
+{
+
+   public static String addResourceToDataStore(FacesContext ctx, UIResource resource)
+   {
+      String baseName = baseNameForViewId(Pages.getCurrentViewId());
+      String viewId = Pages.getViewId(ctx);
+
+      DocumentStore store = DocumentStore.instance();
+      String id = store.newId();
+
+      DocumentType type = new DocumentType("", resource.getContentType());
+
+      DocumentData documentData = new DownloadableDocumentData(baseName, type, resource.getData());
+      documentData.setFilename(resource.getFileName());
+      documentData.setDisposition(resource.getDisposition());
+
+      String url = store.preferredUrlForContent(resource.getFileName(), type.getExtension(), id);
+      url = Manager.instance().encodeConversationId(url, viewId);
+      store.saveData(id, documentData);
+      return url;
+   }
+
+   static class DownloadableDocumentData extends DocumentData
+   {
+
+      private Object data;
+
+      public DownloadableDocumentData(String baseName, DocumentType type, Object data)
+      {
+         super(baseName, type);
+         this.data = data;
+      }
+
+      @Override
+      public void writeDataToStream(OutputStream os) throws IOException
+      {
+         if (data instanceof byte[])
+         {
+            os.write((byte[]) data);
+         }
+         else if (data instanceof File)
+         {
+            writeStream(os, new FileInputStream((File) data));
+         }
+         else if (data instanceof InputStream)
+         {
+            writeStream(os, (InputStream) data);
+         }
+
+      }
+
+      private void writeStream(OutputStream os, InputStream is) throws IOException
+      {
+         while (is.available() > 0)
+         {
+            os.write(is.read());
+         }
+      }
+
+   }
+
+   /**
+    * TODO: This part should be refactored in to Pages.java Used (at least)
+    * here, in pdf module and excel
+    * 
+    */
+   public static String baseNameForViewId(String viewId)
+   {
+      int pos = viewId.lastIndexOf("/");
+      if (pos != -1)
+      {
+         viewId = viewId.substring(pos + 1);
+      }
+
+      pos = viewId.lastIndexOf(".");
+      if (pos != -1)
+      {
+         viewId = viewId.substring(0, pos);
+      }
+
+      return viewId;
+   }
+
+}

Added: trunk/ui/src/main/java/org/jboss/seam/ui/component/UIDownload.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/component/UIDownload.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/component/UIDownload.java	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,14 @@
+package org.jboss.seam.ui.component;
+
+/**
+ * 
+ * @author Daniel Roth
+ * 
+ */
+public abstract class UIDownload extends UILink
+{
+   public abstract String getSrc();
+
+   public abstract void setSrc(String src);
+
+}

Added: trunk/ui/src/main/java/org/jboss/seam/ui/component/UIResource.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/component/UIResource.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/component/UIResource.java	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,41 @@
+package org.jboss.seam.ui.component;
+
+import java.io.IOException;
+
+import javax.faces.component.UIComponentBase;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+
+/**
+ * @author Daniel Roth
+ */
+public abstract class UIResource extends UIComponentBase
+{
+
+   public abstract Object getData();
+
+   public abstract void setData(Object data);
+
+   public abstract String getContentType();
+
+   public abstract void setContentType(String contentType);
+
+   public abstract String getDisposition();
+
+   public abstract void setDisposition(String disposition);
+
+   public abstract String getFileName();
+
+   public abstract void setFileName(String fileName);
+
+   @Override
+   public void encodeBegin(FacesContext arg0) throws IOException
+   {
+      if (!(getParent() instanceof UIViewRoot || getParent() instanceof UIDownload))  
+      {
+         throw new IllegalArgumentException("s:remote must be nested in a s:download or alone in the page");
+      }
+      super.encodeBegin(arg0);
+   }
+
+}

Added: trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/DownloadRendererBase.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/DownloadRendererBase.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/DownloadRendererBase.java	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,81 @@
+package org.jboss.seam.ui.renderkit;
+
+import java.io.IOException;
+
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIParameter;
+import javax.faces.context.FacesContext;
+
+import org.jboss.seam.ui.component.UIDownload;
+import org.jboss.seam.ui.component.UIResource;
+import org.jboss.seam.ui.util.HTML;
+import org.jboss.seam.ui.util.ViewUrlBuilder;
+import org.jboss.seam.ui.util.cdk.RendererBase;
+
+public class DownloadRendererBase extends RendererBase
+{
+
+   @Override
+   protected Class getComponentClass()
+   {
+      return UIDownload.class;
+   }
+
+   @Override
+   protected void doEncodeBegin(javax.faces.context.ResponseWriter writer, FacesContext ctx, UIComponent component) throws IOException
+   {
+      UIDownload download = (UIDownload) component;
+
+      if (!download.isRendered())
+         return;
+
+      if (download.getSrc() != null)
+      {
+
+         ViewUrlBuilder builder = new ViewUrlBuilder(download.getSrc(), null);
+
+         for (Object child : download.getChildren())
+         {
+            if (child instanceof UIParameter)
+            {
+               builder.addParameter((UIParameter) child);
+            }
+
+         }
+         writeStartTag(writer, download, builder.getEncodedUrl());
+         renderNonResourceChildren(ctx, download);
+         writer.endElement(HTML.ANCHOR_ELEM);
+
+      }
+
+   }
+
+   private void renderNonResourceChildren(FacesContext ctx, UIComponent component) throws IOException
+   {
+      for (Object child : component.getChildren())
+      {
+         if (!(child instanceof UIParameter) && !(child instanceof UIResource) && (child instanceof UIComponent))
+         {
+            renderChild(ctx, (UIComponent) child);
+         }
+
+      }
+   }
+
+   private void writeStartTag(javax.faces.context.ResponseWriter writer, UIDownload download, String url) throws IOException
+   {
+      writer.startElement(HTML.ANCHOR_ELEM, null);
+      writer.writeAttribute(HTML.HREF_ATTR, url, null);
+      if (download.getStyle() != null)
+         writer.writeAttribute(HTML.STYLE_ATTR, download.getStyle(), null);
+      if (download.getStyleClass() != null)
+         writer.writeAttribute(HTML.STYLE_CLASS_ATTR, download.getStyleClass(), null);
+   }
+
+   @Override
+   public boolean getRendersChildren()
+   {
+      return true;
+   }
+
+}

Added: trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/ResourceRendererBase.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/ResourceRendererBase.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/renderkit/ResourceRendererBase.java	2009-01-02 22:56:08 UTC (rev 9855)
@@ -0,0 +1,35 @@
+package org.jboss.seam.ui.renderkit;
+
+import java.io.IOException;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+import org.jboss.seam.ui.DocumentStoreUtils;
+import org.jboss.seam.ui.component.UIResource;
+import org.jboss.seam.ui.util.cdk.RendererBase;
+
+public class ResourceRendererBase extends RendererBase
+{
+
+   @Override
+   protected Class getComponentClass()
+   {
+      return UIResource.class;
+   }
+
+   @Override
+   protected void doEncodeBegin(javax.faces.context.ResponseWriter writer, FacesContext ctx, UIComponent component) throws IOException
+   {
+
+      UIResource resource = (UIResource) component;
+
+      String url = DocumentStoreUtils.addResourceToDataStore(ctx, resource);
+
+      ctx.getExternalContext().redirect(url);
+
+   }
+
+
+
+}




More information about the seam-commits mailing list