in geronimo-javamail_1.4_spec/src/main/java/javax/mail/internet/MimeBodyPart.java [525:604]
protected void updateHeaders() throws MessagingException {
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.
ContentType content = new ContentType(type);
// we might need to reconcile the content type and our explicitly set type
String explicitType = getSingleHeader("Content-Type");
// is this a multipart content?
if (content.match("multipart/*")) {
// the content is suppose to be a MimeMultipart. Ping it to update it's headers as well.
try {
MimeMultipart part = (MimeMultipart)handler.getContent();
part.updateHeaders();
} catch (ClassCastException e) {
throw new MessagingException("Message content is not MimeMultipart", e);
}
}
else if (!content.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 (content.match("text/*")) {
// the charset should be specified as a parameter on the MIME type. If not there,
// try to figure one out.
if (content.getParameter("charset") == null) {
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")) {
content.setParameter("charset", "us-ascii");
}
else {
// get the global default.
content.setParameter("charset", MimeUtility.getDefaultMIMECharset());
}
// replace the datasource provided type
type = content.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.
String disp = getHeader("Content-Disposition", null);
if (disp != null) {
// parse up the string value of the disposition
ContentDisposition disposition = new ContentDisposition(disp);
// now check for a filename value
String filename = disposition.getParameter("filename");
// copy and rename the parameter, if it exists.
if (filename != null) {
content.setParameter("name", filename);
// and update the string version
type = content.toString();
}
}
// set the header with the updated content type information.
setHeader("Content-Type", type);
}
} catch (IOException e) {
throw new MessagingException("Error updating message headers", e);
}
}