commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/resolver/DataSourceClassPathResolver.java [32:122]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class DataSourceClassPathResolver extends DataSourceBaseResolver {
    /** The base string of the resource relative to the classpath when resolving relative paths */
    private final String classPathBase;

    /**
     * Constructs a new instance.
     */
    public DataSourceClassPathResolver() {
        this("/");
    }

    /**
     * Constructs a new instance.
     *
     * @param classPathBase a base class path
     */
    public DataSourceClassPathResolver(final String classPathBase) {
        this(classPathBase, false);
    }

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

    /**
     * Gets the class path base.
     *
     * @return the classPathBase
     */
    public String getClassPathBase() {
        return classPathBase;
    }

    /**
     * Returns the resource name for a given resource location.
     *
     * @param resourceLocation the resource location
     * @return {@link #getClassPathBase()} + {@code resourceLocation}
     * @see #getClassPathBase()
     */
    private String getResourceName(final String resourceLocation) {
        return (getClassPathBase() + resourceLocation).replace("//", "/");
    }

    /** {@inheritDoc} */
    @Override
    public DataSource resolve(final String resourceLocation) throws IOException {
        return resolve(resourceLocation, isLenient());
    }

    /** {@inheritDoc} */
    @Override
    public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
        try {
            if (!isCid(resourceLocation) && !isHttpUrl(resourceLocation)) {
                final String mimeType = FileTypeMap.getDefaultFileTypeMap().getContentType(resourceLocation);
                final String resourceName = getResourceName(resourceLocation);
                try (InputStream inputStream = DataSourceClassPathResolver.class.getResourceAsStream(resourceName)) {
                    if (inputStream == null) {
                        if (isLenient) {
                            return null;
                        }
                        throw new IOException("The following class path resource was not found : " + resourceLocation);
                    }
                    final ByteArrayDataSource ds = new ByteArrayDataSource(inputStream, mimeType);
                    // EMAIL-125: set the name of the DataSource to the normalized resource URL
                    // similar to other DataSource implementations, e.g. FileDataSource, URLDataSource
                    final URL resource = DataSourceClassPathResolver.class.getResource(resourceName);
                    if (resource != null) {
                        ds.setName(resource.toString());
                    } else if (isLenient) {
                        return null;
                    } else {
                        throw new IOException("The following class path resource was not found : " + resourceName);
                    }
                    return ds;
                }
            }
            return null;
        } catch (final IOException e) {
            if (isLenient) {
                return null;
            }
            throw e;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



commons-email2-javax/src/main/java/org/apache/commons/mail2/javax/resolver/DataSourceClassPathResolver.java [32:122]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class DataSourceClassPathResolver extends DataSourceBaseResolver {
    /** The base string of the resource relative to the classpath when resolving relative paths */
    private final String classPathBase;

    /**
     * Constructs a new instance.
     */
    public DataSourceClassPathResolver() {
        this("/");
    }

    /**
     * Constructs a new instance.
     *
     * @param classPathBase a base class path
     */
    public DataSourceClassPathResolver(final String classPathBase) {
        this(classPathBase, false);
    }

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

    /**
     * Gets the class path base.
     *
     * @return the classPathBase
     */
    public String getClassPathBase() {
        return classPathBase;
    }

    /**
     * Returns the resource name for a given resource location.
     *
     * @param resourceLocation the resource location
     * @return {@link #getClassPathBase()} + {@code resourceLocation}
     * @see #getClassPathBase()
     */
    private String getResourceName(final String resourceLocation) {
        return (getClassPathBase() + resourceLocation).replace("//", "/");
    }

    /** {@inheritDoc} */
    @Override
    public DataSource resolve(final String resourceLocation) throws IOException {
        return resolve(resourceLocation, isLenient());
    }

    /** {@inheritDoc} */
    @Override
    public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
        try {
            if (!isCid(resourceLocation) && !isHttpUrl(resourceLocation)) {
                final String mimeType = FileTypeMap.getDefaultFileTypeMap().getContentType(resourceLocation);
                final String resourceName = getResourceName(resourceLocation);
                try (InputStream inputStream = DataSourceClassPathResolver.class.getResourceAsStream(resourceName)) {
                    if (inputStream == null) {
                        if (isLenient) {
                            return null;
                        }
                        throw new IOException("The following class path resource was not found : " + resourceLocation);
                    }
                    final ByteArrayDataSource ds = new ByteArrayDataSource(inputStream, mimeType);
                    // EMAIL-125: set the name of the DataSource to the normalized resource URL
                    // similar to other DataSource implementations, e.g. FileDataSource, URLDataSource
                    final URL resource = DataSourceClassPathResolver.class.getResource(resourceName);
                    if (resource != null) {
                        ds.setName(resource.toString());
                    } else if (isLenient) {
                        return null;
                    } else {
                        throw new IOException("The following class path resource was not found : " + resourceName);
                    }
                    return ds;
                }
            }
            return null;
        } catch (final IOException e) {
            if (isLenient) {
                return null;
            }
            throw e;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



