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