View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * @author Thomas Scheffler (yagee)
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  }