X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fruntime%2Forg.argeo.eclipse.ui.rap%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Feclipse%2Fui%2Fspecific%2FFileHandler.java;h=2058328a30570e8187ec1ad37b6fc735c203eb1b;hb=5a00c032d8c7b9617777db83b0a6d60ab6b74d18;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..2058328a3 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,129 @@ 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 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.apache.commons.io; +import org.eclipse.rwt.RWT; public class FileHandler { - private BufferedInputStream bis; + private static Log log = LogFactory.getLog(FileHandler.class); public FileHandler() { } - public File createTmpFile(String fileName, String suffix, InputStream is) { + public void openFile(String fileName, InputStream is) { + + // // Which file to download? + // String fileName = RWT.getRequest().getParameter( "filename" ); + // // Get the file content + // byte[] download = MyDataStore.getByteArrayData( fileName ); + // // 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 e1 ) { + // e1.printStackTrace(); + // } + // + // + + 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); + } + + } + + private File createTmpFile(String prefix, String suffix, InputStream is) { File tmpFile = null; OutputStream os = null; try { - tmpFile = File.createTempFile(fileName, suffix); + tmpFile = File.createTempFile(prefix, suffix); os = new FileOutputStream(tmpFile); IOUtils.copy(is, os); } catch (IOException e) { - throw new ArgeoException("Cannot open file " + fileName, e); + throw new ArgeoException("Cannot open file " + prefix + "." + + suffix, e); } finally { IOUtils.closeQuietly(os); } return tmpFile; } - 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); - } - } - - - 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); - } - } }