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.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   */
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       * Constructor.
37       */
38      public DataSourceFileResolver()
39      {
40          baseDir = new File(".");
41      }
42  
43      /**
44       * Constructor.
45       *
46       * @param baseDir the base directory of the resource when resolving relative paths
47       */
48      public DataSourceFileResolver(final File baseDir)
49      {
50          this.baseDir = baseDir;
51      }
52  
53      /**
54       * Constructor.
55       *
56       * @param baseDir the base directory of the resource when resolving relative paths
57       * @param lenient shall we ignore resources not found or complain with an exception
58       */
59      public DataSourceFileResolver(final File baseDir, final boolean lenient)
60      {
61          super(lenient);
62          this.baseDir = baseDir;
63      }
64  
65      /**
66       * Get the base directory used for resolving relative resource locations.
67       *
68       * @return the baseUrl
69       */
70      public File getBaseDir()
71      {
72          return baseDir;
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public DataSource resolve(final String resourceLocation) throws IOException
78      {
79          return resolve(resourceLocation, isLenient());
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException
85      {
86          File file;
87          DataSource result = null;
88  
89          if (!isCid(resourceLocation))
90          {
91              file = new File(resourceLocation);
92  
93              if (!file.isAbsolute())
94              {
95                  file = getBaseDir() != null ? new File(getBaseDir(), resourceLocation) : new File(resourceLocation);
96              }
97  
98              if (file.exists())
99              {
100                 result = new FileDataSource(file);
101             }
102             else
103             {
104                 if (!isLenient)
105                 {
106                     throw new IOException("Cant resolve the following file resource :" + file.getAbsolutePath());
107                 }
108             }
109         }
110 
111         return result;
112     }
113 }