1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.mail2.jakarta.resolver;
18
19 import java.io.IOException;
20 import java.nio.file.Files;
21 import java.nio.file.OpenOption;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24
25 import org.apache.commons.mail2.jakarta.activation.PathDataSource;
26
27 import jakarta.activation.DataSource;
28 import jakarta.activation.FileTypeMap;
29
30
31
32
33
34
35 public final class DataSourcePathResolver extends DataSourceBaseResolver {
36
37
38
39
40 private final Path baseDir;
41
42
43
44
45 private final OpenOption[] options;
46
47
48
49
50 public DataSourcePathResolver() {
51 this(Paths.get("."));
52 }
53
54
55
56
57
58
59 public DataSourcePathResolver(final Path baseDir) {
60 this(baseDir, false);
61 }
62
63
64
65
66
67
68
69
70 public DataSourcePathResolver(final Path baseDir, final boolean lenient, final OpenOption... options) {
71 super(lenient);
72 this.baseDir = baseDir;
73 this.options = options;
74 }
75
76
77
78
79
80
81 public Path getBaseDir() {
82 return baseDir;
83 }
84
85
86 @Override
87 public DataSource resolve(final String resourceLocation) throws IOException {
88 return resolve(resourceLocation, isLenient());
89 }
90
91
92 @Override
93 public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
94 Path file;
95 DataSource result = null;
96
97 if (!isCid(resourceLocation)) {
98 file = Paths.get(resourceLocation);
99
100 if (!file.isAbsolute()) {
101 file = getBaseDir() != null ? getBaseDir().resolve(resourceLocation) : Paths.get(resourceLocation);
102 }
103
104 if (Files.exists(file)) {
105 result = new PathDataSource(file, FileTypeMap.getDefaultFileTypeMap(), options);
106 } else if (!isLenient) {
107 throw new IOException("Cant resolve the following file resource :" + file.toAbsolutePath());
108 }
109 }
110
111 return result;
112 }
113 }