james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r411113 - in /james/server/trunk/src: java/org/apache/james/transport/mailets/AddSubjectPrefix.java test/org/apache/james/transport/mailets/AddSubjectPrefixTest.java
Date Fri, 02 Jun 2006 10:07:17 GMT
Author: norman
Date: Fri Jun  2 03:07:16 2006
New Revision: 411113

URL: http://svn.apache.org/viewvc?rev=411113&view=rev
Log:
Add new mailet to add a prefix to subject. See JAMES-518

Added:
    james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java
    james/server/trunk/src/test/org/apache/james/transport/mailets/AddSubjectPrefixTest.java

Added: james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java?rev=411113&view=auto
==============================================================================
--- james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java (added)
+++ james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java Fri
Jun  2 03:07:16 2006
@@ -0,0 +1,74 @@
+/***********************************************************************
+ * Copyright (c) 2006 The Apache Software Foundation.                  *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * 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.apache.james.transport.mailets;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.mailet.GenericMailet;
+import org.apache.mailet.Mail;
+
+/**
+ * Add an prefix (tag) to the subject of a message <br>
+ * <br>
+ * 
+ * Sample Configuration: <br>
+ * <br>
+ * &lt;mailet match="RecipientIs=robot@james.apache.org" class="TagMessage"&gt;
+ * &lt;subjectPrefix&gt;[robot]&lt;/subjectPrefix&gt; &lt;/mailet&gt;
<br>
+ * <br>
+ */
+public class AddSubjectPrefix extends GenericMailet {
+
+    // prefix to add
+    private String subjectPrefix = null;
+
+    /**
+     * Initialize the mailet.
+     */
+    public void init() throws MessagingException {
+        subjectPrefix = getInitParameter("subjectPrefix");
+
+        if (subjectPrefix == null || subjectPrefix.equals("")) {
+            throw new MessagingException(
+                    "Please configure a valid subjectPrefix");
+        }
+    }
+
+    /**
+     * Takes the message and adds a prefix to the subject
+     * 
+     * @param mail
+     *            the mail being processed
+     * 
+     * @throws MessagingException
+     *             if an error arises during message processing
+     */
+    public void service(Mail mail) throws MessagingException {
+        MimeMessage m = mail.getMessage();
+
+        String subject = m.getSubject();
+
+        if (subject != null) {
+            m.setSubject(subjectPrefix + " " + subject);
+        } else {
+            m.setSubject(subjectPrefix);
+        }
+    }
+
+}

Added: james/server/trunk/src/test/org/apache/james/transport/mailets/AddSubjectPrefixTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/transport/mailets/AddSubjectPrefixTest.java?rev=411113&view=auto
==============================================================================
--- james/server/trunk/src/test/org/apache/james/transport/mailets/AddSubjectPrefixTest.java
(added)
+++ james/server/trunk/src/test/org/apache/james/transport/mailets/AddSubjectPrefixTest.java
Fri Jun  2 03:07:16 2006
@@ -0,0 +1,143 @@
+/***********************************************************************
+ * Copyright (c) 2006 The Apache Software Foundation.                  *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * 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.apache.james.transport.mailets;
+
+import org.apache.james.test.mock.javaxmail.MockMimeMessage;
+import org.apache.james.test.mock.mailet.MockMail;
+import org.apache.james.test.mock.mailet.MockMailContext;
+import org.apache.james.test.mock.mailet.MockMailetConfig;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.Mailet;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.ParseException;
+import javax.mail.internet.MimeMessage.RecipientType;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+public class AddSubjectPrefixTest extends TestCase {
+
+    private MimeMessage mockedMimeMessage;
+
+    private Mail mockedMail;
+
+    private Mailet mailet;
+
+    private final String SUBJECT_PREFIX = "JUNIT";
+
+    private String subject = null;
+
+    public AddSubjectPrefixTest(String arg0)
+            throws UnsupportedEncodingException {
+        super(arg0);
+    }
+
+    private void setSubject(String subject) {
+        this.subject = subject;
+    }
+
+    private void setupMockedMimeMessage() throws MessagingException {
+        String sender = "test@james.apache.org";
+        String rcpt = "test2@james.apache.org";
+
+        mockedMimeMessage = new MockMimeMessage();
+        mockedMimeMessage.setFrom(new InternetAddress(sender));
+        mockedMimeMessage.setRecipients(RecipientType.TO, rcpt);
+        mockedMimeMessage.setSubject(subject);
+        mockedMimeMessage.setText("testtext");
+        mockedMimeMessage.saveChanges();
+
+    }
+
+    private void setupMockedMail(MimeMessage m) throws ParseException {
+        mockedMail = new MockMail();
+        mockedMail.setMessage(m);
+        mockedMail.setRecipients(Arrays.asList(new MailAddress[] {
+                new MailAddress("test@james.apache.org"),
+                new MailAddress("test2@james.apache.org") }));
+
+    }
+
+    private void setupMailet() throws MessagingException {
+        setupMockedMimeMessage();
+        mailet = new AddSubjectPrefix();
+        MockMailetConfig mci = new MockMailetConfig("Test",
+                new MockMailContext());
+        mci.setProperty("subjectPrefix", SUBJECT_PREFIX);
+
+        mailet.init(mci);
+    }
+
+    private void setupInvalidMailet() throws MessagingException {
+        setupMockedMimeMessage();
+        mailet = new AddSubjectPrefix();
+        MockMailetConfig mci = new MockMailetConfig("Test",
+                new MockMailContext());
+        mci.setProperty("subjectPrefix", "");
+
+        mailet.init(mci);
+    }
+
+    // test if prefix was added
+    public void testSubjectPrefixWasAdded() throws MessagingException {
+        setSubject("test");
+        setupMockedMimeMessage();
+        setupMockedMail(mockedMimeMessage);
+        setupMailet();
+
+        mailet.service(mockedMail);
+
+        assertEquals(SUBJECT_PREFIX + " " + subject, mockedMail.getMessage()
+                .getSubject());
+
+    }
+
+    // test if prefix was added to message without subject
+    public void testSubjectPrefixWasAddedWithoutSubject()
+            throws MessagingException {
+        setupMockedMimeMessage();
+        setupMockedMail(mockedMimeMessage);
+        setupMailet();
+
+        mailet.service(mockedMail);
+
+        assertEquals(SUBJECT_PREFIX, mockedMail.getMessage().getSubject());
+
+    }
+
+    // test if exception was thrown cause missing configure value
+    public void testThrowException() throws MessagingException {
+        boolean exceptionThrown = false;
+        setupMockedMimeMessage();
+        setupMockedMail(mockedMimeMessage);
+
+        try {
+            setupInvalidMailet();
+        } catch (MessagingException m) {
+            exceptionThrown = true;
+        }
+        assertTrue(exceptionThrown);
+
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message