]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.client.rap/src/main/java/org/argeo/slc/client/ui/specific/OpenJcrFile.java
Start migrating SLC client
[gpl/argeo-slc.git] / 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 import java.util.UUID;
20
21 import javax.jcr.Node;
22 import javax.jcr.Session;
23
24 import org.argeo.slc.SlcException;
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.rwt.service.IServiceManager;
34 import org.eclipse.ui.PlatformUI;
35
36 /**
37 * Rap specific command handler to open a file retrieved from a distant JCR
38 * Repository. It creates and register a service instantiated with the
39 * corresponding JCR node, forwards the request, and un register the service on
40 * dispose
41 *
42 * This command and the corresponding service are specific for RAP version [1.3,
43 * 2)
44 */
45 public class OpenJcrFile extends AbstractHandler {
46
47 // Use (new OpenJcrFileCmdId()).getCmdId() instead.
48 // public final String ID = SlcRapPlugin.PLUGIN_ID + ".openJcrFile";
49
50 public final static String PARAM_REPO_NODE_PATH = "param.repoNodePath";
51 public final static String PARAM_REPO_URI = "param.repoUri";
52 public final static String PARAM_WORKSPACE_NAME = "param.workspaceName";
53 public final static String PARAM_FILE_PATH = "param.filePath";
54
55 private RepoService repoService;
56 private String currentServiceId;
57
58 public Object execute(ExecutionEvent event) throws ExecutionException {
59
60 String repoNodePath = event.getParameter(PARAM_REPO_NODE_PATH);
61 String repoUri = event.getParameter(PARAM_REPO_URI);
62 String wkspName = event.getParameter(PARAM_WORKSPACE_NAME);
63 String filePath = event.getParameter(PARAM_FILE_PATH);
64
65 // TODO sanity check
66 if (filePath == null || "".equals(filePath.trim()))
67 return null;
68 Session businessSession = null;
69 try {
70 businessSession = repoService.getRemoteSession(repoNodePath,
71 repoUri, wkspName);
72 Node result = businessSession.getNode(filePath);
73
74 // Create a temporary service. No better solution has been found
75 // yet.
76 currentServiceId = UUID.randomUUID().toString();
77 OpenJcrFileService ojfs = new OpenJcrFileService(result);
78 IServiceManager manager = RWT.getServiceManager();
79 manager.registerServiceHandler(currentServiceId, ojfs);
80 String urlStr = createFullDownloadUrl(currentServiceId);
81 URL url = new URL(urlStr);
82 PlatformUI.getWorkbench().getBrowserSupport()
83 .createBrowser("DownloadDialog").openURL(url);
84 } catch (Exception e) {
85 throw new SlcException("Unable to open Jcr File for path "
86 + filePath, e);
87 }
88
89 return null;
90 }
91
92 @Override
93 public void dispose() {
94 IServiceManager manager = RWT.getServiceManager();
95 manager.unregisterServiceHandler(currentServiceId);
96 super.dispose();
97 }
98
99 private String createFullDownloadUrl(String serviceId) {
100 StringBuilder url = new StringBuilder();
101 url.append(RWT.getRequest().getRequestURL());
102
103 StringBuilder params = new StringBuilder();
104 params.append("?");
105 params.append(IServiceHandler.REQUEST_PARAM).append("=");
106 params.append(serviceId);
107 String encodedURL = RWT.getResponse().encodeURL(params.toString());
108 url.append(encodedURL);
109 return url.toString();
110 }
111
112 /* Dependency Injection */
113 public void setRepoService(RepoService repoService) {
114 this.repoService = repoService;
115 }
116 }