Beispiel #1
0
  /**
   * Gets the message type based on the content type headers.
   *
   * @param msg The message to extract the type from.
   * @return MDN if the message is an MDN message (<a href="http://tools.ietf.org/html/rfc3798">RFC
   *     3798</a>)<br>
   *     DSN if the message is a DNS message (<a href="http://tools.ietf.org/html/rfc3464">RFC
   *     3464</a>)<br>
   *     Normal for all other message type.<br>
   *     Return Unknown if an error occurs.
   */
  public static TxMessageType getMessageType(MimeMessage msg) {
    try {
      ContentType contentType = new ContentType(msg.getContentType());

      if (contentType.match(MDNStandard.MediaType.ReportMessage)
          && contentType.getParameter(MDNStandard.MediaType.ReportType) != null) {

        if (contentType
            .getParameter(MDNStandard.MediaType.ReportType)
            .equalsIgnoreCase(MDNStandard.MediaType.ReportTypeValueNotification))
          return TxMessageType.MDN;
        else if (contentType
            .getParameter(DSNStandard.MediaType.ReportType)
            .equalsIgnoreCase(DSNStandard.MediaType.ReportTypeValueDelivery))
          return TxMessageType.DSN;
      } else if (contentType.match(SMIMEStandard.EncryptedContentMediaType)
          || contentType.match(SMIMEStandard.EncryptedContentMediaTypeAlternative)) {
        return TxMessageType.SMIME;
      }

      return TxMessageType.IMF;
    }
    /// CLOVER:OFF
    catch (ParseException e) {
      LOGGER.warn("Failed to discern message type.", e);
    } catch (MessagingException e) {
      LOGGER.warn("Failed to discern message type.", e);
    }
    return TxMessageType.UNKNOWN;
    /// CLOVER:ON
  }
