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
.argeo
.ArgeoException
;
27 import org
.eclipse
.rap
.rwt
.service
.ServiceHandler
;
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";
35 public FileDownloadServiceHandler(String downloadServicehandlerId
) {
36 this.downloadServicehandlerId
= downloadServicehandlerId
;
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
);
45 File file
= new File(path
);
48 response
.setContentType("application/octet-stream");
49 response
.setContentLength((int) file
.length());
50 String contentDisposition
= "attachment; filename=\"" + fileName
+ "\"";
51 response
.setHeader("Content-Disposition", contentDisposition
);
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
);
62 public String
getDownloadServiceHandlerId(){
63 return downloadServicehandlerId
;