Java Code Examples for org.apache.james.mime4j.stream.BodyDescriptor

The following examples show how to use org.apache.james.mime4j.stream.BodyDescriptor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: email-mime-parser   Source File: Common.java    License: Apache License 2.0 6 votes vote down vote up
public static String getAttachmentNameFromDispositionParameters(BodyDescriptor bd) {
    //Refer RFC : 2047 for mime word decoding
    //Refer RFC : 2183 for Content-Disposition Header Field
    String attachmentName = null;
    if (bd instanceof MaximalBodyDescriptor) {
        attachmentName = getDecodedWord(((MaximalBodyDescriptor) bd).getContentDispositionFilename());
        if (StringUtils.isEmpty(attachmentName)) {
            //Handling case where RFC 2183 is not properly implemented by tech.blueglacier.email creating client.
            attachmentName = getDecodedWord(((MaximalBodyDescriptor) bd).getContentDispositionParameters().get("name"));
        }
        //Added case for supporting RFC 2184 'Parameter Value Character Set and Language Information' 
        // this is currently not supported in mime4j (version 0.7.2)
        if (StringUtils.isEmpty(attachmentName)) {
            attachmentName = getDecodedDispositionFileName(bd);
        }
    }
    return attachmentName;
}
 
Example 2
Source Project: holdmail   Source File: MessageContentExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSetSequenceWithEachNewPart() throws Exception{

    BodyDescriptor bodyMock = mock(BodyDescriptor.class);
    ByteArrayInputStream streamMock = new ByteArrayInputStream(new byte[] {});

    MessageContentExtractor extractor = new MessageContentExtractor();

    extractor.startHeader();
    extractor.body(bodyMock, streamMock);
    assertThat(getCurrentSequence(extractor)).containsExactly(1);

    extractor.startHeader();
    extractor.body(bodyMock, streamMock);
    assertThat(getCurrentSequence(extractor)).containsExactly(1, 2);

    extractor.startHeader();
    extractor.body(bodyMock, streamMock);
    assertThat(getCurrentSequence(extractor)).containsExactly(1, 2, 3);
}
 
