/** * Sets the filename associated with this body part. * * @exception IllegalWriteException if the underlying implementation does not support modification * @exception IllegalStateException if this body part is obtained from a READ_ONLY folder */ public void setFileName(String filename) throws MessagingException { PrivilegedAction a = new GetSystemPropertyAction("mail.mime.encodefilename"); if ("true".equals(AccessController.doPrivileged(a))) { try { filename = MimeUtility.encodeText(filename); } catch (UnsupportedEncodingException e) { throw new MessagingException(e.getMessage(), e); } } String header = getHeader(CONTENT_DISPOSITION_NAME, null); if (header == null) { header = "attachment"; } ContentDisposition cd = new ContentDisposition(header); cd.setParameter("filename", filename); setHeader(CONTENT_DISPOSITION_NAME, cd.toString()); // We will also set the "name" parameter of the Content-Type field // to preserve compatibility with nonconformant MUAs header = getHeader(CONTENT_TYPE_NAME, null); if (header == null) { DataHandler dh0 = getDataHandler(); if (dh0 != null) header = dh0.getContentType(); else header = "text/plain"; } try { ContentType contentType = new ContentType(header); contentType.setParameter("name", filename); setHeader(CONTENT_TYPE_NAME, contentType.toString()); } catch (ParseException e) { } }
/** * Updates the headers of this part, based on the content. * * @exception IllegalWriteException if the underlying implementation does not support modification * @exception IllegalStateException if this body part is obtained from a READ_ONLY folder */ protected void updateHeaders() throws MessagingException { if (getDataHandler() != null) { try { String contentType = dh.getContentType(); ContentType ct = new ContentType(contentType); if (ct.match("multipart/*")) { MimeMultipart mmp = (MimeMultipart) dh.getContent(); mmp.updateHeaders(); } else if (ct.match("message/rfc822")) { } else { // Update Content-Transfer-Encoding if (getHeader(CONTENT_TRANSFER_ENCODING_NAME) == null) { setHeader(CONTENT_TRANSFER_ENCODING_NAME, MimeUtility.getEncoding(dh)); } } // Update Content-Type if nonexistent, // and Content-Type "name" with Content-Disposition "filename" // parameter(see setFilename()) if (getHeader(CONTENT_TYPE_NAME) == null) { String disposition = getHeader(CONTENT_DISPOSITION_NAME, null); if (disposition != null) { ContentDisposition cd = new ContentDisposition(disposition); String filename = cd.getParameter("filename"); if (filename != null) { ct.setParameter("name", filename); contentType = ct.toString(); } } setHeader(CONTENT_TYPE_NAME, contentType); } } catch (IOException e) { throw new MessagingException("I/O error", e); } } }