DataSourcePathResolver.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.IOException;
  19. import java.nio.file.Files;
  20. import java.nio.file.OpenOption;
  21. import java.nio.file.Path;
  22. import java.nio.file.Paths;

  23. import javax.activation.DataSource;
  24. import javax.activation.FileTypeMap;

  25. import org.apache.commons.mail2.javax.activation.PathDataSource;

  26. /**
  27.  * Creates a {@link DataSource} based on a {@link Path}. The implementation also resolves file resources.
  28.  *
  29.  * @since 1.6.0
  30.  */
  31. public final class DataSourcePathResolver extends DataSourceBaseResolver {

  32.     /**
  33.      * The base directory of the resource when resolving relative paths.
  34.      */
  35.     private final Path baseDir;

  36.     /**
  37.      * NIO options to open the data source.
  38.      */
  39.     private final OpenOption[] options;

  40.     /**
  41.      * Constructs a new instance.
  42.      */
  43.     public DataSourcePathResolver() {
  44.         this(Paths.get("."));
  45.     }

  46.     /**
  47.      * Constructs a new instance.
  48.      *
  49.      * @param baseDir the base directory of the resource when resolving relative paths
  50.      */
  51.     public DataSourcePathResolver(final Path baseDir) {
  52.         this(baseDir, false);
  53.     }

  54.     /**
  55.      * Constructs a new instance.
  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.      * @param options options for opening streams.
  60.      */
  61.     public DataSourcePathResolver(final Path baseDir, final boolean lenient, final OpenOption... options) {
  62.         super(lenient);
  63.         this.baseDir = baseDir;
  64.         this.options = options;
  65.     }

  66.     /**
  67.      * Gets the base directory used for resolving relative resource locations.
  68.      *
  69.      * @return the baseUrl
  70.      */
  71.     public Path getBaseDir() {
  72.         return baseDir;
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public DataSource resolve(final String resourceLocation) throws IOException {
  77.         return resolve(resourceLocation, isLenient());
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
  82.         Path file;
  83.         DataSource result = null;

  84.         if (!isCid(resourceLocation)) {
  85.             file = Paths.get(resourceLocation);

  86.             if (!file.isAbsolute()) {
  87.                 file = getBaseDir() != null ? getBaseDir().resolve(resourceLocation) : Paths.get(resourceLocation);
  88.             }

  89.             if (Files.exists(file)) {
  90.                 result = new PathDataSource(file, FileTypeMap.getDefaultFileTypeMap(), options);
  91.             } else if (!isLenient) {
  92.                 throw new IOException("Cant resolve the following file resource :" + file.toAbsolutePath());
  93.             }
  94.         }

  95.         return result;
  96.     }
  97. }