]> git.argeo.org Git - lgpl/argeo-commons.git/blob - DavexFsProvider.java
331e9acdd948062ebb6644b4b70e26424d1f1be5
[lgpl/argeo-commons.git] / 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.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;
23
24 /**
25 * A file system provider based on a JCR repository remotely accessed via the
26 * DAVEX protocol.
27 */
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";
31
32 private Map<String, JcrFileSystem> fileSystems = new HashMap<>();
33
34 @Override
35 public String getScheme() {
36 return "davex";
37 }
38
39 @Override
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");
43 try {
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);
52 }
53 }
54
55 private JcrFileSystem tryGetRepo(RepositoryFactory repositoryFactory, URI repoUri, String workspace)
56 throws IOException {
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;
62 try {
63 repository = repositoryFactory.getRepository(params);
64 if (repository != null)
65 session = repository.login(workspace);
66 } catch (Exception e) {
67 // silent
68 }
69
70 if (session == null) {
71 if (repoUri.getPath() == null || repoUri.getPath().equals("/"))
72 return null;
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);
78 URI nextUri;
79 try {
80 nextUri = new URI(nextRepoUriStr);
81 } catch (URISyntaxException e) {
82 throw new ArgeoJcrException("Badly formatted URI", e);
83 }
84 return tryGetRepo(repositoryFactory, nextUri, nextWorkspace);
85 } else {
86 JcrFileSystem fileSystem = new JcrFileSystem(this, repository);
87 fileSystems.put(repoUri.toString() + "/" + workspace, fileSystem);
88 return fileSystem;
89 }
90 }
91
92 @Override
93 public FileSystem getFileSystem(URI uri) {
94 return currentUserFileSystem(uri);
95 }
96
97 @Override
98 public Path getPath(URI uri) {
99 JcrFileSystem fileSystem = currentUserFileSystem(uri);
100 if (fileSystem == null)
101 try {
102 fileSystem = (JcrFileSystem) newFileSystem(uri, new HashMap<String, Object>());
103 } catch (IOException e) {
104 throw new JcrFsException("Could not autocreate file system", e);
105 }
106 URI repoUri = null;
107 try {
108 repoUri = new URI("http", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null, null);
109 } catch (URISyntaxException e) {
110 throw new IllegalArgumentException(e);
111 }
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());
117 }
118 }
119 if ("".equals(localPath))
120 localPath = "/";
121 return fileSystem.getPath(localPath);
122 }
123
124 private JcrFileSystem currentUserFileSystem(URI uri) {
125 for (String key : fileSystems.keySet()) {
126 if (uri.toString().startsWith(key))
127 return fileSystems.get(key);
128 }
129 return null;
130 }
131
132 public static void main(String args[]) {
133 try {
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);
138 for (Path p : ds) {
139 System.out.println("- " + p);
140 }
141 } catch (Exception e) {
142 e.printStackTrace();
143 }
144 }
145 }