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.mail2.javax.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.mail2.javax.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    /**
038     * The base directory of the resource when resolving relative paths.
039     */
040    private final Path baseDir;
041
042    /**
043     * NIO options to open the data source.
044     */
045    private final OpenOption[] options;
046
047    /**
048     * Constructs a new instance.
049     */
050    public DataSourcePathResolver() {
051        this(Paths.get("."));
052    }
053
054    /**
055     * Constructs a new instance.
056     *
057     * @param baseDir the base directory of the resource when resolving relative paths
058     */
059    public DataSourcePathResolver(final Path baseDir) {
060        this(baseDir, false);
061    }
062
063    /**
064     * Constructs a new instance.
065     *
066     * @param baseDir the base directory of the resource when resolving relative paths
067     * @param lenient shall we ignore resources not found or complain with an exception
068     * @param options options for opening streams.
069     */
070    public DataSourcePathResolver(final Path baseDir, final boolean lenient, final OpenOption... options) {
071        super(lenient);
072        this.baseDir = baseDir;
073        this.options = options;
074    }
075
076    /**
077     * Gets the base directory used for resolving relative resource locations.
078     *
079     * @return the baseUrl
080     */
081    public Path getBaseDir() {
082        return baseDir;
083    }
084
085    /** {@inheritDoc} */
086    @Override
087    public DataSource resolve(final String resourceLocation) throws IOException {
088        return resolve(resourceLocation, isLenient());
089    }
090
091    /** {@inheritDoc} */
092    @Override
093    public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
094        Path file;
095        DataSource result = null;
096
097        if (!isCid(resourceLocation)) {
098            file = Paths.get(resourceLocation);
099
100            if (!file.isAbsolute()) {
101                file = getBaseDir() != null ? getBaseDir().resolve(resourceLocation) : Paths.get(resourceLocation);
102            }
103
104            if (Files.exists(file)) {
105                result = new PathDataSource(file, FileTypeMap.getDefaultFileTypeMap(), options);
106            } else if (!isLenient) {
107                throw new IOException("Cant resolve the following file resource :" + file.toAbsolutePath());
108            }
109        }
110
111        return result;
112    }
113}