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