-> Finalisation of file download for RAP
authorBruno Sinou <bsinou@argeo.org>
Wed, 9 Mar 2011 22:44:10 +0000 (22:44 +0000)
committerBruno Sinou <bsinou@argeo.org>
Wed, 9 Mar 2011 22:44:10 +0000 (22:44 +0000)
-> some little UI Bugs

git-svn-id: https://svn.argeo.org/commons/trunk@4276 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java [new file with mode: 0644]
eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java
eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileProvider.java [new file with mode: 0644]
eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java

diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java
new file mode 100644 (file)
index 0000000..7d47d56
--- /dev/null
@@ -0,0 +1,41 @@
+package org.argeo.eclipse.ui.specific;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
+
+import org.argeo.ArgeoException;
+import org.eclipse.rwt.RWT;
+import org.eclipse.rwt.service.IServiceHandler;
+
+public class DownloadServiceHandler implements IServiceHandler {
+
+       private FileProvider provider;
+
+       public DownloadServiceHandler(FileProvider provider) {
+               this.provider = provider;
+       }
+
+       public void service() throws IOException, ServletException {
+               // Which file to download?
+               String fileName = RWT.getRequest().getParameter("filename");
+               String fileId = RWT.getRequest().getParameter("fileid");
+
+               // Get the file content
+               byte[] download = provider.getByteArrayFileFromId(fileId);
+
+               // Send the file in the response
+               HttpServletResponse response = RWT.getResponse();
+               response.setContentType("application/octet-stream");
+               response.setContentLength(download.length);
+               String contentDisposition = "attachment; filename=\"" + fileName + "\"";
+               response.setHeader("Content-Disposition", contentDisposition);
+               try {
+                       response.getOutputStream().write(download);
+               } catch (IOException ioe) {
+                       throw new ArgeoException("Error while writing the file " + fileName
+                                       + " to the servlet response", ioe);
+               }
+       }
+}
index 2058328a30570e8187ec1ad37b6fc735c203eb1b..289cd2b903c412cf762aee3e3a7c050dc1430200 100644 (file)
@@ -1,28 +1,77 @@
 package org.argeo.eclipse.ui.specific;
 
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
+import java.net.URL;
 
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.ArgeoException;
 import org.eclipse.rwt.RWT;
