1 package org
.argeo
.slc
.client
.ui
.specific
;
3 import java
.awt
.Desktop
;
5 import java
.io
.FileOutputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
8 import java
.io
.OutputStream
;
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
;
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
;
25 * RCP specific command handler to open a file retrieved from a local or distant
28 public class OpenJcrFile
extends AbstractHandler
{
29 // private final static Log log = LogFactory.getLog(OpenJcrFile.class);
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";
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";
40 /* DEPENDENCY INJECTION */
41 private RepoService repoService
;
43 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
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
);
51 if (filePath
== null || "".equals(filePath
.trim()))
53 retrieveAndOpen(repoNodePath
, repoUri
, wkspName
, filePath
);
58 protected void retrieveAndOpen(String repoNodePath
, String repoUri
,
59 String wkspName
, String filePath
) {
60 Session businessSession
= null;
62 businessSession
= repoService
.getRemoteSession(repoNodePath
,
64 Node result
= businessSession
.getNode(filePath
);
66 boolean isValid
= true;
68 if (!result
.isNodeType(NodeType
.NT_FILE
))
71 child
= result
.getNode(Property
.JCR_CONTENT
);
72 if (!(child
.isNodeType(NodeType
.NT_RESOURCE
) || child
73 .hasProperty(Property
.JCR_DATA
)))
80 InputStream fis
= null;
81 String prefix
= "", extension
= "";
82 String fileName
= JcrUtils
.lastPathElement(filePath
);
83 int ind
= fileName
.lastIndexOf('.');
85 prefix
= fileName
.substring(0, ind
);
86 extension
= fileName
.substring(ind
);
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();
98 } catch (Exception e
) {
99 throw new SlcException(
100 "Stream error while opening file " + filePath
101 + " from repo " + repoUri
== null ? repoNodePath
104 IOUtils
.closeQuietly(fis
);
106 } catch (RepositoryException e
) {
107 throw new SlcException("Unexpected error while "
108 + "getting repoNode info for repoNode at path "
111 JcrUtils
.logoutQuietly(businessSession
);
115 private File
createTmpFile(String prefix
, String suffix
, InputStream is
) {
117 OutputStream os
= null;
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
,
126 IOUtils
.closeQuietly(os
);
131 /* DEPENDENCY INJECTION */
132 public void setRepoService(RepoService repoService
) {
133 this.repoService
= repoService
;