]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.rap/src/org/argeo/eclipse/ui/specific/OpenFileService.java
Add dependency to new project CMS UI RAP.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.rap / src / org / argeo / eclipse / ui / specific / OpenFileService.java
1 package org.argeo.eclipse.ui.specific;
2
3 import static org.argeo.eclipse.ui.util.SingleSourcingConstants.FILE_SCHEME;
4 import static org.argeo.eclipse.ui.util.SingleSourcingConstants.SCHEME_HOST_SEPARATOR;
5
6 import java.io.IOException;
7 import java.nio.file.Files;
8 import java.nio.file.Path;
9 import java.nio.file.Paths;
10
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import org.argeo.eclipse.ui.EclipseUiUtils;
16 import org.argeo.eclipse.ui.util.SingleSourcingConstants;
17 import org.eclipse.rap.rwt.service.ServiceHandler;
18
19 /**
20 * RWT specific Basic Default service handler that retrieves a file on the
21 * server file system using its absolute path and forwards it to the end user
22 * browser.
23 *
24 * Clients might extend to provide context specific services
25 */
26 public class OpenFileService implements ServiceHandler {
27 public OpenFileService() {
28 }
29
30 public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
31 String fileName = request.getParameter(SingleSourcingConstants.PARAM_FILE_NAME);
32 String uri = request.getParameter(SingleSourcingConstants.PARAM_FILE_URI);
33
34 // Use buffered array to directly write the stream?
35 if (!uri.startsWith(SingleSourcingConstants.FILE_SCHEME))
36 throw new IllegalArgumentException(
37 "Open file service can only handle files that are on the server file system");
38
39 // Set the Metadata
40 response.setContentLength((int) getFileSize(uri));
41 if (EclipseUiUtils.isEmpty(fileName))
42 fileName = getFileName(uri);
43 response.setContentType(getMimeType(uri, fileName));
44 String contentDisposition = "attachment; filename=\"" + fileName + "\"";
45 response.setHeader("Content-Disposition", contentDisposition);
46
47 // Useless for current use
48 // response.setHeader("Content-Transfer-Encoding", "binary");
49 // response.setHeader("Pragma", "no-cache");
50 // response.setHeader("Cache-Control", "no-cache, must-revalidate");
51
52 Path path = Paths.get(getAbsPathFromUri(uri));
53 Files.copy(path, response.getOutputStream());
54
55 // FIXME we always use temporary files for the time being.
56 // the deleteOnClose file only works when the JVM is closed so we
57 // explicitly delete to avoid overloading the server
58 if (path.startsWith("/tmp"))
59 path.toFile().delete();
60 }
61
62 protected long getFileSize(String uri) throws IOException {
63 if (uri.startsWith(SingleSourcingConstants.FILE_SCHEME)) {
64 Path path = Paths.get(getAbsPathFromUri(uri));
65 return Files.size(path);
66 }
67 return -1l;
68 }
69
70 protected String getFileName(String uri) {
71 if (uri.startsWith(SingleSourcingConstants.FILE_SCHEME)) {
72 Path path = Paths.get(getAbsPathFromUri(uri));
73 return path.getFileName().toString();
74 }
75 return null;
76 }
77
78 private String getAbsPathFromUri(String uri) {
79 if (uri.startsWith(FILE_SCHEME))
80 return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length());
81 // else if (uri.startsWith(JCR_SCHEME))
82 // return uri.substring((JCR_SCHEME + SCHEME_HOST_SEPARATOR).length());
83 else
84 throw new IllegalArgumentException("Unknown URI prefix for" + uri);
85 }
86
87 protected String getMimeType(String uri, String fileName) throws IOException {
88 if (uri.startsWith(FILE_SCHEME)) {
89 Path path = Paths.get(getAbsPathFromUri(uri));
90 String mimeType = Files.probeContentType(path);
91 if (EclipseUiUtils.notEmpty(mimeType))
92 return mimeType;
93 }
94 return "application/octet-stream";
95 }
96 }