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   * @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          super();
44          this.baseUrl = baseUrl;
45      }
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          super(lenient);
56          this.baseUrl = baseUrl;
57      }
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          return baseUrl;
67      }
68  
69      /** {@inheritDoc} */
70      public DataSource resolve(String resourceLocation) throws IOException
71      {
72          return resolve(resourceLocation, isLenient());
73      }
74  
75      /** {@inheritDoc} */
76      public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException
77      {
78          DataSource result = null;
79  
80          try
81          {
82              if (!isCid(resourceLocation))
83              {
84                  URL url = createUrl(resourceLocation);
85                  result = new URLDataSource(url);
86                  result.getInputStream();
87              }
88  
89              return result;
90          }
91          catch (IOException e)
92          {
93              if (isLenient)
94              {
95                  return null;
96              }
97              else
98              {
99                  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         if (baseUrl == null)
117         {
118             return new URL(resourceLocation);
119         }
120 
121         // if we get an non-existing location what we shall do?
122         if (resourceLocation == null || resourceLocation.length() == 0)
123         {
124             throw new IllegalArgumentException("No resource defined");
125         }
126 
127         // if we get a stand-alone resource than ignore the base url
128         if (isFileUrl(resourceLocation) || isHttpUrl(resourceLocation))
129         {
130             return new URL(resourceLocation);
131         }
132 
133         return new URL(getBaseUrl(), resourceLocation.replaceAll("&amp;", "&"));
134     }
135 }