Beispiel #2
0
  public static String getText(Part part) {

    try {
      ContentType contentType = new ContentType(part.getContentType());
      System.err.println(
          "contentType: "
              + part.getContentType()
              + ", class: "
              + part.getContent().getClass().getName());

      if (part.isMimeType("text/*")) {
        String charset = contentType.getParameter("charset");
        System.err.println("Charset: " + charset);

        return (String) part.getContent();

      } else if (part.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) part.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
          String text = getText(mp.getBodyPart(i));

          if (text != null) {
            return text;
          }
        }
      }
      return null;
    } catch (ParseException e) {
      throw new RuntimeException(e);
    } catch (MessagingException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  public void multipartMixedTest(MimeBodyPart part1, MimeBodyPart part2) throws Exception {
    MimeMultipart mp = new MimeMultipart();

    mp.addBodyPart(part1);
    mp.addBodyPart(part2);

    MimeBodyPart m = new MimeBodyPart();

    m.setContent(mp);

    MimeMultipart smm =
        generateMultiPartRsa("SHA1withRSA", m, SMIMESignedGenerator.RFC3851_MICALGS);
    SMIMESigned s = new SMIMESigned(smm);

    verifySigners(s.getCertificates(), s.getSignerInfos());

    AttributeTable attr =
        ((SignerInformation) s.getSignerInfos().getSigners().iterator().next())
            .getSignedAttributes();

    Attribute a = attr.get(CMSAttributes.messageDigest);
    byte[] contentDigest =
        ASN1OctetString.getInstance(a.getAttrValues().getObjectAt(0)).getOctets();

    mp = (MimeMultipart) m.getContent();
    ContentType contentType = new ContentType(mp.getContentType());
    String boundary = "--" + contentType.getParameter("boundary");

    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    LineOutputStream lOut = new LineOutputStream(bOut);

    Enumeration headers = m.getAllHeaderLines();
    while (headers.hasMoreElements()) {
      lOut.writeln((String) headers.nextElement());
    }

    lOut.writeln(); // CRLF separator

    lOut.writeln(boundary);
    writePart(mp.getBodyPart(0), bOut);
    lOut.writeln(); // CRLF terminator

    lOut.writeln(boundary);
    writePart(mp.getBodyPart(1), bOut);
    lOut.writeln();

    lOut.writeln(boundary + "--");

    MessageDigest dig = MessageDigest.getInstance("SHA1", BC);

    assertTrue(Arrays.equals(contentDigest, dig.digest(bOut.toByteArray())));
  }
 /**
  * Constructor with a String. The MIME type should include a charset parameter specifying the
  * charset to use to encode the string; otherwise, the platform default is used.
  *
  * @param data the string
  * @param type the MIME type
  */
 public ByteArrayDataSource(String data, String type) throws IOException {
   try {
     ContentType ct = new ContentType(type);
     String charset = ct.getParameter("charset");
     String jcharset =
         (charset == null)
             ? MimeUtility.getDefaultJavaCharset()
             : MimeUtility.javaCharset(charset);
     if (jcharset == null) throw new UnsupportedEncodingException(charset);
     this.data = data.getBytes(jcharset);
     this.type = type;
   } catch (ParseException e) {
     IOException e2 = new IOException("can't parse MIME type");
     e2.initCause(e);
     throw e2;
   }
 }
  public FileContentInfo create(FileContent fileContent) throws FileSystemException {
    MimeFileObject mimeFile = (MimeFileObject) fileContent.getFile();
    Part part = mimeFile.getPart();

    String contentTypeString = null;
    String charset = null;

    try {
      // special handling for multipart
      if (mimeFile.isMultipart()) {
        // get the original content type, but ...
        contentTypeString = part.getContentType();

        // .... we deliver the preamble instead of an inupt string
        // the preamble will be delivered in UTF-8 - fixed
        charset = MimeFileSystem.PREAMBLE_CHARSET;
      }
    } catch (MessagingException e) {
      throw new FileSystemException(e);
    }

    if (contentTypeString == null) {
      // normal message ... get the content type
      try {
        contentTypeString = part.getContentType();
      } catch (MessagingException e) {
        throw new FileSystemException(e);
      }
    }

    ContentType contentType;
    try {
      contentType = new ContentType(contentTypeString);
    } catch (MessagingException e) {
      throw new FileSystemException(e);
    }

    if (charset == null) {
      // charset might already be set by the multipart message stuff, else
      // extract it from the contentType now
      charset = contentType.getParameter("charset"); // NON-NLS
    }

    return new DefaultFileContentInfo(contentType.getBaseType(), charset);
  }
 @Override
 public Object unmarshal(Exchange exchange, InputStream stream)
     throws IOException, MessagingException {
   MimeBodyPart mimeMessage;
   String contentType;
   Message camelMessage;
   Object content = null;
   if (headersInline) {
     mimeMessage = new MimeBodyPart(stream);
     camelMessage = exchange.getOut();
     MessageHelper.copyHeaders(exchange.getIn(), camelMessage, true);
     contentType = mimeMessage.getHeader(CONTENT_TYPE, null);
     // write the MIME headers not generated by javamail as Camel headers
     Enumeration<?> headersEnum = mimeMessage.getNonMatchingHeaders(STANDARD_HEADERS);
     while (headersEnum.hasMoreElements()) {
       Object ho = headersEnum.nextElement();
       if (ho instanceof Header) {
         Header header = (Header) ho;
         camelMessage.setHeader(header.getName(), header.getValue());
       }
     }
   } else {
     // check if this a multipart at all. Otherwise do nothing
     contentType = exchange.getIn().getHeader(CONTENT_TYPE, String.class);
     if (contentType == null) {
       return stream;
     }
     try {
       ContentType ct = new ContentType(contentType);
       if (!ct.match("multipart/*")) {
         return stream;
       }
     } catch (ParseException e) {
       LOG.warn("Invalid Content-Type " + contentType + " ignored");
       return stream;
     }
     camelMessage = exchange.getOut();
     MessageHelper.copyHeaders(exchange.getIn(), camelMessage, true);
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     IOHelper.copyAndCloseInput(stream, bos);
     InternetHeaders headers = new InternetHeaders();
     extractHeader(CONTENT_TYPE, camelMessage, headers);
     extractHeader(MIME_VERSION, camelMessage, headers);
     mimeMessage = new MimeBodyPart(headers, bos.toByteArray());
     bos.close();
   }
   DataHandler dh;
   try {
     dh = mimeMessage.getDataHandler();
     if (dh != null) {
       content = dh.getContent();
       contentType = dh.getContentType();
     }
   } catch (MessagingException e) {
     LOG.warn("cannot parse message, no unmarshalling done");
   }
   if (content instanceof MimeMultipart) {
     MimeMultipart mp = (MimeMultipart) content;
     content = mp.getBodyPart(0);
     for (int i = 1; i < mp.getCount(); i++) {
       BodyPart bp = mp.getBodyPart(i);
       DefaultAttachment camelAttachment = new DefaultAttachment(bp.getDataHandler());
       @SuppressWarnings("unchecked")
       Enumeration<Header> headers = bp.getAllHeaders();
       while (headers.hasMoreElements()) {
         Header header = headers.nextElement();
         camelAttachment.addHeader(header.getName(), header.getValue());
       }
       camelMessage.addAttachmentObject(getAttachmentKey(bp), camelAttachment);
     }
   }
   if (content instanceof BodyPart) {
     BodyPart bp = (BodyPart) content;
     camelMessage.setBody(bp.getInputStream());
     contentType = bp.getContentType();
     if (contentType != null && !DEFAULT_CONTENT_TYPE.equals(contentType)) {
       camelMessage.setHeader(CONTENT_TYPE, contentType);
       ContentType ct = new ContentType(contentType);
       String charset = ct.getParameter("charset");
       if (charset != null) {
         camelMessage.setHeader(Exchange.CONTENT_ENCODING, MimeUtility.javaCharset(charset));
       }
     }
   } else {
     // If we find no body part, try to leave the message alone
     LOG.info("no MIME part found");
   }
   return camelMessage;
 }