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.jakarta.resolver;
018
019import java.io.File;
020import java.io.IOException;
021
022import jakarta.activation.DataSource;
023import jakarta.activation.FileDataSource;
024
025/**
026 * Creates a {@code DataSource} based on a File. The implementation also resolves file resources.
027 *
028 * @since 1.3
029 */
030public class DataSourceFileResolver extends DataSourceBaseResolver {
031
032    /** The base directory of the resource when resolving relative paths */
033    private final File baseDir;
034
035    /**
036     * Constructs a new instance.
037     */
038    public DataSourceFileResolver() {
039        baseDir = new File(".");
040    }
041
042    /**
043     * Constructs a new instance.
044     *
045     * @param baseDir the base directory of the resource when resolving relative paths
046     */
047    public DataSourceFileResolver(final File baseDir) {
048        this.baseDir = baseDir;
049    }
050
051    /**
052     * Constructs a new instance.
053     *
054     * @param baseDir the base directory of the resource when resolving relative paths
055     * @param lenient shall we ignore resources not found or complain with an exception
056     */
057    public DataSourceFileResolver(final File baseDir, final boolean lenient) {
058        super(lenient);
059        this.baseDir = baseDir;
060    }
061
062    /**
063     * Gets the base directory used for resolving relative resource locations.
064     *
065     * @return the baseUrl
066     */
067    public File getBaseDir() {
068        return baseDir;
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public DataSource resolve(final String resourceLocation) throws IOException {
074        return resolve(resourceLocation, isLenient());
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
080        File file;
081        DataSource result = null;
082
083        if (!isCid(resourceLocation)) {
084            file = new File(resourceLocation);
085
086            if (!file.isAbsolute()) {
087                file = getBaseDir() != null ? new File(getBaseDir(), resourceLocation) : new File(resourceLocation);
088            }
089
090            if (file.exists()) {
091                result = new FileDataSource(file);
092            } else if (!isLenient) {
093                throw new IOException("Cant resolve the following file resource :" + file.getAbsolutePath());
094            }
095        }
096
097        return result;
098    }
099}