]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileDownloadServiceHandler.java
First try to solve RAP specific file download service issue revealed by the upgrade...
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui.rap / src / main / java / org / argeo / eclipse / ui / specific / FileDownloadServiceHandler.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 /** Provide a basic handler that returns a file from the file system in Rap. */
30 public class FileDownloadServiceHandler implements ServiceHandler {
31 private final String downloadServicehandlerId;
32 public final static String PARAM_FILE_NAME = "param.fileName";
33 public final static String PARAM_FILE_PATH = "param.filePath";
34
35 public FileDownloadServiceHandler(String downloadServicehandlerId) {
36 this.downloadServicehandlerId = downloadServicehandlerId;
37 }
38
39 public void service(HttpServletRequest request, HttpServletResponse response)
40 throws IOException, ServletException {
41 String fileName = request.getParameter(PARAM_FILE_NAME);
42 String path = request.getParameter(PARAM_FILE_PATH);
43
44 // Get the file
45 File file = new File(path);
46
47 // Send the Metadata
48 response.setContentType("application/octet-stream");
49 response.setContentLength((int) file.length());
50 String contentDisposition = "attachment; filename=\"" + fileName + "\"";
51 response.setHeader("Content-Disposition", contentDisposition);
52
53 try {
54 response.getOutputStream().write(
55 FileUtils.readFileToByteArray(new File(path)));
56 } catch (IOException ioe) {
57 throw new ArgeoException("Error while writing the file " + fileName
58 + " to the servlet response", ioe);
59 }
60 }
61
62 public String getDownloadServiceHandlerId(){
63 return downloadServicehandlerId;
64 }
65 }