Java Code Examples for javax.activation.DataHandler#getContentType()

The following examples show how to use javax.activation.DataHandler#getContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MimeBodyPart.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Examine the content of this body part and update the appropriate
 * MIME headers.  Typical headers that get set here are
 * <code>Content-Type</code> and <code>Content-Transfer-Encoding</code>.
 * Headers might need to be updated in two cases:
 *
 * <br>
 * - A message being crafted by a mail application will certainly
 * need to activate this method at some point to fill up its internal
 * headers.
 *
 * <br>
 * - A message read in from a Store will have obtained
 * all its headers from the store, and so doesn't need this.
 * However, if this message is editable and if any edits have
 * been made to either the content or message structure, we might
 * need to resync our headers.
 *
 * <br>
 * In both cases this method is typically called by the
 * <code>Message.saveChanges</code> method.
 */
protected void updateHeaders() throws MessagingException {
    DataHandler dh = getDataHandler();
    if (dh == null) // Huh ?
        return;

    try {
        String type = dh.getContentType();
        boolean composite = false;
        boolean needCTHeader = getHeader("Content-Type") == null;

        ContentType cType = new ContentType(type);
        if (cType.match("multipart/*")) {
            // If multipart, recurse
            composite = true;
            Object o = dh.getContent();
            ((MimeMultipart) o).updateHeaders();
        } else if (cType.match("message/rfc822")) {
            composite = true;
        }

        // Content-Transfer-Encoding, but only if we don't
        // already have one
        if (!composite) {   // not allowed on composite parts
            if (getHeader("Content-Transfer-Encoding") == null)
                setEncoding(MimeUtility.getEncoding(dh));

            if (needCTHeader && setDefaultTextCharset &&
                    cType.match("text/*") &&
                    cType.getParameter("charset") == null) {
                /*
                 * Set a default charset for text parts.
                 * We really should examine the data to determine
                 * whether or not it's all ASCII, but that's too
                 * expensive so we make an assumption:  If we
                 * chose 7bit encoding for this data, it's probably
                 * ASCII.  (MimeUtility.getEncoding will choose
                 * 7bit only in this case, but someone might've
                 * set the Content-Transfer-Encoding header manually.)
                 */
                String charset;
                String enc = getEncoding();
                if (enc != null && enc.equalsIgnoreCase("7bit"))
                    charset = "us-ascii";
                else
                    charset = MimeUtility.getDefaultMIMECharset();
                cType.setParameter("charset", charset);
                type = cType.toString();
            }
        }

        // Now, let's update our own headers ...

        // Content-type, but only if we don't already have one
        if (needCTHeader) {
            /*
             * Pull out "filename" from Content-Disposition, and
             * use that to set the "name" parameter. This is to
             * satisfy older MUAs (DtMail, Roam and probably
             * a bunch of others).
             */
            String s = getHeader("Content-Disposition", null);
            if (s != null) {
                // Parse the header ..
                ContentDisposition cd = new ContentDisposition(s);
                String filename = cd.getParameter("filename");
                if (filename != null) {
                    cType.setParameter("name", filename);
                    type = cType.toString();
                }
            }

            setHeader("Content-Type", type);
        }
    } catch (IOException ex) {
        throw new MessagingException("IOException updating headers", ex);
    }
}
 
Example 2
Source File: MimeBodyPart.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Examine the content of this body part and update the appropriate
 * MIME headers.  Typical headers that get set here are
 * <code>Content-Type</code> and <code>Content-Transfer-Encoding</code>.
 * Headers might need to be updated in two cases:
 *
 * <br>
 * - A message being crafted by a mail application will certainly
 * need to activate this method at some point to fill up its internal
 * headers.
 *
 * <br>
 * - A message read in from a Store will have obtained
 * all its headers from the store, and so doesn't need this.
 * However, if this message is editable and if any edits have
 * been made to either the content or message structure, we might
 * need to resync our headers.
 *
 * <br>
 * In both cases this method is typically called by the
 * <code>Message.saveChanges</code> method.
 */
