Coverage Report - org.apache.commons.mail.resolver.DataSourceUrlResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
DataSourceUrlResolver
80%
20/25
64%
9/14
3.333
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.mail.resolver;
 18  
 
 19  
 import javax.activation.DataSource;
 20  
 import javax.activation.URLDataSource;
 21  
 import java.io.IOException;
 22  
 import java.net.MalformedURLException;
 23  
 import java.net.URL;
 24  
 
 25  
 /**
 26  
  * Creates a <code>DataSource</code> based on an URL.
 27  
  *
 28  
  * @since 1.3
 29  
  * @version $Id: DataSourceUrlResolver.java 1420381 2012-12-11 20:18:05Z tn $
 30  
  */
 31  
 public class DataSourceUrlResolver extends DataSourceBaseResolver
 32  
 {
 33  
     /** the base url of the resource when resolving relative paths */
 34  
     private final URL baseUrl;
 35  
 
 36  
     /**
 37  
      * Constructor.
 38  
      *
 39  
      * @param baseUrl the base URL used for resolving relative resource locations
 40  
      */
 41  
     public DataSourceUrlResolver(final URL baseUrl)
 42  
     {
 43  0
         super();
 44  0
         this.baseUrl = baseUrl;
 45  0
     }
 46  
 
 47  
     /**
 48  
      * Constructor.
 49  
      *
 50  
      * @param baseUrl the base URL used for resolving relative resource locations
 51  
      * @param lenient shall we ignore resources not found or complain with an exception
 52  
      */
 53  
     public DataSourceUrlResolver(final URL baseUrl, final boolean lenient)
 54  
     {
 55  11
         super(lenient);
 56  11
         this.baseUrl = baseUrl;
 57  11
     }
 58  
 
 59  
     /**
 60  
      * Get the base URL used for resolving relative resource locations.
 61  
      *
 62  
      * @return the baseUrl
 63  
      */
 64  
     public URL getBaseUrl()
 65  
     {
 66  18
         return baseUrl;
 67  
     }
 68  
 
 69  
     /** {@inheritDoc} */
 70  
     public DataSource resolve(String resourceLocation) throws IOException
 71  
     {
 72  17
         return resolve(resourceLocation, isLenient());
 73  
     }
 74  
 
 75  
     /** {@inheritDoc} */
 76  
     public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException
 77  
     {
 78  21
         DataSource result = null;
 79  
 
 80  
         try
 81  
         {
 82  21
             if (!isCid(resourceLocation))
 83  
             {
 84  21
                 URL url = createUrl(resourceLocation);
 85  21
                 result = new URLDataSource(url);
 86  21
                 result.getInputStream();
 87  
             }
 88  
 
 89  14
             return result;
 90  
         }
 91  7
         catch (IOException e)
 92  
         {
 93  7
             if (isLenient)
 94  
             {
 95  6
                 return null;
 96  
             }
 97  
             else
 98  
             {
 99  1
                 throw e;
 100  
             }
 101  
         }
 102  
     }
 103  
 
 104  
     /**
 105  
      * Create an URL based on a base URL and a resource location suitable for loading
 106  
      * the resource.
 107  
      *
 108  
      * @param resourceLocation a resource location
 109  
      * @return the corresponding URL
 110  
      * @throws java.net.MalformedURLException creating the URL failed
 111  
      */
 112  
     private URL createUrl(final String resourceLocation) throws MalformedURLException
 113  
     {
 114  
         // if we get an non-existing base url than the resource can
 115  
         // be directly used to create an URL
 116  21
         if (baseUrl == null)
 117  
         {
 118  0
             return new URL(resourceLocation);
 119  
         }
 120  
 
 121  
         // if we get an non-existing location what we shall do?
 122  21
         if (resourceLocation == null || resourceLocation.length() == 0)
 123  
         {
 124  0
             throw new IllegalArgumentException("No resource defined");
 125  
         }
 126  
 
 127  
         // if we get a stand-alone resource than ignore the base url
 128  21
         if (isFileUrl(resourceLocation) || isHttpUrl(resourceLocation))
 129  
         {
 130  3
             return new URL(resourceLocation);
 131  
         }
 132  
 
 133  18
         return new URL(getBaseUrl(), resourceLocation.replaceAll("&amp;", "&"));
 134  
     }
 135  
 }