2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.eclipse
.ui
.specific
;
19 import java
.io
.IOException
;
21 import javax
.servlet
.ServletException
;
22 import javax
.servlet
.http
.HttpServletRequest
;
23 import javax
.servlet
.http
.HttpServletResponse
;
25 import org
.apache
.commons
.io
.FileUtils
;
26 import org
.eclipse
.rap
.rwt
.service
.ServiceHandler
;
29 * RWT specific Basic Default service handler that retrieves a file on the
30 * server file system using its absolute path and forwards it to the end user
33 * Clients might extend to provide context specific services
35 public class OpenFileService
implements ServiceHandler
{
36 public final static String PARAM_FILE_NAME
= "param.fileName";
37 public final static String PARAM_FILE_URI
= "param.fileURI";
39 public final static String SCHEME_HOST_SEPARATOR
= "://";
40 public final static String FILE_SCHEME
= "file";
42 public OpenFileService() {
45 public void service(HttpServletRequest request
, HttpServletResponse response
)
46 throws IOException
, ServletException
{
47 String fileName
= request
.getParameter(PARAM_FILE_NAME
);
48 String uri
= request
.getParameter(PARAM_FILE_URI
);
51 response
.setContentLength((int) getFileLength(uri
));
52 if (fileName
== null || "".equals(fileName
.trim()))
53 fileName
= getFileName(uri
);
54 response
.setContentType(getMimeTypeFromName(fileName
));
55 String contentDisposition
= "attachment; filename=\"" + fileName
+ "\"";
56 response
.setHeader("Content-Disposition", contentDisposition
);
58 // Useless for current use
59 // response.setContentType("application/force-download");
60 // response.setHeader("Content-Transfer-Encoding", "binary");
61 // response.setHeader("Pragma", "no-cache");
62 // response.setHeader("Cache-Control", "no-cache, must-revalidate");
64 // TODO use buffered array to directly write the stream?
65 response
.getOutputStream().write(getFileAsByteArray(uri
));
69 * Retrieves the data as Byte Array given an uri.
72 * Overwrite to provide application specific abilities, among other to open
73 * from a JCR repository
76 protected byte[] getFileAsByteArray(String uri
) {
77 if (uri
.startsWith(FILE_SCHEME
)) {
79 return FileUtils
.readFileToByteArray(new File(
80 getFilePathFromUri(uri
)));
81 } catch (IOException ioe
) {
82 throw new SingleSourcingException("Error getting the file at "
89 protected long getFileLength(String uri
) {
90 if (uri
.startsWith(FILE_SCHEME
)) {
91 return new File(getFilePathFromUri(uri
)).length();
96 protected String
getFileName(String uri
) {
97 if (uri
.startsWith(FILE_SCHEME
)) {
98 return new File(getFilePathFromUri(uri
)).getName();
103 private String
getFilePathFromUri(String uri
) {
104 return uri
.substring((FILE_SCHEME
+ SCHEME_HOST_SEPARATOR
).length());
107 /** Overwrite to precise the content type */
108 protected String
getMimeTypeFromName(String fileName
) {
109 return "application/octet-stream";