protected void updateHeaders() throws MessagingException {
    DataHandler dh = getDataHandler();
    if (dh == null) // Huh ?
        return;

    try {
        String type = dh.getContentType();
        boolean composite = false;
        boolean needCTHeader = getHeader("Content-Type") == null;

        ContentType cType = new ContentType(type);
        if (cType.match("multipart/*")) {
            // If multipart, recurse
            composite = true;
            Object o = dh.getContent();
            ((MimeMultipart) o).updateHeaders();
        } else if (cType.match("message/rfc822")) {
            composite = true;
        }

        // Content-Transfer-Encoding, but only if we don't
        // already have one
        if (!composite) {   // not allowed on composite parts
            if (getHeader("Content-Transfer-Encoding") == null)
                setEncoding(MimeUtility.getEncoding(dh));

            if (needCTHeader && setDefaultTextCharset &&
                    cType.match("text/*") &&
                    cType.getParameter("charset") == null) {
                /*
                 * Set a default charset for text parts.
                 * We really should examine the data to determine
                 * whether or not it's all ASCII, but that's too
                 * expensive so we make an assumption:  If we
                 * chose 7bit encoding for this data, it's probably
                 * ASCII.  (MimeUtility.getEncoding will choose
                 * 7bit only in this case, but someone might've
                 * set the Content-Transfer-Encoding header manually.)
                 */
                String charset;
                String enc = getEncoding();
                if (enc != null && enc.equalsIgnoreCase("7bit"))
                    charset = "us-ascii";
                else
                    charset = MimeUtility.getDefaultMIMECharset();
                cType.setParameter("charset", charset);
                type = cType.toString();
            }
        }

        // Now, let's update our own headers ...

        // Content-type, but only if we don't already have one
        if (needCTHeader) {
            /*
             * Pull out "filename" from Content-Disposition, and
             * use that to set the "name" parameter. This is to
             * satisfy older MUAs (DtMail, Roam and probably
             * a bunch of others).
             */
            String s = getHeader("Content-Disposition", null);
            if (s != null) {
                // Parse the header ..
                ContentDisposition cd = new ContentDisposition(s);
                String filename = cd.getParameter("filename");
                if (filename != null) {
                    cType.setParameter("name", filename);
                    type = cType.toString();
                }
            }

            setHeader("Content-Type", type);
        }
    } catch (IOException ex) {
        throw new MessagingException("IOException updating headers", ex);
    }
}
 
Example 3
Source File: MimeBodyPart.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Examine the content of this body part and update the appropriate
 * MIME headers.  Typical headers that get set here are
 * <code>Content-Type</code> and <code>Content-Transfer-Encoding</code>.
 * Headers might need to be updated in two cases:
 *
 * <br>
 * - A message being crafted by a mail application will certainly
 * need to activate this method at some point to fill up its internal
 * headers.
 *
 * <br>
 * - A message read in from a Store will have obtained
 * all its headers from the store, and so doesn't need this.
 * However, if this message is editable and if any edits have
 * been made to either the content or message structure, we might
 * need to resync our headers.
 *
 * <br>
 * In both cases this method is typically called by the
 * <code>Message.saveChanges</code> method.
 */
protected void updateHeaders() throws MessagingException {
    DataHandler dh = getDataHandler();
    if (dh == null) // Huh ?
        return;

    try {
        String type = dh.getContentType();
        boolean composite = false;
        boolean needCTHeader = getHeader("Content-Type") == null;

        ContentType cType = new ContentType(type);
        if (cType.match("multipart/*")) {
            // If multipart, recurse
            composite = true;
            Object o = dh.getContent();
            ((MimeMultipart) o).updateHeaders();
        } else if (cType.match("message/rfc822")) {
            composite = true;
        }

        // Content-Transfer-Encoding, but only if we don't
        // already have one
        if (!composite) {   // not allowed on composite parts
            if (getHeader("Content-Transfer-Encoding") == null)
                setEncoding(MimeUtility.getEncoding(dh));

            if (needCTHeader && setDefaultTextCharset &&
                    cType.match("text/*") &&
                    cType.getParameter("charset") == null) {
                /*
                 * Set a default charset for text parts.
                 * We really should examine the data to determine
                 * whether or not it's all ASCII, but that's too
                 * expensive so we make an assumption:  If we
                 * chose 7bit encoding for this data, it's probably
                 * ASCII.  (MimeUtility.getEncoding will choose
                 * 7bit only in this case, but someone might've
                 * set the Content-Transfer-Encoding header manually.)
                 */
                String charset;
                String enc = getEncoding();
                if (enc != null && enc.equalsIgnoreCase("7bit"))
                    charset = "us-ascii";
                else
                    charset = MimeUtility.getDefaultMIMECharset();
                cType.setParameter("charset", charset);
                type = cType.toString();
            }
        }

        // Now, let's update our own headers ...

        // Content-type, but only if we don't already have one
        if (needCTHeader) {
            /*
             * Pull out "filename" from Content-Disposition, and
             * use that to set the "name" parameter. This is to
             * satisfy older MUAs (DtMail, Roam and probably
             * a bunch of others).
             */
            String s = getHeader("Content-Disposition", null);
            if (s != null) {
                // Parse the header ..
                ContentDisposition cd = new ContentDisposition(s);
                String filename = cd.getParameter("filename");
                if (filename != null) {
                    cType.setParameter("name", filename);
                    type = cType.toString();
                }
            }

            setHeader("Content-Type", type);
        }
    } catch (IOException ex) {
        throw new MessagingException("IOException updating headers", ex);
    }
}
 
