camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mills, Gary (GE Digital)" <>
Subject RE: zipfile question - thank you
Date Wed, 02 Nov 2016 14:23:26 GMT

This is my code and what was changed to make it work.  The 2 commented statements, plus the


import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.activation.DataHandler;
import javax.mail.Address;
import javax.mail.Message;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.log4j.Logger;


public class EmailProcessor implements Processor {

	private static final Logger LOG = Logger.getLogger(EmailProcessor.class);

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");

	public void process(Exchange exchange) throws Exception {"Entering EmailProcessor...");

		Map<String, DataHandler> attachments = exchange.getIn().getAttachments();

		if ((null == attachments) || (attachments.size() < 1)) {
			throw new EmailProcessorException("Null or 0 attachements");
		} else {"attachments.size = " + attachments.size());

		Map<String, String> emailAttr = gatherEmailHeaderInformation(exchange);

		List<String> attachmentFilenames = new ArrayList<String>();

		try {

			List<FilenameAndContents> attachmentArray = new ArrayList<FilenameAndContents>();

			for (String name : attachments.keySet()) {

				DataHandler dh = attachments.get(name);
				String filename = dh.getName();

				//String contents = exchange.getContext().getTypeConverter().convertTo(String.class, dh.getInputStream());
				byte[] contents = exchange.getContext().getTypeConverter().convertTo(byte[].class, dh.getInputStream());"Attachment file name: " + filename);

				FilenameAndContents attachmentFile = new FilenameAndContents();
				//attachmentFile.setFileContents(new String(contents));



		} catch (org.apache.camel.TypeConversionException tce) {
			throw new EmailProcessorException(
					"Unable to type convert from file to string", tce);
		} catch ( ioe) {
			throw new EmailProcessorException(
					"IOException while obtaining Input Stream", ioe);
		} catch (java.lang.UnsupportedOperationException uoe) {
			throw new EmailProcessorException(
					"UnsupportedOperationException add operation is not supported by list",
		} catch (java.lang.ClassCastException cce) {
			throw new EmailProcessorException(
					"ClassCastException element prevents it from being added to list",
		} catch (java.lang.NullPointerException npe) {
			throw new EmailProcessorException(
					"NullPointerException element is null", npe);
		} catch (java.lang.IllegalArgumentException iae) {
			throw new EmailProcessorException(
					"IllegalArgumentException property of element prevents it from being added to list",

		archiveEmail(emailAttr, attachmentFilenames, exchange);"Exiting EmailProcessor.");

	private Map<String, String> gatherEmailHeaderInformation(Exchange exchange) {

		final String emailBody = exchange.getIn().getBody(String.class);

		final Message mailMessage = exchange.getIn().getBody(javax.mail.Message.class);

		Map<String, String> attr = new HashMap<String, String>();

		try {
			if (null != mailMessage) {

				final Address[] fromArray = mailMessage.getFrom();
				if (null != fromArray) {
					String fromStr = convertAddressListToString(fromArray);
					attr.put("from", fromStr);

				final Address[] toArray = mailMessage
				if (null != toArray) {
					String toStr = convertAddressListToString(fromArray);
					attr.put("to", toStr);

				final Address[] ccArray = mailMessage
				if (null != ccArray) {
					String ccStr = convertAddressListToString(fromArray);
					attr.put("CC", ccStr);

				final Address[] bccArray = mailMessage
				if (null != bccArray) {
					String bccStr = convertAddressListToString(fromArray);
					attr.put("BCC", bccStr);

				final String subjectStr = mailMessage.getSubject();
				if (null != subjectStr) {
					attr.put("subject", subjectStr);

				final Date sentDate = mailMessage.getReceivedDate();
				if (null != sentDate) {
					attr.put("sentDate", sdf.format(sentDate));

				final Date receivedDate = mailMessage.getSentDate();
				if (null != receivedDate) {
					attr.put("receivedDate", sdf.format(receivedDate));

				if (null != emailBody) {
					attr.put("body", emailBody);

		} catch (javax.mail.MessagingException me) {
			LOG.error("Unable to gather email header information");

		return attr;


	private void archiveEmail(Map<String, String> attr,
			List<String> attachmentFilenames, Exchange exchange) {

		final String archivePath = exchange.getIn().getHeader("emailArchive", String.class);

		Path parentP = Paths.get(archivePath);
		Path fileP;
		if (null != attr.get("receivedDate")) {
			fileP = Paths.get(archivePath, exchange.getExchangeId() + "." + attr.get("receivedDate"));
		} else {
			fileP = Paths.get(archivePath, exchange.getExchangeId());

		try {
		} catch (IOException ioe) {
			LOG.error("Unable to create email archive directories");

		Charset charset = Charset.forName("utf-8");

		try (BufferedWriter bufferedWriter = Files.newBufferedWriter(fileP,
				charset)) {

			if (null != attr.get("from")) {
				bufferedWriter.write("From:" + attr.get("from"));

			if (null != attr.get("to")) {
				bufferedWriter.write("To:" + attr.get("to"));

			if (null != attr.get("CC")) {
				bufferedWriter.write("CC:" + attr.get("CC"));

			if (null != attr.get("BCC")) {
				bufferedWriter.write("BCC" + attr.get("BCC"));

			if (null != attr.get("subject")) {
				bufferedWriter.write("Subject:" + attr.get("subject"));

			if (null != attr.get("sentDate")) {
				bufferedWriter.write("Sent Date:" + attr.get("sentDate"));

			if (null != attr.get("receivedDate")) {
				bufferedWriter.write("Received Date:"
						+ attr.get("receivedDate"));

			if (null != attr.get("body")) {
				bufferedWriter.write("Body:" + attr.get("body"));

			for (String s : attachmentFilenames) {
				bufferedWriter.write("Attachment File:" + s);

		} catch (IOException ioe) {
			LOG.error("Unable to write email archive");


	private String convertAddressListToString(Address[] adds) {

		String returnStr = "";

		for (Address adr : adds) {
			returnStr = returnStr + adr.toString() + " ";

		return returnStr;


 * DownloadFile is a simple POJO that is a wrapper around a
 * fileName and fileContents
public class FilenameAndContents {

	private String fileName;
	//private String fileContents;
	private byte[] fileContents;

	public String getFileName() {
		return fileName;

	public void setFileName(String fileName) {
		this.fileName = fileName;

	public byte[] getFileContents() {
		return fileContents;
//	public byte[] getFileContents() {
//		return fileContents;
//	}

//	public void setFileContents(String fileContents) {
//		this.fileContents = fileContents;
//	}

	public void setFileContents(byte[] fileContents) {
		this.fileContents = fileContents;

-----Original Message-----
From: Mills, Gary (GE Digital) 
Sent: Wednesday, November 02, 2016 9:18 AM
Subject: RE: zipfile question - thank you

Thank you so much Stephan for your response. I was thinking I was doing something wrong on
these forums or email chats - thank you. You are completely right, I have that in place didn't
share it yet.  However, I believe I have solved this.  It was type conversion - go figure,
I was changing the type to String object instead of using the serialized entry byte stream.
 After this change - I'm golden. 

Thank you again, 
Have a great day!

-----Original Message-----
From: Siano, Stephan [] 
Sent: Wednesday, November 02, 2016 2:36 AM
Subject: EXT: RE: zipfile question - thank you


how do you retreive the message from the mail server? On the server an email with a zip file
attachment is actually a MIME-Multipart. One of the parts within that is the (usually base64
encoded) zip file. If you just read the message with some form(pop3:xxx) thing, and try to
unmarshal that as a zip file, this will of course fail (because you are feeding the message
body, not the attachment into it).

If you want to process the attachment, you will first need to add some processor that fetches
the attachment from the message and puts the InputStream of the payload as the main message

Best regards

-----Original Message-----
From: Mills, Gary (GE Digital) [] 
Sent: Dienstag, 1. November 2016 19:41
Subject: RE: zipfile question - thank you

DefaultErrorHandler              | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133
| Failed delivery for (MessageId: ID-alphprdfuse2i-43262-1478008924653-2-36907 on ExchangeId:
ID-alphprdfuse2i-43262-1478008924653-2-36908). Exhausted after delivery attempt: 1 caught:
org.apache.camel.InvalidPayloadException: No body available of type: java.lang.String but
has value: org.apache.camel.dataformat.zipfile.ZipInputStreamWrapper@bcbe474 of type: org.apache.camel.dataformat.zipfile.ZipInputStreamWrapper
on: Message: [Body is instance of]. Caused by: Error during type conversion
from type: java.lang.String to the required type: java.lang.String with value [Body is instance
of] due invalid code lengths set. Exchange[Message:
[Body is instance of]]. Caused by: [org.apache.camel.TypeConversionException
- Error during type conversion from type: java.lang.String to the required type: java.lang.String
with value [Body is instance of] due invalid
code lengths set]

I am very confused.  If I take that zip file and simply drop it in the fileEntranceEndpoint
everything works fine. If I send it through email it doesn't work. The only thing I've noticed
is that the zip file appears to be corrupted after extracting the attachment and the above
is what error is thrown.

Thank you

From: Mills, Gary (GE Digital)
Sent: Tuesday, November 01, 2016 11:52 AM
To: Mills, Gary (GE Digital) <>
Subject: RE: zipfile question - thank you


When I send an email to the endpoint email address, with an zip file attachment, and I open
the zip file attachment directly from email endpoint mailbox, the zip file attachment opens
ok.  It appears to be related to the extraction of the attachment in Camel ?


From: Mills, Gary (GE Digital)
Sent: Tuesday, November 01, 2016 10:34 AM
To: '' <<>>
Subject: RE: zipfile question - thank you

Here is the code used to unmarshall the zip file

       public void configure() throws Exception {

              ZipFileDataFormat zipFile = new ZipFileDataFormat();

received zip file: ${file:name}")
              .log("file names from ZipFile: ${file:name}").to("file://" + outDirectory);

From: Mills, Gary (GE Digital)
Sent: Tuesday, November 01, 2016 10:58 AM
To: '' <<>>
Subject: zipfile question - thank you

Camel users, ( thank you )
My project is to receive a zip compressed and encrypted file attachment from a customer, naturally,
decompress and decrypt the attachment via email transfer.

I am only focusing on the zip portion now because over email I can't get it to work.  Seems
that the compression size changes when sent over email.

It seems my logic works, if I take the zip file and simply drop it in the route from my Jboss
environment it works. I can zip, unzip, the file from command line and the camel route. However,
if I send the file as an attachment over email, the file size changes, and of course it does
not work and throws the error...
org.apache.camel.RuntimeCamelException: invalid code lengths set

which suggests the zip file attachment has been altered during its transfer over email. The
original file size is 9044 Bytes, after email xfr becomes, 15580 Bytes.  If I try opening
it from the OS with unzip, gunzip, it shows:

[root@alphprdfuse2i latest]# unzip ge-ip/core/tobeprocessed/archive/
Archive:  ge-ip/core/tobeprocessed/archive/
caution:  zipfile comment truncated
error [ge-ip/core/tobeprocessed/archive/]:  missing 3282862893 bytes in
  (attempting to process anyway)
error [ge-ip/core/tobeprocessed/archive/]:  attempt to seek before beginning
of zipfile
  (please check that you have transferred or created the zipfile in the
  appropriate BINARY mode and that you have compiled UnZip properly)

I've tried changing the file extension to JPEG, PNG, ge, etc... with no success.

Does anyone know what I can do to get around this???

Thank you !!

Gary Mills

View raw message