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