001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.mail.resolver;
018
019import java.io.IOException;
020import java.nio.file.Files;
021import java.nio.file.OpenOption;
022import java.nio.file.Path;
023import java.nio.file.Paths;
024
025import javax.activation.DataSource;
026import javax.activation.FileTypeMap;
027
028import org.apache.commons.mail.activation.PathDataSource;
029
030/**
031 * Creates a {@link DataSource} based on a {@link Path}. The implementation also resolves file resources.
032 *
033 * @since 1.6.0
034 */
035public final class DataSourcePathResolver extends DataSourceBaseResolver {
036
037    /** The base directory of the resource when resolving relative paths */
038    private final Path baseDir;
039    private final OpenOption[] options;
040
041    /**
042     * Constructs a new instance.
043     */
044    public DataSourcePathResolver() {
045        this(Paths.get("."));
046    }
047
048    /**
049     * Constructs a new instance.
050     *
051     * @param baseDir the base directory of the resource when resolving relative paths
052     */
053    public DataSourcePathResolver(final Path baseDir) {
054        this(baseDir, false);
055    }
056
057    /**
058     * Constructs a new instance.
059     *
060     * @param baseDir the base directory of the resource when resolving relative paths
061     * @param lenient shall we ignore resources not found or complain with an exception
062     * @param options options for opening streams.
063     */
064    public DataSourcePathResolver(final Path baseDir, final boolean lenient, final OpenOption... options) {
065        super(lenient);
066        this.baseDir = baseDir;
067        this.options = options;
068    }
069
070    /**
071     * Gets the base directory used for resolving relative resource locations.
072     *
073     * @return the baseUrl
074     */
075    public Path getBaseDir() {
076        return baseDir;
077    }
078
079    /** {@inheritDoc} */
080    @Override
081    public DataSource resolve(final String resourceLocation) throws IOException {
082        return resolve(resourceLocation, isLenient());
083    }
084
085    /** {@inheritDoc} */
086    @Override
087    public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
088        Path file;
089        DataSource result = null;
090
091        if (!isCid(resourceLocation)) {
092            file = Paths.get(resourceLocation);
093
094            if (!file.isAbsolute()) {
095                file = getBaseDir() != null ? getBaseDir().resolve(resourceLocation) : Paths.get(resourceLocation);
096            }
097
098            if (Files.exists(file)) {
099                result = new PathDataSource(file, FileTypeMap.getDefaultFileTypeMap(), options);
100            } else if (!isLenient) {
101                throw new IOException("Cant resolve the following file resource :" + file.toAbsolutePath());
102            }
103        }
104
105        return result;
106    }
107}