Revision: 434
          http://svn.sourceforge.net/stripes/?rev=434&view=rev
Author:   tfenne
Date:     2006-10-11 05:14:19 -0700 (Wed, 11 Oct 2006)

Log Message:
-----------
Changes to the example app to better support non-text attachments.

Modified Paths:
--------------
    
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/DownloadAttachmentActionBean.java
    
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/SingleBugActionBean.java
    
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/biz/Attachment.java

Modified: 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/DownloadAttachmentActionBean.java
===================================================================
--- 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/DownloadAttachmentActionBean.java
      2006-10-07 18:15:27 UTC (rev 433)
+++ 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/DownloadAttachmentActionBean.java
      2006-10-11 12:14:19 UTC (rev 434)
@@ -6,9 +6,8 @@
 import net.sourceforge.stripes.examples.bugzooky.biz.Attachment;
 import net.sourceforge.stripes.examples.bugzooky.biz.Bug;
 import net.sourceforge.stripes.examples.bugzooky.biz.BugManager;
-import net.sourceforge.stripes.examples.bugzooky.BugzookyActionBean;
 
-import java.io.StringReader;
+import java.io.ByteArrayInputStream;
 
 /**
  * Action that responds to a user's request to download an attachment to a bug.
@@ -35,7 +34,7 @@
         // Note the use of the chained .setFilename() method, which causes the
         // browser to [prompt to] save the "file" instead of displaying it in 
browser
         return new StreamingResolution
-                (attachment.getContentType(), new 
StringReader(attachment.getData()))
+                (attachment.getContentType(), new 
ByteArrayInputStream(attachment.getData()))
                     .setFilename(attachment.getName());
     }
 }

Modified: 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/SingleBugActionBean.java
===================================================================
--- 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/SingleBugActionBean.java
       2006-10-07 18:15:27 UTC (rev 433)
+++ 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/SingleBugActionBean.java
       2006-10-11 12:14:19 UTC (rev 434)
@@ -3,6 +3,7 @@
 import net.sourceforge.stripes.action.DefaultHandler;
 import net.sourceforge.stripes.action.DontValidate;
 import net.sourceforge.stripes.action.FileBean;
+import net.sourceforge.stripes.action.ForwardResolution;
 import net.sourceforge.stripes.action.RedirectResolution;
 import net.sourceforge.stripes.action.Resolution;
 import net.sourceforge.stripes.examples.bugzooky.biz.Attachment;
@@ -12,9 +13,8 @@
 import net.sourceforge.stripes.validation.Validate;
 import net.sourceforge.stripes.validation.ValidateNestedProperties;
 
-import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
+import java.io.InputStream;
 
 /**
  * ActionBean that provides method for editing a single bug in detail. 
Includes an
@@ -45,7 +45,7 @@
     public Resolution preEdit() {
         BugManager bm = new BugManager();
         this.bug = bm.getBug( this.bug.getId() );
-        return new RedirectResolution("/bugzooky/AddEditBug.jsp").flash(this);
+        return new ForwardResolution("/bugzooky/AddEditBug.jsp");
     }
 
     /** Saves (or updates) a bug, and then returns the user to the bug list. */
@@ -60,16 +60,10 @@
             attachment.setSize(this.newAttachment.getSize());
             attachment.setContentType(this.newAttachment.getContentType());
 
-            BufferedReader reader = new BufferedReader
-                    ( new 
InputStreamReader(this.newAttachment.getInputStream()) );
-            StringBuilder builder = new StringBuilder();
-            String line;
-
-            while ( (line = reader.readLine()) != null ) {
-                builder.append(line).append('\n');
-            }
-
-            attachment.setData(builder.toString());
+            byte[] data = new byte[(int) this.newAttachment.getSize()];
+            InputStream in = this.newAttachment.getInputStream();
+            in.read(data);
+            attachment.setData(data);
             newBug.addAttachment(attachment);
         }
 

Modified: 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/biz/Attachment.java
===================================================================
--- 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/biz/Attachment.java
    2006-10-07 18:15:27 UTC (rev 433)
+++ 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/biz/Attachment.java
    2006-10-11 12:14:19 UTC (rev 434)
@@ -9,7 +9,7 @@
 public class Attachment {
     private String name;
     private long size;
-    private String data;
+    private byte[] data;
     private String contentType;
 
     public String getName() { return name; }
@@ -18,14 +18,19 @@
     public long getSize() { return size; }
     public void setSize(long size) { this.size = size; }
 
-    public String getData() { return data; }
-    public void setData(String data) { this.data = data; }
+    public byte[] getData() { return data; }
+    public void setData(byte[] data) { this.data = data; }
 
     public String getContentType() { return contentType; }
     public void setContentType(String contentType) { this.contentType = 
contentType; }
 
     public String getPreview() {
-        int endIndex = Math.min(data.length(), 30);
-        return data.substring(0, endIndex);
+        if (contentType.startsWith("text")) {
+            int amount = Math.min(data.length, 30);
+            return new String(data, 0, amount);
+        }
+        else {
+            return "[Binary File]";
+        }
     }
 }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to