]> git.argeo.org Git - lgpl/argeo-commons.git/blob - argeo/jackrabbit/fs/DavexFsProvider.java
Prepare next development cycle
[lgpl/argeo-commons.git] / argeo / jackrabbit / fs / DavexFsProvider.java
1 package org.argeo.jackrabbit.fs;
2
3 import java.io.IOException;
4 import java.net.URI;
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;
12 import java.util.Map;
13
14 import javax.jcr.Repository;
15 import javax.jcr.RepositoryFactory;
16 import javax.jcr.Session;
17
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;
22
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";
26
27 private Map<String, JcrFileSystem> fileSystems = new HashMap<>();
28
29 @Override
30 public String getScheme() {
31 return "davex";
32 }
33
34 @Override
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");
38 try {
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);
47 }
48 }
49
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;
56 try {
57 repository = repositoryFactory.getRepository(params);
58 if (repository != null)
59 session = repository.login(workspace);
60 } catch (Exception e) {
61 // silent
62 }
63
64 if (session == null) {
65 if (repoUri.getPath() == null || repoUri.getPath().equals("/"))
66 return null;
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);
72 URI nextUri;
73 try {
74 nextUri = new URI(nextRepoUriStr);
75 } catch (URISyntaxException e) {
76 throw new ArgeoJcrException("Badly formatted URI", e);
77 }
78 return tryGetRepo(repositoryFactory, nextUri, nextWorkspace);
79 } else {
80 JcrFileSystem fileSystem = new JcrFileSystem(this, session);
81 fileSystems.put(repoUri.toString() + "/" + workspace, fileSystem);
82 return fileSystem;
83 }
84 }
85
86 @Override
87 public FileSystem getFileSystem(URI uri) {
88 return currentUserFileSystem(uri);
89 }
90
91 @Override
92 public Path getPath(URI uri) {
93 JcrFileSystem fileSystem = currentUserFileSystem(uri);
94 if (fileSystem == null)
95 try {
96 fileSystem = (JcrFileSystem) newFileSystem(uri, new HashMap<String, Object>());
97 } catch (IOException e) {
98 throw new JcrFsException("Could not autocreate file system", e);
99 }
100 URI repoUri = null;
101 try {
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
105 e.printStackTrace();
106 }
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());
112 }
113 }
114 return fileSystem.getPath(localPath);
115 }
116
117 private JcrFileSystem currentUserFileSystem(URI uri) {
118 for (String key : fileSystems.keySet()) {
119 if (uri.toString().startsWith(key))
120 return fileSystems.get(key);
121 }
122 return null;
123 }
124
125 public static void main(String args[]) {
126 try {
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);
131 for (Path p : ds) {
132 System.out.println("- " + p);
133 }
134 } catch (Exception e) {
135 e.printStackTrace();
136 }
137 }
138 }