1 package org
.argeo
.jackrabbit
.fs
;
3 import java
.io
.IOException
;
5 import java
.net
.URISyntaxException
;
6 import java
.nio
.file
.DirectoryStream
;
7 import java
.nio
.file
.FileSystem
;
8 import java
.nio
.file
.FileSystemAlreadyExistsException
;
9 import java
.nio
.file
.Files
;
10 import java
.nio
.file
.Path
;
11 import java
.util
.HashMap
;
14 import javax
.jcr
.Repository
;
15 import javax
.jcr
.RepositoryFactory
;
16 import javax
.jcr
.Session
;
18 import org
.argeo
.jackrabbit
.client
.ClientDavexRepositoryFactory
;
19 import org
.argeo
.jcr
.ArgeoJcrException
;
20 import org
.argeo
.jcr
.fs
.JcrFileSystem
;
21 import org
.argeo
.jcr
.fs
.JcrFsException
;
24 * A file system provider based on a JCR repository remotely accessed via the
27 public class DavexFsProvider
extends AbstractJackrabbitFsProvider
{
28 // final static String JACKRABBIT_REPOSITORY_URI = "org.apache.jackrabbit.repository.uri";
29 // final static String JACKRABBIT_REMOTE_DEFAULT_WORKSPACE = "org.apache.jackrabbit.spi2davex.WorkspaceNameDefault";
31 private Map
<String
, JcrFileSystem
> fileSystems
= new HashMap
<>();
34 public String
getScheme() {
39 public FileSystem
newFileSystem(URI uri
, Map
<String
, ?
> env
) throws IOException
{
40 if (uri
.getHost() == null)
41 throw new ArgeoJcrException("An host should be provided");
43 URI repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
44 String repoKey
= repoUri
.toString();
45 if (fileSystems
.containsKey(repoKey
))
46 throw new FileSystemAlreadyExistsException("CMS file system already exists for " + repoKey
);
47 RepositoryFactory repositoryFactory
= new ClientDavexRepositoryFactory();
48 return tryGetRepo(repositoryFactory
, repoUri
, "home");
49 } catch (Exception e
) {
50 throw new ArgeoJcrException("Cannot open file system " + uri
, e
);
54 private JcrFileSystem
tryGetRepo(RepositoryFactory repositoryFactory
, URI repoUri
, String workspace
)
56 Map
<String
, String
> params
= new HashMap
<String
, String
>();
57 params
.put(ClientDavexRepositoryFactory
.JACKRABBIT_DAVEX_URI
, repoUri
.toString());
58 params
.put(ClientDavexRepositoryFactory
.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
, "main");
59 Repository repository
= null;
60 Session session
= null;
62 repository
= repositoryFactory
.getRepository(params
);
63 if (repository
!= null)
64 session
= repository
.login(workspace
);
65 } catch (Exception e
) {
69 if (session
== null) {
70 if (repoUri
.getPath() == null || repoUri
.getPath().equals("/"))
72 String repoUriStr
= repoUri
.toString();
73 if (repoUriStr
.endsWith("/"))
74 repoUriStr
= repoUriStr
.substring(0, repoUriStr
.length() - 1);
75 String nextRepoUriStr
= repoUriStr
.substring(0, repoUriStr
.lastIndexOf('/'));
76 String nextWorkspace
= repoUriStr
.substring(repoUriStr
.lastIndexOf('/') + 1);
79 nextUri
= new URI(nextRepoUriStr
);
80 } catch (URISyntaxException e
) {
81 throw new ArgeoJcrException("Badly formatted URI", e
);
83 return tryGetRepo(repositoryFactory
, nextUri
, nextWorkspace
);
85 JcrFileSystem fileSystem
= new JcrFileSystem(this, repository
);
86 fileSystems
.put(repoUri
.toString() + "/" + workspace
, fileSystem
);
92 public FileSystem
getFileSystem(URI uri
) {
93 return currentUserFileSystem(uri
);
97 public Path
getPath(URI uri
) {
98 JcrFileSystem fileSystem
= currentUserFileSystem(uri
);
99 if (fileSystem
== null)
101 fileSystem
= (JcrFileSystem
) newFileSystem(uri
, new HashMap
<String
, Object
>());
102 } catch (IOException e
) {
103 throw new JcrFsException("Could not autocreate file system", e
);
107 repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
108 } catch (URISyntaxException e
) {
109 throw new IllegalArgumentException(e
);
111 String uriStr
= repoUri
.toString();
112 String localPath
= null;
113 for (String key
: fileSystems
.keySet()) {
114 if (uriStr
.startsWith(key
)) {
115 localPath
= uriStr
.toString().substring(key
.length());
118 if ("".equals(localPath
))
120 return fileSystem
.getPath(localPath
);
123 private JcrFileSystem
currentUserFileSystem(URI uri
) {
124 for (String key
: fileSystems
.keySet()) {
125 if (uri
.toString().startsWith(key
))
126 return fileSystems
.get(key
);
131 public static void main(String args
[]) {
133 DavexFsProvider fsProvider
= new DavexFsProvider();
134 Path path
= fsProvider
.getPath(new URI("davex://root:demo@localhost:7070/jcr/ego/"));
135 System
.out
.println(path
);
136 DirectoryStream
<Path
> ds
= Files
.newDirectoryStream(path
);
138 System
.out
.println("- " + p
);
140 } catch (Exception e
) {