]> source.dussan.org Git - vaadin-framework.git/commitdiff
Test case and fix for #3817 - Upload: Browsers send different filenames
authorArtur Signell <artur.signell@itmill.com>
Tue, 15 Dec 2009 09:19:08 +0000 (09:19 +0000)
committerArtur Signell <artur.signell@itmill.com>
Tue, 15 Dec 2009 09:19:08 +0000 (09:19 +0000)
svn changeset:10305/svn branch:6.2

src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java
tests/src/com/vaadin/tests/components/upload/TestUploadFilename.java [new file with mode: 0644]

index 34d37fca0617c52a9b4ad92cd8c3df3e1b552eec..dc2973ebe525925fd2e6e1cb0d060747eb8e67a3 100644 (file)
@@ -375,7 +375,9 @@ public abstract class AbstractCommunicationManager implements
             while (iter.hasNext()) {
                 final FileItemStream item = iter.next();
                 final String name = item.getFieldName();
-                final String filename = item.getName();
+                // Should report only the filename even if the browser sends the
+                // path
+                final String filename = removePath(item.getName());
                 final String mimeType = item.getContentType();
                 final InputStream stream = item.openStream();
                 if (item.isFormField()) {
@@ -441,6 +443,21 @@ public abstract class AbstractCommunicationManager implements
         sendUploadResponse(request, response);
     }
 
+    /**
+     * Removes any possible path information from the filename and returns the
+     * filename. Separators / and \\ are used.
+     * 
+     * @param name
+     * @return
+     */
+    private static String removePath(String filename) {
+        if (filename != null) {
+            filename = filename.replaceAll("^.*[/\\\\]", "");
+        }
+
+        return filename;
+    }
+
     /**
      * TODO document
      * 
diff --git a/tests/src/com/vaadin/tests/components/upload/TestUploadFilename.java b/tests/src/com/vaadin/tests/components/upload/TestUploadFilename.java
new file mode 100644 (file)
index 0000000..1200641
--- /dev/null
@@ -0,0 +1,61 @@
+package com.vaadin.tests.components.upload;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Upload;
+import com.vaadin.ui.Upload.FinishedEvent;
+import com.vaadin.ui.Upload.Receiver;
+
+public class TestUploadFilename extends TestBase {
+
+    private Label result = new Label("Waiting for upload");
+    private UploadReceiver receiver = new UploadReceiver();
+
+    @Override
+    protected void setup() {
+
+        Upload upload = new Upload("Upload a file", receiver);
+
+        addComponent(upload);
+        addComponent(result);
+
+        upload.addListener(new Upload.FinishedListener() {
+            public void uploadFinished(FinishedEvent event) {
+                result.setValue("Got file (should not contain path): "
+                        + receiver.getFilename());
+            }
+        });
+
+    }
+
+    public static class UploadReceiver implements Receiver {
+
+        private String filename;
+
+        public OutputStream receiveUpload(String filename, String MIMEType) {
+            this.filename = filename;
+            return new ByteArrayOutputStream();
+        }
+
+        public String getFilename() {
+            return filename;
+        }
+
+    }
+
+    @Override
+    protected String getDescription() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+}