]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java
Go on working on the rap specific file download issue. New better name for command...
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui.rap / src / main / java / org / argeo / eclipse / ui / specific / DownloadServiceHandler.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.eclipse.ui.specific;
17
18 import java.io.IOException;
19
20 import javax.servlet.ServletException;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.argeo.ArgeoException;
25 import org.eclipse.rap.rwt.service.ServiceHandler;
26
27 @Deprecated
28 public class DownloadServiceHandler implements ServiceHandler {
29
30 private FileProvider provider;
31
32 public DownloadServiceHandler(FileProvider provider) {
33 this.provider = provider;
34 }
35
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");
41
42 // Get the file content
43 byte[] download = provider.getByteArrayFileFromId(fileId);
44
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);
50
51 // Various header fields that can be set to solve some issues with some
52 // old browsers.
53 // Unused.
54 // String contentType = "application/force-download; name=\"" + fileName
55 // + "\"";
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");
64
65 try {
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);
70 }
71 }
72 }