DataSourceClassPathResolver.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.jakarta.resolver;

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

  21. import jakarta.activation.DataSource;
  22. import jakarta.activation.FileTypeMap;
  23. import jakarta.mail.util.ByteArrayDataSource;

  24. /**
  25.  * Creates a {@code DataSource} based on an class path.
  26.  *
  27.  * @since 1.3
  28.  */
  29. public class DataSourceClassPathResolver extends DataSourceBaseResolver {
  30.     /** The base string of the resource relative to the classpath when resolving relative paths */
  31.     private final String classPathBase;

  32.     /**
  33.      * Constructs a new instance.
  34.      */
  35.     public DataSourceClassPathResolver() {
  36.         this("/");
  37.     }

  38.     /**
  39.      * Constructs a new instance.
  40.      *
  41.      * @param classPathBase a base class path
  42.      */
  43.     public DataSourceClassPathResolver(final String classPathBase) {
  44.         this(classPathBase, false);
  45.     }

  46.     /**
  47.      * Constructs a new instance.
  48.      *
  49.      * @param classPathBase a base class path
  50.      * @param lenient       shall we ignore resources not found or throw an exception?
  51.      */
  52.     public DataSourceClassPathResolver(final String classPathBase, final boolean lenient) {
  53.         super(lenient);
  54.         this.classPathBase = classPathBase.endsWith("/") ? classPathBase : classPathBase + "/";
  55.     }

  56.     /**
  57.      * Gets the class path base.
  58.      *
  59.      * @return the classPathBase
  60.      */
  61.     public String getClassPathBase() {
  62.         return classPathBase;
  63.     }

  64.     /**
  65.      * Returns the resource name for a given resource location.
  66.      *
  67.      * @param resourceLocation the resource location
  68.      * @return {@link #getClassPathBase()} + {@code resourceLocation}
  69.      * @see #getClassPathBase()
  70.      */
  71.     private String getResourceName(final String resourceLocation) {
  72.         return (getClassPathBase() + resourceLocation).replace("//", "/");
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public DataSource resolve(final String resourceLocation) throws IOException {
  77.         return resolve(resourceLocation, isLenient());
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
  82.         try {
  83.             if (!isCid(resourceLocation) && !isHttpUrl(resourceLocation)) {
  84.                 final String mimeType = FileTypeMap.getDefaultFileTypeMap().getContentType(resourceLocation);
  85.                 final String resourceName = getResourceName(resourceLocation);
  86.                 try (InputStream inputStream = DataSourceClassPathResolver.class.getResourceAsStream(resourceName)) {
  87.                     if (inputStream == null) {
  88.                         if (isLenient) {
  89.                             return null;
  90.                         }
  91.                         throw new IOException("The following class path resource was not found : " + resourceLocation);
  92.                     }
  93.                     final ByteArrayDataSource ds = new ByteArrayDataSource(inputStream, mimeType);
  94.                     // EMAIL-125: set the name of the DataSource to the normalized resource URL
  95.                     // similar to other DataSource implementations, e.g. FileDataSource, URLDataSource
  96.                     final URL resource = DataSourceClassPathResolver.class.getResource(resourceName);
  97.                     if (resource != null) {
  98.                         ds.setName(resource.toString());
  99.                     } else if (isLenient) {
  100.                         return null;
  101.                     } else {
  102.                         throw new IOException("The following class path resource was not found : " + resourceName);
  103.                     }
  104.                     return ds;
  105.                 }
  106.             }
  107.             return null;
  108.         } catch (final IOException e) {
  109.             if (isLenient) {
  110.                 return null;
  111.             }
  112.             throw e;
  113.         }
  114.     }
  115. }