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
;
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
;
28 * RAP SPECIFIC handler to enable the opening of a download dialog box triggered
29 * by whatever action in the UI
31 * Manages the registration of the effective DownloadServiceHandler at
34 * Manages the process of forwarding the request to the handler at runtime to
38 public class FileHandler
{
39 public final static String FORCED_DOWNLOAD_URL_BASE_PROPERTY
= "argeo.rap.specific.forcedDownloadUrlBase";
41 private final static Log log
= LogFactory
.getLog(FileHandler
.class);
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
);
50 public void openFile(String fileName
, String fileId
) {
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
58 if (log
.isTraceEnabled())
59 log
.trace("URL : " + createFullDownloadUrl(fileName
, fileId
));
61 URL url
= new URL(createFullDownloadUrl(fileName
, fileId
));
62 PlatformUI
.getWorkbench().getBrowserSupport()
63 .createBrowser("DownloadDialog").openURL(url
);
64 } catch (Exception e
) {
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");
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
);
86 url
.append(RWT
.getRequest().getRequestURL());
87 url
.append(createParamUrl(fileName
, fileId
));
88 return url
.toString();
91 private String
createParamUrl(String filename
, String fileId
) {
92 StringBuilder url
= new StringBuilder();
94 url
.append(IServiceHandler
.REQUEST_PARAM
);
95 url
.append("=downloadServiceHandler");
96 url
.append("&filename=");
98 url
.append("&fileid=");
100 String encodedURL
= RWT
.getResponse().encodeURL(url
.toString());