Title: [976] trunk/components/base/src/main/java/org/servicemix/components/email: Added MimeMailPoller and method to MimeMailMarshaller
- Revision
- 976
- Author
- gastaldi
- Date
- 2005-11-30 13:46:50 -0500 (Wed, 30 Nov 2005)
Log Message
Added MimeMailPoller and method to MimeMailMarshaller
to marshall a MimeMessage to a NormalizedMessage.
Need Test Case.
Modified Paths
Added Paths
Diff
Modified: trunk/components/base/src/main/java/org/servicemix/components/email/MimeMailMarshaler.java (975 => 976)
--- trunk/components/base/src/main/java/org/servicemix/components/email/MimeMailMarshaler.java 2005-11-30 17:02:27 UTC (rev 975)
+++ trunk/components/base/src/main/java/org/servicemix/components/email/MimeMailMarshaler.java 2005-11-30 18:46:50 UTC (rev 976)
@@ -17,6 +17,7 @@
**/
package org.servicemix.components.email;
+import java.io.IOException;
import java.util.Date;
import javax.jbi.messaging.MessageExchange;
@@ -25,9 +26,12 @@
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.xml.transform.TransformerException;
+import org.servicemix.jbi.jaxp.StringSource;
+
/**
* The default marshaller from the [EMAIL PROTECTED] NormalizedMessage} to a Mime email using
* expressions for each field required on the email.
@@ -38,6 +42,43 @@
/**
+ * Populates the MessageExchange with values extracted from the mail message using expressions.
+ *
+ * @param exchange the JBI message exchange
+ * @param normalizedMessage the normalized message from JBI
+ * @param mimeMessage the mime email
+ * @throws javax.mail.MessagingException if the message could not be constructed or there was an error creating an address
+ */
+ public void prepareExchange(MessageExchange exchange, NormalizedMessage normalizedMessage, MimeMessage mimeMessage) throws javax.mail.MessagingException {
+ String from = InternetAddress.toString(mimeMessage.getFrom());
+ String to = InternetAddress.toString(mimeMessage.getRecipients(Message.RecipientType.TO));
+ String cc = InternetAddress.toString(mimeMessage.getRecipients(Message.RecipientType.CC));
+ String replyTo = InternetAddress.toString(mimeMessage.getReplyTo());
+ String sentDate = getDateFormat().format(mimeMessage.getSentDate());
+ String text;
+ try {
+ //TODO: Add Message Attachments and allow multipart messages.
+ text = asString(mimeMessage.getContent());
+ } catch (IOException e) {
+ throw new javax.mail.MessagingException("Error while fetching content",e);
+ }
+
+ normalizedMessage.setProperty("org.servicemix.email.from",from);
+ normalizedMessage.setProperty("org.servicemix.email.to",to);
+ normalizedMessage.setProperty("org.servicemix.email.cc",cc);
+ normalizedMessage.setProperty("org.servicemix.email.text",text);
+ normalizedMessage.setProperty("org.servicemix.email.replyTo",replyTo);
+ normalizedMessage.setProperty("org.servicemix.email.sentDate",sentDate);
+
+ //TODO: Change this to something that makes more sense
+ try {
+ normalizedMessage.setContent(new StringSource(text));
+ } catch (MessagingException e) {
+ throw new javax.mail.MessagingException("Error while setting content on normalized message",e);
+ }
+ }
+
+ /**
* Populates the mime email message with values extracted from the message exchange using expressions.
*
* @param mimeMessage the mime email
Added: trunk/components/base/src/main/java/org/servicemix/components/email/MimeMailPoller.java (975 => 976)
--- trunk/components/base/src/main/java/org/servicemix/components/email/MimeMailPoller.java 2005-11-30 17:02:27 UTC (rev 975)
+++ trunk/components/base/src/main/java/org/servicemix/components/email/MimeMailPoller.java 2005-11-30 18:46:50 UTC (rev 976)
@@ -0,0 +1,235 @@
+/**
+ *
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ *
+ * Licensed 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.servicemix.components.email;
+
+import javax.jbi.JBIException;
+import javax.jbi.messaging.DeliveryChannel;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.MessageExchangeFactory;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.mail.Flags;
+import javax.mail.Folder;
+import javax.mail.Session;
+import javax.mail.Store;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.servicemix.components.util.PollingComponentSupport;
+
+/**
+ * A polling component which looks for emails in a mail server and sends them
+ * into the JBI bus as messages, deleting the messages by default when they
+ * are processed.
+ *
+ * @version $Revision: 712 $
+ */
+public class MimeMailPoller extends PollingComponentSupport {
+
+ private static Log log = LogFactory.getLog(MimeMailPoller.class);
+
+ private Session session;
+ private String hostName;
+ private String userName;
+ private String password;
+ private String mailBox;
+ private boolean debug;
+ private int maxFetchSize = 5;
+ private MimeMailMarshaler marshaler = new MimeMailMarshaler();
+
+ protected void init() throws JBIException {
+ super.init();
+ if (session == null) {
+ log.debug("No Session informed. Using default instance");
+ this.session = Session.getDefaultInstance(System.getProperties());
+ }
+ if (mailBox == null) {
+ log.debug("No mailbox informed. Using INBOX");
+ mailBox = "INBOX";
+ }
+ if (hostName == null) {
+ throw new JBIException("HostName not informed");
+ }
+ if (userName == null) {
+ throw new JBIException("UserName not informed");
+ }
+ if (password == null) {
+ throw new JBIException("Password not informed");
+ }
+ if (maxFetchSize < 1) {
+ throw new JBIException("Fetch Size must be at least 1");
+ }
+
+ }
+
+ /**
+ * @return Returns the hostName.
+ */
+ public String getHostName() {
+ return hostName;
+ }
+
+ /**
+ * @param hostName The hostName to set.
+ */
+ public void setHostName(String hostName) {
+ this.hostName = hostName;
+ }
+
+ /**
+ * @return Returns the marshaler.
+ */
+ public MimeMailMarshaler getMarshaler() {
+ return marshaler;
+ }
+
+ /**
+ * @param marshaler The marshaler to set.
+ */
+ public void setMarshaler(MimeMailMarshaler marshaler) {
+ this.marshaler = marshaler;
+ }
+
+ public void poll() throws Exception {
+ Store store = null;
+ Folder folder = null;
+ try {
+ session.setDebug(isDebug());
+ store = session.getStore((mailBox.equals("INBOX")) ? "pop3"
+ : "imap");
+ store.connect(hostName, userName, password);
+ folder = store.getFolder(mailBox);
+ if (folder == null || !folder.exists()) {
+ throw new Exception("Folder not found or invalid: " + mailBox);
+ }
+ folder.open(Folder.READ_WRITE);
+ int msgCount = Math.min(folder.getMessageCount(),maxFetchSize);
+ DeliveryChannel channel = getDeliveryChannel();
+ MessageExchangeFactory mef = getExchangeFactory();
+ for(int i=1; i <= msgCount;i++) {
+ MimeMessage mailMsg = (MimeMessage) folder.getMessage(i);
+ InOnly io = mef.createInOnlyExchange();
+ NormalizedMessage normalizedMessage = io.createMessage();
+ this.marshaler.prepareExchange(io,normalizedMessage,mailMsg);
+ io.setInMessage(normalizedMessage);
+ channel.send(io);
+ mailMsg.setFlag(Flags.Flag.DELETED,true);
+ }
+ } finally {
+ try {
+ if (folder != null) {
+ folder.close(true);
+ }
+ if (store != null) {
+ store.close();
+ }
+ } catch (Exception ignored) {}
+ }
+ }
+
+ /**
+ * @return Returns the debug.
+ */
+ public boolean isDebug() {
+ return debug;
+ }
+
+ /**
+ * @param debug
+ * The debug to set.
+ */
+ public void setDebug(boolean debug) {
+ this.debug = debug;
+ }
+
+ /**
+ * @return Returns the mailBox.
+ */
+ public String getMailBox() {
+ return mailBox;
+ }
+
+ /**
+ * @param mailBox
+ * The mailBox to set.
+ */
+ public void setMailBox(String mailBox) {
+ this.mailBox = mailBox;
+ }
+
+ /**
+ * @return Returns the password.
+ */
+ public String getPassword() {
+ return password;
+ }
+
+ /**
+ * @param password
+ * The password to set.
+ */
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ /**
+ * @return Returns the userName.
+ */
+ public String getUserName() {
+ return userName;
+ }
+
+ /**
+ * @param userName
+ * The userName to set.
+ */
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+
+ /**
+ * @return Returns the session.
+ */
+ public Session getSession() {
+ return session;
+ }
+
+ /**
+ * @param session
+ * The session to set.
+ */
+ public void setSession(Session session) {
+ this.session = session;
+ }
+
+ /**
+ * @return Returns the maxFetchSize.
+ */
+ public int getMaxFetchSize() {
+ return maxFetchSize;
+ }
+
+ /**
+ * @param maxFetchSize The maxFetchSize to set.
+ */
+ public void setMaxFetchSize(int maxFetchSize) {
+ this.maxFetchSize = maxFetchSize;
+ }
+
+}