X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.eclipse.ui.rap%2Fsrc%2Forg%2Fargeo%2Feclipse%2Fui%2Fspecific%2FOpenFileService.java;h=a9302e1948d50765cb8d259b1fedfb78d2b06556;hb=0af549d05ec45b5e31df9026b6627de9038d39eb;hp=e48babf0bba7df111a64f2d8ccf9cf6c57e00c5e;hpb=e71b1737815da9f0fdaff1ee1c69773a8c073ef1;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java b/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java index e48babf0b..a9302e194 100644 --- a/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java +++ b/org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java @@ -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. + * + *

+ * Overwrite to provide application specific abilities, among other to open + * from a JCR repository + *

+ */ 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