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
.jcr2dav
.Jcr2davRepositoryFactory
;
19 import org
.argeo
.jcr
.ArgeoJcrException
;
20 import org
.argeo
.jcr
.fs
.JcrFileSystem
;
21 import org
.argeo
.jcr
.fs
.JcrFsException
;
23 public class DavexFsProvider
extends AbstractJackrabbitFsProvider
{
24 final static String JACKRABBIT_REPOSITORY_URI
= "org.apache.jackrabbit.repository.uri";
25 final static String JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
= "org.apache.jackrabbit.spi2davex.WorkspaceNameDefault";
27 private Map
<String
, JcrFileSystem
> fileSystems
= new HashMap
<>();
30 public String
getScheme() {
35 public FileSystem
newFileSystem(URI uri
, Map
<String
, ?
> env
) throws IOException
{
36 if (uri
.getHost() == null)
37 throw new ArgeoJcrException("An host should be provided");
39 URI repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
40 String repoKey
= repoUri
.toString();
41 if (fileSystems
.containsKey(repoKey
))
42 throw new FileSystemAlreadyExistsException("CMS file system already exists for " + repoKey
);
43 RepositoryFactory repositoryFactory
= new Jcr2davRepositoryFactory();
44 return tryGetRepo(repositoryFactory
, repoUri
, "main");
45 } catch (Exception e
) {
46 throw new ArgeoJcrException("Cannot open file system " + uri
, e
);
50 private JcrFileSystem
tryGetRepo(RepositoryFactory repositoryFactory
, URI repoUri
, String workspace
)
52 Map
<String
, String
> params
= new HashMap
<String
, String
>();
53 params
.put(JACKRABBIT_REPOSITORY_URI
, repoUri
.toString());
54 params
.put(JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
, "main");
55 Repository repository
= null;
56 Session session
= null;
58 repository
= repositoryFactory
.getRepository(params
);
59 if (repository
!= null)
60 session
= repository
.login(workspace
);
61 } catch (Exception e
) {
65 if (session
== null) {
66 if (repoUri
.getPath() == null || repoUri
.getPath().equals("/"))
68 String repoUriStr
= repoUri
.toString();
69 if (repoUriStr
.endsWith("/"))
70 repoUriStr
= repoUriStr
.substring(0, repoUriStr
.length() - 1);
71 String nextRepoUriStr
= repoUriStr
.substring(0, repoUriStr
.lastIndexOf('/'));
72 String nextWorkspace
= repoUriStr
.substring(repoUriStr
.lastIndexOf('/') + 1);
75 nextUri
= new URI(nextRepoUriStr
);
76 } catch (URISyntaxException e
) {
77 throw new ArgeoJcrException("Badly formatted URI", e
);
79 return tryGetRepo(repositoryFactory
, nextUri
, nextWorkspace
);
81 JcrFileSystem fileSystem
= new JcrFileSystem(this, session
);
82 fileSystems
.put(repoUri
.toString() + "/" + workspace
, fileSystem
);
88 public FileSystem
getFileSystem(URI uri
) {
89 return currentUserFileSystem(uri
);
93 public Path
getPath(URI uri
) {
94 JcrFileSystem fileSystem
= currentUserFileSystem(uri
);
95 if (fileSystem
== null)
97 fileSystem
= (JcrFileSystem
) newFileSystem(uri
, new HashMap
<String
, Object
>());
98 } catch (IOException e
) {
99 throw new JcrFsException("Could not autocreate file system", e
);
103 repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
104 } catch (URISyntaxException e
) {
105 // TODO Auto-generated catch block
108 String uriStr
= repoUri
.toString();
109 String localPath
= null;
110 for (String key
: fileSystems
.keySet()) {
111 if (uriStr
.startsWith(key
)) {
112 localPath
= uriStr
.toString().substring(key
.length());
115 return fileSystem
.getPath(localPath
);
118 private JcrFileSystem
currentUserFileSystem(URI uri
) {
119 for (String key
: fileSystems
.keySet()) {
120 if (uri
.toString().startsWith(key
))
121 return fileSystems
.get(key
);
126 public static void main(String args
[]) {
128 DavexFsProvider fsProvider
= new DavexFsProvider();
129 Path path
= fsProvider
.getPath(new URI("davex://root:demo@localhost:7070/jcr/node/main/home/"));
130 System
.out
.println(path
);
131 DirectoryStream
<Path
> ds
= Files
.newDirectoryStream(path
);
133 System
.out
.println("- " + p
);
135 } catch (Exception e
) {