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
;
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.
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";
37 public DownloadFsFileService() {
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
);
46 File file
= new File(path
);
49 response
.setContentType("application/octet-stream");
50 response
.setContentLength((int) file
.length());
51 String contentDisposition
= "attachment; filename=\"" + fileName
+ "\"";
52 response
.setHeader("Content-Disposition", contentDisposition
);
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
);