]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.rcp/src/main/java/org/argeo/slc/client/ui/specific/OpenJcrFile.java
work on file downloads
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.rcp / 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.awt.Desktop;
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import javax.jcr.Node;
26 import javax.jcr.Property;
27 import javax.jcr.RepositoryException;
28 import javax.jcr.Session;
29 import javax.jcr.nodetype.NodeType;
30
31 import org.apache.commons.io.IOUtils;
32 import org.argeo.jcr.JcrUtils;
33 import org.argeo.slc.SlcException;
34 import org.argeo.slc.client.rcp.SlcRcpPlugin;
35 import org.argeo.slc.repo.RepoService;
36 import org.eclipse.core.commands.AbstractHandler;
37 import org.eclipse.core.commands.ExecutionEvent;
38 import org.eclipse.core.commands.ExecutionException;
39
40 /**
41 * RCP specific command handler to open a file retrieved from a local or distant
42 * JCR Repository.
43 */
44 public class OpenJcrFile extends AbstractHandler {
45 // private final static Log log = LogFactory.getLog(OpenJcrFile.class);
46
47 // Here is the trick that enable single sourcing: the ID is determined at
48 // runtime. ALWAYS use this public variable to call the command.
49 public final static String ID = SlcRcpPlugin.PLUGIN_ID + ".openJcrFile";
50
51 public final static String PARAM_REPO_NODE_PATH = "param.repoNodePath";
52 public final static String PARAM_REPO_URI = "param.repoUri";
53 public final static String PARAM_WORKSPACE_NAME = "param.workspaceName";
54 public final static String PARAM_FILE_PATH = "param.filePath";
55
56 /* DEPENDENCY INJECTION */
57 private RepoService repoService;
58
59 public Object execute(ExecutionEvent event) throws ExecutionException {
60
61 String repoNodePath = event.getParameter(PARAM_REPO_NODE_PATH);
62 String repoUri = event.getParameter(PARAM_REPO_URI);
63 String wkspName = event.getParameter(PARAM_WORKSPACE_NAME);
64 String filePath = event.getParameter(PARAM_FILE_PATH);
65
66 // TODO sanity check
67 if (filePath == null || "".equals(filePath.trim()))
68 return null;
69 retrieveAndOpen(repoNodePath, repoUri, wkspName, filePath);
70
71 return null;
72 }
73
74 protected void retrieveAndOpen(String repoNodePath, String repoUri,
75 String wkspName, String filePath) {
76 Session businessSession = null;
77 try {
78 businessSession = repoService.getRemoteSession(repoNodePath,
79 repoUri, wkspName);
80 Node result = businessSession.getNode(filePath);
81
82 boolean isValid = true;
83 Node child = null;
84 if (!result.isNodeType(NodeType.NT_FILE))
85 isValid = false;
86 else {
87 child = result.getNode(Property.JCR_CONTENT);
88 if (!(child.isNodeType(NodeType.NT_RESOURCE) || child
89 .hasProperty(Property.JCR_DATA)))
90 isValid = false;
91 }
92
93 if (!isValid)
94 return;
95
96 InputStream fis = null;
97 String prefix = "", extension = "";
98 String fileName = JcrUtils.lastPathElement(filePath);
99 int ind = fileName.lastIndexOf('.');
100 if (ind > 0) {
101 prefix = fileName.substring(0, ind);
102 extension = fileName.substring(ind);
103 }
104 try {
105
106 fis = (InputStream) child.getProperty(Property.JCR_DATA)
107 .getBinary().getStream();
108 File file = createTmpFile(prefix, extension, fis);
109 Desktop desktop = null;
110 if (Desktop.isDesktopSupported()) {
111 desktop = Desktop.getDesktop();
112 }
113 desktop.open(file);
114 } catch (Exception e) {
115 throw new SlcException(
116 "Stream error while opening file " + filePath
117 + " from repo " + repoUri == null ? repoNodePath
118 : repoUri, e);
119 } finally {
120 IOUtils.closeQuietly(fis);
121 }
122 } catch (RepositoryException e) {
123 throw new SlcException("Unexpected error while "
124 + "getting repoNode info for repoNode at path "
125 + repoNodePath, e);
126 } finally {
127 JcrUtils.logoutQuietly(businessSession);
128 }
129 }
130
131 private File createTmpFile(String prefix, String suffix, InputStream is) {
132 File tmpFile = null;
133 OutputStream os = null;
134 try {
135 tmpFile = File.createTempFile(prefix, suffix);
136 os = new FileOutputStream(tmpFile);
137 IOUtils.copy(is, os);
138 } catch (IOException e) {
139 throw new SlcException("Cannot open file " + prefix + "." + suffix,
140 e);
141 } finally {
142 IOUtils.closeQuietly(os);
143 }
144 return tmpFile;
145 }
146
147 /* DEPENDENCY INJECTION */
148 public void setRepoService(RepoService repoService) {
149 this.repoService = repoService;
150 }
151 }