/** * Returns the filename associated with this body part. * * <p>This method returns the value of the "filename" parameter from the Content-Disposition * header field. If the latter is not available, it returns the value of the "name" parameter from * the Content-Type header field. */ public String getFileName() throws MessagingException { String filename = null; String header = getHeader(CONTENT_DISPOSITION_NAME, null); if (header != null) { ContentDisposition cd = new ContentDisposition(header); filename = cd.getParameter("filename"); } if (filename == null) { header = getHeader(CONTENT_TYPE_NAME, null); if (header != null) { try { ContentType contentType = new ContentType(header); filename = contentType.getParameter("name"); } catch (ParseException e) { } } } PrivilegedAction a = new GetSystemPropertyAction("mail.mime.decodefilename"); if ("true".equals(AccessController.doPrivileged(a))) { try { filename = MimeUtility.decodeText(filename); } catch (UnsupportedEncodingException e) { throw new MessagingException(e.getMessage(), e); } } return filename; }
/** * 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) { } }
/** * Sets the Content-Disposition header field of this 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 setDisposition(String disposition) throws MessagingException { if (disposition == null) { removeHeader(CONTENT_DISPOSITION_NAME); } else { String value = getHeader(CONTENT_DISPOSITION_NAME, null); if (value != null) { ContentDisposition cd = new ContentDisposition(value); cd.setDisposition(disposition); disposition = cd.toString(); } setHeader(CONTENT_DISPOSITION_NAME, disposition); } }
/** * 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); } } }