]> 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
2.x specific - Adapt file download service to Rap 2.x
[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 DOWNLOAD_SERVICE_NAME = "argeo.rap.download.service";
36 private final static Log log = LogFactory.getLog(FileHandler.class);
37
38 public FileHandler(FileProvider provider) {
39 ServiceHandler handler = new DownloadServiceHandler(provider);
40 RWT.getServiceManager().registerServiceHandler(DOWNLOAD_SERVICE_NAME,
41 handler);
42 }
43
44 public void openFile(String fileName, String fileId) {
45 try {
46 String downloadUrl = RWT.getServiceManager().getServiceHandlerUrl(
47 DOWNLOAD_SERVICE_NAME)
48 + createParamUrl(fileName, fileId);
49 if (log.isTraceEnabled())
50 log.debug("URL : " + downloadUrl);
51 UrlLauncher launcher = RWT.getClient()
52 .getService(UrlLauncher.class);
53 launcher.openURL(downloadUrl);
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57 // These lines are useless in the current use case but might be
58 // necessary with new browsers. Stored here for memo
59 // response.setContentType("application/force-download");
60 // response.setHeader("Content-Disposition", contentDisposition);
61 // response.setHeader("Content-Transfer-Encoding", "binary");
62 // response.setHeader("Pragma", "no-cache");
63 // response.setHeader("Cache-Control", "no-cache, must-revalidate");
64 }
65
66 private String createParamUrl(String filename, String fileId) {
67 StringBuilder url = new StringBuilder();
68 url.append("&filename=");
69 url.append(filename);
70 url.append("&fileid=");
71 url.append(fileId);
72 return url.toString();
73 }
74 }