camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [1/2] camel git commit: CAMEL-6649: AWS Simple Email Service - add attachment support
Date Sun, 12 Jul 2015 10:40:14 GMT
Repository: camel
Updated Branches:
  refs/heads/master 308529ff3 -> 88fca852f


CAMEL-6649: AWS Simple Email Service - add attachment support


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/52495d3c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/52495d3c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/52495d3c

Branch: refs/heads/master
Commit: 52495d3cacb0f796c0f5c86abb915e5733b717a4
Parents: 308529f
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Sun Jul 12 12:05:52 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Sun Jul 12 12:38:43 2015 +0200

----------------------------------------------------------------------
 .../camel/component/aws/ses/SesProducer.java    |  67 +++++-
 .../component/aws/ses/AmazonSESClientMock.java  |  16 ++
 .../camel/component/aws/ses/MockMessage.java    | 235 +++++++++++++++++++
 .../aws/ses/SesComponentSpringTest.java         |  27 +++
 4 files changed, 337 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/52495d3c/components/camel-aws/src/main/java/org/apache/camel/component/aws/ses/SesProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ses/SesProducer.java
b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ses/SesProducer.java
index 78a0295..f5ecaf2 100644
--- a/components/camel-aws/src/main/java/org/apache/camel/component/aws/ses/SesProducer.java
+++ b/components/camel-aws/src/main/java/org/apache/camel/component/aws/ses/SesProducer.java
@@ -16,14 +16,26 @@
  */
 package org.apache.camel.component.aws.ses;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
+
+import javax.activation.DataHandler;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
 
 import com.amazonaws.services.simpleemail.model.Body;
 import com.amazonaws.services.simpleemail.model.Content;
 import com.amazonaws.services.simpleemail.model.Destination;
 import com.amazonaws.services.simpleemail.model.SendEmailRequest;
 import com.amazonaws.services.simpleemail.model.SendEmailResult;
