]> git.argeo.org Git - lgpl/argeo-commons.git/blob - DownloadServiceHandler.java
0c7eac67f1415f702127d33307c4a499a1d5239b
[lgpl/argeo-commons.git] / DownloadServiceHandler.java
1 package org.argeo.eclipse.ui.specific;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.argeo.ArgeoException;
9 import org.eclipse.rwt.RWT;
10 import org.eclipse.rwt.service.IServiceHandler;
11
12 public class DownloadServiceHandler implements IServiceHandler {
13
14 private FileProvider provider;
15
16 public DownloadServiceHandler(FileProvider provider) {
17 this.provider = provider;
18 }
19
20 public void service() throws IOException, ServletException {
21 // Which file to download?
22 String fileName = RWT.getRequest().getParameter("filename");
23 String fileId = RWT.getRequest().getParameter("fileid");
24
25 // Get the file content
26 byte[] download = provider.getByteArrayFileFromId(fileId);
27
28 // Send the file in the response
29 HttpServletResponse response = RWT.getResponse();
30 response.setContentType("application/octet-stream");
31 response.setContentLength(download.length);
32 String contentDisposition = "attachment; filename=\"" + fileName + "\"";
33 response.setHeader("Content-Disposition", contentDisposition);
34 // response.setHeader( "Cache-Control", "no-cache" );
35
36 try {
37 response.getOutputStream().write(download);
38 } catch (IOException ioe) {
39 throw new ArgeoException("Error while writing the file " + fileName
40 + " to the servlet response", ioe);
41 }
42 }
43 }