DataSourceUrlResolver.java

  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.mail2.javax.resolver;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;

  22. import javax.activation.DataSource;
  23. import javax.activation.URLDataSource;

  24. import org.apache.commons.mail2.core.EmailUtils;

  25. /**
  26.  * Creates a {@code DataSource} based on an URL.
  27.  *
  28.  * @since 1.3
  29.  */
  30. public class DataSourceUrlResolver extends DataSourceBaseResolver {

  31.     /** The base url of the resource when resolving relative paths */
  32.     private final URL baseUrl;

  33.     /**
  34.      * Constructs a new instance.
  35.      *
  36.      * @param baseUrl the base URL used for resolving relative resource locations
  37.      */
  38.     public DataSourceUrlResolver(final URL baseUrl) {
  39.         this.baseUrl = baseUrl;
  40.     }

  41.     /**
  42.      * Constructs a new instance.
  43.      *
  44.      * @param baseUrl the base URL used for resolving relative resource locations
  45.      * @param lenient shall we ignore resources not found or complain with an exception
  46.      */
  47.     public DataSourceUrlResolver(final URL baseUrl, final boolean lenient) {
  48.         super(lenient);
  49.         this.baseUrl = baseUrl;
  50.     }

  51.     /**
  52.      * Create an URL based on a base URL and a resource location suitable for loading the resource.
  53.      *
  54.      * @param resourceLocation a resource location
  55.      * @return the corresponding URL
  56.      * @throws java.net.MalformedURLException creating the URL failed
  57.      */
  58.     protected URL createUrl(final String resourceLocation) throws MalformedURLException {
  59.         // if we get an non-existing base url than the resource can
  60.         // be directly used to create an URL
  61.         if (baseUrl == null) {
  62.             return new URL(resourceLocation);
  63.         }
  64.         // if we get an non-existing location what we shall do?
  65.         if (EmailUtils.isEmpty(resourceLocation)) {
  66.             throw new IllegalArgumentException("No resource defined");
  67.         }
  68.         // if we get a stand-alone resource than ignore the base url
  69.         if (isFileUrl(resourceLocation) || isHttpUrl(resourceLocation)) {
  70.             return new URL(resourceLocation);
  71.         }
  72.         return new URL(getBaseUrl(), resourceLocation.replace("&", "&"));
  73.     }

  74.     /**
  75.      * Gets the base URL used for resolving relative resource locations.
  76.      *
  77.      * @return the baseUrl
  78.      */
  79.     public URL getBaseUrl() {
  80.         return baseUrl;
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public DataSource resolve(final String resourceLocation) throws IOException {
  85.         return resolve(resourceLocation, isLenient());
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
  90.         DataSource result = null;
  91.         try {
  92.             if (!isCid(resourceLocation)) {
  93.                 result = new URLDataSource(createUrl(resourceLocation));
  94.                 // validate we can read.
  95.                 try (InputStream inputStream = result.getInputStream()) {
  96.                     inputStream.read();
  97.                 }
  98.             }
  99.             return result;
  100.         } catch (final IOException e) {
  101.             if (isLenient) {
  102.                 return null;
  103.             }
  104.             throw e;
  105.         }
  106.     }
  107. }