View Javadoc
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.repo.RepoService;
27  import org.eclipse.core.commands.AbstractHandler;
28  import org.eclipse.core.commands.ExecutionEvent;
29  import org.eclipse.core.commands.ExecutionException;
30  import org.eclipse.rap.rwt.RWT;
31  //import org.eclipse.rap.rwt.service.IServiceHandler;
32  //import org.eclipse.rap.rwt.service.IServiceManager;
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 creates and register a service instantiated with the
38   * corresponding JCR node, forwards the request, and un register the service on
39   * dispose
40   * 
41   * This command and the corresponding service are specific for RAP version [1.3,
42   * 2)
43   */
44  public class OpenJcrFile extends AbstractHandler {
45  
46  	// Use (new OpenJcrFileCmdId()).getCmdId() instead.
47  	// public final String ID = SlcRapPlugin.PLUGIN_ID + ".openJcrFile";
48  	
49  	public final static String PARAM_REPO_NODE_PATH = "param.repoNodePath";
50  	public final static String PARAM_REPO_URI = "param.repoUri";
51  	public final static String PARAM_WORKSPACE_NAME = "param.workspaceName";
52  	public final static String PARAM_FILE_PATH = "param.filePath";
53  
54  	private RepoService repoService;
55  	private String currentServiceId;
56  
57  	public Object execute(ExecutionEvent event) throws ExecutionException {
58  
59  		String repoNodePath = event.getParameter(PARAM_REPO_NODE_PATH);
60  		String repoUri = event.getParameter(PARAM_REPO_URI);
61  		String wkspName = event.getParameter(PARAM_WORKSPACE_NAME);
62  		String filePath = event.getParameter(PARAM_FILE_PATH);
63  
64  		// TODO sanity check
65  		if (filePath == null || "".equals(filePath.trim()))
66  			return null;
67  		Session businessSession = null;
68  		try {
69  			businessSession = repoService.getRemoteSession(repoNodePath,
70  					repoUri, wkspName);
71  			Node result = businessSession.getNode(filePath);
72  
73  			// Create a temporary service. No better solution has been found
74  			// yet.
75  			currentServiceId = UUID.randomUUID().toString();
76  			OpenJcrFileService ojfs = new OpenJcrFileService(result);
77  			// FIXME replace it
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 		// FIXME commented out so that it builds
106 		//params.append(IServiceHandler.REQUEST_PARAM).append("=");
107 		params.append(serviceId);
108 		String encodedURL = RWT.getResponse().encodeURL(params.toString());
109 		url.append(encodedURL);
110 		return url.toString();
111 	}
112 
113 	/* Dependency Injection */
114 	public void setRepoService(RepoService repoService) {
115 		this.repoService = repoService;
116 	}
117 }