View Javadoc
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   */
30  public class DataSourceUrlResolver extends DataSourceBaseResolver
31  {
32      /** the base url of the resource when resolving relative paths */
33      private final URL baseUrl;
34  
35      /**
36       * Constructor.
37       *
38       * @param baseUrl the base URL used for resolving relative resource locations
39       */
40      public DataSourceUrlResolver(final URL baseUrl)
41      {
42          super();
43          this.baseUrl = baseUrl;
44      }
45  
46      /**
47       * Constructor.
48       *
49       * @param baseUrl the base URL used for resolving relative resource locations
50       * @param lenient shall we ignore resources not found or complain with an exception
51       */
52      public DataSourceUrlResolver(final URL baseUrl, final boolean lenient)
53      {
54          super(lenient);
55          this.baseUrl = baseUrl;
56      }
57  
58      /**
59       * Get the base URL used for resolving relative resource locations.
60       *
61       * @return the baseUrl
62       */
63      public URL getBaseUrl()
64      {
65          return baseUrl;
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public DataSource resolve(final String resourceLocation) throws IOException
71      {
72          return resolve(resourceLocation, isLenient());
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException
78      {
79          DataSource result = null;
80  
81          try
82          {
83              if (!isCid(resourceLocation))
84              {
85                  final URL url = createUrl(resourceLocation);
86                  result = new URLDataSource(url);
87                  result.getInputStream();
88              }
89  
90              return result;
91          }
92          catch (final IOException e)
93          {
94              if (isLenient)
95              {
96                  return null;
97              }
98              throw e;
99          }
100     }
101 
102     /**
103      * Create an URL based on a base URL and a resource location suitable for loading
104      * the resource.
105      *
106      * @param resourceLocation a resource location
107      * @return the corresponding URL
108      * @throws java.net.MalformedURLException creating the URL failed
109      */
110     protected URL createUrl(final String resourceLocation) throws MalformedURLException
111     {
112         // if we get an non-existing base url than the resource can
113         // be directly used to create an URL
114         if (baseUrl == null)
115         {
116             return new URL(resourceLocation);
117         }
118 
119         // if we get an non-existing location what we shall do?
120         if (resourceLocation == null || resourceLocation.length() == 0)
121         {
122             throw new IllegalArgumentException("No resource defined");
123         }
124 
125         // if we get a stand-alone resource than ignore the base url
126         if (isFileUrl(resourceLocation) || isHttpUrl(resourceLocation))
127         {
128             return new URL(resourceLocation);
129         }
130 
131         return new URL(getBaseUrl(), resourceLocation.replaceAll("&amp;", "&"));
132     }
133 }