]> git.argeo.org Git - lgpl/argeo-commons.git/blob - specific/OpenFileService.java
Prepare next development cycle
[lgpl/argeo-commons.git] / specific / 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.argeo.ArgeoException;
27 import org.eclipse.rap.rwt.service.ServiceHandler;
28
29 /**
30 * Basic Default service handler that retrieves a file on the server file system
31 * using its absolute path and forwards it to the end user browser. Rap
32 * specific.
33 *
34 * Clients might extend to provide context specific services (to open files from
35 * a JCR repository for instance)
36 */
37 public class OpenFileService implements ServiceHandler {
38 public final static String PARAM_FILE_NAME = "param.fileName";
39 public final static String PARAM_FILE_URI = "param.fileURI";
40
41 public final static String SCHEME_HOST_SEPARATOR = "://";
42 public final static String FILE_SCHEME = "file";
43
44 public OpenFileService() {
45 }
46
47 public void service(HttpServletRequest request, HttpServletResponse response)
48 throws IOException, ServletException {
49 String fileName = request.getParameter(PARAM_FILE_NAME);
50 String uri = request.getParameter(PARAM_FILE_URI);
51
52 // Set the Metadata
53 response.setContentType("application/octet-stream");
54 response.setContentLength((int) getFileLength(uri));
55 if (fileName == null || "".equals(fileName.trim()))
56 fileName = getFileName(uri);
57 String contentDisposition = "attachment; filename=\"" + fileName + "\"";
58 response.setHeader("Content-Disposition", contentDisposition);
59
60 response.getOutputStream().write(getFileAsByteArray(uri));
61 // FileUtils.readFileToByteArray(new File(path))
62 }
63
64 protected byte[] getFileAsByteArray(String uri) {
65 if (uri.startsWith(FILE_SCHEME)) {
66 try {
67 return FileUtils.readFileToByteArray(new File(
68 getFilePathFromUri(uri)));
69 } catch (IOException ioe) {
70 throw new ArgeoException("Error while getting the file at "
71 + uri, ioe);
72 }
73 }
74 return null;
75 }
76
77 protected long getFileLength(String uri) {
78 if (uri.startsWith(FILE_SCHEME)) {
79 return new File(getFilePathFromUri(uri)).length();
80 }
81 return -1l;
82 }
83
84 protected String getFileName(String uri) {
85 if (uri.startsWith(FILE_SCHEME)) {
86 return new File(getFilePathFromUri(uri)).getName();
87 }
88 return null;
89 }
90
91 private String getFilePathFromUri(String uri) {
92 return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length());
93 }
94
95 }