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