protected void updateHeaders()

in geronimo-mail_2.1_spec/src/main/java/jakarta/mail/internet/MimeBodyPart.java [560:654]


    protected void updateHeaders() throws MessagingException {
        final DataHandler handler = getDataHandler();

        try {
            // figure out the content type.  If not set, we'll need to figure this out.
            String type = dh.getContentType();
            // parse this content type out so we can do matches/compares.
            final ContentType contentType = new ContentType(type);
            
            // we might need to reconcile the content type and our explicitly set type
            final String explicitType = getSingleHeader("Content-Type"); 
            // is this a multipart content?
            if (contentType.match("multipart/*")) {
                // the content is suppose to be a MimeMultipart.  Ping it to update it's headers as well.
                try {
                    final MimeMultipart part = (MimeMultipart)handler.getContent();
                    part.updateHeaders();
                } catch (final ClassCastException e) {
                    throw new MessagingException("Message content is not MimeMultipart", e);
                }
            }
            else if (!contentType.match("message/rfc822")) {
                // simple part, we need to update the header type information
                // if no encoding is set yet, figure this out from the data handler.
                if (getSingleHeader("Content-Transfer-Encoding") == null) {
                    setHeader("Content-Transfer-Encoding", MimeUtility.getEncoding(handler));
                }

                // is a content type header set?  Check the property to see if we need to set this.
                if (explicitType == null) {
                    if (SessionUtil.getBooleanProperty(MIME_SETDEFAULTTEXTCHARSET, true)) {
                        // is this a text type?  Figure out the encoding and make sure it is set.
                        if (contentType.match("text/*")) {
                            // the charset should be specified as a parameter on the MIME type.  If not there,
                            // try to figure one out.
                            if (contentType.getParameter("charset") == null) {

                                final String encoding = getEncoding();
                                // if we're sending this as 7-bit ASCII, our character set need to be
                                // compatible.
                                if (encoding != null && encoding.equalsIgnoreCase("7bit")) {
                                    contentType.setParameter("charset", "us-ascii");
                                }
                                else {
                                    // get the global default.
                                    contentType.setParameter("charset", MimeUtility.getDefaultMIMECharset());
                                }
                                // replace the datasource provided type 
                                type = contentType.toString(); 
                            }
                        }
                    }
                }
            }

            // if we don't have a content type header, then create one.
            if (explicitType == null) {
                // get the disposition header, and if it is there, copy the filename parameter into the
                // name parameter of the type.
                final String disp = getHeader("Content-Disposition", null);
                if (disp != null) {
                    // parse up the string value of the disposition
                    final ContentDisposition disposition = new ContentDisposition(disp);
                    // now check for a filename value
                    final String filename = disposition.getParameter("filename");
                    // copy and rename the parameter, if it exists.
                    if (filename != null) {
                        contentType.setParameter("name", filename);
                        // and update the string version 
                        type = contentType.toString(); 
                    }
                }
                // set the header with the updated content type information.
                setHeader("Content-Type", type);
            }
            
            
            if (cachedContent != null) {
                dh = new DataHandler(cachedContent, getContentType());
                cachedContent = null;
                content = null;
                if (contentStream != null) {
                    try {
                        contentStream.close();
                    } catch (final IOException ioex) {
                        //np-op
                    }
                }
                contentStream = null;
            }

        } catch (final IOException e) {
            throw new MessagingException("Error updating message headers", e);
        }
    }