]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java
Remove unused ThreadNLS
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.rap / src / org / argeo / eclipse / ui / specific / OpenFileService.java
index e48babf0bba7df111a64f2d8ccf9cf6c57e00c5e..a9302e1948d50765cb8d259b1fedfb78d2b06556 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";
@@ -50,25 +49,39 @@ public class OpenFileService implements ServiceHandler {
                String uri = request.getParameter(PARAM_FILE_URI);
 
                // Set the Metadata
-               response.setContentType("application/octet-stream");
                response.setContentLength((int) getFileLength(uri));
                if (fileName == null || "".equals(fileName.trim()))
                        fileName = getFileName(uri);
+               response.setContentType(getMimeTypeFromName(fileName));
                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;
@@ -92,4 +105,8 @@ public class OpenFileService implements ServiceHandler {
                return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length());
        }
 
+       /** Overwrite to precise the content type */
+       protected String getMimeTypeFromName(String fileName) {
+               return "application/octet-stream";
+       }
 }
\ No newline at end of file