|
1 | 1 | package com.fasterxml.jackson.databind.ext;
|
2 | 2 |
|
| 3 | +import java.io.File; |
3 | 4 | import java.io.IOException;
|
4 | 5 | import java.net.URI;
|
5 | 6 | import java.net.URISyntaxException;
|
| 7 | +import java.nio.file.FileSystemNotFoundException; |
6 | 8 | import java.nio.file.Path;
|
7 | 9 | import java.nio.file.Paths;
|
| 10 | +import java.nio.file.spi.FileSystemProvider; |
| 11 | +import java.util.ServiceLoader; |
8 | 12 |
|
9 | 13 | import com.fasterxml.jackson.core.JsonParser;
|
10 | 14 | import com.fasterxml.jackson.core.JsonToken;
|
11 | 15 | import com.fasterxml.jackson.databind.DeserializationContext;
|
12 | 16 | import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
|
13 | 17 |
|
| 18 | +import static java.lang.Character.isLetter; |
| 19 | + |
14 | 20 | /**
|
15 | 21 | * @since 2.8
|
16 | 22 | */
|
17 | 23 | public class NioPathDeserializer extends StdScalarDeserializer<Path>
|
18 | 24 | {
|
19 | 25 | private static final long serialVersionUID = 1;
|
20 | 26 |
|
| 27 | + private static final boolean areWindowsFilePathsSupported; |
| 28 | + static { |
| 29 | + boolean isWindowsRootFound = false; |
| 30 | + for (File file : File.listRoots()) { |
| 31 | + String path = file.getPath(); |
| 32 | + if (path.length() >= 2 && isLetter(path.charAt(0)) && path.charAt(1) == ':') { |
| 33 | + isWindowsRootFound = true; |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + areWindowsFilePathsSupported = isWindowsRootFound; |
| 38 | + } |
| 39 | + |
21 | 40 | public NioPathDeserializer() { super(Path.class); }
|
22 | 41 |
|
23 | 42 | @Override
|
24 | 43 | public Path deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
25 | 44 | if (!p.hasToken(JsonToken.VALUE_STRING)) {
|
26 | 45 | return (Path) ctxt.handleUnexpectedToken(Path.class, p);
|
27 | 46 | }
|
| 47 | + |
28 | 48 | final String value = p.getText();
|
| 49 | + |
29 | 50 | // If someone gives us an input with no : at all, treat as local path, instead of failing
|
30 | 51 | // with invalid URI.
|
31 | 52 | if (value.indexOf(':') < 0) {
|
32 | 53 | return Paths.get(value);
|
33 | 54 | }
|
| 55 | + |
| 56 | + if (areWindowsFilePathsSupported) { |
| 57 | + if (value.length() >= 2 && isLetter(value.charAt(0)) && value.charAt(1) == ':') { |
| 58 | + return Paths.get(value); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + final URI uri; |
34 | 63 | try {
|
35 |
| - URI uri = new URI(value); |
36 |
| - return Paths.get(uri); |
| 64 | + uri = new URI(value); |
37 | 65 | } catch (URISyntaxException e) {
|
38 | 66 | return (Path) ctxt.handleInstantiationProblem(handledType(), value, e);
|
39 | 67 | }
|
| 68 | + try { |
| 69 | + return Paths.get(uri); |
| 70 | + } catch (FileSystemNotFoundException cause) { |
| 71 | + try { |
| 72 | + final String scheme = uri.getScheme(); |
| 73 | + // We want to use the current thread's context class loader, not system class loader that is used in Paths.get(): |
| 74 | + for (FileSystemProvider provider : ServiceLoader.load(FileSystemProvider.class)) { |
| 75 | + if (provider.getScheme().equalsIgnoreCase(scheme)) { |
| 76 | + return provider.getPath(uri); |
| 77 | + } |
| 78 | + } |
| 79 | + return (Path) ctxt.handleInstantiationProblem(handledType(), value, cause); |
| 80 | + } catch (Throwable e) { |
| 81 | + e.addSuppressed(cause); |
| 82 | + return (Path) ctxt.handleInstantiationProblem(handledType(), value, e); |
| 83 | + } |
| 84 | + } catch (Throwable e) { |
| 85 | + return (Path) ctxt.handleInstantiationProblem(handledType(), value, e); |
| 86 | + } |
40 | 87 | }
|
41 | 88 | }
|
0 commit comments