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