Author: rdonkin
Date: Thu Aug  9 14:47:01 2007
New Revision: 564395

URL: http://svn.apache.org/viewvc?view=rev&rev=564395
Log:
Partial implementation of http://tools.ietf.org/html/draft-ietf-sieve-body-00. 
https://issues.apache.org/jira/browse/JSIEVE-15. Contributed by Steve Smith.

Added:
    james/jsieve/trunk/src/main/java/org/apache/jsieve/tests/optional/Body.java
    james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/BodyTest.java
Modified:
    james/jsieve/trunk/src/main/java/org/apache/jsieve/mail/MailAdapter.java
    
james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
    james/jsieve/trunk/src/site/xdoc/index.xml
    
james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/utils/SieveMailAdapter.java
    james/jsieve/trunk/src/test/resources/sieveConfig.xml

Modified: 
james/jsieve/trunk/src/main/java/org/apache/jsieve/mail/MailAdapter.java
URL: 
http://svn.apache.org/viewvc/james/jsieve/trunk/src/main/java/org/apache/jsieve/mail/MailAdapter.java?view=diff&rev=564395&r1=564394&r2=564395
==============================================================================
--- james/jsieve/trunk/src/main/java/org/apache/jsieve/mail/MailAdapter.java 
(original)
+++ james/jsieve/trunk/src/main/java/org/apache/jsieve/mail/MailAdapter.java 
Thu Aug  9 14:47:01 2007
@@ -112,4 +112,20 @@
      */
     int getSize() throws SieveMailException;
 
+
+    /**
+     * Method getContentType returns string/mime representation of the
+     * message type.
+     * @return String
+     * @throws SieveMailException
+     */
+    public String getContentType() throws SieveMailException;
+
+    /**
+     * Method getContent returns object containing the message content.
+     * @return Object
+     * @throws SieveMailException
+     */
+    public Object getContent() throws SieveMailException;
+
 }

