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
.fs
.JcrFileSystem
;
20 import org
.argeo
.jcr
.fs
.JcrFsException
;
23 * A file system provider based on a JCR repository remotely accessed via the
26 public class DavexFsProvider
extends AbstractJackrabbitFsProvider
{
27 // final static String JACKRABBIT_REPOSITORY_URI = "org.apache.jackrabbit.repository.uri";
28 // final static String JACKRABBIT_REMOTE_DEFAULT_WORKSPACE = "org.apache.jackrabbit.spi2davex.WorkspaceNameDefault";
30 private Map
<String
, JcrFileSystem
> fileSystems
= new HashMap
<>();
33 public String
getScheme() {
38 public FileSystem
newFileSystem(URI uri
, Map
<String
, ?
> env
) throws IOException
{
39 if (uri
.getHost() == null)
40 throw new IllegalArgumentException("An host should be provided");
42 URI repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
43 String repoKey
= repoUri
.toString();
44 if (fileSystems
.containsKey(repoKey
))
45 throw new FileSystemAlreadyExistsException("CMS file system already exists for " + repoKey
);
46 RepositoryFactory repositoryFactory
= new ClientDavexRepositoryFactory();
47 return tryGetRepo(repositoryFactory
, repoUri
, "home");
48 } catch (URISyntaxException e
) {
49 throw new IllegalArgumentException("Cannot open file system " + uri
, e
);
53 private JcrFileSystem
tryGetRepo(RepositoryFactory repositoryFactory
, URI repoUri
, String workspace
)
55 Map
<String
, String
> params
= new HashMap
<String
, String
>();
56 params
.put(ClientDavexRepositoryFactory
.JACKRABBIT_DAVEX_URI
, repoUri
.toString());
57 // FIXME make it configurable
58 params
.put(ClientDavexRepositoryFactory
.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
, "sys");
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 IllegalArgumentException("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
) {