]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/FileHandler.java
Internationalization support
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui.rap / src / main / java / org / argeo / eclipse / ui / specific / FileHandler.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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.net.URL;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.eclipse.rwt.RWT;
23 import org.eclipse.rwt.service.IServiceHandler;
24 import org.eclipse.rwt.service.IServiceManager;
25 import org.eclipse.ui.PlatformUI;
26
27 /**
28 * RAP SPECIFIC handler to enable the opening of a download dialog box triggered
29 * by whatever action in the UI
30 *
31 * Manages the registration of the effective DownloadServiceHandler at
32 * instantiation time.
33 *
34 * Manages the process of forwarding the request to the handler at runtime to
35 * open the dialog box
36 *
37 */
38 public class FileHandler {
39 public final static String FORCED_DOWNLOAD_URL_BASE_PROPERTY = "argeo.rap.specific.forcedDownloadUrlBase";
40
41 private final static Log log = LogFactory.getLog(FileHandler.class);
42
43 public FileHandler(FileProvider provider) {
44 // Instantiate and register the DownloadServicHandler.
45 IServiceManager manager = RWT.getServiceManager();
46 IServiceHandler handler = new DownloadServiceHandler(provider);
47 manager.registerServiceHandler("downloadServiceHandler", handler);
48 }
49
50 public void openFile(String fileName, String fileId) {
51
52 // See RAP FAQ:
53 // http://wiki.eclipse.org/RAP/FAQ#How_to_provide_download_link.3F
54 // And forum discussion :
55 // http://www.eclipse.org/forums/index.php?t=msg&th=205487&start=0&S=43d85dacc88b505402420592109c7240
56
57 try {
58 if (log.isTraceEnabled())
59 log.trace("URL : " + createFullDownloadUrl(fileName, fileId));
60
61 URL url = new URL(createFullDownloadUrl(fileName, fileId));
62 PlatformUI.getWorkbench().getBrowserSupport()
63 .createBrowser("DownloadDialog").openURL(url);
64 } catch (Exception e) {
65 e.printStackTrace();
66 }
67
68 // These lines are useless in the current use case but might be
69 // necessary with new browsers. Stored here for memo
70 // response.setContentType("application/force-download");
71 // response.setHeader("Content-Disposition", contentDisposition);
72 // response.setHeader("Content-Transfer-Encoding", "binary");
73 // response.setHeader("Pragma", "no-cache");
74 // response.setHeader("Cache-Control", "no-cache, must-revalidate");
75 }
76
77 private String createFullDownloadUrl(String fileName, String fileId) {
78 StringBuilder url = new StringBuilder();
79 // in case RAP is proxied we need to specify the actual base URL
80 // TODO find a cleaner way
81 String forcedDownloadUrlBase = System
82 .getProperty(FORCED_DOWNLOAD_URL_BASE_PROPERTY);
83 if (forcedDownloadUrlBase != null)
84 url.append(forcedDownloadUrlBase);
85 else
86 url.append(RWT.getRequest().getRequestURL());
87 url.append(createParamUrl(fileName, fileId));
88 return url.toString();
89 }
90
91 private String createParamUrl(String filename, String fileId) {
92 StringBuilder url = new StringBuilder();
93 url.append("?");
94 url.append(IServiceHandler.REQUEST_PARAM);
95 url.append("=downloadServiceHandler");
96 url.append("&filename=");
97 url.append(filename);
98 url.append("&fileid=");
99 url.append(fileId);
100 String encodedURL = RWT.getResponse().encodeURL(url.toString());
101 return encodedURL;
102 }
103 }