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 DEFAULT_JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
= "sys";
29 private Map
<String
, JcrFileSystem
> fileSystems
= new HashMap
<>();
32 public String
getScheme() {
37 public FileSystem
newFileSystem(URI uri
, Map
<String
, ?
> env
) throws IOException
{
38 if (uri
.getHost() == null)
39 throw new IllegalArgumentException("An host should be provided");
41 URI repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
42 String repoKey
= repoUri
.toString();
43 if (fileSystems
.containsKey(repoKey
))
44 throw new FileSystemAlreadyExistsException("CMS file system already exists for " + repoKey
);
45 RepositoryFactory repositoryFactory
= new ClientDavexRepositoryFactory();
46 return tryGetRepo(repositoryFactory
, repoUri
, "home");
47 } catch (URISyntaxException e
) {
48 throw new IllegalArgumentException("Cannot open file system " + uri
, e
);
52 private JcrFileSystem
tryGetRepo(RepositoryFactory repositoryFactory
, URI repoUri
, String workspace
)
54 Map
<String
, String
> params
= new HashMap
<String
, String
>();
55 params
.put(ClientDavexRepositoryFactory
.JACKRABBIT_DAVEX_URI
, repoUri
.toString());
56 // TODO better integrate with OSGi or other configuration than system
58 String remoteDefaultWorkspace
= System
.getProperty(
59 ClientDavexRepositoryFactory
.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
,
60 DEFAULT_JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
);
61 params
.put(ClientDavexRepositoryFactory
.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE
, remoteDefaultWorkspace
);
62 Repository repository
= null;
63 Session session
= null;
65 repository
= repositoryFactory
.getRepository(params
);
66 if (repository
!= null)
67 session
= repository
.login(workspace
);
68 } catch (Exception e
) {
72 if (session
== null) {
73 if (repoUri
.getPath() == null || repoUri
.getPath().equals("/"))
75 String repoUriStr
= repoUri
.toString();
76 if (repoUriStr
.endsWith("/"))
77 repoUriStr
= repoUriStr
.substring(0, repoUriStr
.length() - 1);
78 String nextRepoUriStr
= repoUriStr
.substring(0, repoUriStr
.lastIndexOf('/'));
79 String nextWorkspace
= repoUriStr
.substring(repoUriStr
.lastIndexOf('/') + 1);
82 nextUri
= new URI(nextRepoUriStr
);
83 } catch (URISyntaxException e
) {
84 throw new IllegalArgumentException("Badly formatted URI", e
);
86 return tryGetRepo(repositoryFactory
, nextUri
, nextWorkspace
);
88 JcrFileSystem fileSystem
= new JcrFileSystem(this, repository
);
89 fileSystems
.put(repoUri
.toString() + "/" + workspace
, fileSystem
);
95 public FileSystem
getFileSystem(URI uri
) {
96 return currentUserFileSystem(uri
);
100 public Path
getPath(URI uri
) {
101 JcrFileSystem fileSystem
= currentUserFileSystem(uri
);
102 if (fileSystem
== null)
104 fileSystem
= (JcrFileSystem
) newFileSystem(uri
, new HashMap
<String
, Object
>());
105 if (fileSystem
== null)
106 throw new IllegalArgumentException("No file system found for " + uri
);
107 } catch (IOException e
) {
108 throw new JcrFsException("Could not autocreate file system", e
);
112 repoUri
= new URI("http", uri
.getUserInfo(), uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null);
113 } catch (URISyntaxException e
) {
114 throw new IllegalArgumentException(e
);
116 String uriStr
= repoUri
.toString();
117 String localPath
= null;
118 for (String key
: fileSystems
.keySet()) {
119 if (uriStr
.startsWith(key
)) {
120 localPath
= uriStr
.toString().substring(key
.length());
123 if ("".equals(localPath
))
125 return fileSystem
.getPath(localPath
);
128 private JcrFileSystem
currentUserFileSystem(URI uri
) {
129 for (String key
: fileSystems
.keySet()) {
130 if (uri
.toString().startsWith(key
))
131 return fileSystems
.get(key
);
136 public static void main(String args
[]) {
138 DavexFsProvider fsProvider
= new DavexFsProvider();
139 Path path
= fsProvider
.getPath(new URI("davex://root:demo@localhost:7070/jcr/ego/"));
140 System
.out
.println(path
);
141 DirectoryStream
<Path
> ds
= Files
.newDirectoryStream(path
);
143 System
.out
.println("- " + p
);
145 } catch (Exception e
) {