Example 4
Source File: MimeBodyPart.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Examine the content of this body part and update the appropriate
 * MIME headers.  Typical headers that get set here are
 * <code>Content-Type</code> and <code>Content-Transfer-Encoding</code>.
 * Headers might need to be updated in two cases:
 *
 * <br>
 * - A message being crafted by a mail application will certainly
 * need to activate this method at some point to fill up its internal
 * headers.
 *
 * <br>
 * - A message read in from a Store will have obtained
 * all its headers from the store, and so doesn't need this.
 * However, if this message is editable and if any edits have
 * been made to either the content or message structure, we might
 * need to resync our headers.
 *
 * <br>
 * In both cases this method is typically called by the
 * <code>Message.saveChanges</code> method.
 */
protected void updateHeaders() throws MessagingException {
    DataHandler dh = getDataHandler();
    if (dh == null) // Huh ?
        return;

    try {
        String type = dh.getContentType();
        boolean composite = false;
        boolean needCTHeader = getHeader("Content-Type") == null;

        ContentType cType = new ContentType(type);
        if (cType.match("multipart/*")) {
            // If multipart, recurse
            composite = true;
            Object o = dh.getContent();
            ((MimeMultipart) o).updateHeaders();
        } else if (cType.match("message/rfc822")) {
            composite = true;
        }

        // Content-Transfer-Encoding, but only if we don't
        // already have one
        if (!composite) {   // not allowed on composite parts
            if (getHeader("Content-Transfer-Encoding") == null)
                setEncoding(MimeUtility.getEncoding(dh));

            if (needCTHeader && setDefaultTextCharset &&
                    cType.match("text/*") &&
                    cType.getParameter("charset") == null) {
                /*
                 * Set a default charset for text parts.
                 * We really should examine the data to determine
                 * whether or not it's all ASCII, but that's too
                 * expensive so we make an assumption:  If we
                 * chose 7bit encoding for this data, it's probably
                 * ASCII.  (MimeUtility.getEncoding will choose
                 * 7bit only in this case, but someone might've
                 * set the Content-Transfer-Encoding header manually.)
                 */
                String charset;
                String enc = getEncoding();
                if (enc != null && enc.equalsIgnoreCase("7bit"))
                    charset = "us-ascii";
                else
                    charset = MimeUtility.getDefaultMIMECharset();
                cType.setParameter("charset", charset);
                type = cType.toString();
            }
        }

        // Now, let's update our own headers ...

        // Content-type, but only if we don't already have one
        if (needCTHeader) {
            /*
             * Pull out "filename" from Content-Disposition, and
             * use that to set the "name" parameter. This is to
             * satisfy older MUAs (DtMail, Roam and probably
             * a bunch of others).
             */
            String s = getHeader("Content-Disposition", null);
            if (s != null) {
                // Parse the header ..
                ContentDisposition cd = new ContentDisposition(s);
                String filename = cd.getParameter("filename");
                if (filename != null) {
                    cType.setParameter("name", filename);
                    type = cType.toString();
                }
            }

            setHeader("Content-Type", type);
        }
    } catch (IOException ex) {
        throw new MessagingException("IOException updating headers", ex);
    }
}
 
Example 5
Source File: MimeBodyPart.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Examine the content of this body part and update the appropriate
 * MIME headers.  Typical headers that get set here are
 * <code>Content-Type</code> and <code>Content-Transfer-Encoding</code>.
 * Headers might need to be updated in two cases:
 *
 * <br>
 * - A message being crafted by a mail application will certainly
 * need to activate this method at some point to fill up its internal
 * headers.
 *
 * <br>
 * - A message read in from a Store will have obtained
 * all its headers from the store, and so doesn't need this.
 * However, if this message is editable and if any edits have
 * been made to either the content or message structure, we might
 * need to resync our headers.
 *
 * <br>
 * In both cases this method is typically called by the
 * <code>Message.saveChanges</code> method.
 */
