Beispiel #1
0
  /**
   * 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);
      }
    }
  }