public String embed()

in commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/HtmlEmail.java [473:498]


    public String embed(final URL url, final String name) throws EmailException {
        EmailException.checkNonEmpty(name, () -> "Name cannot be null or empty");
        // check if a URLDataSource for this name has already been attached;
        // if so, return the cached CID value.
        final InlineImage inlineImage = inlineEmbeds.get(name);
        if (inlineImage != null) {
            final URLDataSource urlDataSource = (URLDataSource) inlineImage.getDataSource();
            // make sure the supplied URL points to the same thing
            // as the one already associated with this name.
            // NOTE: Comparing URLs with URL.equals() is a blocking operation
            // in the case of a network failure therefore we use
            // url.toExternalForm().equals() here.
            if (url.toExternalForm().equals(urlDataSource.getURL().toExternalForm())) {
                return inlineImage.getCid();
            }
            throw new EmailException("embedded name '" + name + "' is already bound to URL " + urlDataSource.getURL() + "; existing names cannot be rebound");
        }
        // verify that the URL is valid
        try (InputStream inputStream = url.openStream()) {
            // Make sure we can read.
            inputStream.read();
        } catch (final IOException e) {
            throw new EmailException("Invalid URL", e);
        }
        return embed(new URLDataSource(url), name);
    }