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
) {
51 Map
<String
, String
> params
= new HashMap
<String
, String
>();
52 params
.put(JACKRABBIT_REPOSITORY_URI
, repoUri
.toString());
53 params
.put(JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
, "main");
54 Repository repository
= null;
55 Session session
= null;
57 repository
= repositoryFactory
.getRepository(params
);
58 if (repository
!= null)
59 session
= repository
.login(workspace
);
60 } catch (Exception e
) {
64 if (session
== null) {
65 if (repoUri
.getPath() == null || repoUri
.getPath().equals("/"))
67 String repoUriStr
= repoUri
.toString();
68 if (repoUriStr
.endsWith("/"))
69 repoUriStr
= repoUriStr
.substring(0, repoUriStr
.length() - 1);
70 String nextRepoUriStr
= repoUriStr
.substring(0, repoUriStr
.lastIndexOf('/'));
71 String nextWorkspace
= repoUriStr
.substring(repoUriStr
.lastIndexOf('/') + 1);
74 nextUri
= new URI(nextRepoUriStr
);
75 } catch (URISyntaxException e
) {
76 throw new ArgeoJcrException("Badly formatted URI", e
);
78 return tryGetRepo(repositoryFactory
, nextUri
, nextWorkspace
);
80 JcrFileSystem fileSystem
= new JcrFileSystem(this, session
);
81 fileSystems
.put(repoUri
.toString() + "/" + workspace
, fileSystem
);
87 public FileSystem
getFileSystem(URI uri
) {
88 return currentUserFileSystem(uri
);
92 public Path
getPath(URI uri
) {
93 JcrFileSystem fileSystem
= currentUserFileSystem(uri
);
94 if (fileSystem
== null)
96 fileSystem
= (JcrFileSystem
) newFileSystem(uri
, new HashMap
<String
, Object
>());
97 } catch (IOException e
) {
98 throw new JcrFsException("Could not autocreate file system", e
);
102 repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
103 } catch (URISyntaxException e
) {
104 // TODO Auto-generated catch block
107 String uriStr
= repoUri
.toString();
108 String localPath
= null;
109 for (String key
: fileSystems
.keySet()) {
110 if (uriStr
.startsWith(key
)) {
111 localPath
= uriStr
.toString().substring(key
.length());
114 return fileSystem
.getPath(localPath
);
117 private JcrFileSystem
currentUserFileSystem(URI uri
) {
118 for (String key
: fileSystems
.keySet()) {
119 if (uri
.toString().startsWith(key
))
120 return fileSystems
.get(key
);
125 public static void main(String args
[]) {
127 DavexFsProvider fsProvider
= new DavexFsProvider();
128 Path path
= fsProvider
.getPath(new URI("davex://root:demo@localhost:7070/jcr/node/main/home/"));
129 System
.out
.println(path
);
130 DirectoryStream
<Path
> ds
= Files
.newDirectoryStream(path
);
132 System
.out
.println("- " + p
);
134 } catch (Exception e
) {