Example 3
Source Project: SnowGraph   Source File: MboxHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void body(BodyDescriptor bd, InputStream is) throws MimeException,
        IOException {
    String r = "";
    byte[] buffer = new byte[200];
    String s;
    int len;
    try {
        while ((len = is.read(buffer)) != -1) {
            if (len != 200) {
                byte buffer2[] = new byte[len];
                System.arraycopy(buffer, 0, buffer2, 0, len);
                s = new String(buffer2);
            } else {
                s = new String(buffer);
            }
            if (s != null)
                r += s;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    mailInfo.body = r;
    //System.out.println("body");
    //System.out.println(r);
}
 
Example 4
Source Project: email-mime-parser   Source File: Email.java    License: Apache License 2.0 5 votes vote down vote up
public void fillEmailContents(BodyDescriptor bd, InputStream is){
	LOGGER.info("mime part received");
	if(addPlainTextEmailBody(bd,is)){}
	else if(addHTMLEmailBody(bd,is)){}
	else if(addCalendar(bd,is)){}
	else{
		addAttachments(bd,is);
	}
}
 
Example 5
Source Project: email-mime-parser   Source File: Email.java    License: Apache License 2.0 5 votes vote down vote up
private boolean addCalendar(BodyDescriptor bd, InputStream is) {
	boolean isBodySet = false;
	BodyDescriptor calendarBodyDescriptor = bd;
	if(calendarBody == null){
		if(isCalendarBody(calendarBodyDescriptor)){
			calendarBody = new CalendarBody(bd,is);
			isBodySet = true;
			LOGGER.info("Email calendar body identified");
		}
	}
	
	return isBodySet;
}
 
Example 6
Source Project: email-mime-parser   Source File: Email.java    License: Apache License 2.0 5 votes vote down vote up
private boolean addHTMLEmailBody(BodyDescriptor bd, InputStream is) {
	boolean isBodySet = false;
	BodyDescriptor emailHTMLBodyDescriptor = bd;
	if(htmlEmailBody == null){
		if(isHTMLBody(emailHTMLBodyDescriptor)){
			htmlEmailBody = new HtmlEmailBody(bd,is);
			isBodySet = true;
			LOGGER.info("Email html body identified");
		}
	}else{
		if(isHTMLBody(emailHTMLBodyDescriptor)){
			if(multipartStack.peek().getBodyDescriptor().getMimeType().equalsIgnoreCase("multipart/mixed")){
				InputStream mainInputStream;
				try {
					mainInputStream = concatInputStream(is, htmlEmailBody.getIs());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
				htmlEmailBody.setIs(mainInputStream);
			}else{
				addAttachments(new HtmlEmailBody(bd,is));
			}
			isBodySet = true;
		}
	}		
	return isBodySet;
}
 
Example 7
Source Project: email-mime-parser   Source File: Email.java    License: Apache License 2.0 5 votes vote down vote up
private boolean addPlainTextEmailBody(BodyDescriptor bd, InputStream is) {
	boolean isBodySet = false;
	BodyDescriptor emailPlainBodyDescriptor = bd;
	if(plainTextEmailBody == null){
		if(isPlainTextBody(emailPlainBodyDescriptor)){
			plainTextEmailBody = new PlainTextEmailBody(bd,is);
			isBodySet = true;
			LOGGER.info("Email plain text body identified");
		}
	}else{
		if(isPlainTextBody(emailPlainBodyDescriptor)){
			if(multipartStack.peek().getBodyDescriptor().getMimeType().equalsIgnoreCase("multipart/mixed")){
				InputStream mainInputStream;
				try {
					mainInputStream = concatInputStream(is,plainTextEmailBody.getIs());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
				plainTextEmailBody.setIs(mainInputStream);
			}else{
				addAttachments(new PlainTextEmailBody(bd,is));
			}
			isBodySet = true;
		}
	}		
	return isBodySet;
}
 
Example 8
Source Project: email-mime-parser   Source File: CustomContentHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void body(BodyDescriptor bd, InputStream is) throws MimeException, IOException {
    // Gracefully switching off the case of email attached within an email
    if (email.getMessageStack().peek().getEmailMessageTypeHierarchy() == EmailMessageTypeHierarchy.parent) {
        email.fillEmailContents(bd, is);
    }
}
 
Example 9
Source Project: email-mime-parser   Source File: Common.java    License: Apache License 2.0 5 votes vote down vote up
private static String getDecodedDispositionFileName(BodyDescriptor bd){
	String attachmentName = null;
	try {
		attachmentName = ContentDispositionDecoder.decodeDispositionFileName(((MaximalBodyDescriptor)bd).getContentDispositionParameters());
	} catch (MimeException e) {
		throw new RuntimeException(e);
	}
	return attachmentName;
}
 
Example 10
Source Project: email-mime-parser   Source File: Common.java    License: Apache License 2.0 5 votes vote down vote up
public static String getAttachmentNameFromContentTypeParmaeters(BodyDescriptor bd) {
	String attachmentName = null;
	if(bd instanceof MaximalBodyDescriptor){
		Map<String, String> contentTypeParameters = ((MaximalBodyDescriptor)bd).getContentTypeParameters();
		String nameKey = null;
		if(contentTypeParameters.containsKey(nameKey = "name") || contentTypeParameters.containsKey(nameKey = "NAME")
				|| contentTypeParameters.containsKey(nameKey = "Name")){
			attachmentName = contentTypeParameters.get(nameKey);
		}
		attachmentName = getDecodedWord(attachmentName);
	}
	return attachmentName;
}
 
Example 11
Source Project: email-mime-parser   Source File: Common.java    License: Apache License 2.0 5 votes vote down vote up
public static String getAttachmentName(BodyDescriptor bd){		
	// Content tech.blueglacier.disposition 'filename' is more standard, so it's taken as default first
	String attachmentName = Common.getAttachmentNameFromDispositionParameters(bd);
	if(attachmentName == null || attachmentName.isEmpty()){			
		// Content type 'name' is other alternative so it's taken as alternative too
		attachmentName = Common.getAttachmentNameFromContentTypeParmaeters(bd);			
	}		
	return attachmentName;		
}
 
Example 12
private AbstractContentHandler createMdnContentHandler(final ResultCollector resultCollector) {
    return new AbstractContentHandler() {
        @Override
        public void body(BodyDescriptor bodyDescriptor, InputStream inputStream) throws MimeException, IOException {
            if (bodyDescriptor.getMimeType().equalsIgnoreCase("message/disposition-notification")) {
                resultCollector.setResult(MDNReportParser.parse(inputStream, bodyDescriptor.getCharset())
                    .map(report -> report.getDispositionField().getSendingMode() == DispositionSendingMode.Automatic)
                    .getOrElse(() -> false));
            }
        }
    };
}
 
Example 13
Source Project: email-mime-parser   Source File: EmailAttachment.java    License: Apache License 2.0 4 votes vote down vote up
public EmailAttachment(BodyDescriptor bd, InputStream is){
	super(bd, is);
}
 
Example 14
Source Project: email-mime-parser   Source File: PlainTextEmailBody.java    License: Apache License 2.0 4 votes vote down vote up
public PlainTextEmailBody(BodyDescriptor bd, InputStream is) {
	super(bd, is);
}
 
Example 15
Source Project: email-mime-parser   Source File: MultipartType.java    License: Apache License 2.0 4 votes vote down vote up
public MultipartType(BodyDescriptor bodyDescriptor){
	this.bodyDiscriptor = bodyDescriptor; 
}
 
Example 16
Source Project: email-mime-parser   Source File: MultipartType.java    License: Apache License 2.0 4 votes vote down vote up
public BodyDescriptor getBodyDescriptor() {
	return bodyDiscriptor;
}
 
Example 17
Source Project: email-mime-parser   Source File: CalendarBody.java    License: Apache License 2.0 4 votes vote down vote up
public CalendarBody(BodyDescriptor bd, InputStream is) {
	super(bd, is);
}
 
Example 18
Source Project: email-mime-parser   Source File: Email.java    License: Apache License 2.0 4 votes vote down vote up
private void addAttachments(BodyDescriptor bd, InputStream is) {
   attachments.add(new EmailAttachment(bd,is));
   LOGGER.info("Email attachment identified");
}
 
Example 19
Source Project: email-mime-parser   Source File: Email.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isHTMLBody(BodyDescriptor emailHTMLBodyDescriptor) {
	String bodyName = Common.getAttachmentName(emailHTMLBodyDescriptor);
	return (emailHTMLBodyDescriptor.getMimeType().equalsIgnoreCase("text/html") && bodyName == null);
}
 
Example 20
Source Project: email-mime-parser   Source File: Email.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isCalendarBody(BodyDescriptor emailCalendarBodyDescriptor) {
	String bodyName = Common.getAttachmentName(emailCalendarBodyDescriptor);
	return (emailCalendarBodyDescriptor.getMimeType().equalsIgnoreCase("text/calendar") && bodyName == null);
}
 
Example 21
Source Project: email-mime-parser   Source File: Email.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isPlainTextBody(BodyDescriptor emailPlainBodyDescriptor) {
	String bodyName = Common.getAttachmentName(emailPlainBodyDescriptor);
	return (emailPlainBodyDescriptor.getMimeType().equalsIgnoreCase("text/plain") && bodyName == null);
}
 
Example 22
Source Project: email-mime-parser   Source File: Attachment.java    License: Apache License 2.0 4 votes vote down vote up
public BodyDescriptor getBd() {
    return bd;
}
 
Example 23
Source Project: email-mime-parser   Source File: Attachment.java    License: Apache License 2.0 4 votes vote down vote up
public Attachment(BodyDescriptor bd, InputStream is) {
    this.bd = bd;
    attachmentSize = 0;
    setIs(is);
}
 
Example 24
Source Project: email-mime-parser   Source File: HtmlEmailBody.java    License: Apache License 2.0 4 votes vote down vote up
public HtmlEmailBody(BodyDescriptor bd, InputStream is) {
	super(bd, is);
}
 
Example 25
Source Project: email-mime-parser   Source File: CustomContentHandler.java    License: Apache License 2.0 4 votes vote down vote up
public void startMultipart(BodyDescriptor bd) throws MimeException {
    email.getMultipartStack().push(new MultipartType(bd));
}
 
Example 26
Source Project: SnowGraph   Source File: MboxHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void startMultipart(BodyDescriptor bd) throws MimeException {
}
 
Example 27
Source Project: holdmail   Source File: MessageContentExtractor.java    License: Apache License 2.0 3 votes vote down vote up
@Override
public void body(BodyDescriptor bd, InputStream is) throws IOException {

    nextPotentialPart.setContent(is);
    bodyParts.addPart(nextPotentialPart);

}