1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.niofs;
20
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.nio.file.FileSystemNotFoundException;
24 import java.nio.file.InvalidPathException;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.nio.file.spi.FileSystemProvider;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Objects;
31
32 import org.apache.logging.log4j.LogManager;
33 import org.mycore.common.config.MCRConfiguration2;
34
35
36
37
38
39 final class MCRPaths {
40
41 static final String DEFAULT_SCHEME_PROPERTY = "MCR.NIO.DefaultScheme";
42
43 static List<FileSystemProvider> webAppProvider = new ArrayList<>(4);
44
45 private MCRPaths() {
46 }
47
48 static URI getURI(String owner, String path) throws URISyntaxException {
49 String scheme = getDefaultScheme();
50 return getURI(scheme, owner, path);
51 }
52
53 private static String getDefaultScheme() {
54 return MCRConfiguration2.getString(DEFAULT_SCHEME_PROPERTY).orElse("ifs2");
55 }
56
57 static URI getURI(String scheme, String owner, String path) throws URISyntaxException {
58 boolean needSeperator = Objects.requireNonNull(path, "No 'path' specified.").isEmpty()
59 || path.charAt(0) != MCRAbstractFileSystem.SEPARATOR;
60 String aPath = needSeperator ? (MCRAbstractFileSystem.SEPARATOR_STRING + path) : path;
61 String finalPath = MCRAbstractFileSystem.SEPARATOR_STRING
62 + Objects.requireNonNull(owner, "No 'owner' specified.") + ":" + aPath;
63 return new URI(Objects.requireNonNull(scheme, "No 'scheme' specified."), null, finalPath, null);
64 }
65
66 static Path getPath(String owner, String path) {
67 String scheme = getDefaultScheme();
68 return getPath(scheme, owner, path);
69 }
70
71 static Path getPath(String scheme, String owner, String path) {
72 URI uri;
73 try {
74 uri = getURI(scheme, owner, path);
75 LogManager.getLogger(MCRPaths.class).debug("Generated path URI:{}", uri);
76 } catch (URISyntaxException e) {
77 throw new InvalidPathException(path, "URI syntax error (" + e.getMessage() + ") for path");
78 }
79 try {
80 return Paths.get(uri);
81 } catch (FileSystemNotFoundException e) {
82 for (FileSystemProvider provider : webAppProvider) {
83 if (provider.getScheme().equals(uri.getScheme())) {
84 return provider.getPath(uri);
85 }
86 }
87 throw e;
88 }
89 }
90
91 static void addFileSystemProvider(FileSystemProvider provider) {
92 webAppProvider.add(provider);
93 }
94 }