protected void updateHeaders() throws MessagingException {
    DataHandler dh = getDataHandler();
    if (dh == null) // Huh ?
        return;

    try {
        String type = dh.getContentType();
        boolean composite = false;
        boolean needCTHeader = getHeader("Content-Type") == null;

        ContentType cType = new ContentType(type);
        if (cType.match("multipart/*")) {
            // If multipart, recurse
            composite = true;
            Object o = dh.getContent();
            ((MimeMultipart) o).updateHeaders();
        } else if (cType.match("message/rfc822")) {
            composite = true;
        }

        // Content-Transfer-Encoding, but only if we don't
        // already have one
        if (!composite) {   // not allowed on composite parts
            if (getHeader("Content-Transfer-Encoding") == null)
                setEncoding(MimeUtility.getEncoding(dh));

            if (needCTHeader && setDefaultTextCharset &&
                    cType.match("text/*") &&
                    cType.getParameter("charset") == null) {
                /*
                 * Set a default charset for text parts.
                 * We really should examine the data to determine
                 * whether or not it's all ASCII, but that's too
                 * expensive so we make an assumption:  If we
                 * chose 7bit encoding for this data, it's probably
                 * ASCII.  (MimeUtility.getEncoding will choose
                 * 7bit only in this case, but someone might've
                 * set the Content-Transfer-Encoding header manually.)
                 */
                String charset;
                String enc = getEncoding();
                if (enc != null && enc.equalsIgnoreCase("7bit"))
                    charset = "us-ascii";
                else
                    charset = MimeUtility.getDefaultMIMECharset();
                cType.setParameter("charset", charset);
                type = cType.toString();
            }
        }

        // Now, let's update our own headers ...

        // Content-type, but only if we don't already have one
        if (needCTHeader) {
            /*
             * Pull out "filename" from Content-Disposition, and
             * use that to set the "name" parameter. This is to
             * satisfy older MUAs (DtMail, Roam and probably
             * a bunch of others).
             */
            String s = getHeader("Content-Disposition", null);
            if (s != null) {
                // Parse the header ..
                ContentDisposition cd = new ContentDisposition(s);
                String filename = cd.getParameter("filename");
                if (filename != null) {
                    cType.setParameter("name", filename);
                    type = cType.toString();
                }
            }

            setHeader("Content-Type", type);
        }
    } catch (IOException ex) {
        throw new MessagingException("IOException updating headers", ex);
    }
}
 
Example 6
Source File: MimeBodyPart.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Examine the content of this body part and update the appropriate
 * MIME headers.  Typical headers that get set here are
 * <code>Content-Type</code> and <code>Content-Transfer-Encoding</code>.
 * Headers might need to be updated in two cases:
 *
 * <br>
 * - A message being crafted by a mail application will certainly
 * need to activate this method at some point to fill up its internal
 * headers.
 *
 * <br>
 * - A message read in from a Store will have obtained
 * all its headers from the store, and so doesn't need this.
 * However, if this message is editable and if any edits have
 * been made to either the content or message structure, we might
 * need to resync our headers.
 *
 * <br>
 * In both cases this method is typically called by the
 * <code>Message.saveChanges</code> method.
 */
