]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java
Update license headers
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui.rap / src / main / java / org / argeo / eclipse / ui / specific / FileHandler.java
index 289cd2b903c412cf762aee3e3a7c050dc1430200..ad551b47939e657625c8622af6c8830040262ad5 100644 (file)
@@ -1,6 +1,20 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * 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 java.net.URL;
 
 import org.apache.commons.logging.Log;
@@ -20,10 +34,9 @@ import org.eclipse.ui.PlatformUI;
  * Manages the process of forwarding the request to the handler at runtime to
  * open the dialog box
  * 
- * @author bsinou
- * 
  */
 public class FileHandler {
+       public final static String FORCED_DOWNLOAD_URL_BASE_PROPERTY = "argeo.rap.specific.forcedDownloadUrlBase";
 
        private final static Log log = LogFactory.getLog(FileHandler.class);
 
@@ -32,152 +45,45 @@ public class FileHandler {
                IServiceManager manager = RWT.getServiceManager();
                IServiceHandler handler = new DownloadServiceHandler(provider);
                manager.registerServiceHandler("downloadServiceHandler", handler);
-
        }
 
-       public void openFile(String fileName, String fileId, InputStream is) {
+       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 {
-                       if (log.isDebugEnabled())
-                               log.debug("URL : " + createFullDownloadUrl(fileName, fileId));
+                       if (log.isTraceEnabled())
+                               log.trace("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" );
-               // // 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);
-                * 
-                * 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); }
-                */
-
+               // 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 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());
+               // 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();
        }