]> git.argeo.org Git - lgpl/argeo-commons.git/blob - OpenFile.java
b55521b126d804b07ca2332f2600f9dda9bbae4d
[lgpl/argeo-commons.git] / OpenFile.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.core.commands.AbstractHandler;
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.rap.rwt.RWT;
24 import org.eclipse.rap.rwt.client.service.UrlLauncher;
25
26 /**
27 * Rap specific command handler to open a file retrieved from the server. It
28 * forwards the request to the correct service after encoding file name and path
29 * in the request URI.
30 *
31 * The parameter "URI" is used to determine the correct file service, the path
32 * and the file name. An optional file name can be precised to present a
33 * different file name as the one used to retrieve it to the end user/
34 *
35 * Various instances of this handler with different command ID might coexist in
36 * order to provide context specific download service.
37 *
38 * The instance specific service is called by its ID and must have been
39 * externally created
40 */
41 public class OpenFile extends AbstractHandler {
42 private final static Log log = LogFactory.getLog(OpenFile.class);
43
44 /* DEPENDENCY INJECTION */
45 private String openFileServiceId;
46
47 public final static String PARAM_FILE_NAME = OpenFileService.PARAM_FILE_NAME;
48 public final static String PARAM_FILE_URI = OpenFileService.PARAM_FILE_URI; // "param.fileURI";
49
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51 String fileName = event.getParameter(PARAM_FILE_NAME);
52 String fileUri = event.getParameter(PARAM_FILE_URI);
53
54 // sanity check
55 if (fileUri == null || "".equals(fileUri.trim())
56 || openFileServiceId == null
57 || "".equals(openFileServiceId.trim()))
58 return null;
59
60 StringBuilder url = new StringBuilder();
61 url.append(RWT.getServiceManager().getServiceHandlerUrl(
62 openFileServiceId));
63
64 url.append("&").append(PARAM_FILE_NAME).append("=");
65 url.append(fileName);
66 url.append("&").append(PARAM_FILE_URI).append("=");
67 url.append(fileUri);
68
69 String downloadUrl = url.toString();
70 if (log.isTraceEnabled())
71 log.debug("URL : " + downloadUrl);
72
73 UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
74 launcher.openURL(downloadUrl);
75
76 // These lines are useless in the current use case but might be
77 // necessary with new browsers. Stored here for memo
78 // response.setContentType("application/force-download");
79 // response.setHeader("Content-Disposition", contentDisposition);
80 // response.setHeader("Content-Transfer-Encoding", "binary");
81 // response.setHeader("Pragma", "no-cache");
82 // response.setHeader("Cache-Control", "no-cache, must-revalidate");
83 return null;
84 }
85
86 /* DEPENDENCY INJECTION */
87 public void setOpenFileServiceId(String openFileServiceId) {
88 this.openFileServiceId = openFileServiceId;
89 }
90 }