]> git.argeo.org Git - lgpl/argeo-commons.git/blob - specific/DownloadServiceHandler.java
Prepare next development cycle
[lgpl/argeo-commons.git] / specific / 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 try {
35 response.getOutputStream().write(download);
36 } catch (IOException ioe) {
37 throw new ArgeoException("Error while writing the file " + fileName
38 + " to the servlet response", ioe);
39 }
40 }
41 }