]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/DownloadFsFileService.java
Go on working on the rap specific file download issue. New better name for command...
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui.rap / src / main / java / org / argeo / eclipse / ui / specific / DownloadFsFileService.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 service handler that retrieves a file in the server file system using
31 * an absolute path and forward it to the end user browser. Rap specific.
32 */
33 public class DownloadFsFileService implements ServiceHandler {
34 public final static String PARAM_FILE_NAME = "param.fileName";
35 public final static String PARAM_FILE_PATH = "param.filePath";
36
37 public DownloadFsFileService() {
38 }
39
40 public void service(HttpServletRequest request, HttpServletResponse response)
41 throws IOException, ServletException {
42 String fileName = request.getParameter(PARAM_FILE_NAME);
43 String path = request.getParameter(PARAM_FILE_PATH);
44
45 // Get the file
46 File file = new File(path);
47
48 // Send the Metadata
49 response.setContentType("application/octet-stream");
50 response.setContentLength((int) file.length());
51 String contentDisposition = "attachment; filename=\"" + fileName + "\"";
52 response.setHeader("Content-Disposition", contentDisposition);
53
54 try {
55 response.getOutputStream().write(
56 FileUtils.readFileToByteArray(new File(path)));
57 } catch (IOException ioe) {
58 throw new ArgeoException("Error while writing the file " + fileName
59 + " to the servlet response", ioe);
60 }
61 }
62 }