james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject svn commit: r1720163 - in /james/project/trunk: protocols/ protocols/managesieve/ protocols/managesieve/src/main/java/org/apache/james/managesieve/core/ protocols/managesieve/src/test/java/org/apache/james/managesieve/core/ protocols/managesieve/src/te...
Date Tue, 15 Dec 2015 13:57:37 GMT
Author: btellier
Date: Tue Dec 15 13:57:37 2015
New Revision: 1720163

URL: http://svn.apache.org/viewvc?rev=1720163&view=rev
Log:
JAMES-1615 SieveRepository should return an InputStream - contributed by Erwan Guyomarc'h

Modified:
    james/project/trunk/protocols/managesieve/pom.xml
    james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/core/CoreProcessor.java
    james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/core/CoreProcessorTestCase.java
    james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveRepository.java
    james/project/trunk/protocols/pom.xml
    james/project/trunk/server/data/data-api/src/main/java/org/apache/james/sieverepository/api/SieveRepository.java
    james/project/trunk/server/data/data-file/src/main/java/org/apache/james/sieverepository/file/SieveFileRepository.java
    james/project/trunk/server/data/data-library/src/test/java/org/apache/james/sieverepository/lib/AbstractSieveRepositoryTest.java
    james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java

Modified: james/project/trunk/protocols/managesieve/pom.xml
URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/pom.xml?rev=1720163&r1=1720162&r2=1720163&view=diff
==============================================================================
--- james/project/trunk/protocols/managesieve/pom.xml (original)
+++ james/project/trunk/protocols/managesieve/pom.xml Tue Dec 15 13:57:37 2015
@@ -44,7 +44,10 @@
             <groupId>org.apache.james</groupId>
             <artifactId>james-server-data-api</artifactId>
         </dependency>
-
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-io</artifactId>
+        </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>

Modified: james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/core/CoreProcessor.java
URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/core/CoreProcessor.java?rev=1720163&r1=1720162&r2=1720163&view=diff
==============================================================================
--- james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/core/CoreProcessor.java
(original)
+++ james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/core/CoreProcessor.java
Tue Dec 15 13:57:37 2015
@@ -20,6 +20,7 @@
 
 package org.apache.james.managesieve.core;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.managesieve.api.AuthenticationRequiredException;
 import org.apache.james.managesieve.api.ManageSieveRuntimeException;
 import org.apache.james.managesieve.api.Session;
@@ -37,6 +38,7 @@ import org.apache.james.sieverepository.
 import org.apache.james.sieverepository.api.exception.StorageException;
 import org.apache.james.sieverepository.api.exception.UserNotFoundException;
 
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -136,12 +138,15 @@ public class CoreProcessor implements Co
     public String getScript(String name) throws AuthenticationRequiredException,
         ScriptNotFoundException, StorageException {
         authenticationCheck();
-        String script = null;
+        String script;
         try {
-            script = _repository.getScript(getUser(), name);
+            script = IOUtils.toString(_repository.getScript(getUser(), name), "UTF-8");
         } catch (UserNotFoundException ex) {
             // Should not happen as the UserListener should ensure the session user is defined
in the repository
             throw new ManageSieveRuntimeException(ex);
+        } catch (IOException ex) {
+            // Unable to read script InputStream
+            throw new ManageSieveRuntimeException(ex);
         }
         return script;
     }
