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