in src/main/java/org/apache/commons/mail/HtmlEmail.java [253:304]
public String embed(final URL url, final String name) throws EmailException
{
if (EmailUtils.isEmpty(name))
{
throw new EmailException("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 ii = inlineEmbeds.get(name);
if (ii != null)
{
final URLDataSource urlDataSource = (URLDataSource) ii.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 ii.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
InputStream is = null;
try
{
is = url.openStream();
}
catch (final IOException e)
{
throw new EmailException("Invalid URL", e);
}
finally
{
try
{
if (is != null)
{
is.close();
}
}
catch (final IOException ioe) // NOPMD
{ /* sigh */ }
}
return embed(new URLDataSource(url), name);
}