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