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
.apache
.jackrabbit
.core
.security
.authentication
.LocalAuthContext
;
19 import org
.argeo
.jackrabbit
.client
.ClientDavexRepositoryFactory
;
20 import org
.argeo
.jcr
.ArgeoJcrException
;
21 import org
.argeo
.jcr
.fs
.JcrFileSystem
;
22 import org
.argeo
.jcr
.fs
.JcrFsException
;
25 * A file system provider based on a JCR repository remotely accessed via the
28 public class DavexFsProvider
extends AbstractJackrabbitFsProvider
{
29 // final static String JACKRABBIT_REPOSITORY_URI = "org.apache.jackrabbit.repository.uri";
30 // final static String JACKRABBIT_REMOTE_DEFAULT_WORKSPACE = "org.apache.jackrabbit.spi2davex.WorkspaceNameDefault";
32 private Map
<String
, JcrFileSystem
> fileSystems
= new HashMap
<>();
35 public String
getScheme() {
40 public FileSystem
newFileSystem(URI uri
, Map
<String
, ?
> env
) throws IOException
{
41 if (uri
.getHost() == null)
42 throw new ArgeoJcrException("An host should be provided");
44 URI repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
45 String repoKey
= repoUri
.toString();
46 if (fileSystems
.containsKey(repoKey
))
47 throw new FileSystemAlreadyExistsException("CMS file system already exists for " + repoKey
);
48 RepositoryFactory repositoryFactory
= new ClientDavexRepositoryFactory();
49 return tryGetRepo(repositoryFactory
, repoUri
, "home");
50 } catch (Exception e
) {
51 throw new ArgeoJcrException("Cannot open file system " + uri
, e
);
55 private JcrFileSystem
tryGetRepo(RepositoryFactory repositoryFactory
, URI repoUri
, String workspace
)
57 Map
<String
, String
> params
= new HashMap
<String
, String
>();
58 params
.put(ClientDavexRepositoryFactory
.JACKRABBIT_DAVEX_URI
, repoUri
.toString());
59 params
.put(ClientDavexRepositoryFactory
.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
, "main");
60 Repository repository
= null;
61 Session session
= null;
63 repository
= repositoryFactory
.getRepository(params
);
64 if (repository
!= null)
65 session
= repository
.login(workspace
);
66 } catch (Exception e
) {
70 if (session
== null) {
71 if (repoUri
.getPath() == null || repoUri
.getPath().equals("/"))
73 String repoUriStr
= repoUri
.toString();
74 if (repoUriStr
.endsWith("/"))
75 repoUriStr
= repoUriStr
.substring(0, repoUriStr
.length() - 1);
76 String nextRepoUriStr
= repoUriStr
.substring(0, repoUriStr
.lastIndexOf('/'));
77 String nextWorkspace
= repoUriStr
.substring(repoUriStr
.lastIndexOf('/') + 1);
80 nextUri
= new URI(nextRepoUriStr
);
81 } catch (URISyntaxException e
) {
82 throw new ArgeoJcrException("Badly formatted URI", e
);
84 return tryGetRepo(repositoryFactory
, nextUri
, nextWorkspace
);
86 JcrFileSystem fileSystem
= new JcrFileSystem(this, repository
);
87 fileSystems
.put(repoUri
.toString() + "/" + workspace
, fileSystem
);
93 public FileSystem
getFileSystem(URI uri
) {
94 return currentUserFileSystem(uri
);
98 public Path
getPath(URI uri
) {
99 JcrFileSystem fileSystem
= currentUserFileSystem(uri
);
100 if (fileSystem
== null)
102 fileSystem
= (JcrFileSystem
) newFileSystem(uri
, new HashMap
<String
, Object
>());
103 } catch (IOException e
) {
104 throw new JcrFsException("Could not autocreate file system", e
);
108 repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
109 } catch (URISyntaxException e
) {
110 throw new IllegalArgumentException(e
);
112 String uriStr
= repoUri
.toString();
113 String localPath
= null;
114 for (String key
: fileSystems
.keySet()) {
115 if (uriStr
.startsWith(key
)) {
116 localPath
= uriStr
.toString().substring(key
.length());
119 if ("".equals(localPath
))
121 return fileSystem
.getPath(localPath
);
124 private JcrFileSystem
currentUserFileSystem(URI uri
) {
125 for (String key
: fileSystems
.keySet()) {
126 if (uri
.toString().startsWith(key
))
127 return fileSystems
.get(key
);
132 public static void main(String args
[]) {
134 DavexFsProvider fsProvider
= new DavexFsProvider();
135 Path path
= fsProvider
.getPath(new URI("davex://root:demo@localhost:7070/jcr/ego/"));
136 System
.out
.println(path
);
137 DirectoryStream
<Path
> ds
= Files
.newDirectoryStream(path
);
139 System
.out
.println("- " + p
);
141 } catch (Exception e
) {