DataSourceFileResolver.java

  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. import java.io.File;
  19. import java.io.IOException;

  20. import javax.activation.DataSource;
  21. import javax.activation.FileDataSource;

  22. /**
  23.  * Creates a {@code DataSource} based on a File. The implementation also resolves file resources.
  24.  *
  25.  * @since 1.3
  26.  */
  27. public class DataSourceFileResolver extends DataSourceBaseResolver {

  28.     /** The base directory of the resource when resolving relative paths */
  29.     private final File baseDir;

  30.     /**
  31.      * Constructs a new instance.
  32.      */
  33.     public DataSourceFileResolver() {
  34.         baseDir = new File(".");
  35.     }

  36.     /**
  37.      * Constructs a new instance.
  38.      *
  39.      * @param baseDir the base directory of the resource when resolving relative paths
  40.      */
  41.     public DataSourceFileResolver(final File baseDir) {
  42.         this.baseDir = baseDir;
  43.     }

  44.     /**
  45.      * Constructs a new instance.
  46.      *
  47.      * @param baseDir the base directory of the resource when resolving relative paths
  48.      * @param lenient shall we ignore resources not found or complain with an exception
  49.      */
  50.     public DataSourceFileResolver(final File baseDir, final boolean lenient) {
  51.         super(lenient);
  52.         this.baseDir = baseDir;
  53.     }

  54.     /**
  55.      * Gets the base directory used for resolving relative resource locations.
  56.      *
  57.      * @return the baseUrl
  58.      */
  59.     public File getBaseDir() {
  60.         return baseDir;
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public DataSource resolve(final String resourceLocation) throws IOException {
  65.         return resolve(resourceLocation, isLenient());
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
  70.         File file;
  71.         DataSource result = null;

  72.         if (!isCid(resourceLocation)) {
  73.             file = new File(resourceLocation);

  74.             if (!file.isAbsolute()) {
  75.                 file = getBaseDir() != null ? new File(getBaseDir(), resourceLocation) : new File(resourceLocation);
  76.             }

  77.             if (file.exists()) {
  78.                 result = new FileDataSource(file);
  79.             } else if (!isLenient) {
  80.                 throw new IOException("Cant resolve the following file resource :" + file.getAbsolutePath());
  81.             }
  82.         }

  83.         return result;
  84.     }
  85. }