+import com.amazonaws.services.simpleemail.model.SendRawEmailRequest;
+import com.amazonaws.services.simpleemail.model.SendRawEmailResult;
+
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -41,14 +53,21 @@ public class SesProducer extends DefaultProducer {
     }
 
     public void process(Exchange exchange) throws Exception {
-        SendEmailRequest request = createMailRequest(exchange);
-        log.trace("Sending request [{}] from exchange [{}]...", request, exchange);
-        
-        SendEmailResult result = getEndpoint().getSESClient().sendEmail(request);
-
-        log.trace("Received result [{}]", result);
-        Message message = getMessageForResponse(exchange);
-        message.setHeader(SesConstants.MESSAGE_ID, result.getMessageId());
+    	if (!(exchange.getIn().getBody() instanceof javax.mail.Message)) {
+    		SendEmailRequest request = createMailRequest(exchange);
+            log.trace("Sending request [{}] from exchange [{}]...", request, exchange); 
          
+            SendEmailResult result = getEndpoint().getSESClient().sendEmail(request);
+            log.trace("Received result [{}]", result);
+            Message message = getMessageForResponse(exchange);
+            message.setHeader(SesConstants.MESSAGE_ID, result.getMessageId());
+    	} else {
+    		SendRawEmailRequest request = createRawMailRequest(exchange);
+            log.trace("Sending request [{}] from exchange [{}]...", request, exchange); 
          
+            SendRawEmailResult result = getEndpoint().getSESClient().sendRawEmail(request);
+            log.trace("Received result [{}]", result);
+            Message message = getMessageForResponse(exchange);
+            message.setHeader(SesConstants.MESSAGE_ID, result.getMessageId());
+    	}
     }
 
     private SendEmailRequest createMailRequest(Exchange exchange) {
@@ -61,6 +80,14 @@ public class SesProducer extends DefaultProducer {
 
         return request;
     }
+    
+    private SendRawEmailRequest createRawMailRequest(Exchange exchange) {
+    	SendRawEmailRequest request = new SendRawEmailRequest();
+        request.setSource(determineFrom(exchange));
+        request.setDestinations(determineRawTo(exchange));
+        request.setRawMessage(createRawMessage(exchange));
+        return request;
+    }
 
     private com.amazonaws.services.simpleemail.model.Message createMessage(Exchange exchange)
{
         com.amazonaws.services.simpleemail.model.Message message = new com.amazonaws.services.simpleemail.model.Message();
@@ -75,6 +102,21 @@ public class SesProducer extends DefaultProducer {
         return message;
     }
     
+    private com.amazonaws.services.simpleemail.model.RawMessage createRawMessage(Exchange
exchange) {
+        com.amazonaws.services.simpleemail.model.RawMessage message = new com.amazonaws.services.simpleemail.model.RawMessage();
+        javax.mail.Message content = exchange.getIn().getBody(javax.mail.Message.class);
+        OutputStream byteOutput=new ByteArrayOutputStream();
+        try {
+			content.writeTo(byteOutput);
+		} catch (Exception e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+        byte[] messageByteArray=((ByteArrayOutputStream)byteOutput).toByteArray();
+        message.setData(ByteBuffer.wrap(messageByteArray));
+        return message;
+    }
+    
     @SuppressWarnings("unchecked")
     private Collection<String> determineReplyToAddresses(Exchange exchange) {
         List<String> replyToAddresses = exchange.getIn().getHeader(SesConstants.REPLY_TO_ADDRESSES,
List.class);
@@ -100,6 +142,15 @@ public class SesProducer extends DefaultProducer {
         }
         return new Destination(to);
     }
+    
+    @SuppressWarnings("unchecked")
+    private List determineRawTo(Exchange exchange) {
+        List<String> to = exchange.getIn().getHeader(SesConstants.TO, List.class);
+        if (to == null) {
+            to = getConfiguration().getTo();
+        }
+        return to;
+    }
 
     private String determineFrom(Exchange exchange) {
         String from = exchange.getIn().getHeader(SesConstants.FROM, String.class);

http://git-wip-us.apache.org/repos/asf/camel/blob/52495d3c/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/AmazonSESClientMock.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/AmazonSESClientMock.java
b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/AmazonSESClientMock.java
index 3ed184a..21669ee 100644
--- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/AmazonSESClientMock.java
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/AmazonSESClientMock.java
@@ -22,9 +22,12 @@ import com.amazonaws.auth.BasicAWSCredentials;
 import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
 import com.amazonaws.services.simpleemail.model.SendEmailRequest;
 import com.amazonaws.services.simpleemail.model.SendEmailResult;
+import com.amazonaws.services.simpleemail.model.SendRawEmailRequest;
+import com.amazonaws.services.simpleemail.model.SendRawEmailResult;
 
 public class AmazonSESClientMock extends AmazonSimpleEmailServiceClient {
     private SendEmailRequest sendEmailRequest;
+    private SendRawEmailRequest sendRawEmailRequest;
 
     public AmazonSESClientMock() {
         super(new BasicAWSCredentials("myAccessKey", "mySecretKey"));
@@ -38,8 +41,21 @@ public class AmazonSESClientMock extends AmazonSimpleEmailServiceClient
{
         
         return result;
     }
+    
+    @Override
+    public SendRawEmailResult sendRawEmail(SendRawEmailRequest sendRawEmailRequest) throws
AmazonServiceException, AmazonClientException {
+        this.sendRawEmailRequest = sendRawEmailRequest;
+        SendRawEmailResult result = new SendRawEmailResult();
+        result.setMessageId("1");
+        
+        return result;
+    }
 
     public SendEmailRequest getSendEmailRequest() {
         return sendEmailRequest;
     }
+    
+    public SendRawEmailRequest getSendRawEmailRequest() {
+        return sendRawEmailRequest;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/52495d3c/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/MockMessage.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/MockMessage.java
b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/MockMessage.java
new file mode 100644
index 0000000..5e4e0db
--- /dev/null
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/MockMessage.java
@@ -0,0 +1,235 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws.ses;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Date;
+import java.util.Enumeration;
+
+import javax.activation.DataHandler;
+import javax.mail.Address;
+import javax.mail.Flags;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Multipart;
+
+public class MockMessage extends Message {
+
+	@Override
+	public int getSize() throws MessagingException {
+		return 0;
+	}
+
+	@Override
+	public int getLineCount() throws MessagingException {
+		return 0;
+	}
+
+	@Override
+	public String getContentType() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public boolean isMimeType(String mimeType) throws MessagingException {
+		return false;
+	}
+
+	@Override
+	public String getDisposition() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public void setDisposition(String disposition) throws MessagingException {		
+	}
+
+	@Override
+	public String getDescription() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public void setDescription(String description) throws MessagingException {
+	}
+
+	@Override
+	public String getFileName() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public void setFileName(String filename) throws MessagingException {
+	}
+
+	@Override
+	public InputStream getInputStream() throws IOException, MessagingException {
+		return null;
+	}
+
+	@Override
+	public DataHandler getDataHandler() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public Object getContent() throws IOException, MessagingException {
+		return null;
+	}
+
+	@Override
+	public void setDataHandler(DataHandler dh) throws MessagingException {
+	}
+
+	@Override
+	public void setContent(Object obj, String type) throws MessagingException {
+	}
+
+	@Override
+	public void setText(String text) throws MessagingException {
+	}
+
+	@Override
+	public void setContent(Multipart mp) throws MessagingException {	
+	}
+
+	@Override
+	public void writeTo(OutputStream os) throws IOException, MessagingException {		
+	}
+
+	@Override
+	public String[] getHeader(String header_name) throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public void setHeader(String header_name, String header_value)
+			throws MessagingException {
+	}
+
+	@Override
+	public void addHeader(String header_name, String header_value)
+			throws MessagingException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void removeHeader(String header_name) throws MessagingException {
+	}
+
+	@Override
+	public Enumeration getAllHeaders() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public Enumeration getMatchingHeaders(String[] header_names)
+			throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public Enumeration getNonMatchingHeaders(String[] header_names)
+			throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public Address[] getFrom() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public void setFrom() throws MessagingException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void setFrom(Address address) throws MessagingException {	
+	}
+
+	@Override
+	public void addFrom(Address[] addresses) throws MessagingException {		
+	}
+
+	@Override
+	public Address[] getRecipients(RecipientType type)
+			throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public void setRecipients(RecipientType type, Address[] addresses)
+			throws MessagingException {
+	}
+
+	@Override
+	public void addRecipients(RecipientType type, Address[] addresses)
+			throws MessagingException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public String getSubject() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public void setSubject(String subject) throws MessagingException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public Date getSentDate() throws MessagingException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public void setSentDate(Date date) throws MessagingException {		
+	}
+
+	@Override
+	public Date getReceivedDate() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public Flags getFlags() throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public void setFlags(Flags flag, boolean set) throws MessagingException {		
+	}
+
+	@Override
+	public Message reply(boolean replyToAll) throws MessagingException {
+		return null;
+	}
+
+	@Override
+	public void saveChanges() throws MessagingException {		
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/52495d3c/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/SesComponentSpringTest.java
----------------------------------------------------------------------
diff --git a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/SesComponentSpringTest.java
b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/SesComponentSpringTest.java
index 1fa7d12..4b736c6 100644
--- a/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/SesComponentSpringTest.java
+++ b/components/camel-aws/src/test/java/org/apache/camel/component/aws/ses/SesComponentSpringTest.java
@@ -18,8 +18,13 @@ package org.apache.camel.component.aws.ses;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.Properties;
+
+import javax.mail.Message;
 
 import com.amazonaws.services.simpleemail.model.SendEmailRequest;
+import com.amazonaws.services.simpleemail.model.SendRawEmailRequest;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.test.spring.CamelSpringTestSupport;
@@ -75,6 +80,24 @@ public class SesComponentSpringTest extends CamelSpringTestSupport {
         
         assertEquals("1", exchange.getOut().getHeader(SesConstants.MESSAGE_ID));
     }
+    
+    @Test
+    public void sendRawMessage() throws Exception {
+        final MockMessage mess = new MockMessage();
+    	  
+        Exchange exchange = template.request("direct:start", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody(mess);
+            }
+        });
+        
+        assertEquals("1", exchange.getOut().getHeader(SesConstants.MESSAGE_ID));
+        
+        SendRawEmailRequest sendRawEmailRequest = sesClient.getSendRawEmailRequest();
+        assertEquals("from@example.com", sendRawEmailRequest.getSource());
+        assertEquals(2, getTo(sendRawEmailRequest).size());
+    }
 
     @Test
     public void sendMessageUsingMessageHeaders() throws Exception {
@@ -123,4 +146,8 @@ public class SesComponentSpringTest extends CamelSpringTestSupport {
     private List<String> getTo(SendEmailRequest sendEmailRequest) {
         return sendEmailRequest.getDestination().getToAddresses();
     }
+    
+    private List<String> getTo(SendRawEmailRequest sendEmailRequest) {
+        return sendEmailRequest.getDestinations();
+    }
 }
\ No newline at end of file


Mime
View raw message