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 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
;
25 * RAP SPECIFIC handler to enable the opening of a download dialog box triggered
26 * by whatever action in the UI
28 * Manages the registration of the effective DownloadServiceHandler at
31 * Manages the process of forwarding the request to the handler at runtime to
32 * open the dialog box encodedURL
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);
39 public FileHandler(FileProvider provider
) {
40 ServiceHandler handler
= new DownloadServiceHandler(provider
);
41 RWT
.getServiceManager().registerServiceHandler(DOWNLOAD_SERVICE_NAME
,
45 public void openFile(String fileName
, String fileId
) {
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
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
) {
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");
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);
85 // url.append(RWT.getRequest().getRequestURL());
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
));
92 url
.append(createParamUrl(fileName
, fileId
));
93 return url
.toString();
96 private String
createParamUrl(String filename
, String fileId
) {
98 StringBuilder url
= new StringBuilder();
100 // url.append(ServiceHandler.REQUEST_PARAM);
101 // url.append("=downloadServiceHandler");
102 url
.append("&filename=");
103 url
.append(filename
);
104 url
.append("&fileid=");
106 return url
.toString();