]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.rap/src/main/java/org/argeo/slc/client/ui/specific/OpenJcrFile.java
21ed8eb088844b64498bc11317c2f3965629031f
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.rap / src / main / java / org / argeo / slc / client / ui / specific / OpenJcrFile.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.slc.client.ui.specific;
17
18 import java.net.URL;
19
20 import javax.jcr.Session;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.jcr.JcrUtils;
25 import org.argeo.slc.client.rap.OpenJcrFileService;
26 import org.argeo.slc.client.rap.SlcRapPlugin;
27 import org.argeo.slc.repo.RepoService;
28 import org.eclipse.core.commands.AbstractHandler;
29 import org.eclipse.core.commands.ExecutionEvent;
30 import org.eclipse.core.commands.ExecutionException;
31 import org.eclipse.rwt.RWT;
32 import org.eclipse.rwt.service.IServiceHandler;
33 import org.eclipse.ui.PlatformUI;
34
35 /**
36 * Rap specific command handler to open a file retrieved from a distant JCR
37 * Repository. It forwards the request to the correct service after encoding
38 * file name and path in the request URI.
39 *
40 * This command and the corresponding service are specific for RAP version [1.3,
41 * 2)
42 */
43 public class OpenJcrFile extends AbstractHandler {
44 private final static Log log = LogFactory.getLog(OpenJcrFile.class);
45
46 public final static String ID = SlcRapPlugin.PLUGIN_ID + ".openJcrFile";
47
48 public final static String PARAM_REPO_NODE_PATH = OpenJcrFileService.PARAM_REPO_NODE_PATH;
49 public final static String PARAM_REPO_URI = OpenJcrFileService.PARAM_REPO_URI;
50 public final static String PARAM_WORKSPACE_NAME = OpenJcrFileService.PARAM_WORKSPACE_NAME;
51 public final static String PARAM_FILE_PATH = OpenJcrFileService.PARAM_FILE_PATH;
52
53 private RepoService repoService;
54
55 public Object execute(ExecutionEvent event) throws ExecutionException {
56
57 String repoNodePath = event.getParameter(PARAM_REPO_NODE_PATH);
58 String repoUri = event.getParameter(PARAM_REPO_URI);
59 String wkspName = event.getParameter(PARAM_WORKSPACE_NAME);
60 String filePath = event.getParameter(PARAM_FILE_PATH);
61
62 // TODO remove
63 Session session = repoService.getRemoteSession(repoNodePath, repoUri,
64 wkspName);
65 JcrUtils.logoutQuietly(session);
66
67 // TODO sanity check
68 if (filePath == null || "".equals(filePath.trim()))
69 return null;
70
71 try {
72 if (log.isDebugEnabled())
73 log.debug("URL : "
74 + createFullDownloadUrl(repoNodePath, repoUri,
75 wkspName, filePath));
76 // RWT.getResponse().sendRedirect(createFullDownloadUrl(repoNodePath,
77 // repoUri,
78 // wkspName, filePath));
79
80 URL url = new URL(createFullDownloadUrl(repoNodePath, repoUri,
81 wkspName, filePath));
82 PlatformUI.getWorkbench().getBrowserSupport()
83 .createBrowser("DownloadDialog").openURL(url);
84 } catch (Exception e) {
85 e.printStackTrace();
86 }
87
88 return null;
89 }
90
91 private String createFullDownloadUrl(String repoNodePath, String repoUri,
92 String wkspName, String filePath) {
93 StringBuilder url = new StringBuilder();
94 url.append(RWT.getRequest().getRequestURL());
95
96 StringBuilder params = new StringBuilder();
97 params.append("?");
98 params.append(IServiceHandler.REQUEST_PARAM).append("=");
99 params.append(OpenJcrFileService.ID);
100 if (repoNodePath != null)
101 params.append("&").append(OpenJcrFileService.PARAM_REPO_NODE_PATH)
102 .append("=").append(repoNodePath);
103
104 if (repoUri != null)
105 params.append("&").append(OpenJcrFileService.PARAM_REPO_URI)
106 .append("=").append(repoUri);
107
108 if (wkspName != null)
109 params.append("&").append(OpenJcrFileService.PARAM_WORKSPACE_NAME)
110 .append("=").append(wkspName);
111
112 if (filePath != null)
113 params.append("&").append(OpenJcrFileService.PARAM_FILE_PATH)
114 .append("=").append(filePath);
115
116 String encodedURL = RWT.getResponse().encodeURL(params.toString());
117 url.append(encodedURL);
118
119 return url.toString();
120 }
121
122 /* Dependency Injection */
123 // only used as a workaround to force the service instantiation
124 public void setOpenJcrFileService(OpenJcrFileService openJcrFileService) {
125 // do nothing.
126 }
127
128 public void setRepoService(RepoService repoService) {
129 this.repoService = repoService;
130 }
131
132 }