// check if a URLDataSource for this name has already been attached;
// if so, return the cached CID value.
if (inlineEmbeds.containsKey(name))
{
InlineImage ii = (InlineImage) inlineEmbeds.get(name);
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();
}
else
{
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 (IOException e)
{
throw new EmailException("Invalid URL", e);
}
finally
{
try
{
if (is != null)
{
is.close();
}
}
catch (IOException ioe)
{ /* sigh */ }
}
return embed(new URLDataSource(url), name);
}