Rename packages in order to make future stable documentation clearer.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.rap / src / org / argeo / eclipse / ui / specific / OpenFileService.java
index 2818fc7ea131377d8fa084048d024e93c9c55d13..4a75cc1f6eb20c11f91bf1a8df11f3c798dd2e48 100644 (file)
  */
 package org.argeo.eclipse.ui.specific;
 
-import java.io.File;
+import static org.argeo.eclipse.ui.util.SingleSourcingConstants.FILE_SCHEME;
+import static org.argeo.eclipse.ui.util.SingleSourcingConstants.SCHEME_HOST_SEPARATOR;
+
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.io.FileUtils;
+import org.argeo.eclipse.ui.EclipseUiUtils;
+import org.argeo.eclipse.ui.util.SingleSourcingConstants;
 import org.eclipse.rap.rwt.service.ServiceHandler;
 
 /**
@@ -33,79 +39,73 @@ import org.eclipse.rap.rwt.service.ServiceHandler;
  * Clients might extend to provide context specific services
  */
 public class OpenFileService implements ServiceHandler {
-       public final static String PARAM_FILE_NAME = "param.fileName";
-       public final static String PARAM_FILE_URI = "param.fileURI";
-
-       public final static String SCHEME_HOST_SEPARATOR = "://";
-       public final static String FILE_SCHEME = "file";
-
        public OpenFileService() {
        }
 
-       public void service(HttpServletRequest request, HttpServletResponse response)
-                       throws IOException, ServletException {
-               String fileName = request.getParameter(PARAM_FILE_NAME);
-               String uri = request.getParameter(PARAM_FILE_URI);
+       public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
+               String fileName = request.getParameter(SingleSourcingConstants.PARAM_FILE_NAME);
+               String uri = request.getParameter(SingleSourcingConstants.PARAM_FILE_URI);
+
+               // Use buffered array to directly write the stream?
+               if (!uri.startsWith(SingleSourcingConstants.FILE_SCHEME))
+                       throw new IllegalArgumentException(
+                                       "Open file service can only handle files that are on the server file system");
 
                // Set the Metadata
-               response.setContentLength((int) getFileLength(uri));
-               if (fileName == null || "".equals(fileName.trim()))
+               response.setContentLength((int) getFileSize(uri));
+               if (EclipseUiUtils.isEmpty(fileName))
                        fileName = getFileName(uri);
-               response.setContentType(getMimeTypeFromName(fileName));
+               response.setContentType(getMimeType(uri, 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));
-       }
+               Path path = Paths.get(getAbsPathFromUri(uri));
+               Files.copy(path, response.getOutputStream());
 
-       /**
-        * 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 SingleSourcingException("Error getting the file at "
-                                               + uri, ioe);
-                       }
-               }
-               return null;
+               // FIXME we always use temporary files for the time being.
+               // the deleteOnClose file only works when the JVM is closed so we
+               // explicitly delete to avoid overloading the server
+               if (path.startsWith("/tmp"))
+                       path.toFile().delete();
        }
 
-       protected long getFileLength(String uri) {
-               if (uri.startsWith(FILE_SCHEME)) {
-                       return new File(getFilePathFromUri(uri)).length();
+       protected long getFileSize(String uri) throws IOException {
+               if (uri.startsWith(SingleSourcingConstants.FILE_SCHEME)) {
+                       Path path = Paths.get(getAbsPathFromUri(uri));
+                       return Files.size(path);
                }
                return -1l;
        }
 
        protected String getFileName(String uri) {
-               if (uri.startsWith(FILE_SCHEME)) {
-                       return new File(getFilePathFromUri(uri)).getName();
+               if (uri.startsWith(SingleSourcingConstants.FILE_SCHEME)) {
+                       Path path = Paths.get(getAbsPathFromUri(uri));
+                       return path.getFileName().toString();
                }
                return null;
        }
 
-       private String getFilePathFromUri(String uri) {
-               return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length());
+       private String getAbsPathFromUri(String uri) {
+               if (uri.startsWith(FILE_SCHEME))
+                       return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length());
+               // else if (uri.startsWith(JCR_SCHEME))
+               // return uri.substring((JCR_SCHEME + SCHEME_HOST_SEPARATOR).length());
+               else
+                       throw new IllegalArgumentException("Unknown URI prefix for" + uri);
        }
 
-       /** Overwrite to precise the content type */
-       protected String getMimeTypeFromName(String fileName) {
+       protected String getMimeType(String uri, String fileName) throws IOException {
+               if (uri.startsWith(FILE_SCHEME)) {
+                       Path path = Paths.get(getAbsPathFromUri(uri));
+                       String mimeType = Files.probeContentType(path);
+                       if (EclipseUiUtils.notEmpty(mimeType))
+                               return mimeType;
+               }
                return "application/octet-stream";
        }
-}
\ No newline at end of file
+}