]> git.argeo.org Git - lgpl/argeo-commons.git/blob - OpenFileService.java
2818fc7ea131377d8fa084048d024e93c9c55d13
[lgpl/argeo-commons.git] / OpenFileService.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.eclipse.ui.specific;
17
18 import java.io.File;
19 import java.io.IOException;
20
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.commons.io.FileUtils;
26 import org.eclipse.rap.rwt.service.ServiceHandler;
27
28 /**
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
31 * browser.
32 *
33 * Clients might extend to provide context specific services
34 */
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";
38
39 public final static String SCHEME_HOST_SEPARATOR = "://";
40 public final static String FILE_SCHEME = "file";
41
42 public OpenFileService() {
43 }
44
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);
49
50 // Set the Metadata
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);
57
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");
63
64 // TODO use buffered array to directly write the stream?
65 response.getOutputStream().write(getFileAsByteArray(uri));
66 }
67
68 /**
69 * Retrieves the data as Byte Array given an uri.
70 *
71 * <p>
72 * Overwrite to provide application specific abilities, among other to open
73 * from a JCR repository
74 * </p>
75 */
76 protected byte[] getFileAsByteArray(String uri) {
77 if (uri.startsWith(FILE_SCHEME)) {
78 try {
79 return FileUtils.readFileToByteArray(new File(
80 getFilePathFromUri(uri)));
81 } catch (IOException ioe) {
82 throw new SingleSourcingException("Error getting the file at "
83 + uri, ioe);
84 }
85 }
86 return null;
87 }
88
89 protected long getFileLength(String uri) {
90 if (uri.startsWith(FILE_SCHEME)) {
91 return new File(getFilePathFromUri(uri)).length();
92 }
93 return -1l;
94 }
95
96 protected String getFileName(String uri) {
97 if (uri.startsWith(FILE_SCHEME)) {
98 return new File(getFilePathFromUri(uri)).getName();
99 }
100 return null;
101 }
102
103 private String getFilePathFromUri(String uri) {
104 return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length());
105 }
106
107 /** Overwrite to precise the content type */
108 protected String getMimeTypeFromName(String fileName) {
109 return "application/octet-stream";
110 }
111 }