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.File;
20 import java.io.IOException;
21
22 import javax.activation.DataSource;
23 import javax.activation.FileDataSource;
24
25 /**
26 * Creates a {@code DataSource} based on a File. The implementation also resolves file resources.
27 *
28 * @since 1.3
29 */
30 public class DataSourceFileResolver extends DataSourceBaseResolver {
31
32 /** The base directory of the resource when resolving relative paths */
33 private final File baseDir;
34
35 /**
36 * Constructs a new instance.
37 */
38 public DataSourceFileResolver() {
39 baseDir = new File(".");
40 }
41
42 /**
43 * Constructs a new instance.
44 *
45 * @param baseDir the base directory of the resource when resolving relative paths
46 */
47 public DataSourceFileResolver(final File baseDir) {
48 this.baseDir = baseDir;
49 }
50
51 /**
52 * Constructs a new instance.
53 *
54 * @param baseDir the base directory of the resource when resolving relative paths
55 * @param lenient shall we ignore resources not found or complain with an exception
56 */
57 public DataSourceFileResolver(final File baseDir, final boolean lenient) {
58 super(lenient);
59 this.baseDir = baseDir;
60 }
61
62 /**
63 * Gets the base directory used for resolving relative resource locations.
64 *
65 * @return the baseUrl
66 */
67 public File getBaseDir() {
68 return baseDir;
69 }
70
71 /** {@inheritDoc} */
72 @Override
73 public DataSource resolve(final String resourceLocation) throws IOException {
74 return resolve(resourceLocation, isLenient());
75 }
76
77 /** {@inheritDoc} */
78 @Override
79 public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
80 File file;
81 DataSource result = null;
82
83 if (!isCid(resourceLocation)) {
84 file = new File(resourceLocation);
85
86 if (!file.isAbsolute()) {
87 file = getBaseDir() != null ? new File(getBaseDir(), resourceLocation) : new File(resourceLocation);
88 }
89
90 if (file.exists()) {
91 result = new FileDataSource(file);
92 } else if (!isLenient) {
93 throw new IOException("Cant resolve the following file resource :" + file.getAbsolutePath());
94 }
95 }
96
97 return result;
98 }
99 }