X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=eclipse%2Fruntime%2Forg.argeo.eclipse.ui.rap%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fui%2Fspecific%2FFileHandler.java;h=aba79c1c1912a7abfe1721620e3c051135680005;hb=70afc8f7860266f1ac93c852d552ee0971e93c15;hp=8948e73c7a90a5b86d3e1a6a62b798a767b4707e;hpb=5f0b7679ef0cbcb1b101f71673f1e5e24e735b39;p=lgpl%2Fargeo-commons.git diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java index 8948e73c7..aba79c1c1 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java @@ -1,75 +1,88 @@ package org.argeo.eclipse.ui.specific; -import java.awt.Desktop; -import java.io.BufferedInputStream; -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 org.apache.commons.io.IOUtils; -import org.argeo.ArgeoException; - -//import org.apache.commons.io; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +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 + * + */ public class FileHandler { + public final static String FORCED_DOWNLOAD_URL_BASE_PROPERTY = "argeo.rap.specific.forcedDownloadUrlBase"; - private BufferedInputStream bis; + private final static Log log = LogFactory.getLog(FileHandler.class); - public FileHandler() { + public FileHandler(FileProvider provider) { + // Instantiate and register the DownloadServicHandler. + IServiceManager manager = RWT.getServiceManager(); + IServiceHandler handler = new DownloadServiceHandler(provider); + manager.registerServiceHandler("downloadServiceHandler", handler); } - public File createTmpFile(String fileName, String suffix, InputStream is) { - File tmpFile = null; - OutputStream os = null; + public void openFile(String fileName, String fileId) { + + // See RAP FAQ: + // http://wiki.eclipse.org/RAP/FAQ#How_to_provide_download_link.3F + // And forum discussion : + // http://www.eclipse.org/forums/index.php?t=msg&th=205487&start=0&S=43d85dacc88b505402420592109c7240 + try { - tmpFile = File.createTempFile(fileName, suffix); - os = new FileOutputStream(tmpFile); - IOUtils.copy(is, os); - } catch (IOException e) { - throw new ArgeoException("Cannot open file " + fileName, e); - } finally { - IOUtils.closeQuietly(os); + if (log.isTraceEnabled()) + log.trace("URL : " + createFullDownloadUrl(fileName, fileId)); + + URL url = new URL(createFullDownloadUrl(fileName, fileId)); + PlatformUI.getWorkbench().getBrowserSupport() + .createBrowser("DownloadDialog").openURL(url); + } catch (Exception e) { + e.printStackTrace(); } - return tmpFile; + + // 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"); } - public void openFile(String fileName, InputStream is) { - - String prefix ="", extension = ""; - - if (fileName != null){ - int ind = fileName. - if (true){ - } - } - - prefix = .substring(0, - node.getName().lastIndexOf('.')); - extension = node.getName().substring( - node.getName().lastIndexOf('.')); - try { - Desktop desktop = null; - if (Desktop.isDesktopSupported()) { - desktop = Desktop.getDesktop(); - } - desktop.open(file); - } catch (IOException e) { - throw new ArgeoException("Cannot open file " + file.getName(), e); - } + private String createFullDownloadUrl(String fileName, String fileId) { + StringBuilder url = new StringBuilder(); + // in case RAP is proxied we need to specify the actual base URL + // TODO find a cleaner way + String forcedDownloadUrlBase = System + .getProperty(FORCED_DOWNLOAD_URL_BASE_PROPERTY); + if (forcedDownloadUrlBase != null) + url.append(forcedDownloadUrlBase); + else + url.append(RWT.getRequest().getRequestURL()); + url.append(createParamUrl(fileName, fileId)); + return url.toString(); } - - - public void openFile(File file) { - try { - Desktop desktop = null; - if (Desktop.isDesktopSupported()) { - desktop = Desktop.getDesktop(); - } - desktop.open(file); - } catch (IOException e) { - throw new ArgeoException("Cannot open file " + file.getName(), e); - } + + 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; } }