Author: ningjiang
Date: Thu Apr 24 01:37:07 2008
New Revision: 651196
URL: http://svn.apache.org/viewvc?rev=651196&view=rev
Log:
CAMEL-385 applied the patch from Lars
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java?rev=651196&r1=651195&r2=651196&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java
Thu Apr 24 01:37:07 2008
@@ -17,6 +17,9 @@
package org.apache.camel;
import java.util.Map;
+import java.util.Set;
+
+import javax.activation.DataHandler;
/**
* Implements the <a
@@ -132,4 +135,47 @@
* Copies the contents of the other message into this message
*/
void copyFrom(Message message);
+ /**
+ * returns the attachment specified by the id
+ *
+ * @param id the id under which the attachment is stored
+ * @return the data handler for this attachment or null
+ */
+ DataHandler getAttachment(String id);
+
+ /**
+ * returns a set of attachment names of the message
+ *
+ * @return a set of attachment names
+ */
+ Set<String> getAttachmentNames();
+
+ /**
+ * removes the attachment specified by the id
+ *
+ * @param id the id of the attachment to remove
+ */
+ void removeAttachment(String id);
+
+ /**
+ * adds an attachment to the message using the id
+ *
+ * @param id the id to store the attachment under
+ * @param content the data handler for the attachment
+ */
+ void addAttachment(String id, DataHandler content);
+
+ /**
+ * returns all attachments of the message
+ *
+ * @return the attachments in a map or null
+ */
+ Map<String, DataHandler> getAttachments();
+
+ /**
+ * Set all the attachments associated with this message
+ *
+ * @param attachments
+ */
+ void setAttachments(Map<String, DataHandler> attachments);
}
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java?rev=651196&r1=651195&r2=651196&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
Thu Apr 24 01:37:07 2008
@@ -18,6 +18,9 @@
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
+
+import javax.activation.DataHandler;
import org.apache.camel.Message;
@@ -28,6 +31,7 @@
*/
public class DefaultMessage extends MessageSupport {
private Map<String, Object> headers;
+ private Map<String, DataHandler> attachments;
@Override
public String toString() {
@@ -94,5 +98,80 @@
* @param map is the empty header map to populate
*/
protected void populateInitialHeaders(Map<String, Object> map) {
+ }
+
+ /**
+ * A factory method to lazily create the attachments to make it easy to
+ * create efficient Message implementations which only construct and
+ * populate the Map on demand
+ *
+ * @return return a newly constructed Map
+ */
+ protected Map<String, DataHandler> createAttachments() {
+ HashMap<String, DataHandler> map = new HashMap<String, DataHandler>();
+ populateInitialAttachments(map);
+ return map;
+ }
+
+ /**
+ * A strategy method populate the initial set of attachments on an inbound
+ * message from an underlying binding
+ *
+ * @param map is the empty attachment map to populate
+ */
+ protected void populateInitialAttachments(Map<String, DataHandler> map) {
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.camel.Message#addAttachment(java.lang.String,
javax.activation.DataHandler)
+ */
+ public void addAttachment(String id, DataHandler content) {
+ if (attachments == null) {
+ attachments = createAttachments();
+ }
+ attachments.put(id, content);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.camel.Message#getAttachment(java.lang.String)
+ */
+ public DataHandler getAttachment(String id) {
+ return getAttachments().get(id);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.camel.Message#getAttachmentNames()
+ */
+ public Set<String> getAttachmentNames() {
+ if (attachments == null) {
+ attachments = createAttachments();
+ }
+ return attachments.keySet();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.camel.Message#removeAttachment(java.lang.String)
+ */
+ public void removeAttachment(String id) {
+ if (attachments != null && attachments.containsKey(id)) {
+ attachments.remove(id);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.camel.Message#getAttachments()
+ */
+ public Map<String, DataHandler> getAttachments() {
+ if (attachments == null) {
+ attachments = createAttachments();
+ }
+ return attachments;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.camel.Message#setAttachments(java.util.Map)
+ */
+ public void setAttachments(Map<String, DataHandler> attachments) {
+ this.attachments = attachments;
}
}
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java?rev=651196&r1=651195&r2=651196&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
(original)
+++
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
Thu Apr 24 01:37:07 2008
@@ -25,10 +25,10 @@
* A base class for implementation inheritence providing the core
* [EMAIL PROTECTED] Message} body handling features but letting the derived
class deal
* with headers.
- *
+ *
* Unless a specific provider wishes to do something particularly clever with
* headers you probably want to just derive from [EMAIL PROTECTED]
DefaultMessage}
- *
+ *
* @version $Revision$
*/
public abstract class MessageSupport implements Message {
@@ -87,6 +87,7 @@
setMessageId(that.getMessageId());
setBody(that.getBody());
getHeaders().putAll(that.getHeaders());
+ getAttachments().putAll(that.getAttachments());
}
public Exchange getExchange() {
@@ -105,7 +106,7 @@
/**
* A factory method to allow a provider to lazily create the message body
* for inbound messages from other sources
- *
+ *
* @return the value of the message body or null if there is no value
* available
*/
Modified:
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java?rev=651196&r1=651195&r2=651196&view=diff
==============================================================================
---
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java
(original)
+++
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java
Thu Apr 24 01:37:07 2008
@@ -16,14 +16,19 @@
*/
package org.apache.camel;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
+import java.net.URL;
-import junit.framework.TestCase;
+import javax.activation.DataHandler;
+import javax.activation.URLDataSource;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultExchange;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import junit.framework.TestCase;
+
/**
* @version $Revision$
*/
@@ -50,6 +55,11 @@
// TODO better conversion example when the property editor support is
added
}
+
+ public void testConversionOfMessageAttachments() throws Exception {
+ DataHandler handler = exchange.getIn().getAttachment("att");
+ assertNotNull("attachment got lost", handler);
+ }
@Override
protected void setUp() throws Exception {
@@ -59,5 +69,6 @@
Message message = exchange.getIn();
message.setBody("<hello>world!</hello>");
message.setHeader("bar", 567);
+ message.addAttachment("att", new DataHandler(new URLDataSource(new
URL("http://activemq.apache.org/camel/message.html"))));
}
}
Modified:
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?rev=651196&r1=651195&r2=651196&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
(original)
+++
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
Thu Apr 24 01:37:07 2008
@@ -20,11 +20,16 @@
import java.util.Map;
import java.util.Set;
+import javax.activation.DataHandler;
import javax.mail.Address;
+import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
+import javax.mail.Part;
import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
import org.apache.camel.Exchange;
import org.apache.camel.converter.ObjectConverter;
@@ -50,9 +55,16 @@
String from = endpoint.getConfiguration().getFrom();
mimeMessage.setFrom(new InternetAddress(from));
}
- mimeMessage.setText(exchange.getIn().getBody(String.class));
+
+ if (exchange.getIn().getAttachments() != null &&
exchange.getIn().getAttachments().size() > 0) {
+ appendAttachmentsFromCamel(mimeMessage, exchange,
exchange.getIn());
+ } else {
+ mimeMessage.setText(exchange.getIn().getBody(String.class));
+ }
} catch (Exception e) {
- throw new RuntimeMailException("Failed to populate body due to: "
+ e + ". Exchange: " + exchange, e);
+ throw new RuntimeMailException(
+ "Failed to populate body due to: "
+ e + ". Exchange: " + exchange,
+ e);
}
}
@@ -77,7 +89,8 @@
/**
* Appends the Mail headers from the Camel [EMAIL PROTECTED] MailMessage}
*/
- protected void appendHeadersFromCamel(MimeMessage mimeMessage, Exchange
exchange, org.apache.camel.Message camelMessage) throws MessagingException {
+ protected void appendHeadersFromCamel(MimeMessage mimeMessage, Exchange
exchange,
+ org.apache.camel.Message
camelMessage) throws MessagingException {
Set<Map.Entry<String, Object>> entries =
camelMessage.getHeaders().entrySet();
for (Map.Entry<String, Object> entry : entries) {
String headerName = entry.getKey();
@@ -101,6 +114,47 @@
}
/**
+ * Appends the Mail attachments from the Camel [EMAIL PROTECTED]
MailMessage}
+ */
+ protected void appendAttachmentsFromCamel(MimeMessage mimeMessage,
Exchange exchange,
+ org.apache.camel.Message
camelMessage)
+ throws MessagingException {
+
+ // Create a Multipart
+ MimeMultipart multipart = new MimeMultipart();
+
+ // fill the body with text
+ multipart.setSubType("mixed");
+ MimeBodyPart textBodyPart = new MimeBodyPart();
+ textBodyPart.setContent(exchange.getIn().getBody(String.class),
"text/plain");
+ multipart.addBodyPart(textBodyPart);
+
+ BodyPart messageBodyPart = null;
+
+ Set<Map.Entry<String, DataHandler>> entries =
camelMessage.getAttachments().entrySet();
+ for (Map.Entry<String, DataHandler> entry : entries) {
+ String attName = entry.getKey();
+ DataHandler attValue = entry.getValue();
+ if (attValue != null) {
+ if (shouldOutputAttachment(camelMessage, attName, attValue)) {
+ // Create another body part
+ messageBodyPart = new MimeBodyPart();
+ // Set the data handler to the attachment
+ messageBodyPart.setDataHandler(attValue);
+ // Set the filename
+ messageBodyPart.setFileName(attName);
+ // Set Disposition
+ messageBodyPart.setDisposition(Part.ATTACHMENT);
+ // Add part to multipart
+ multipart.addBodyPart(messageBodyPart);
+ }
+ }
+ }
+ // Put parts in message
+ mimeMessage.setContent(multipart);
+ }
+
+ /**
* Converts the given object value to a String
*/
protected String asString(Exchange exchange, Object value) {
@@ -110,7 +164,17 @@
/**
* Strategy to allow filtering of headers which are put on the Mail message
*/
- protected boolean shouldOutputHeader(org.apache.camel.Message
camelMessage, String headerName, Object headerValue) {
+ protected boolean shouldOutputHeader(org.apache.camel.Message
camelMessage, String headerName,
+ Object headerValue) {
+ return true;
+ }
+
+ /**
+ * Strategy to allow filtering of attachments which are put on the Mail
+ * message
+ */
+ protected boolean shouldOutputAttachment(org.apache.camel.Message
camelMessage, String headerName,
+ DataHandler headerValue) {
return true;
}
}
Modified:
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java?rev=651196&r1=651195&r2=651196&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
(original)
+++
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailMessage.java
Thu Apr 24 01:37:07 2008
@@ -16,19 +16,23 @@
*/
package org.apache.camel.component.mail;
+import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
+import javax.activation.DataHandler;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
+import javax.mail.Multipart;
+import javax.mail.Part;
import org.apache.camel.impl.DefaultMessage;
import org.apache.camel.util.CollectionHelper;
/**
* Represents a [EMAIL PROTECTED] org.apache.camel.Message} for working with
Mail
- *
+ *
* @version $Revision:520964 $
*/
public class MailMessage extends DefaultMessage {
@@ -125,11 +129,70 @@
}
}
+ /* (non-Javadoc)
+ * @see
org.apache.camel.impl.DefaultMessage#populateInitialAttachments(java.util.Map)
+ */
+ @Override
+ protected void populateInitialAttachments(Map<String, DataHandler> map) {
+ if (mailMessage != null) {
+ try {
+ extractAttachments(map);
+ } catch (MessagingException ex) {
+ throw new RuntimeMailException("Error populating the initial
mail message attachments", ex);
+ }
+ }
+ }
+
public void copyFrom(org.apache.camel.Message that) {
super.copyFrom(that);
if (that instanceof MailMessage) {
MailMessage mailMessage = (MailMessage) that;
this.mailMessage = mailMessage.mailMessage;
+ }
+ }
+
+ /**
+ * parses the attachments of the mail message and puts them to the message
+ *
+ * @param map the attachments map
+ * @throws javax.mail.MessagingException
+ */
+ protected void extractAttachments(Map<String, DataHandler> map) throws
javax.mail.MessagingException {
+ // now convert the mail attachments and put it to the msg
+ Multipart mp;
+ Object content;
+
+ try {
+ content = this.mailMessage.getContent();
+
+ if (content instanceof Multipart) {
+ // mail with attachment
+ mp = (Multipart)content;
+ int nbMP = mp.getCount();
+ for (int i = 0; i < nbMP; i++) {
+ Part part = mp.getBodyPart(i);
+ String disposition = part.getDisposition();
+
+ if (disposition != null
+ && (disposition.equalsIgnoreCase(Part.ATTACHMENT) ||
disposition
+ .equalsIgnoreCase(Part.INLINE))) {
+ // only add named attachments
+ if (part.getFileName() != null) {
+ // Parts marked with a disposition of
+ // Part.ATTACHMENT
+ // from part.getDisposition() are clearly
+ // attachments
+ DataHandler att = part.getDataHandler();
+ // this is clearly a attachment
+ CollectionHelper.appendValue(map,
part.getFileName(), att);
+ }
+ }
+ }
+ }
+ } catch (MessagingException e) {
+ throw new javax.mail.MessagingException("Error while setting
content on normalized message", e);
+ } catch (IOException e) {
+ throw new javax.mail.MessagingException("Error while fetching
content", e);
}
}
}
Modified:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java?rev=651196&r1=651195&r2=651196&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java
(original)
+++
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java
Thu Apr 24 01:37:07 2008
@@ -74,7 +74,8 @@
}
public void testManyConfigurations() throws Exception {
- MailEndpoint endpoint = resolveMandatoryEndpoint("smtp://[EMAIL
PROTECTED]:30/subject?password=secret&[EMAIL
PROTECTED]&DeleteProcessedMessages=false&defaultEncoding=iso-8859-1&folderName=riders");
+ MailEndpoint endpoint = resolveMandatoryEndpoint(
+ "smtp://[EMAIL PROTECTED]:30/subject?password=secret&[EMAIL
PROTECTED]&DeleteProcessedMessages=false&defaultEncoding=iso-8859-1&folderName=riders");
MailConfiguration config = endpoint.getConfiguration();
assertEquals("getProtocol()", "smtp", config.getProtocol());
assertEquals("getHost()", "myhost", config.getHost());
Modified:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java?rev=651196&r1=651195&r2=651196&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java
(original)
+++
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MimeMessageConsumeTest.java
Thu Apr 24 01:37:07 2008
@@ -16,11 +16,20 @@
*/
package org.apache.camel.component.mail;
+import java.io.File;
+import java.net.URISyntaxException;
+import java.util.Iterator;
import java.util.Properties;
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.activation.FileDataSource;
+import javax.activation.URLDataSource;
+import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
+import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart;
@@ -59,6 +68,13 @@
String text = exchange.getIn().getBody(String.class);
assertEquals("mail body", body, text);
+
+ assertNotNull("attachments got lost",
exchange.getIn().getAttachments());
+ Iterator<String> keyIt =
exchange.getIn().getAttachmentNames().iterator();
+ while (keyIt.hasNext()) {
+ DataHandler dh = exchange.getIn().getAttachment(keyIt.next());
+ log.info("Found attachment: " + dh.getName());
+ }
}
/**
@@ -82,6 +98,30 @@
mixed.addBodyPart(plainPart);
mixed.addBodyPart(htmlPart);
+
+ DataSource ds = null;
+ try {
+ File f = new
File(getClass().getResource("/log4j.properties").toURI());
+ ds = new FileDataSource(f);
+ } catch (URISyntaxException ex) {
+ ds = new
URLDataSource(getClass().getResource("/log4j.properties"));
+ }
+ DataHandler dh = new DataHandler(ds);
+
+ BodyPart attachmentBodyPart = null;
+ // Create another body part
+ attachmentBodyPart = new MimeBodyPart();
+ // Set the data handler to the attachment
+ attachmentBodyPart.setDataHandler(dh);
+ // Set the filename
+ attachmentBodyPart.setFileName(dh.getName());
+ // Set Disposition
+ attachmentBodyPart.setDisposition(Part.ATTACHMENT);
+
+ mixed.addBodyPart(plainPart);
+ mixed.addBodyPart(htmlPart);
+ // Add attachmentBodyPart to multipart
+ mixed.addBodyPart(attachmentBodyPart);
message.setContent(mixed);
}