1 package org
.argeo
.cms
.fs
;
3 import java
.io
.IOException
;
5 import java
.net
.URISyntaxException
;
6 import java
.nio
.file
.FileSystem
;
7 import java
.nio
.file
.Path
;
8 import java
.nio
.file
.spi
.FileSystemProvider
;
10 import javax
.jcr
.NoSuchWorkspaceException
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.NodeIterator
;
13 import javax
.jcr
.Repository
;
14 import javax
.jcr
.RepositoryException
;
15 import javax
.jcr
.Session
;
16 import javax
.jcr
.query
.Query
;
17 import javax
.jcr
.query
.QueryManager
;
19 import org
.argeo
.api
.NodeConstants
;
20 import org
.argeo
.jcr
.Jcr
;
22 /** Utilities around documents. */
23 public class CmsFsUtils
{
24 // TODO make it more robust and configurable
25 private static String baseWorkspaceName
= NodeConstants
.SYS_WORKSPACE
;
27 public static Node
getNode(Repository repository
, Path path
) {
28 String workspaceName
= path
.getNameCount() == 0 ? baseWorkspaceName
: path
.getName(0).toString();
29 String jcrPath
= '/' + path
.subpath(1, path
.getNameCount()).toString();
33 newSession
= repository
.login(workspaceName
);
34 } catch (NoSuchWorkspaceException e
) {
36 newSession
= repository
.login(baseWorkspaceName
);
37 jcrPath
= path
.toString();
39 return newSession
.getNode(jcrPath
);
40 } catch (RepositoryException e
) {
41 throw new IllegalStateException("Cannot get node from path " + path
, e
);
45 public static NodeIterator
getLastUpdatedDocuments(Session session
) {
47 String qStr
= "//element(*, nt:file)";
48 qStr
+= " order by @jcr:lastModified descending";
49 QueryManager queryManager
= session
.getWorkspace().getQueryManager();
50 @SuppressWarnings("deprecation")
51 Query xpathQuery
= queryManager
.createQuery(qStr
, Query
.XPATH
);
52 xpathQuery
.setLimit(8);
53 NodeIterator nit
= xpathQuery
.execute().getNodes();
55 } catch (RepositoryException e
) {
56 throw new IllegalStateException("Unable to retrieve last updated documents", e
);
60 public static Path
getPath(FileSystemProvider nodeFileSystemProvider
, URI uri
) {
62 FileSystem fileSystem
= nodeFileSystemProvider
.getFileSystem(uri
);
63 if (fileSystem
== null)
64 fileSystem
= nodeFileSystemProvider
.newFileSystem(uri
, null);
65 String path
= uri
.getPath();
66 return fileSystem
.getPath(path
);
67 } catch (IOException e
) {
68 throw new IllegalStateException("Unable to initialise file system for " + uri
, e
);
72 public static Path
getPath(FileSystemProvider nodeFileSystemProvider
, Node node
) {
73 String workspaceName
= Jcr
.getWorkspaceName(node
);
74 String fullPath
= baseWorkspaceName
.equals(workspaceName
) ? Jcr
.getPath(node
)
75 : '/' + workspaceName
+ Jcr
.getPath(node
);
78 uri
= new URI(NodeConstants
.SCHEME_NODE
, null, fullPath
, null);
79 } catch (URISyntaxException e
) {
80 throw new IllegalArgumentException("Cannot interpret " + fullPath
+ " as an URI", e
);
82 return getPath(nodeFileSystemProvider
, uri
);
86 private CmsFsUtils() {