]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jackrabbit/fs/DavexFsProvider.java
Move file system support to JCR bundle.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / 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 throws IOException {
52 Map<String, String> params = new HashMap<String, String>();
53 params.put(JACKRABBIT_REPOSITORY_URI, repoUri.toString());
54 params.put(JACKRABBIT_REMOTE_DEFAULT_WORKSPACE, "main");
55 Repository repository = null;
56 Session session = null;
57 try {
58 repository = repositoryFactory.getRepository(params);
59 if (repository != null)
60 session = repository.login(workspace);
61 } catch (Exception e) {
62 // silent
63 }
64
65 if (session == null) {
66 if (repoUri.getPath() == null || repoUri.getPath().equals("/"))
67 return null;
68 String repoUriStr = repoUri.toString();
69 if (repoUriStr.endsWith("/"))
70 repoUriStr = repoUriStr.substring(0, repoUriStr.length() - 1);
71 String nextRepoUriStr = repoUriStr.substring(0, repoUriStr.lastIndexOf('/'));
72 String nextWorkspace = repoUriStr.substring(repoUriStr.lastIndexOf('/') + 1);
73 URI nextUri;
74 try {
75 nextUri = new URI(nextRepoUriStr);
76 } catch (URISyntaxException e) {
77 throw new ArgeoJcrException("Badly formatted URI", e);
78 }
79 return tryGetRepo(repositoryFactory, nextUri, nextWorkspace);
80 } else {
81 JcrFileSystem fileSystem = new JcrFileSystem(this, session);
82 fileSystems.put(repoUri.toString() + "/" + workspace, fileSystem);
83 return fileSystem;
84 }
85 }
86
87 @Override
88 public FileSystem getFileSystem(URI uri) {
89 return currentUserFileSystem(uri);
90 }
91
92 @Override
93 public Path getPath(URI uri) {
94 JcrFileSystem fileSystem = currentUserFileSystem(uri);
95 if (fileSystem == null)
96 try {
97 fileSystem = (JcrFileSystem) newFileSystem(uri, new HashMap<String, Object>());
98 } catch (IOException e) {
99 throw new JcrFsException("Could not autocreate file system", e);
100 }
101 URI repoUri = null;
102 try {
103 repoUri = new URI("http", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null, null);
104 } catch (URISyntaxException e) {
105 // TODO Auto-generated catch block
106 e.printStackTrace();
107 }
108 String uriStr = repoUri.toString();
109 String localPath = null;
110 for (String key : fileSystems.keySet()) {
111 if (uriStr.startsWith(key)) {
112 localPath = uriStr.toString().substring(key.length());
113 }
114 }
115 return fileSystem.getPath(localPath);
116 }
117
118 private JcrFileSystem currentUserFileSystem(URI uri) {
119 for (String key : fileSystems.keySet()) {
120 if (uri.toString().startsWith(key))
121 return fileSystems.get(key);
122 }
123 return null;
124 }
125
126 public static void main(String args[]) {
127 try {
128 DavexFsProvider fsProvider = new DavexFsProvider();
129 Path path = fsProvider.getPath(new URI("davex://root:demo@localhost:7070/jcr/node/main/home/"));
130 System.out.println(path);
131 DirectoryStream<Path> ds = Files.newDirectoryStream(path);
132 for (Path p : ds) {
133 System.out.println("- " + p);
134 }
135 } catch (Exception e) {
136 e.printStackTrace();
137 }
138 }
139 }