Adapt file upload/download to latest RWT version.
authorBruno Sinou <bsinou@argeo.org>
Tue, 10 Feb 2015 20:22:24 +0000 (20:22 +0000)
committerBruno Sinou <bsinou@argeo.org>
Tue, 10 Feb 2015 20:22:24 +0000 (20:22 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@7784 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java [deleted file]
org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/FileHandler.java [deleted file]
org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/GenericUploadControl.java [deleted file]
org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/ImportToServerWizardPage.java [deleted file]
org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFile.java
org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java
org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/ThreadNLS.java
org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/UploadFileWizardPage.java
org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/internal/GenericNodeDoubleClickListener.java
org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/internal/parts/ImportFileSystemWizard.java

diff --git a/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java b/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java
deleted file mode 100644 (file)
index 4173b96..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *         http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.argeo.eclipse.ui.specific;
-
-import java.io.IOException;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.argeo.ArgeoException;
-import org.argeo.eclipse.ui.FileProvider;
-import org.eclipse.rap.rwt.service.ServiceHandler;
-
-@Deprecated
-public class DownloadServiceHandler implements ServiceHandler {
-
-       private FileProvider provider;
-
-       public DownloadServiceHandler(FileProvider provider) {
-               this.provider = provider;
-       }
-
-       public void service(HttpServletRequest request, HttpServletResponse response)
-                       throws IOException, ServletException {
-               // Which file to download?
-               String fileName = request.getParameter("filename");
-               String fileId = request.getParameter("fileid");
-
-               // Get the file content
-               byte[] download = provider.getByteArrayFileFromId(fileId);
-
-               // Send the file in the response
-               response.setContentType("application/octet-stream");
-               response.setContentLength(download.length);
-               String contentDisposition = "attachment; filename=\"" + fileName + "\"";
-               response.setHeader("Content-Disposition", contentDisposition);
-
-               // Various header fields that can be set to solve some issues with some
-               // old browsers.
-               // Unused.
-               // String contentType = "application/force-download; name=\"" + fileName
-               // + "\"";
-               // response.setContentType(contentType);
-               // response.setHeader("Content-Transfer-Encoding", "binary");
-               // response.setHeader("Pragma", "no-cache");
-               // response.setHeader("Cache-Control", "no-cache, must-revalidate");
-               // response.setHeader("Expires", "0");
-               // response.setHeader("Connection", "Keep-Alive");
-               // response.setHeader("Keep-Alive", "timeout=5, max=86");
-               // response.setHeader("transfer-Encoding", "chunked");
-
-               try {
-                       response.getOutputStream().write(download);
-               } catch (IOException ioe) {
-                       throw new ArgeoException("Error while writing the file " + fileName
-                                       + " to the servlet response", ioe);
-               }
-       }
-}
diff --git a/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/FileHandler.java b/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/FileHandler.java
deleted file mode 100644 (file)
index 6a28c0a..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *         http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.argeo.eclipse.ui.specific;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.argeo.ArgeoException;
-import org.argeo.eclipse.ui.FileProvider;
-import org.eclipse.rap.rwt.RWT;
-import org.eclipse.rap.rwt.client.service.UrlLauncher;
-import org.eclipse.rap.rwt.service.ServiceHandler;
-
-/**
- * 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 encodedURL
- */
-@Deprecated
-public class FileHandler {
-       public final static String DOWNLOAD_SERVICE_NAME = "argeo.rap.download.service";
-       private final static Log log = LogFactory.getLog(FileHandler.class);
-
-       public FileHandler(FileProvider provider) {
-               ServiceHandler handler = new DownloadServiceHandler(provider);
-               try {
-                       RWT.getServiceManager().registerServiceHandler(
-                                       DOWNLOAD_SERVICE_NAME, handler);
-               } catch (IllegalArgumentException iae) {
-                       log.warn("Handler is already registered, clean this registering process");
-               }
-       }
-
-       public void openFile(String fileName, String fileId) {
-               try {
-                       String downloadUrl = RWT.getServiceManager().getServiceHandlerUrl(
-                                       DOWNLOAD_SERVICE_NAME)
-                                       + createParamUrl(fileName, fileId);
-                       if (log.isTraceEnabled())
-                               log.debug("URL : " + downloadUrl);
-                       UrlLauncher launcher = RWT.getClient()
-                                       .getService(UrlLauncher.class);
-                       launcher.openURL(downloadUrl);
-               } catch (Exception e) {
-                       throw new ArgeoException("Unable to open file " + fileName, e);
-               }
-               // These lines are useless in the current use case but might be
-               // necessary with new browsers. Stored here for memo
-               // 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");
-       }
-
-       private String createParamUrl(String filename, String fileId) {
-               StringBuilder url = new StringBuilder();
-               url.append("&filename=");
-               url.append(filename);
-               url.append("&fileid=");
-               url.append(fileId);
-               return url.toString();
-       }
-}
\ No newline at end of file
diff --git a/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/GenericUploadControl.java b/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/GenericUploadControl.java
deleted file mode 100644 (file)
index e3b3198..0000000
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *         http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.argeo.eclipse.ui.specific;
-
-import java.io.InputStream;
-
-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.rap.rwt.widgets.Upload;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.ModifyListener;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Composite;
-
-public class GenericUploadControl extends Composite {
-       private final static Log log = LogFactory
-                       .getLog(GenericUploadControl.class);
-
-       //private Upload upload;
-
-       public GenericUploadControl(Composite parent, int style, String browseLabel) {
-               super(parent, style);
-               createControl(this, browseLabel);
-
-       }
-
-       private void createControl(Composite parent, String browseLabel) {
-               parent.setLayout(new GridLayout(1, false));
-               parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-
-//             upload = new Upload(parent, SWT.BORDER);
-//             upload.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-//             upload.setBrowseButtonText(browseLabel);
-               // upload.addModifyListener(new UploadListener());
-               parent.pack();
-       }
-
-       /**
-        * Wrap upload.getLastFileUploaded(). Gets the name of the last uploaded
-        * file. This method can be called even if the upload has not finished yet.
-        */
-       public String getLastFileUploadedName() {
-               return "";
-       }
-
-       public boolean isControlEmpty() {
-               String path = "";
-               if (log.isTraceEnabled())
-                       log.trace("UploadControl chosen path : " + path);
-               if (path == null || "".equals(path.trim()))
-                       return true;
-               else
-                       return false;
-       }
-
-       public byte[] performUpload() {
-//             boolean success = upload.performUpload();
-//             if (success) {
-//                     if (upload.getUploadItem().getFileSize() == -1)
-//                             throw new ArgeoException("File "
-//                                             + upload.getUploadItem().getFileName()
-//                                             + " has not been uploaded, its size is -1");
-//
-//                     InputStream inStream = null;
-//                     byte[] fileBA = null;
-//                     try {
-//                             inStream = upload.getUploadItem().getFileInputStream();
-//                             fileBA = IOUtils.toByteArray(inStream);
-//                     } catch (Exception e) {
-//                             throw new ArgeoException("Cannot read uploaded data", e);
-//                     } finally {
-//                             IOUtils.closeQuietly(inStream);
-//                     }
-//                     return fileBA;
-//             }
-               return null;
-       }
-
-       public void addModifyListener(ModifyListener listener) {
-//             upload.addModifyListener(listener);
-       }
-
-       // 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());
-       // }
-       // }
-       //
-       // }
-       //
-
-}
diff --git a/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/ImportToServerWizardPage.java b/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/ImportToServerWizardPage.java
deleted file mode 100644 (file)
index f66a279..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *         http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.argeo.eclipse.ui.specific;
-
-import java.io.InputStream;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.argeo.ArgeoException;
-import org.eclipse.jface.wizard.WizardPage;
-//import org.eclipse.rap.rwt.widgets.Upload;
-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.Label;
-
-public class ImportToServerWizardPage extends WizardPage {
-       private final static Log log = LogFactory
-                       .getLog(ImportToServerWizardPage.class);
-
-       public final static String FILE_ITEM_TYPE = "FILE";
-       public final static String FOLDER_ITEM_TYPE = "FOLDER";
-
-       //private Upload uploadFile;
-
-       public ImportToServerWizardPage() {
-               super("Import from file system");
-               setDescription("Import files from the local file system to the server");
-       }
-
-       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...");
-               setControl(composite);
-       }
-
-       public String getObjectPath() {
-               // NOTE Returns the full file name of the last uploaded file including
-               // the file path as selected by the user on his local machine.
-               // The full path including the directory and file drive are only
-               // returned, if the browser supports reading this properties. In Firefox
-               // 3, only the filename is returned.
-               return null;
-       }
-
-       public String getObjectName() {
-               return null;
-       }
-
-       public String getObjectType() {
-               return FILE_ITEM_TYPE;
-       }
-
-       public void performFinish() {
-//             boolean success = uploadFile.performUpload();
-//             if (!success)
-//                     throw new ArgeoException("Cannot upload file named "
-//                                     + uploadFile.getPath());
-       }
-
-//     protected void handleUploadFinished(final Upload upload) {
-//     }
-
-       public InputStream getFileInputStream() {
-               return null;
-       }
-
-       public boolean getNeedsProgressMonitor() {
-               return false;
-       }
-
-}
index db65ee785d5cd170cec57b1ad26fefa051a42acf..409b96dedca031f97b5de91b4c54e8a654a1d24d 100644 (file)
@@ -24,19 +24,25 @@ import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.client.service.UrlLauncher;
 
 /**
- * Rap specific command handler to open a file retrieved from the server. It
+ * RWT specific command handler to open a file retrieved from the server. It
  * forwards the request to the correct service after encoding file name and path
  * in the request URI.
  * 
+ * <p>
  * The parameter "URI" is used to determine the correct file service, the path
  * and the file name. An optional file name can be precized to present a
  * different file name as the one used to retrieve it to the end user.
+ * </p>
  * 
+ * <p>
  * Various instances of this handler with different command ID might coexist in
  * order to provide context specific download service.
+ * </p>
  * 
+ * <p>
  * The instance specific service is called by its ID and must have been
  * externally created
+ * </p>
  */
 public class OpenFile extends AbstractHandler {
        private final static Log log = LogFactory.getLog(OpenFile.class);
@@ -51,7 +57,7 @@ public class OpenFile extends AbstractHandler {
                String fileName = event.getParameter(PARAM_FILE_NAME);
                String fileUri = event.getParameter(PARAM_FILE_URI);
 
-               // sanity check
+               // Sanity check
                if (fileUri == null || "".equals(fileUri.trim())
                                || openFileServiceId == null
                                || "".equals(openFileServiceId.trim()))
index e48babf0bba7df111a64f2d8ccf9cf6c57e00c5e..926c8aaaccc72bfc369150925abe0fd812024e74 100644 (file)
@@ -27,12 +27,11 @@ import org.argeo.ArgeoException;
 import org.eclipse.rap.rwt.service.ServiceHandler;
 
 /**
- * Basic Default service handler that retrieves a file on the server file system
- * using its absolute path and forwards it to the end user browser. Rap
- * specific.
+ * RWT specific Basic Default service handler that retrieves a file on the
+ * server file system using its absolute path and forwards it to the end user
+ * browser.
  * 
- * Clients might extend to provide context specific services (to open files from
- * a JCR repository for instance)
+ * Clients might extend to provide context specific services
  */
 public class OpenFileService implements ServiceHandler {
        public final static String PARAM_FILE_NAME = "param.fileName";
@@ -57,18 +56,32 @@ public class OpenFileService implements ServiceHandler {
                String contentDisposition = "attachment; filename=\"" + fileName + "\"";
                response.setHeader("Content-Disposition", contentDisposition);
 
+               // Useless for current use
+               // response.setContentType("application/force-download");
+               // response.setHeader("Content-Transfer-Encoding", "binary");
+               // response.setHeader("Pragma", "no-cache");
+               // response.setHeader("Cache-Control", "no-cache, must-revalidate");
+
+               // TODO use buffered array to directly write the stream?
                response.getOutputStream().write(getFileAsByteArray(uri));
-               // FileUtils.readFileToByteArray(new File(path))
        }
 
+       /**
+        * Retrieves the data as Byte Array given an uri.
+        * 
+        * <p>
+        * Overwrite to provide application specific abilities, among other to open
+        * from a JCR repository
+        * </p>
+        */
        protected byte[] getFileAsByteArray(String uri) {
                if (uri.startsWith(FILE_SCHEME)) {
                        try {
                                return FileUtils.readFileToByteArray(new File(
                                                getFilePathFromUri(uri)));
                        } catch (IOException ioe) {
-                               throw new ArgeoException("Error while getting the file at "
-                                               + uri, ioe);
+                               throw new ArgeoException("Error getting the file at " + uri,
+                                               ioe);
                        }
                }
                return null;
@@ -91,5 +104,4 @@ public class OpenFileService implements ServiceHandler {
        private String getFilePathFromUri(String uri) {
                return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length());
        }
-
 }
\ No newline at end of file
index 8b8837f2ee226c14cc3cf52cc53a1da74f7527d0..f6aaa15ec62ef999e3c837f137027e88b48a7d4e 100644 (file)
@@ -37,7 +37,6 @@ public class ThreadNLS<T extends NLS> extends InheritableThreadLocal<T> {
                this(DEFAULT_BUNDLE_LOCATION, type, false);
        }
 
-       @SuppressWarnings("unchecked")
        @Override
        protected T initialValue() {
                if (utf8)
index 17d4e22f3f0c2df1c16065ab8f98fedfed875e6b..8c8498fe6bba9ab41bc2a367b486c112099749ee 100644 (file)
  */
 package org.argeo.eclipse.ui.specific;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 
 import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.rap.addons.fileupload.FileDetails;
+import org.eclipse.rap.addons.fileupload.FileUploadEvent;
+import org.eclipse.rap.addons.fileupload.FileUploadHandler;
+import org.eclipse.rap.addons.fileupload.FileUploadListener;
+import org.eclipse.rap.addons.fileupload.FileUploadReceiver;
+import org.eclipse.rap.rwt.service.ServerPushSession;
 import org.eclipse.rap.rwt.widgets.FileUpload;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
 
+/**
+ * RWT Specific convenience page that provides a simple interface to upload one
+ * file in a wizard context
+ * 
+ * TODO Finalize clean and refactoring using the latest rap version and upload
+ * dialog addons
+ *
+ */
 public class UploadFileWizardPage extends WizardPage {
        // private final static Log log = LogFactory
        // .getLog(UploadFileWizardPage.class);
@@ -32,7 +53,11 @@ public class UploadFileWizardPage extends WizardPage {
        public final static String FILE_ITEM_TYPE = "FILE";
        public final static String FOLDER_ITEM_TYPE = "FOLDER";
 
+       private File file;
+
        private FileUpload fileUpload;
+       private ServerPushSession pushSession;
+       private Label fileNameLabel;
 
        public UploadFileWizardPage() {
                super("Import from file system");
@@ -41,21 +66,117 @@ public class UploadFileWizardPage extends WizardPage {
 
        public void createControl(Composite parent) {
                Composite composite = new Composite(parent, SWT.NONE);
-               composite.setLayout(new GridLayout(2, false));
+               composite.setLayout(new GridLayout(3, false));
                composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
                new Label(composite, SWT.NONE).setText("Pick up a file");
-               fileUpload = new FileUpload(composite, SWT.BORDER);
-               fileUpload.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-               fileUpload.setText("Browse");
+
+               fileNameLabel = new Label(composite, SWT.NONE | SWT.BEGINNING);
+               fileNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
+                               false));
+
+               fileUpload = new FileUpload(composite, SWT.NONE);
+               fileUpload.setText("Browse...");
+               fileUpload.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
+                               false));
+
+               final String url = startUploadReceiver();
+               pushSession = new ServerPushSession();
+
+               fileUpload.addSelectionListener(new SelectionAdapter() {
+                       private static final long serialVersionUID = 1L;
+
+                       @Override
+                       public void widgetSelected(SelectionEvent e) {
+                               String fileName = fileUpload.getFileName();
+                               fileNameLabel.setText(fileName == null ? "" : fileName);
+                               pushSession.start();
+                               fileUpload.submit(url);
+                       }
+               });
+
                setControl(composite);
        }
 
+       public void performFinish() {
+               // boolean success = uploadFile.performUpload();
+               // if (!success)
+               // throw new ArgeoException("Cannot upload file named "
+               // + uploadFile.getPath());
+       }
+
+       private String startUploadReceiver() {
+               MyFileUploadReceiver receiver = new MyFileUploadReceiver();
+               FileUploadHandler uploadHandler = new FileUploadHandler(receiver);
+               uploadHandler.addUploadListener(new FileUploadListener() {
+
+                       public void uploadProgress(FileUploadEvent event) {
+                               // handle upload progress
+                       }
+
+                       public void uploadFailed(FileUploadEvent event) {
+                               UploadFileWizardPage.this.setErrorMessage("upload failed: "
+                                               + event.getException());
+                       }
+
+                       public void uploadFinished(FileUploadEvent event) {
+
+                               fileNameLabel.getDisplay().asyncExec(new Runnable() {
+                                       public void run() {
+                                               // UploadFileWizardPage.this.getContainer()
+                                               // .updateButtons();
+                                               pushSession.stop();
+                                       }
+                               });
+
+                               // for (FileDetails file : event.getFileDetails()) {
+                               // // addToLog("received: " + file.getFileName());
+                               // }
+                       }
+               });
+               return uploadHandler.getUploadUrl();
+       }
+
+       private class MyFileUploadReceiver extends FileUploadReceiver {
+
+               private static final String TEMP_FILE_PREFIX = "fileupload_";
+
+               @Override
+               public void receive(InputStream dataStream, FileDetails details)
+                               throws IOException {
+                       File result = File.createTempFile(TEMP_FILE_PREFIX, "");
+                       FileOutputStream outputStream = new FileOutputStream(result);
+                       try {
+                               copy(dataStream, outputStream);
+                       } finally {
+                               dataStream.close();
+                               outputStream.close();
+                       }
+                       file = result;
+               }
+       }
+
+       private static void copy(InputStream inputStream, OutputStream outputStream)
+                       throws IOException {
+               byte[] buffer = new byte[8192];
+               boolean finished = false;
+               while (!finished) {
+                       int bytesRead = inputStream.read(buffer);
+                       if (bytesRead != -1) {
+                               outputStream.write(buffer, 0, bytesRead);
+                       } else {
+                               finished = true;
+                       }
+               }
+       }
+
+       /**
+        * The full path including the directory and file drive are only returned,
+        * if the browser supports reading this properties
+        * 
+        * @return The full file name of the last uploaded file including the file
+        *         path as selected by the user on his local machine.
+        */
        public String getObjectPath() {
-               // NOTE Returns the full file name of the last uploaded file including
-               // the file path as selected by the user on his local machine.
-               // The full path including the directory and file drive are only
-               // returned, if the browser supports reading this properties. In Firefox
-               // 3, only the filename is returned.
                return null;
        }
 
@@ -67,22 +188,25 @@ public class UploadFileWizardPage extends WizardPage {
                return FILE_ITEM_TYPE;
        }
 
-       public void performFinish() {
-               // boolean success = uploadFile.performUpload();
-               // if (!success)
-               // throw new ArgeoException("Cannot upload file named "
-               // + uploadFile.getPath());
-       }
-
        // protected void handleUploadFinished(final Upload upload) {
        // }
 
-       public InputStream getFileInputStream() {
-               return null;
+       /** it is caller responsability to close the stream afterwards. */
+       public InputStream getFileInputStream() throws IOException {
+               return new FileInputStream(file);
+               // InputStream fis = null;
+               //
+               // try {
+               // fis = new FileInputStream(file);
+               // return fis;
+               // } catch (Exception e) {
+               // throw new ArgeoException("Unable to retrieve file " + file, e);
+               // } finally {
+               // IOUtils.closeQuietly(fis);
+               // }
        }
 
        public boolean getNeedsProgressMonitor() {
                return false;
        }
-
-}
+}
\ No newline at end of file
index 0a93bce61e4f8a18fe5ea6a7c6ecd0ad9f6a76a8..23c9b8eed913db02214830c074cb0dc8bc532029 100644 (file)
@@ -20,8 +20,6 @@ import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.NodeType;
 
 import org.argeo.ArgeoException;
-import org.argeo.eclipse.ui.jcr.utils.JcrFileProvider;
-import org.argeo.eclipse.ui.specific.FileHandler;
 import org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin;
 import org.argeo.eclipse.ui.workbench.jcr.DefaultNodeEditor;
 import org.argeo.eclipse.ui.workbench.jcr.internal.model.RepositoryElem;
@@ -43,14 +41,15 @@ public class GenericNodeDoubleClickListener implements IDoubleClickListener {
        // .getLog(GenericNodeDoubleClickListener.class);
 
        private TreeViewer nodeViewer;
-       private JcrFileProvider jfp;
-       private FileHandler fileHandler;
+
+       // private JcrFileProvider jfp;
+       // private FileHandler fileHandler;
 
        public GenericNodeDoubleClickListener(TreeViewer nodeViewer) {
                this.nodeViewer = nodeViewer;
-               jfp = new JcrFileProvider();
+               // jfp = new JcrFileProvider();
                // Commented out. see https://www.argeo.org/bugzilla/show_bug.cgi?id=188
-               fileHandler = null;
+               // fileHandler = null;
                // fileHandler = new FileHandler(jfp);
        }
 
@@ -81,6 +80,16 @@ public class GenericNodeDoubleClickListener implements IDoubleClickListener {
                                        String name = node.getName();
                                        String id = node.getIdentifier();
 
+                                       // TODO add integration of direct retrieval of the binary in
+                                       // a JCR repo.
+                                       // Map<String, String> params = new HashMap<String,
+                                       // String>();
+                                       // params.put(OpenFile.PARAM_FILE_NAME, name);
+                                       // params.put(OpenFile.PARAM_FILE_URI, "jcr://" + id);
+                                       // CommandUtils
+                                       // .callCommand("org.argeo.security.ui.specific.openFile",
+                                       // params);
+
                                        // For the file provider to be able to browse the
                                        // various
                                        // repository.
@@ -88,9 +97,9 @@ public class GenericNodeDoubleClickListener implements IDoubleClickListener {
                                        // ITreeContentProvider itcp = (ITreeContentProvider)
                                        // nodeViewer
                                        // .getContentProvider();
-                                       jfp.setReferenceNode(node);
-                                       if (fileHandler != null)
-                                               fileHandler.openFile(name, id);
+                                       // jfp.setReferenceNode(node);
+                                       // if (fileHandler != null)
+                                       // fileHandler.openFile(name, id);
                                }
                                GenericNodeEditorInput gnei = new GenericNodeEditorInput(node);
                                WorkbenchUiPlugin.getDefault().getWorkbench()
index ea3bf56c71ac6131ec64d66d3bdd92792ffe7c77..eea9f72c3ffc9ec5abd3690f691aab0d9ffad8be 100644 (file)
@@ -17,6 +17,7 @@ package org.argeo.eclipse.ui.workbench.jcr.internal.parts;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.InputStream;
 
 import javax.jcr.Binary;
 import javax.jcr.Node;
@@ -28,7 +29,6 @@ import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.ArgeoException;
-import org.argeo.eclipse.ui.specific.ImportToServerWizardPage;
 import org.argeo.eclipse.ui.specific.UploadFileWizardPage;
 import org.argeo.eclipse.ui.workbench.ErrorFeedback;
 import org.eclipse.core.runtime.IProgressMonitor;
@@ -67,23 +67,25 @@ public class ImportFileSystemWizard extends Wizard {
                final String objectPath = importPage.getObjectPath();
 
                // We do not display a progress bar for one file only
-               if (ImportToServerWizardPage.FILE_ITEM_TYPE.equals(objectType)) {
+               if (UploadFileWizardPage.FILE_ITEM_TYPE.equals(objectType)) {
                        // In Rap we must force the "real" upload of the file
-                       importPage.performFinish();
+                       // importPage.performFinish();
                        try {
                                Node fileNode = folder.addNode(importPage.getObjectName(),
                                                NodeType.NT_FILE);
                                Node resNode = fileNode.addNode(Property.JCR_CONTENT,
                                                NodeType.NT_RESOURCE);
                                Binary binary = null;
+                               InputStream is = null;
                                try {
+                                       is = importPage.getFileInputStream();
                                        binary = folder.getSession().getValueFactory()
-                                                       .createBinary(importPage.getFileInputStream());
+                                                       .createBinary(is);
                                        resNode.setProperty(Property.JCR_DATA, binary);
                                } finally {
                                        if (binary != null)
                                                binary.dispose();
-                                       IOUtils.closeQuietly(importPage.getFileInputStream());
+                                       IOUtils.closeQuietly(is);
                                }
                                folder.getSession().save();
                        } catch (Exception e) {
@@ -91,7 +93,7 @@ public class ImportFileSystemWizard extends Wizard {
                                return false;
                        }
                        return true;
-               } else if (ImportToServerWizardPage.FOLDER_ITEM_TYPE.equals(objectType)) {
+               } else if (UploadFileWizardPage.FOLDER_ITEM_TYPE.equals(objectType)) {
                        if (objectPath == null || !new File(objectPath).exists()) {
                                ErrorFeedback.show("Directory " + objectPath
                                                + " does not exist");