@@ -165,7 +170,7 @@ public class CoreProcessor implements Co
      */
     public List<ScriptSummary> listScripts() throws AuthenticationRequiredException
{
         authenticationCheck();
-        List<ScriptSummary> summaries = null;
+        List<ScriptSummary> summaries;
         try {
             summaries = _repository.listScripts(getUser());
         } catch (SieveRepositoryException ex) {
@@ -260,13 +265,16 @@ public class CoreProcessor implements Co
     public String getActive() throws AuthenticationRequiredException, ScriptNotFoundException,
StorageException {
         authenticationCheck();
         
-        String script = null;
+        String script;
         try {
-            script = _repository.getActive(getUser());
+            script = IOUtils.toString(_repository.getActive(getUser()), "UTF-8");
         } catch (UserNotFoundException ex) {
             // Should not happen as the UserListener should ensure the session
             // user is defined in the repository
             throw new ManageSieveRuntimeException(ex);
+        } catch (IOException ex) {
+            // Unable to read script InputStream
+            throw new ManageSieveRuntimeException(ex);
         }
         return script;
     }

Modified: james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/core/CoreProcessorTestCase.java
URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/core/CoreProcessorTestCase.java?rev=1720163&r1=1720162&r2=1720163&view=diff
==============================================================================
--- james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/core/CoreProcessorTestCase.java
(original)
+++ james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/core/CoreProcessorTestCase.java
Tue Dec 15 13:57:37 2015
@@ -20,6 +20,7 @@
 
 package org.apache.james.managesieve.core;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.managesieve.api.AuthenticationRequiredException;
 import org.apache.james.managesieve.api.SyntaxException;
 import org.apache.james.managesieve.api.commands.Capability.Capabilities;
@@ -265,7 +266,7 @@ public class CoreProcessorTestCase {
         session.setAuthentication(true);
         session.setUser("test");
         core.putScript("script", "content");
-        assertEquals("content", repository.getScript("test", "script"));
+        assertEquals("content", IOUtils.toString(repository.getScript("test", "script")));
 
         // Syntax
         success = false;
@@ -297,7 +298,7 @@ public class CoreProcessorTestCase {
         session.setUser("test");
         repository.putScript("test", "oldName", "content");
         core.renameScript("oldName", "newName");
-        assertEquals("content", repository.getScript("test", "oldName"));
+        assertEquals("content", IOUtils.toString(repository.getScript("test", "oldName")));
     }
 
     @Test
@@ -318,7 +319,7 @@ public class CoreProcessorTestCase {
         session.setUser("test");
         repository.putScript("test", "script", "content");
         core.setActive("script");
-        assertEquals("content", repository.getActive("test"));
+        assertEquals("content", IOUtils.toString(repository.getActive("test")));
     }
 
     @Test

Modified: james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveRepository.java?rev=1720163&r1=1720162&r2=1720163&view=diff
==============================================================================
--- james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveRepository.java
(original)
+++ james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveRepository.java
Tue Dec 15 13:57:37 2015
@@ -20,6 +20,7 @@
 
 package org.apache.james.managesieve.mock;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.sieverepository.api.ScriptSummary;
 import org.apache.james.sieverepository.api.SieveRepository;
 import org.apache.james.sieverepository.api.exception.DuplicateException;
@@ -31,6 +32,8 @@ import org.apache.james.sieverepository.
 import org.apache.james.sieverepository.api.exception.StorageException;
 import org.apache.james.sieverepository.api.exception.UserNotFoundException;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -156,7 +159,7 @@ public class MockSieveRepository impleme
     /**
      * @see SieveRepository#getActive(String)
      */
-    public String getActive(String user) throws UserNotFoundException, ScriptNotFoundException
{
+    public InputStream getActive(String user) throws UserNotFoundException, ScriptNotFoundException,
StorageException {
         if (!_repository.containsKey(user))
         {
             throw new UserNotFoundException(user);
@@ -175,7 +178,12 @@ public class MockSieveRepository impleme
         {
             throw new ScriptNotFoundException();
         }
-        return content;
+        try {
+            return IOUtils.toInputStream(content, "UTF-8");
+        } catch (IOException e) {
+            throw new StorageException();
+        }
+
     }
 
     /**
@@ -197,8 +205,8 @@ public class MockSieveRepository impleme
     /**
      * @see SieveRepository#getScript(String, String)
      */
-    public String getScript(String user, String name) throws UserNotFoundException,
-            ScriptNotFoundException {
+    public InputStream getScript(String user, String name) throws UserNotFoundException,
+            ScriptNotFoundException, StorageException {
         if (!_repository.containsKey(user))
         {
             throw new UserNotFoundException(user);
@@ -208,7 +216,11 @@ public class MockSieveRepository impleme
         {
             throw new ScriptNotFoundException(name);
         }
-        return script.getContent();
+        try {
+            return IOUtils.toInputStream(script.getContent(), "UTF-8");
+        } catch (IOException e) {
+            throw new StorageException();
+        }
     }
 
     /**
@@ -316,7 +328,7 @@ public class MockSieveRepository impleme
             ScriptNotFoundException, StorageException {
 
         // Turn off currently active script, if any
-        Entry<String, SieveScript> oldActive = null;
+        Entry<String, SieveScript> oldActive;
         oldActive = getActiveEntry(user);
         if (null != oldActive) {
             oldActive.getValue().setActive(false);

Modified: james/project/trunk/protocols/pom.xml
URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/pom.xml?rev=1720163&r1=1720162&r2=1720163&view=diff
==============================================================================
--- james/project/trunk/protocols/pom.xml (original)
+++ james/project/trunk/protocols/pom.xml Tue Dec 15 13:57:37 2015
@@ -55,6 +55,7 @@
         <commons-lang.version>2.6</commons-lang.version>
         <commons-codec.version>1.7</commons-codec.version>
         <commons-collections.version>3.2.1</commons-collections.version>
+        <apache-commons-io.version>1.3.2</apache-commons-io.version>
         <guava.version>13.0</guava.version>
         <jmock.version>2.5.1</jmock.version>
         <jutf7.version>1.0.0</jutf7.version>
@@ -145,6 +146,11 @@
                 <version>${commons-lang.version}</version>
             </dependency>
             <dependency>
+                <groupId>org.apache.commons</groupId>
+                <artifactId>commons-io</artifactId>
+                <version>${apache-commons-io.version}</version>
+            </dependency>
+            <dependency>
                 <groupId>com.google.guava</groupId>
                 <artifactId>guava</artifactId>
                 <version>${guava.version}</version>

Modified: james/project/trunk/server/data/data-api/src/main/java/org/apache/james/sieverepository/api/SieveRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-api/src/main/java/org/apache/james/sieverepository/api/SieveRepository.java?rev=1720163&r1=1720162&r2=1720163&view=diff
==============================================================================
--- james/project/trunk/server/data/data-api/src/main/java/org/apache/james/sieverepository/api/SieveRepository.java
(original)
+++ james/project/trunk/server/data/data-api/src/main/java/org/apache/james/sieverepository/api/SieveRepository.java
Tue Dec 15 13:57:37 2015
@@ -29,6 +29,7 @@ import org.apache.james.sieverepository.
 import org.apache.james.sieverepository.api.exception.StorageException;
 import org.apache.james.sieverepository.api.exception.UserNotFoundException;
 
+import java.io.InputStream;
 import java.util.List;
 
 
@@ -56,11 +57,11 @@ public interface SieveRepository {
     
     List<ScriptSummary> listScripts(String user) throws UserNotFoundException, StorageException;
     
-    String getActive(String user) throws UserNotFoundException, ScriptNotFoundException,
StorageException;
+    InputStream getActive(String user) throws UserNotFoundException, ScriptNotFoundException,
StorageException;
     
     void setActive(String user, String name) throws UserNotFoundException, ScriptNotFoundException,
StorageException;
     
-    String getScript(String user, String name) throws UserNotFoundException, ScriptNotFoundException,
StorageException;
+    InputStream getScript(String user, String name) throws UserNotFoundException, ScriptNotFoundException,
StorageException;
     
     void deleteScript(String user, String name) throws UserNotFoundException, ScriptNotFoundException,
IsActiveException, StorageException;
     

Modified: james/project/trunk/server/data/data-file/src/main/java/org/apache/james/sieverepository/file/SieveFileRepository.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-file/src/main/java/org/apache/james/sieverepository/file/SieveFileRepository.java?rev=1720163&r1=1720162&r2=1720163&view=diff
==============================================================================
--- james/project/trunk/server/data/data-file/src/main/java/org/apache/james/sieverepository/file/SieveFileRepository.java
(original)
+++ james/project/trunk/server/data/data-file/src/main/java/org/apache/james/sieverepository/file/SieveFileRepository.java
Tue Dec 15 13:57:37 2015
@@ -37,9 +37,11 @@ import org.apache.james.sieverepository.
 import javax.inject.Inject;
 import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.util.ArrayList;
@@ -171,11 +173,11 @@ public class SieveFileRepository impleme
     }
 
     @Override
-    public String getScript(final String user, final String name) throws UserNotFoundException,
+    public InputStream getScript(final String user, final String name) throws UserNotFoundException,
             ScriptNotFoundException, StorageException {
-        String script;
+        InputStream script;
         try {
-            script = toString(getScriptFile(user, name), UTF_8);
+            script = new FileInputStream(getScriptFile(user, name));
         } catch (FileNotFoundException ex) {
             throw new ScriptNotFoundException(ex);
         }
@@ -282,11 +284,11 @@ public class SieveFileRepository impleme
     }
 
     @Override
-    public String getActive(final String user) throws UserNotFoundException,
+    public InputStream getActive(final String user) throws UserNotFoundException,
             ScriptNotFoundException, StorageException {
-        String script;
+        InputStream script;
         try {
-            script = toString(getActiveFile(user), UTF_8);
+            script = new FileInputStream(getActiveFile(user));
         } catch (FileNotFoundException ex) {
             throw new ScriptNotFoundException(ex);
         }

Modified: james/project/trunk/server/data/data-library/src/test/java/org/apache/james/sieverepository/lib/AbstractSieveRepositoryTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/test/java/org/apache/james/sieverepository/lib/AbstractSieveRepositoryTest.java?rev=1720163&r1=1720162&r2=1720163&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/test/java/org/apache/james/sieverepository/lib/AbstractSieveRepositoryTest.java
(original)
+++ james/project/trunk/server/data/data-library/src/test/java/org/apache/james/sieverepository/lib/AbstractSieveRepositoryTest.java
Tue Dec 15 13:57:37 2015
@@ -20,6 +20,7 @@ package org.apache.james.sieverepository
 
 import static org.assertj.core.api.Assertions.assertThat;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.sieverepository.api.ScriptSummary;
 import org.apache.james.sieverepository.api.SieveRepository;
 import org.apache.james.sieverepository.api.exception.DuplicateException;
@@ -32,15 +33,19 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import java.io.IOException;
+import java.io.InputStream;
+
 public abstract class AbstractSieveRepositoryTest {
 
     private static final String USER = "test";
     private static final String SCRIPT_NAME = "script";
-    private static final String SCRIPT_CONTENT = "01234567";
+    private static final String SCRIPT_CONTENT = "\u0048\u0065\u006C\u006C\u006F World";
// test utf-8
     private static final String OTHER_SCRIPT_NAME = "other_script";
-    private static final String OTHER_SCRIPT_CONTENT = "abcdef";
+    private static final String OTHER_SCRIPT_CONTENT = "Other script content";
     private static final long DEFAULT_QUOTA = Long.MAX_VALUE - 1L;
     private static final long USER_QUOTA = Long.MAX_VALUE / 2;
+    private static final String UTF8_ENCODING = "UTF-8";
 
     private SieveRepository sieveRepository;
 
@@ -69,7 +74,7 @@ public abstract class AbstractSieveRepos
     public void getScriptShouldReturnCorrectContent() throws Exception {
         sieveRepository.addUser(USER);
         sieveRepository.putScript(USER, SCRIPT_NAME, SCRIPT_CONTENT);
-        assertThat(sieveRepository.getScript(USER, SCRIPT_NAME)).isEqualTo(SCRIPT_CONTENT);
+        assertThat(getScriptContent(sieveRepository.getScript(USER, SCRIPT_NAME))).isEqualTo(SCRIPT_CONTENT);
     }
 
     @Test(expected = UserNotFoundException.class)
@@ -225,7 +230,7 @@ public abstract class AbstractSieveRepos
         sieveRepository.addUser(USER);
         sieveRepository.putScript(USER, SCRIPT_NAME, SCRIPT_CONTENT);
         sieveRepository.setActive(USER, SCRIPT_NAME);
-        assertThat(sieveRepository.getActive(USER)).isEqualTo(SCRIPT_CONTENT);
+        assertThat(getScriptContent(sieveRepository.getActive(USER))).isEqualTo(SCRIPT_CONTENT);
     }
 
     @Test
@@ -235,7 +240,7 @@ public abstract class AbstractSieveRepos
         sieveRepository.setActive(USER, SCRIPT_NAME);
         sieveRepository.putScript(USER, OTHER_SCRIPT_NAME, OTHER_SCRIPT_CONTENT);
         sieveRepository.setActive(USER, OTHER_SCRIPT_NAME);
-        assertThat(sieveRepository.getActive(USER)).isEqualTo(OTHER_SCRIPT_CONTENT);
+        assertThat(getScriptContent(sieveRepository.getActive(USER))).isEqualTo(OTHER_SCRIPT_CONTENT);
     }
 
     @Test(expected = ScriptNotFoundException.class)
@@ -305,7 +310,7 @@ public abstract class AbstractSieveRepos
         sieveRepository.addUser(USER);
         sieveRepository.putScript(USER, SCRIPT_NAME, SCRIPT_CONTENT);
         sieveRepository.renameScript(USER, SCRIPT_NAME, OTHER_SCRIPT_NAME);
-        assertThat(sieveRepository.getScript(USER, OTHER_SCRIPT_NAME)).isEqualTo(SCRIPT_CONTENT);
+        assertThat(getScriptContent(sieveRepository.getScript(USER, OTHER_SCRIPT_NAME))).isEqualTo(SCRIPT_CONTENT);
     }
 
     @Test
@@ -314,7 +319,7 @@ public abstract class AbstractSieveRepos
         sieveRepository.putScript(USER, SCRIPT_NAME, SCRIPT_CONTENT);
         sieveRepository.setActive(USER, SCRIPT_NAME);
         sieveRepository.renameScript(USER, SCRIPT_NAME, OTHER_SCRIPT_NAME);
-        assertThat(sieveRepository.getActive(USER)).isEqualTo(SCRIPT_CONTENT);
+        assertThat(getScriptContent(sieveRepository.getActive(USER))).isEqualTo(SCRIPT_CONTENT);
     }
 
     @Test(expected = DuplicateException.class)
@@ -424,6 +429,10 @@ public abstract class AbstractSieveRepos
         assertThat(sieveRepository.getQuota(USER)).isEqualTo(DEFAULT_QUOTA);
     }
 
+    protected String getScriptContent(InputStream inputStream) throws IOException {
+        return IOUtils.toString(inputStream, UTF8_ENCODING);
+    }
+
     protected abstract SieveRepository createSieveRepository() throws Exception;
 
     protected abstract void cleanUp() throws Exception;

Modified: james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java?rev=1720163&r1=1720162&r2=1720163&view=diff
==============================================================================
--- james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
(original)
+++ james/project/trunk/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/managesieve/ManageSieveMailetTestCase.java
Tue Dec 15 13:57:37 2015
@@ -20,6 +20,7 @@
 
 package org.apache.james.transport.mailets.managesieve;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.managesieve.mock.MockSieveParser;
 import org.apache.james.managesieve.mock.MockSieveRepository;
 import org.apache.james.sieverepository.api.exception.DuplicateUserException;
@@ -213,7 +214,7 @@ public class ManageSieveMailetTestCase {
             BodyPart part = content.getBodyPart(0);
             String response = (String) part.getContent();
             assertEquals(response, "OK (WARNINGS) \"warning1\" \"warning2\"");
-            assertEquals(scriptContent, _repository.getScript(message.getSender().toString(),
scriptName));
+            assertEquals(scriptContent, IOUtils.toString(_repository.getScript(message.getSender().toString(),
scriptName)));
         }
 
         // Extra arguments
@@ -259,7 +260,7 @@ public class ManageSieveMailetTestCase {
             BodyPart part = content.getBodyPart(0);
             String response = (String) part.getContent();
             assertTrue(response.startsWith("NO \"Syntax Error: "));
-            assertEquals(scriptContent, _repository.getScript(message.getSender().toString(),
scriptName));
+            assertEquals(scriptContent, IOUtils.toString(_repository.getScript(message.getSender().toString(),
scriptName)));
         }
 
         // No script



---------------------------------------------------------------------
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