]> git.argeo.org Git - lgpl/argeo-commons.git/blob - fs/DavexFsProvider.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.argeo.jackrabbit.client.ClientDavexRepositoryFactory;
19 import org.argeo.jcr.fs.JcrFileSystem;
20 import org.argeo.jcr.fs.JcrFsException;
21
22 /**
23 * A file system provider based on a JCR repository remotely accessed via the
24 * DAVEX protocol.
25 */
26 public class DavexFsProvider extends AbstractJackrabbitFsProvider {
27 final static String DEFAULT_JACKRABBIT_REMOTE_DEFAULT_WORKSPACE = "sys";
28
29 private Map<String, JcrFileSystem> fileSystems = new HashMap<>();
30
31 @Override
32 public String getScheme() {
33 return "davex";
34 }
35
36 @Override
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");
40 try {
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);
49 }
50 }
51
52 private JcrFileSystem tryGetRepo(RepositoryFactory repositoryFactory, URI repoUri, String workspace)
53 throws IOException {
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
57 // properties.
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;
64 try {
65 repository = repositoryFactory.getRepository(params);
66 if (repository != null)
67 session = repository.login(workspace);
68 } catch (Exception e) {
69 // silent
70 }
71
72 if (session == null) {
73 if (repoUri.getPath() == null || repoUri.getPath().equals("/"))
74 return null;
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);
80 URI nextUri;
81 try {
82 nextUri = new URI(nextRepoUriStr);
83 } catch (URISyntaxException e) {
84 throw new IllegalArgumentException("Badly formatted URI", e);
85 }
86 return tryGetRepo(repositoryFactory, nextUri, nextWorkspace);
87 } else {
88 JcrFileSystem fileSystem = new JcrFileSystem(this, repository);
89 fileSystems.put(repoUri.toString() + "/" + workspace, fileSystem);
90 return fileSystem;
91 }
92 }
93
94 @Override
95 public FileSystem getFileSystem(URI uri) {
96 return currentUserFileSystem(uri);
97 }
98
99 @Override
100 public Path getPath(URI uri) {
101 JcrFileSystem fileSystem = currentUserFileSystem(uri);
102 if (fileSystem == null)
103 try {
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);
109 }
110 URI repoUri = null;
111 try {
112 repoUri = new URI("http", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null, null);
113 } catch (URISyntaxException e) {
114 throw new IllegalArgumentException(e);
115 }
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());
121 }
122 }
123 if ("".equals(localPath))
124 localPath = "/";
125 return fileSystem.getPath(localPath);
126 }
127
128 private JcrFileSystem currentUserFileSystem(URI uri) {
129 for (String key : fileSystems.keySet()) {
130 if (uri.toString().startsWith(key))
131 return fileSystems.get(key);
132 }
133 return null;
134 }
135
136 public static void main(String args[]) {
137 try {
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);
142 for (Path p : ds) {
143 System.out.println("- " + p);
144 }
145 } catch (Exception e) {
146 e.printStackTrace();
147 }
148 }
149 }