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