in geronimo-mail_2.1_spec/src/main/java/jakarta/mail/internet/MimeMessage.java [1502:1609]
protected void updateHeaders() throws MessagingException {
final DataHandler handler = getDataHandler();
try {
//RFC 2822 requires a Date header. The MimeMessage.updateHeaders method
//now sets the Date header if it's not already set
if (getSentDate() == null) {
setSentDate(new Date());
}
// figure out the content type. If not set, we'll need to figure this out.
String type = dh.getContentType();
// we might need to reconcile the content type and our explicitly set type
final String explicitType = getSingleHeader("Content-Type");
// parse this content type out so we can do matches/compares.
final ContentType contentType = new ContentType(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 content.
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(session, "MIME_MAIL_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 original type string
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 = getSingleHeader("Content-Disposition");
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);
// set the header with the updated content type information.
type = contentType.toString();
}
}
// if no header has been set, then copy our current type string (which may
// have been modified above)
setHeader("Content-Type", type);
}
// make sure we set the MIME version
setHeader("MIME-Version", "1.0");
// new javamail 1.4 requirement.
updateMessageID();
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);
}
}