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
;
18 import java
.io
.IOException
;
20 import javax
.servlet
.ServletException
;
21 import javax
.servlet
.http
.HttpServletResponse
;
23 import org
.argeo
.ArgeoException
;
24 import org
.eclipse
.rwt
.RWT
;
25 import org
.eclipse
.rwt
.service
.IServiceHandler
;
27 public class DownloadServiceHandler
implements IServiceHandler
{
29 private FileProvider provider
;
31 public DownloadServiceHandler(FileProvider provider
) {
32 this.provider
= provider
;
35 public void service() throws IOException
, ServletException
{
36 // Which file to download?
37 String fileName
= RWT
.getRequest().getParameter("filename");
38 String fileId
= RWT
.getRequest().getParameter("fileid");
40 // Get the file content
41 byte[] download
= provider
.getByteArrayFileFromId(fileId
);
43 // Send the file in the response
44 HttpServletResponse response
= RWT
.getResponse();
45 response
.setContentType("application/octet-stream");
46 response
.setContentLength(download
.length
);
47 String contentDisposition
= "attachment; filename=\"" + fileName
+ "\"";
48 response
.setHeader("Content-Disposition", contentDisposition
);
50 // Various header fields that can be set to solve some issues with some
53 // String contentType = "application/force-download; name=\"" + fileName
55 // response.setContentType(contentType);
56 // response.setHeader("Content-Transfer-Encoding", "binary");
57 // response.setHeader("Pragma", "no-cache");
58 // response.setHeader("Cache-Control", "no-cache, must-revalidate");
59 // response.setHeader("Expires", "0");
60 // response.setHeader("Connection", "Keep-Alive");
61 // response.setHeader("Keep-Alive", "timeout=5, max=86");
62 // response.setHeader("transfer-Encoding", "chunked");
65 response
.getOutputStream().write(download
);
66 } catch (IOException ioe
) {
67 throw new ArgeoException("Error while writing the file " + fileName
68 + " to the servlet response", ioe
);