-
+import org.eclipse.rwt.service.IServiceHandler;
+import org.eclipse.rwt.service.IServiceManager;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * RAP SPECIFIC handler to enable the opening of a download dialog box triggered
+ * by whatever action in the UI
+ * 
+ * Manages the registration of the effective DownloadServiceHandler at
+ * instantiation time.
+ * 
+ * Manages the process of forwarding the request to the handler at runtime to
+ * open the dialog box
+ * 
+ * @author bsinou
+ * 
+ */
 public class FileHandler {
 
-       private static Log log = LogFactory.getLog(FileHandler.class);
+       private final static Log log = LogFactory.getLog(FileHandler.class);
+
+       public FileHandler(FileProvider provider) {
+               // Instantiate and register the DownloadServicHandler.
+               IServiceManager manager = RWT.getServiceManager();
+               IServiceHandler handler = new DownloadServiceHandler(provider);
+               manager.registerServiceHandler("downloadServiceHandler", handler);
 
-       public FileHandler() {
        }
 
-       public void openFile(String fileName, InputStream is) {
+       public void openFile(String fileName, String fileId, InputStream is) {
+               try {
+                       if (log.isDebugEnabled())
+                               log.debug("URL : " + createFullDownloadUrl(fileName, fileId));
+
+                       URL url = new URL(createFullDownloadUrl(fileName, fileId));
+                       PlatformUI.getWorkbench().getBrowserSupport()
+                                       .createBrowser("DownloadDialog").openURL(url);
+
+                       /*
+                        * // New try : must define a service handler for the current file,
+                        * // register it and redirect the URL to it.
+                        * 
+                        * // workaround : create a tmp file. String prefix = "", suffix =
+                        * ""; if (fileName != null) { int ind = fileName.lastIndexOf('.');
+                        * if (ind > 0) { prefix = fileName.substring(0, ind); suffix =
+                        * fileName.substring(ind); } }
+                        * 
+                        * File tmpFile = createTmpFile(prefix, suffix, is);
+                        * HttpServletResponse response = RWT.getResponse();
+                        * 
+                        * DownloadHandler dh = new DownloadHandler(tmpFile.getName(),
+                        * tmpFile, "application/pdf", fileName);
+                        * 
+                        * log.debug("Got a DH : " + dh.toString());
+                        * 
+                        * // TODO : should try with that. // String encodedURL =
+                        * RWT.getResponse().encodeURL(url.toString());
+                        * response.sendRedirect(dh.getURL());
+                        */
+
+                       // final Browser browser = new Browser(parent, SWT.NONE);
+                       // browser.setText(createDownloadHtml("test.pdf", "Download file"));
+
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
 
                // // Which file to download?
                // String fileName = RWT.getRequest().getParameter( "filename" );
@@ -43,87 +92,106 @@ public class FileHandler {
                //
                //
 
-               try {
-
-                       // / workaround : create a tmp file.
-                       String prefix = "", suffix = "";
-                       if (fileName != null) {
-                               int ind = fileName.lastIndexOf('.');
-                               if (ind > 0) {
-                                       prefix = fileName.substring(0, ind);
-                                       suffix = fileName.substring(ind);
-                               }
-                       }
-
-                       File tmpFile = createTmpFile(prefix, suffix, is);
-
-                       // Send the file in the response
-                       HttpServletResponse response = RWT.getResponse();
-                       byte[] ba = null;
-                       ba = FileUtils.readFileToByteArray(tmpFile);
-
-                       response.setContentLength(ba.length);
-
-                       // String contentDisposition = "attachment; filename=\"" + fileName
-                       // + "\"";
-                       String contentDisposition = "attachment; filename=\"" + fileName
-                                       + "\"";
-                       response.setContentType("application/force-download");
-                       response.setHeader("Content-Disposition", contentDisposition);
-                       response.setHeader("Content-Transfer-Encoding", "binary");
-                       response.setHeader("Pragma", "no-cache");
-                       response.setHeader("Cache-Control", "no-cache, must-revalidate");
-
-                       // must-revalidate");
-
-                       if (log.isDebugEnabled()) {
-                               log.debug("Header Set ");
-                       }
-
-                       // header("Content-Type: application/force-download; name=\"".$localName."\"");
-                       // 852 header("Content-Transfer-Encoding: binary");
-                       // 853 if($gzip){
-                       // 854 header("Content-Encoding: gzip");
-                       // 855 // If gzip, recompute data size!
-                       // 856 $gzippedData =
-                       // ($data?gzencode($filePathOrData,9):gzencode(file_get_contents($filePathOrData),
-                       // 9));
-                       // 857 $size = strlen($gzippedData);
-                       // 858 }
-                       // 859 header("Content-Length: ".$size);
-                       // 860 if ($isFile && ($size != 0)) header("Content-Range: bytes 0-"
-                       // . ($size - 1) . "/" . $size . ";");
-                       // 861
-                       // header("Content-Disposition: attachment; filename=\"".$localName."\"");
-                       // 862 header("Expires: 0");
-                       // 863 header("Cache-Control: no-cache, must-revalidate");
-                       // 864 header("Pragma: no-cache");
-
-                       // IOUtils.copy(is, response.getOutputStream());
-                       response.getOutputStream().write(ba);
-                       // Error.show("In Open File for RAP.");
-               } catch (IOException ioe) {
-
-                       throw new ArgeoException("Cannot copy input stream from file "
-                                       + fileName + " to HttpServletResponse", ioe);
-               }
+               /**
+                * try {
+                * 
+                * // / workaround : create a tmp file. String prefix = "", suffix = "";
+                * if (fileName != null) { int ind = fileName.lastIndexOf('.'); if (ind
+                * > 0) { prefix = fileName.substring(0, ind); suffix =
+                * fileName.substring(ind); } }
+                * 
+                * File tmpFile = createTmpFile(prefix, suffix, is);
+                * 
+                * // Send the file in the response HttpServletResponse response =
+                * RWT.getResponse(); byte[] ba = null; ba =
+                * FileUtils.readFileToByteArray(tmpFile);
+                * 
+                * long l = tmpFile.length();
+                * 
+                * if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) throw new
+                * ArgeoException("IllegalArgumentException : " + l +
+                * " cannot be cast to int without changing its value.");
+                * response.setContentLength((int) l);
+                * 
+                * // response.setContentLength(ba.length);
+                * 
+                * // String contentDisposition = "attachment; filename=\"" + fileName
+                * // + "\""; String contentDisposition = "attachment; filename=\"" +
+                * fileName + "\"";
+                * response.setContentType("application/force-download");
+                * response.setHeader("Content-Disposition", contentDisposition);
+                * response.setHeader("Content-Transfer-Encoding", "binary");
+                * response.setHeader("Pragma", "no-cache");
+                * response.setHeader("Cache-Control", "no-cache, must-revalidate");
+                * 
+                * // must-revalidate");
+                * 
+                * if (log.isDebugEnabled()) { log.debug("Header Set "); }
+                * 
+                * // header("Content-Type: application/force-download; name=\"".
+                * $localName."\""); // 852 header("Content-Transfer-Encoding: binary");
+                * // 853 if($gzip){ // 854 header("Content-Encoding: gzip"); // 855 //
+                * If gzip, recompute data size! // 856 $gzippedData = //
+                * ($data?gzencode
+                * ($filePathOrData,9):gzencode(file_get_contents($filePathOrData), //
+                * 9)); // 857 $size = strlen($gzippedData); // 858 } // 859
+                * header("Content-Length: ".$size); // 860 if ($isFile && ($size != 0))
+                * header("Content-Range: bytes 0-" // . ($size - 1) . "/" . $size .
+                * ";"); // 861 //
+                * header("Content-Disposition: attachment; filename=\"".
+                * $localName."\""); // 862 header("Expires: 0"); // 863
+                * header("Cache-Control: no-cache, must-revalidate"); // 864
+                * header("Pragma: no-cache");
+                * 
+                * // IOUtils.copy(is, response.getOutputStream());
+                * response.getOutputStream().write(ba); //
+                * Error.show("In Open File for RAP.");
+                * 
+                * 
+                * 
+                * 
+                * } catch (IOException ioe) {
+                * 
+                * throw new ArgeoException("Cannot copy input stream from file " +
+                * fileName + " to HttpServletResponse", ioe); }
+                */
 
        }
 
-       private File createTmpFile(String prefix, String suffix, InputStream is) {
-               File tmpFile = null;
-               OutputStream os = null;
-               try {
-                       tmpFile = File.createTempFile(prefix, suffix);
-                       os = new FileOutputStream(tmpFile);
-                       IOUtils.copy(is, os);
-               } catch (IOException e) {
-                       throw new ArgeoException("Cannot open file " + prefix + "."
-                                       + suffix, e);
-               } finally {
-                       IOUtils.closeQuietly(os);
-               }
-               return tmpFile;
+       // private File createTmpFile(String prefix, String suffix, InputStream is)
+       // {
+       // File tmpFile = null;
+       // OutputStream os = null;
+       // try {
+       // tmpFile = File.createTempFile(prefix, suffix);
+       // os = new FileOutputStream(tmpFile);
+       // IOUtils.copy(is, os);
+       // } catch (IOException e) {
+       // throw new ArgeoException("Cannot open file " + prefix + "."
+       // + suffix, e);
+       // } finally {
+       // IOUtils.closeQuietly(os);
+       // }
+       // return tmpFile;
+       // }
+
+       private String createFullDownloadUrl(String fileName, String fileId) {
+               StringBuilder url = new StringBuilder();
+               url.append(RWT.getRequest().getRequestURL());
+               url.append(createParamUrl(fileName, fileId));
+               return url.toString();
        }
 
+       private String createParamUrl(String filename, String fileId) {
+               StringBuilder url = new StringBuilder();
+               url.append("?");
+               url.append(IServiceHandler.REQUEST_PARAM);
+               url.append("=downloadServiceHandler");
+               url.append("&filename=");
+               url.append(filename);
+               url.append("&fileid=");
+               url.append(fileId);
+               String encodedURL = RWT.getResponse().encodeURL(url.toString());
+               return encodedURL;
+       }
 }
diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileProvider.java b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileProvider.java
new file mode 100644 (file)
index 0000000..ba4946d
--- /dev/null
@@ -0,0 +1,14 @@
+package org.argeo.eclipse.ui.specific;
+
+/**
+ * Used for file download : subclasses must implement model specific methods to
+ * get a byte array representing a file given is ID.
+ * 
+ * @author bsinou
+ * 
+ */
+public interface FileProvider {
+
+       public byte[] getByteArrayFileFromId(String fileId);
+
+}
index 829ceda6a4015958a4992642476e93807172e52b..e0a008991bef59b90061ae77ed32c90719752e6e 100644 (file)
@@ -5,21 +5,16 @@ import java.io.InputStream;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
-import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.wizard.WizardPage;
 import org.eclipse.rwt.widgets.Upload;
-import org.eclipse.rwt.widgets.UploadAdapter;
-import org.eclipse.rwt.widgets.UploadEvent;
-import org.eclipse.rwt.widgets.UploadItem;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 
 public class ImportFileSystemWizardPage extends WizardPage {
-       private Log log = LogFactory.getLog(ImportFileSystemWizardPage.class);
+       private final static Log log = LogFactory.getLog(ImportFileSystemWizardPage.class);
 
        private Upload uploadFile;
 
@@ -29,18 +24,13 @@ public class ImportFileSystemWizardPage extends WizardPage {
        }
 
        public void createControl(Composite parent) {
-
                Composite composite = new Composite(parent, SWT.NONE);
                composite.setLayout(new GridLayout(2, false));
                composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-
                new Label(composite, SWT.NONE).setText("Pick up a file");
-
                uploadFile = new Upload(composite, SWT.BORDER);
                uploadFile.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
                uploadFile.setBrowseButtonText("Open...");
-               uploadFile.addUploadListener(new UploadManager(uploadFile));
-
                setControl(composite);
        }
 
@@ -69,38 +59,9 @@ public class ImportFileSystemWizardPage extends WizardPage {
        }
 
        protected void handleUploadFinished(final Upload upload) {
-               if (log.isTraceEnabled()) {
-                       UploadItem uploadItem = upload.getUploadItem();
-                       log.trace("filename     : " + uploadItem.getFileName());
-                       log.trace("content-type : " + uploadItem.getContentType());
-               }
        }
 
        public InputStream getFileInputStream() {
                return uploadFile.getUploadItem().getFileInputStream();
        }
-
-       private class UploadManager extends UploadAdapter {
-               private Upload upload;
-
-               public UploadManager(Upload upload) {
-                       super();
-                       this.upload = upload;
-               }
-
-               public void uploadFinished(UploadEvent uploadEvent) {
-                       handleUploadFinished(upload);
-               }
-
-               public void uploadInProgress(UploadEvent uploadEvent) {
-               }
-
-               public void uploadException(UploadEvent uploadEvent) {
-                       Exception exc = uploadEvent.getUploadException();
-                       if (exc != null) {
-                               MessageDialog.openError(Display.getCurrent().getActiveShell(),
-                                               "Error", exc.getMessage());
-                       }
-               }
-       }
 }