View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.mail2.javax.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 javax.activation.DataSource;
26  import javax.activation.FileTypeMap;
27  
28  import org.apache.commons.mail2.javax.activation.PathDataSource;
29  
30  /**
31   * Creates a {@link DataSource} based on a {@link Path}. The implementation also resolves file resources.
32   *
33   * @since 1.6.0
34   */
35  public final class DataSourcePathResolver extends DataSourceBaseResolver {
36  
37      /**
38       * The base directory of the resource when resolving relative paths.
39       */
40      private final Path baseDir;
41  
42      /**
43       * NIO options to open the data source.
44       */
45      private final OpenOption[] options;
46  
47      /**
48       * Constructs a new instance.
49       */
50      public DataSourcePathResolver() {
51          this(Paths.get("."));
52      }
53  
54      /**
55       * Constructs a new instance.
56       *
57       * @param baseDir the base directory of the resource when resolving relative paths
58       */
59      public DataSourcePathResolver(final Path baseDir) {
60          this(baseDir, false);
61      }
62  
63      /**
64       * Constructs a new instance.
65       *
66       * @param baseDir the base directory of the resource when resolving relative paths
67       * @param lenient shall we ignore resources not found or complain with an exception
68       * @param options options for opening streams.
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       * Gets the base directory used for resolving relative resource locations.
78       *
79       * @return the baseUrl
80       */
81      public Path getBaseDir() {
82          return baseDir;
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public DataSource resolve(final String resourceLocation) throws IOException {
88          return resolve(resourceLocation, isLenient());
89      }
90  
91      /** {@inheritDoc} */
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 }