protected void updateHeaders() throws MessagingException {
    DataHandler dh = getDataHandler();
    if (dh == null) // Huh ?
        return;

    try {
        String type = dh.getContentType();
        boolean composite = false;
        boolean needCTHeader = getHeader("Content-Type") == null;

        ContentType cType = new ContentType(type);
        if (cType.match("multipart/*")) {
            // If multipart, recurse
            composite = true;
            Object o = dh.getContent();
            ((MimeMultipart) o).updateHeaders();
        } else if (cType.match("message/rfc822")) {
            composite = true;
        }

        // Content-Transfer-Encoding, but only if we don't
        // already have one
        if (!composite) {   // not allowed on composite parts
            if (getHeader("Content-Transfer-Encoding") == null)
                setEncoding(MimeUtility.getEncoding(dh));

            if (needCTHeader && setDefaultTextCharset &&
                    cType.match("text/*") &&
                    cType.getParameter("charset") == null) {
                /*
                 * Set a default charset for text parts.
                 * We really should examine the data to determine
                 * whether or not it's all ASCII, but that's too
                 * expensive so we make an assumption:  If we
                 * chose 7bit encoding for this data, it's probably
                 * ASCII.  (MimeUtility.getEncoding will choose
                 * 7bit only in this case, but someone might've
                 * set the Content-Transfer-Encoding header manually.)
                 */
                String charset;
                String enc = getEncoding();
                if (enc != null && enc.equalsIgnoreCase("7bit"))
                    charset = "us-ascii";
                else
                    charset = MimeUtility.getDefaultMIMECharset();
                cType.setParameter("charset", charset);
                type = cType.toString();
            }
        }

        // Now, let's update our own headers ...

        // Content-type, but only if we don't already have one
        if (needCTHeader) {
            /*
             * Pull out "filename" from Content-Disposition, and
             * use that to set the "name" parameter. This is to
             * satisfy older MUAs (DtMail, Roam and probably
             * a bunch of others).
             */
            String s = getHeader("Content-Disposition", null);
            if (s != null) {
                // Parse the header ..
                ContentDisposition cd = new ContentDisposition(s);
                String filename = cd.getParameter("filename");
                if (filename != null) {
                    cType.setParameter("name", filename);
                    type = cType.toString();
                }
            }

            setHeader("Content-Type", type);
        }
    } catch (IOException ex) {
        throw new MessagingException("IOException updating headers", ex);
    }
}
 
Example 7
Source File: MimeBodyPart.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Examine the content of this body part and update the appropriate
 * MIME headers.  Typical headers that get set here are
 * <code>Content-Type</code> and <code>Content-Transfer-Encoding</code>.
 * Headers might need to be updated in two cases:
 *
 * <br>
 * - A message being crafted by a mail application will certainly
 * need to activate this method at some point to fill up its internal
 * headers.
 *
 * <br>
 * - A message read in from a Store will have obtained
 * all its headers from the store, and so doesn't need this.
 * However, if this message is editable and if any edits have
 * been made to either the content or message structure, we might
 * need to resync our headers.
 *
 * <br>
 * In both cases this method is typically called by the
 * <code>Message.saveChanges</code> method.
 */
protected void updateHeaders() throws MessagingException {
    DataHandler dh = getDataHandler();
    if (dh == null) // Huh ?
        return;

    try {
        String type = dh.getContentType();
        boolean composite = false;
        boolean needCTHeader = getHeader("Content-Type") == null;

        ContentType cType = new ContentType(type);
        if (cType.match("multipart/*")) {
            // If multipart, recurse
            composite = true;
            Object o = dh.getContent();
            ((MimeMultipart) o).updateHeaders();
        } else if (cType.match("message/rfc822")) {
            composite = true;
        }

        // Content-Transfer-Encoding, but only if we don't
        // already have one
        if (!composite) {   // not allowed on composite parts
            if (getHeader("Content-Transfer-Encoding") == null)
                setEncoding(MimeUtility.getEncoding(dh));

            if (needCTHeader && setDefaultTextCharset &&
                    cType.match("text/*") &&
                    cType.getParameter("charset") == null) {
                /*
                 * Set a default charset for text parts.
                 * We really should examine the data to determine
                 * whether or not it's all ASCII, but that's too
                 * expensive so we make an assumption:  If we
                 * chose 7bit encoding for this data, it's probably
                 * ASCII.  (MimeUtility.getEncoding will choose
                 * 7bit only in this case, but someone might've
                 * set the Content-Transfer-Encoding header manually.)
                 */
                String charset;
                String enc = getEncoding();
                if (enc != null && enc.equalsIgnoreCase("7bit"))
                    charset = "us-ascii";
                else
                    charset = MimeUtility.getDefaultMIMECharset();
                cType.setParameter("charset", charset);
                type = cType.toString();
            }
        }

        // Now, let's update our own headers ...

        // Content-type, but only if we don't already have one
        if (needCTHeader) {
            /*
             * Pull out "filename" from Content-Disposition, and
             * use that to set the "name" parameter. This is to
             * satisfy older MUAs (DtMail, Roam and probably
             * a bunch of others).
             */
            String s = getHeader("Content-Disposition", null);
            if (s != null) {
                // Parse the header ..
                ContentDisposition cd = new ContentDisposition(s);
                String filename = cd.getParameter("filename");
                if (filename != null) {
                    cType.setParameter("name", filename);
                    type = cType.toString();
                }
            }

            setHeader("Content-Type", type);
        }
    } catch (IOException ex) {
        throw new MessagingException("IOException updating headers", ex);
    }
}