1 package org
.argeo
.eclipse
.ui
.specific
;
3 import static org
.argeo
.eclipse
.ui
.util
.SingleSourcingConstants
.FILE_SCHEME
;
4 import static org
.argeo
.eclipse
.ui
.util
.SingleSourcingConstants
.SCHEME_HOST_SEPARATOR
;
6 import java
.io
.IOException
;
7 import java
.nio
.file
.Files
;
8 import java
.nio
.file
.Path
;
9 import java
.nio
.file
.Paths
;
11 import javax
.servlet
.ServletException
;
12 import javax
.servlet
.http
.HttpServletRequest
;
13 import javax
.servlet
.http
.HttpServletResponse
;
15 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
16 import org
.argeo
.eclipse
.ui
.util
.SingleSourcingConstants
;
17 import org
.eclipse
.rap
.rwt
.service
.ServiceHandler
;
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
24 * Clients might extend to provide context specific services
26 public class OpenFileService
implements ServiceHandler
{
27 public OpenFileService() {
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
);
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");
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
);
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");
52 Path path
= Paths
.get(getAbsPathFromUri(uri
));
53 Files
.copy(path
, response
.getOutputStream());
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();
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
);
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();
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());
84 throw new IllegalArgumentException("Unknown URI prefix for" + uri
);
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
))
94 return "application/octet-stream";