Added: 
james/jsieve/trunk/src/main/java/org/apache/jsieve/tests/optional/Body.java
URL: 
http://svn.apache.org/viewvc/james/jsieve/trunk/src/main/java/org/apache/jsieve/tests/optional/Body.java?view=auto&rev=564395
==============================================================================
--- james/jsieve/trunk/src/main/java/org/apache/jsieve/tests/optional/Body.java 
(added)
+++ james/jsieve/trunk/src/main/java/org/apache/jsieve/tests/optional/Body.java 
Thu Aug  9 14:47:01 2007
@@ -0,0 +1,113 @@
+/****************************************************************
+ * 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.jsieve.tests.optional;
+
+import java.lang.String;
+import java.lang.ClassCastException;
+import java.util.List;
+import java.util.ListIterator;
+
+import javax.mail.Message;
+import javax.mail.MessagingException;
+
+import org.apache.jsieve.SieveException;
+import org.apache.jsieve.SyntaxException;
+import org.apache.jsieve.SieveContext;
+import org.apache.jsieve.Arguments;
+import org.apache.jsieve.TagArgument;
+import org.apache.jsieve.StringListArgument;
+import org.apache.jsieve.tests.AbstractTest;
+import org.apache.jsieve.mail.MailAdapter;
+import org.apache.jsieve.mail.SieveMailException;
+
+/**
+ * This implements a single subset of the 'body' Sieve test as define here:
+ *     http://tools.ietf.org/html/draft-ietf-sieve-body-00
+ **/
+public class Body extends AbstractTest
+{
+    private StringListArgument strings;
+
+    public Body()
+    {
+        super();
+        strings = null;
+    }
+
+    // Validate (sorta); we're only implementing part of the spec
+    protected void validateArguments(Arguments args, SieveContext ctx)
+        throws SieveException
+    {
+
+        List arglist = args.getArgumentList();
+        if (arglist.size() != 2) {
+            throw new SyntaxException("Currently body-test can only two 
arguments");
+        }
+
+        // FIXME: As this is a limited implementation force the use of
+        // ':contains'.
+        Object arg = arglist.get(0);
+        if (! (arg instanceof TagArgument)) {
+            throw new SyntaxException("Body expects a :contains tag");
+        }
+
+        if (! ((TagArgument) arg).getTag().equals(":contains")) {
+            throw new SyntaxException("Body expects a :contains tag");
+        }
+
+        // Get list of strings to search for
+        arg = arglist.get(1);
+        if (! (arg instanceof StringListArgument)) {
+            throw new SyntaxException("Body expects a list of strings");
+        }
+        strings = (StringListArgument) args.getArgumentList().get(1);
+    }
+
+
+    // This implement body tests of the form
+    //   "body :contains ['string' 'string' ....]"
+    protected boolean executeBasic(MailAdapter mail,
+                                   Arguments args,
+                                   SieveContext ctx)
+        throws SieveException
+    {
+        // Attempt to fetch content as a string. If we can't do this it's
+        // not a message we can handle.
+        if (mail.getContentType().indexOf("text/") != 0) {
+            throw new SieveMailException("Message is not of type 'text'");
+        }
+        String body = (String) mail.getContent();
+        body = body.toLowerCase();
+
+
+        // Compare each test string with body, ignoring case
+        ListIterator iter = strings.getList().listIterator();
+        while (iter.hasNext()) {
+            String str = (String) iter.next();
+            if (body.indexOf(str.toLowerCase()) != -1) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+
+}

Modified: 
james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
URL: 
http://svn.apache.org/viewvc/james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java?view=diff&rev=564395&r1=564394&r2=564395
==============================================================================
--- 
james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
 (original)
+++ 
james/jsieve/trunk/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
 Thu Aug  9 14:47:01 2007
@@ -19,6 +19,8 @@
 
 package org.apache.jsieve.util.check;
 
+import java.io.IOException;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -222,6 +224,46 @@
             try {
                 result = mail.getSize();
             } catch (MessagingException e) {
+                throw new SieveMailException(e);
+            }
+        }
+        return result;
+    }
+
+
+    /**
+     * Method getContentType returns string/mime representation of the
+     * message type.
+     * @return String
+     * @throws SieveMailException
+     */
+    public String getContentType() throws SieveMailException {
+        String result = null;
+        if (mail != null)
+        {
+            try {
+                result = mail.getContentType();
+            } catch (MessagingException e) {
+                throw new SieveMailException(e);
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Method getContent returns object containing the message content.
+     * @return Object
+     * @throws SieveMailException
+     */
+    public Object getContent() throws SieveMailException {
+        Object result = null;
+        if (mail != null)
+        {
+            try {
+                result = mail.getContent();
+            } catch (MessagingException e) {
+                throw new SieveMailException(e);
+            } catch (IOException e) {
                 throw new SieveMailException(e);
             }
         }

Modified: james/jsieve/trunk/src/site/xdoc/index.xml
URL: 
http://svn.apache.org/viewvc/james/jsieve/trunk/src/site/xdoc/index.xml?view=diff&rev=564395&r1=564394&r2=564395
==============================================================================
--- james/jsieve/trunk/src/site/xdoc/index.xml (original)
+++ james/jsieve/trunk/src/site/xdoc/index.xml Thu Aug  9 14:47:01 2007
@@ -108,6 +108,8 @@
 <td>size</td><td>RFC 3028 (REQUIRED)</td><td>yes</td>
 </tr><tr>
 <td>true</td><td>RFC 3028 (REQUIRED)</td><td>yes</td>
+</tr><tr>
+<td>body</td><td><a 
href='http://tools.ietf.org/html/draft-ietf-sieve-body-00'>SIEVE body 
extension</a></td><td>partial</td>
 </tr>
 </table>
 </subsection>

Added: james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/BodyTest.java
URL: 
http://svn.apache.org/viewvc/james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/BodyTest.java?view=auto&rev=564395
==============================================================================
--- james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/BodyTest.java 
(added)
+++ james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/BodyTest.java Thu 
Aug  9 14:47:01 2007
@@ -0,0 +1,205 @@
+/****************************************************************
+ * 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.jsieve.junit;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMultipart;
+import junit.framework.TestCase;
+
+import org.apache.jsieve.CommandManager;
+import org.apache.jsieve.SieveException;
+import org.apache.jsieve.TestManager;
+import org.apache.jsieve.junit.commands.ThrowTestException;
+import org.apache.jsieve.junit.utils.*;
+import org.apache.jsieve.parser.generated.ParseException;
+
+
+/**
+ * Class BodyTest
+ */
+public class BodyTest extends TestCase
+{
+
+    /**
+     * Constructor for BodyTest.
+     * @param arg0
+     */
+    public BodyTest(String arg0)
+    {
+        super(arg0);
+    }
+
+    public static void main(String[] args)
+    {
+        junit.swingui.TestRunner.run(BodyTest.class);
+    }
+
+    /**
+     * @see TestCase#setUp()
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        CommandManager.resetInstance();
+        TestManager.resetInstance();         
+    }    
+
+    /**
+     * @see TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+    }
+
+
+    protected SieveMailAdapter textMail() 
+        throws MessagingException
+    {
+        SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
+        mail.getMessage().setContent("Wibble\n\n" +
+                                     "Wibble\n", "text/plain");
+        return mail;
+    }
+
+    protected SieveMailAdapter nonTextMail()
+        throws MessagingException, SieveException
+    {
+        SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
+        // FIXME: This doesn't work
+        mail.getMessage().setContent(new MimeMultipart("image/png"));
+        return mail;
+    }
+
+    
+    /**
+     * Test for Test 'header'
+     */
+    public void testBasic()
+    {
+        boolean isTestPassed = false;
+        String script = "if body :contains [\"Wibble\"] {throwTestException;}";
+        try
+        {
+            JUnitUtils.interpret(textMail(), script);
+        }
+        catch (MessagingException e)
+        {
+        }        
+        catch (ThrowTestException.TestException e)
+        {
+            isTestPassed = true;
+        }        
+        catch (ParseException e)
+        {
+        }
+        catch (SieveException e)
+        {
+        }
+        assertTrue(isTestPassed);
+    }
+
+    
+    /**
+     * Test for Test 'body'
+     */
+    public void testBodyCaseInsensitivity()
+    {
+        boolean isTestPassed = false;
+        String script = "if body :contains [\"wibble\"] {throwTestException;}";
+        try
+        {
+            JUnitUtils.interpret(textMail(), script);
+        }
+        catch (MessagingException e)
+        {
+        }        
+        catch (ThrowTestException.TestException e)
+        {
+            isTestPassed = true;
+        }        
+        catch (ParseException e)
+        {
+        }
+        catch (SieveException e)
+        {
+        }
+        assertTrue(isTestPassed);
+    }    
+
+    /**
+     * Test for Test 'body'
+     */
+    public void testBodyNoContains()
+    {
+        boolean isTestPassed = false;
+        String script = "if body [\"wibble\"] {throwTestException;}";
+        try
+        {
+            JUnitUtils.interpret(textMail(), script);
+        }
+        catch (MessagingException e)
+        {
+        }        
+        catch (ThrowTestException.TestException e)
+        {
+        }        
+        catch (ParseException e)
+        {
+        }
+        catch (SieveException e)
+        {
+            isTestPassed = true;
+        }
+        assertTrue(isTestPassed);
+    }    
+
+
+    /**
+     * Test for Test 'body'
+     */
+    // FIXME: I can't find a method of forcing the mime type, so this test
+    // always fails ...
+//     public void testBodyNonText()
+//     {
+//         boolean isTestPassed = false;
+//         String script = "if body :contains [\"wibble\"] 
{throwTestException;}";
+//         try
+//         {
+//             JUnitUtils.interpret(nonTextMail(), script);
+//         }
+//         catch (MessagingException e)
+//         {
+//         }        
+//         catch (ThrowTestException.TestException e)
+//         {
+//         }        
+//         catch (ParseException e)
+//         {
+//         }
+//         catch (SieveException e)
+//         {
+//             isTestPassed = true;
+//         }
+//         assertTrue(isTestPassed);
+//     }    
+
+}

Modified: 
james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/utils/SieveMailAdapter.java
URL: 
http://svn.apache.org/viewvc/james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/utils/SieveMailAdapter.java?view=diff&rev=564395&r1=564394&r2=564395
==============================================================================
--- 
james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/utils/SieveMailAdapter.java
 (original)
+++ 
james/jsieve/trunk/src/test/java/org/apache/jsieve/junit/utils/SieveMailAdapter.java
 Thu Aug  9 14:47:01 2007
@@ -20,6 +20,8 @@
  
 package org.apache.jsieve.junit.utils;
 
+import java.io.IOException;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Enumeration;
@@ -242,5 +244,41 @@
             throw new SieveMailException(ex);
         }
     }
+
+    /**
+     * @see org.apache.jsieve.mail.MailAdapter#getContentType()
+     */
+    public String getContentType() throws SieveMailException
+    {
+        try
+        {
+            return getMessage().getContentType();
+        }
+        catch (MessagingException ex)
+        {
+            throw new SieveMailException(ex);
+        }
+    }
+
+    /**
+     * @see org.apache.jsieve.mail.MailAdapter#getContent()
+     */
+    public Object getContent() throws SieveMailException
+    {
+        try
+        {
+            return getMessage().getContent();
+        }
+        catch (MessagingException ex)
+        {
+            throw new SieveMailException(ex);
+        }
+        catch (IOException ex)
+        {
+            throw new SieveMailException(ex);
+        }
+    }
+
+
 
 }

Modified: james/jsieve/trunk/src/test/resources/sieveConfig.xml
URL: 
http://svn.apache.org/viewvc/james/jsieve/trunk/src/test/resources/sieveConfig.xml?view=diff&rev=564395&r1=564394&r2=564395
==============================================================================
--- james/jsieve/trunk/src/test/resources/sieveConfig.xml (original)
+++ james/jsieve/trunk/src/test/resources/sieveConfig.xml Thu Aug  9 14:47:01 
2007
@@ -107,6 +107,12 @@
             <name>envelope</name>
             <class>org.apache.jsieve.tests.optional.Envelope</class>
         </entry>            
+
+    <!-- Limited implementation-->
+        <entry>
+            <name>body</name>
+            <class>org.apache.jsieve.tests.optional.Body</class>
+        </entry>            
     </testMap>
 
     <!-- Declare supported comparator mappings -->    



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to