]> 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
new proposal to address the generic "open file" issue using a cleaner strategy.
[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.argeo.ArgeoException;
21 import org.eclipse.rap.rwt.RWT;
22 import org.eclipse.rap.rwt.client.service.UrlLauncher;
23 import org.eclipse.rap.rwt.service.ServiceHandler;
24
25 /**
26 * RAP SPECIFIC handler to enable the opening of a download dialog box triggered
27 * by whatever action in the UI
28 *
29 * Manages the registration of the effective DownloadServiceHandler at
30 * instantiation time.
31 *
32 * Manages the process of forwarding the request to the handler at runtime to
33 * open the dialog box encodedURL
34 */
35 @Deprecated
36 public class FileHandler {
37 public final static String DOWNLOAD_SERVICE_NAME = "argeo.rap.download.service";
38 private final static Log log = LogFactory.getLog(FileHandler.class);
39
40 public FileHandler(FileProvider provider) {
41 ServiceHandler handler = new DownloadServiceHandler(provider);
42 try {
43 RWT.getServiceManager().registerServiceHandler(
44 DOWNLOAD_SERVICE_NAME, handler);
45 } catch (IllegalArgumentException iae) {
46 log.warn("Handler is already registered, clean this registering process");
47 }
48 }
49
50 public void openFile(String fileName, String fileId) {
51 try {
52 String downloadUrl = RWT.getServiceManager().getServiceHandlerUrl(
53 DOWNLOAD_SERVICE_NAME)
54 + createParamUrl(fileName, fileId);
55 if (log.isTraceEnabled())
56 log.debug("URL : " + downloadUrl);
57 UrlLauncher launcher = RWT.getClient()
58 .getService(UrlLauncher.class);
59 launcher.openURL(downloadUrl);
60 } catch (Exception e) {
61 throw new ArgeoException("Unable to open file " + fileName, e);
62 }
63 // These lines are useless in the current use case but might be
64 // necessary with new browsers. Stored here for memo
65 // response.setContentType("application/force-download");
66 // response.setHeader("Content-Disposition", contentDisposition);
67 // response.setHeader("Content-Transfer-Encoding", "binary");
68 // response.setHeader("Pragma", "no-cache");
69 // response.setHeader("Cache-Control", "no-cache, must-revalidate");
70 }
71
72 private String createParamUrl(String filename, String fileId) {
73 StringBuilder url = new StringBuilder();
74 url.append("&filename=");
75 url.append(filename);
76 url.append("&fileid=");
77 url.append(fileId);
78 return url.toString();
79 }
80 }