Example usage for javax.activation URLDataSource getURL

List of usage examples for javax.activation URLDataSource getURL

Introduction

In this page you can find the example usage for javax.activation URLDataSource getURL.

Prototype

public URL getURL() 

Source Link

Document

Return the URL used to create this DataSource.

Usage

From source file:immf.MyHtmlEmail.java

/**
 * Embeds an URL in the HTML./*from   ww w  .  j a  v a2 s. c o m*/
 *
 * <p>This method embeds a file located by an URL into
 * the mail body. It allows, for instance, to add inline images
 * to the email.  Inline files may be referenced with a
 * <code>cid:xxxxxx</code> URL, where xxxxxx is the Content-ID
 * returned by the embed function. It is an error to bind the same name
 * to more than one URL; if the same URL is embedded multiple times, the
 * same Content-ID is guaranteed to be returned.
 *
 * <p>While functionally the same as passing <code>URLDataSource</code> to
 * {@link #embed(DataSource, String, String)}, this method attempts
 * to validate the URL before embedding it in the message and will throw
 * <code>EmailException</code> if the validation fails. In this case, the
 * <code>HtmlEmail</code> object will not be changed.
 *
 * <p>
 * NOTE: Clients should take care to ensure that different URLs are bound to
 * different names. This implementation tries to detect this and throw
 * <code>EmailException</code>. However, it is not guaranteed to catch
 * all cases, especially when the URL refers to a remote HTTP host that
 * may be part of a virtual host cluster.
 *
 * @param url The URL of the file.
 * @param name The name that will be set in the filename header
 * field.
 * @return A String with the Content-ID of the file.
 * @throws EmailException when URL supplied is invalid or if <code> is null
 * or empty; also see {@link javax.mail.internet.MimeBodyPart} for definitions
 * @since 1.0
 */
public String embed(URL url, String name) throws EmailException {
    if (StringUtils.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.
    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);
}