2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.specific
;
18 import java
.awt
.Desktop
;
20 import java
.io
.FileOutputStream
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.io
.OutputStream
;
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
;
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
;
41 * RCP specific command handler to open a file retrieved from a local or distant
44 public class OpenJcrFile
extends AbstractHandler
{
45 // private final static Log log = LogFactory.getLog(OpenJcrFile.class);
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";
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";
56 /* DEPENDENCY INJECTION */
57 private RepoService repoService
;
59 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
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
);
67 if (filePath
== null || "".equals(filePath
.trim()))
69 retrieveAndOpen(repoNodePath
, repoUri
, wkspName
, filePath
);
74 protected void retrieveAndOpen(String repoNodePath
, String repoUri
,
75 String wkspName
, String filePath
) {
76 Session businessSession
= null;
78 businessSession
= repoService
.getRemoteSession(repoNodePath
,
80 Node result
= businessSession
.getNode(filePath
);
82 boolean isValid
= true;
84 if (!result
.isNodeType(NodeType
.NT_FILE
))
87 child
= result
.getNode(Property
.JCR_CONTENT
);
88 if (!(child
.isNodeType(NodeType
.NT_RESOURCE
) || child
89 .hasProperty(Property
.JCR_DATA
)))
96 InputStream fis
= null;
97 String prefix
= "", extension
= "";
98 String fileName
= JcrUtils
.lastPathElement(filePath
);
99 int ind
= fileName
.lastIndexOf('.');
101 prefix
= fileName
.substring(0, ind
);
102 extension
= fileName
.substring(ind
);
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();
114 } catch (Exception e
) {
115 throw new SlcException(
116 "Stream error while opening file " + filePath
117 + " from repo " + repoUri
== null ? repoNodePath
120 IOUtils
.closeQuietly(fis
);
122 } catch (RepositoryException e
) {
123 throw new SlcException("Unexpected error while "
124 + "getting repoNode info for repoNode at path "
127 JcrUtils
.logoutQuietly(businessSession
);
131 private File
createTmpFile(String prefix
, String suffix
, InputStream is
) {
133 OutputStream os
= null;
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
,
142 IOUtils
.closeQuietly(os
);
147 /* DEPENDENCY INJECTION */
148 public void setRepoService(RepoService repoService
) {
149 this.repoService
= repoService
;