Added: james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/LineToCoreToLine.java URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/LineToCoreToLine.java?rev=1716451&view=auto ============================================================================== --- james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/LineToCoreToLine.java (added) +++ james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/LineToCoreToLine.java Wed Nov 25 16:01:15 2015 @@ -0,0 +1,259 @@ +/* + * 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.james.managesieve.transcode; + +import org.apache.james.managesieve.api.ArgumentException; +import org.apache.james.managesieve.api.AuthenticationRequiredException; +import org.apache.james.managesieve.api.SyntaxException; +import org.apache.james.managesieve.api.commands.Capability.Capabilities; +import org.apache.james.sieverepository.api.ScriptSummary; +import org.apache.james.sieverepository.api.exception.DuplicateException; +import org.apache.james.sieverepository.api.exception.IsActiveException; +import org.apache.james.sieverepository.api.exception.QuotaExceededException; +import org.apache.james.sieverepository.api.exception.ScriptNotFoundException; +import org.apache.james.sieverepository.api.exception.StorageException; + +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; + +/** + * <code>LineToCoreToLine</code> + */ +public class LineToCoreToLine { + + private LineToCore _lineToCore = null; + + /** + * Creates a new instance of LineToCoreToLine. + * + */ + private LineToCoreToLine() { + super(); + } + + public LineToCoreToLine(LineToCore lineToCore) { + this(); + _lineToCore = lineToCore; + } + + public String capability(String args) { + Set<Entry<Capabilities, String>> entries = null; + try { + entries =_lineToCore.capability(args).entrySet(); + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } + + StringBuilder builder = new StringBuilder(); + for (Entry<Capabilities, String> entry : entries) + { + builder + .append(entry.getKey().toString()) + .append(' ') + .append(null == entry.getValue() ? "" : entry.getValue()) + .append("\r\n"); + } + builder.append("OK"); + return builder.toString(); + } + + public String checkScript(String args) { + List<String> warnings = null; + try { + warnings = _lineToCore.checkScript(args); + } catch (AuthenticationRequiredException ex) { + return "NO"; + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } catch (SyntaxException ex) { + return "NO \"Syntax Error: " + ex.getMessage() + "\""; + } + + StringBuilder builder = new StringBuilder(); + if (!warnings.isEmpty()) { + builder.append("OK (WARNINGS)"); + for (String warning : warnings) { + builder + .append(" \"") + .append(warning) + .append('"'); + } + } else { + builder.append("OK"); + } + return builder.toString(); + } + + public String deleteScript(String args) { + try { + _lineToCore.deleteScript(args); + } catch (AuthenticationRequiredException ex) { + return "NO"; + } catch (ScriptNotFoundException ex) { + return "NO (NONEXISTENT) \"There is no script by that name\""; + } catch (IsActiveException ex) { + return "NO (ACTIVE) \"You may not delete an active script\""; + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } + return "OK"; + } + + public String getScript(String args) { + String content = null; + try { + content = _lineToCore.getScript(args); + } catch (AuthenticationRequiredException ex) { + return "NO"; + } catch (ScriptNotFoundException ex) { + return "NO (NONEXISTENT) \"There is no script by that name\""; + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } catch (StorageException ex) { + return "NO \"" + ex.getMessage() + "\""; + } + StringBuilder builder = new StringBuilder(content); + builder + .append("\r\n") + .append("OK"); + return builder.toString(); + } + + public String haveSpace(String args) { + try { + _lineToCore.haveSpace(args); + } catch (AuthenticationRequiredException ex) { + return "NO"; + } catch (QuotaExceededException ex) { + return "NO (QUOTA/MAXSIZE) \"Quota exceeded\""; + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } + return "OK"; + } + + public String listScripts(String args) { + List<ScriptSummary> summaries = null; + try { + summaries = _lineToCore.listScripts(args); + } catch (AuthenticationRequiredException ex) { + return "NO"; + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } + StringBuilder builder = new StringBuilder(); + for (ScriptSummary summary : summaries) + { + builder + .append('"') + .append(summary.getName()) + .append('"'); + if (summary.isActive()) + { + builder + .append(' ') + .append("ACTIVE"); + } + builder + .append("\r\n"); + } + builder.append("OK"); + return builder.toString(); + } + + public String putScript(String args) { + List<String> warnings = null; + try { + warnings = _lineToCore.putScript(args); + } catch (AuthenticationRequiredException ex) { + return "NO"; + } catch (SyntaxException ex) { + return "NO \"Syntax Error: " + ex.getMessage() + "\""; + } catch (QuotaExceededException ex) { + return "NO (QUOTA/MAXSIZE) \"Quota exceeded\""; + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } + StringBuilder builder = new StringBuilder(); + if (!warnings.isEmpty()) { + builder.append("OK (WARNINGS)"); + for (String warning : warnings) { + builder + .append(" \"") + .append(warning) + .append('"'); + } + } else { + builder.append("OK"); + } + return builder.toString(); + } + + public String renameScript(String args) { + try { + _lineToCore.renameScript(args); + } catch (AuthenticationRequiredException ex) { + return "NO"; + } catch (ScriptNotFoundException ex) { + return "NO (NONEXISTENT) \"There is no script by that name\""; + } catch (DuplicateException ex) { + return "NO (ALREADYEXISTS) \"A script with that name already exists\""; + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } + return "OK"; + } + + public String setActive(String args) { + try { + _lineToCore.setActive(args); + } catch (AuthenticationRequiredException ex) { + return "NO"; + } catch (ScriptNotFoundException ex) { + return "NO (NONEXISTENT) \"There is no script by that name\""; + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } + return "OK"; + } + + public String getActive(String args) { + String content = null; + try { + content = _lineToCore.getActive(args); + } catch (AuthenticationRequiredException ex) { + return "NO"; + } catch (ScriptNotFoundException ex) { + return "NO (NONEXISTENT) \"" + ex.getMessage() + "\""; + } catch (ArgumentException ex) { + return "NO \"" + ex.getMessage() + "\""; + } catch (StorageException ex) { + return "NO \"" + ex.getMessage() + "\""; + } + StringBuilder builder = new StringBuilder(content); + builder + .append("\r\n") + .append("OK"); + return builder.toString(); + } + +}
Added: james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/ParserUtils.java URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/ParserUtils.java?rev=1716451&view=auto ============================================================================== --- james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/ParserUtils.java (added) +++ james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/ParserUtils.java Wed Nov 25 16:01:15 2015 @@ -0,0 +1,56 @@ +/* + * 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.james.managesieve.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * <code>ParserUtils</code> + */ +public class ParserUtils { + + private static final Pattern SCRIPT_NAME_REGEX = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'"); + + public static String getScriptName(String args) { + Matcher regexMatcher = SCRIPT_NAME_REGEX.matcher(args); + regexMatcher.find(); + String name = null; + try { + name = regexMatcher.group(); + } catch(IllegalStateException ex) + { + // no op + } + return name; + } + + public static String unquote(String quoted) { + String result = quoted; + if (quoted.startsWith("\"") && quoted.endsWith("\"")) { + result = quoted.substring(1, quoted.length() - 1); + } else if (quoted.startsWith("'") && quoted.endsWith("'")) { + result = quoted.substring(1, quoted.length() - 1); + } + return result; + } + +} Added: james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/SettableSession.java URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/SettableSession.java?rev=1716451&view=auto ============================================================================== --- james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/SettableSession.java (added) +++ james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/SettableSession.java Wed Nov 25 16:01:15 2015 @@ -0,0 +1,96 @@ +/* + * 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.james.managesieve.util; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.james.managesieve.api.Session; + +/** + * <code>SettableSession</code> + */ +public class SettableSession implements Session { + + String _user = null; + boolean _isAuthenticated = false; + List<UserListener> _userListeners = new ArrayList<UserListener>(); + List<AuthenticationListener> _authenticationListeners = new ArrayList<AuthenticationListener>(); + + /** + * @see org.apache.james.managesieve.api.Session#addAuthenticationListener(org.apache.james.managesieve.api.Session.AuthenticationListener) + */ + public void addAuthenticationListener(AuthenticationListener listener) { + _authenticationListeners.add(listener); + } + + /** + * @see org.apache.james.managesieve.api.Session#addUserListener(org.apache.james.managesieve.api.Session.UserListener) + */ + public void addUserListener(UserListener listener) { + _userListeners.add(listener); + } + + /** + * @see org.apache.james.managesieve.api.Session#getUser() + */ + public String getUser() { + return _user; + } + + /** + * @see org.apache.james.managesieve.api.Session#isAuthenticated() + */ + public boolean isAuthenticated() { + return _isAuthenticated; + } + + /** + * @see org.apache.james.managesieve.api.Session#removeAuthenticationListener(org.apache.james.managesieve.api.Session.AuthenticationListener) + */ + public void removeAuthenticationListener(AuthenticationListener listener) { + _authenticationListeners.remove(listener); + } + + /** + * @see org.apache.james.managesieve.api.Session#removeUserListener(org.apache.james.managesieve.api.Session.UserListener) + */ + public void removeUserListener(UserListener listener) { + _userListeners.remove(listener); + } + + public void setAuthentication(boolean isAuthenticated) { + _isAuthenticated = isAuthenticated; + for(AuthenticationListener listener : _authenticationListeners) + { + listener.notifyChange(isAuthenticated); + } + } + + public void setUser(String user) { + _user = user; + for(UserListener listener : _userListeners) + { + listener.notifyChange(user); + } + } + +} Added: 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=1716451&view=auto ============================================================================== --- james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/core/CoreProcessorTestCase.java (added) +++ james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/core/CoreProcessorTestCase.java Wed Nov 25 16:01:15 2015 @@ -0,0 +1,364 @@ +/* + * 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.james.managesieve.core; + +import org.apache.james.managesieve.api.AuthenticationRequiredException; +import org.apache.james.managesieve.api.SyntaxException; +import org.apache.james.managesieve.api.commands.Capability.Capabilities; +import org.apache.james.managesieve.mock.MockSession; +import org.apache.james.managesieve.mock.MockSieveParser; +import org.apache.james.managesieve.mock.MockSieveRepository; +import org.apache.james.sieverepository.api.exception.IsActiveException; +import org.apache.james.sieverepository.api.exception.QuotaExceededException; +import org.apache.james.sieverepository.api.exception.ScriptNotFoundException; +import org.apache.james.sieverepository.api.ScriptSummary; +import org.apache.james.sieverepository.api.exception.StorageException; +import org.apache.james.sieverepository.api.exception.UserNotFoundException; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * <code>CoreProcessorTestCase</code> + */ +public class CoreProcessorTestCase { + + private MockSession session; + private MockSieveParser parser; + private MockSieveRepository repository; + private CoreProcessor core; + + @Before + public void setUp() throws Exception { + session = new MockSession(); + parser = new MockSieveParser(); + repository = new MockSieveRepository(); + core = new CoreProcessor(session, repository, parser); + } + + @Test + public final void testCapability() { + // Unauthenticated + session.setAuthentication(false); + parser.setExtensions(Arrays.asList("a", "b", "c")); + Map<Capabilities, String> capabilities = core.capability(); + assertEquals(CoreProcessor.IMPLEMENTATION_DESCRIPTION, capabilities.get(Capabilities.IMPLEMENTATION)); + assertEquals(CoreProcessor.MANAGE_SIEVE_VERSION, capabilities.get(Capabilities.VERSION)); + assertEquals("a b c", capabilities.get(Capabilities.SIEVE)); + assertFalse(capabilities.containsKey(Capabilities.OWNER)); + assertTrue(capabilities.containsKey(Capabilities.GETACTIVE)); + + // Authenticated + session.setAuthentication(true); + parser.setExtensions(Arrays.asList("a", "b", "c")); + session.setUser("test"); + capabilities = core.capability(); + assertEquals(CoreProcessor.IMPLEMENTATION_DESCRIPTION, capabilities.get(Capabilities.IMPLEMENTATION)); + assertEquals(CoreProcessor.MANAGE_SIEVE_VERSION, capabilities.get(Capabilities.VERSION)); + assertEquals("a b c", capabilities.get(Capabilities.SIEVE)); + assertEquals("test", capabilities.get(Capabilities.OWNER)); + assertTrue(capabilities.containsKey(Capabilities.GETACTIVE)); + } + + @Test + public final void testCheckScript() throws AuthenticationRequiredException, SyntaxException { + // Unauthorised + boolean success = false; + session.setAuthentication(false); + try { + core.checkScript("warning"); + } catch (AuthenticationRequiredException ex) { + success = true; + } + assertTrue("Expected AuthenticationRequiredException", success); + + // Authorised + session.setAuthentication(true); + session.setUser("test"); + List<String> warnings = core.checkScript("warning"); + assertEquals(2, warnings.size()); + assertEquals("warning1", warnings.get(0)); + assertEquals("warning2", warnings.get(1)); + + // Syntax + success = false; + session.setAuthentication(true); + session.setUser("test"); + try { + core.checkScript("SyntaxException"); + } catch (SyntaxException ex) { + success = true; + } + assertTrue("Expected SyntaxException", success); + } + + @Test + public final void testDeleteScript() throws Exception { + // Unauthorised + boolean success = false; + session.setAuthentication(false); + try { + core.deleteScript("script"); + } catch (AuthenticationRequiredException ex) { + success = true; + } + assertTrue("Expected AuthenticationRequiredException", success); + + // Authorised - non-existent script + success = false; + session.setAuthentication(true); + session.setUser("test"); + try { + core.deleteScript("script"); + } catch (ScriptNotFoundException ex) { + success = true; + } + assertTrue("Expected ScriptNotFoundException", success); + + // Authorised - existent script + session.setAuthentication(true); + session.setUser("test"); + repository.putScript("test", "script", "content"); + core.deleteScript("script"); + success = false; + try { + repository.getScript("test", "script"); + } catch (ScriptNotFoundException ex) { + success = true; + } + assertTrue("Expected ScriptNotFoundException", success); + + // Authorised - active script + success = false; + session.setAuthentication(true); + session.setUser("test"); + repository.putScript("test", "script", "content"); + repository.setActive("test", "script"); + try { + core.deleteScript("script"); + } catch (IsActiveException ex) { + success = true; + } + assertTrue("Expected IsActiveException", success); + } + + @Test + public final void testGetScript() throws ScriptNotFoundException, AuthenticationRequiredException, UserNotFoundException, StorageException, QuotaExceededException { + // Unauthorised + boolean success = false; + session.setAuthentication(false); + try { + core.getScript("script"); + } catch (AuthenticationRequiredException ex) { + success = true; + } + assertTrue("Expected AuthenticationRequiredException", success); + + // Authorised - non-existent script + success = false; + session.setAuthentication(true); + session.setUser("test"); + try { + core.getScript("script"); + System.out.println("yop yop"); + } catch (ScriptNotFoundException ex) { + success = true; + ex.printStackTrace(); + } catch (Exception e) { + System.out.println("Euh ... "); + e.printStackTrace(); + System.out.println("Yolo"); + } + assertTrue("Expected ScriptNotFoundException", success); + + // Authorised - existent script + session.setAuthentication(true); + session.setUser("test"); + repository.putScript("test", "script", "content"); + core.getScript("script"); + } + + @Test + public final void testHaveSpace() throws Exception { + // Unauthorised + boolean success = false; + session.setAuthentication(false); + try { + core.haveSpace("script", Long.MAX_VALUE); + } catch (AuthenticationRequiredException ex) { + success = true; + } + assertTrue("Expected AuthenticationRequiredException", success); + + // Authorised - existent script + session.setAuthentication(true); + session.setUser("test"); + core.haveSpace("script", Long.MAX_VALUE); + } + + @Test + public final void testListScripts() throws Exception { + // Unauthorised + boolean success = false; + session.setAuthentication(false); + try { + core.listScripts(); + } catch (AuthenticationRequiredException ex) { + success = true; + } + assertTrue("Expected AuthenticationRequiredException", success); + + // Authorised - non-existent script + success = false; + session.setAuthentication(true); + session.setUser("test"); + List<ScriptSummary> summaries = core.listScripts(); + assertTrue(summaries.isEmpty()); + + // Authorised - existent script + session.setAuthentication(true); + session.setUser("test"); + repository.putScript("test", "script", "content"); + summaries = core.listScripts(); + assertEquals(1, summaries.size()); + } + + @Test + public final void testPutScript() throws Exception { + // Unauthorised + boolean success = false; + session.setAuthentication(false); + try { + core.putScript("script", "content"); + } catch (AuthenticationRequiredException ex) { + success = true; + } + assertTrue("Expected AuthenticationRequiredException", success); + + // Authorised + success = false; + session.setAuthentication(true); + session.setUser("test"); + core.putScript("script", "content"); + assertEquals("content", repository.getScript("test", "script")); + + // Syntax + success = false; + session.setAuthentication(true); + session.setUser("test"); + try { + core.putScript("script", "SyntaxException"); + } catch (SyntaxException ex) { + success = true; + } + assertTrue("Expected SyntaxException", success); + } + + @Test + public final void testRenameScript() throws Exception { + // Unauthorised + boolean success = false; + session.setAuthentication(false); + try { + core.renameScript("oldName", "newName"); + } catch (AuthenticationRequiredException ex) { + success = true; + } + assertTrue("Expected AuthenticationRequiredException", success); + + // Authorised + success = false; + session.setAuthentication(true); + session.setUser("test"); + repository.putScript("test", "oldName", "content"); + core.renameScript("oldName", "newName"); + assertEquals("content", repository.getScript("test", "oldName")); + } + + @Test + public final void testSetActive() throws Exception { + // Unauthorised + boolean success = false; + session.setAuthentication(false); + try { + core.setActive("script"); + } catch (AuthenticationRequiredException ex) { + success = true; + } + assertTrue("Expected AuthenticationRequiredException", success); + + // Authorised + success = false; + session.setAuthentication(true); + session.setUser("test"); + repository.putScript("test", "script", "content"); + core.setActive("script"); + assertEquals("content", repository.getActive("test")); + } + + @Test + public final void testGetActive() throws Exception { + // Unauthorised + boolean success = false; + session.setAuthentication(false); + try { + core.getActive(); + } catch (AuthenticationRequiredException ex) { + success = true; + } + assertTrue("Expected AuthenticationRequiredException", success); + + // Authorised - non-existent script + success = false; + session.setAuthentication(true); + session.setUser("test"); + try { + core.getActive(); + } catch (ScriptNotFoundException ex) { + success = true; + } + assertTrue("Expected ScriptNotFoundException", success); + + // Authorised - existent script, inactive + session.setAuthentication(true); + session.setUser("test"); + repository.putScript("test", "script", "content"); + try { + core.getActive(); + } catch (ScriptNotFoundException ex) { + success = true; + } + assertTrue("Expected ScriptNotFoundException", success); + + // Authorised - existent script, active + session.setAuthentication(true); + session.setUser("test"); + repository.setActive("test", "script"); + core.getActive(); + } +} Added: james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSession.java URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSession.java?rev=1716451&view=auto ============================================================================== --- james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSession.java (added) +++ james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSession.java Wed Nov 25 16:01:15 2015 @@ -0,0 +1,83 @@ +/* + * 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.james.managesieve.mock; + +import org.apache.james.managesieve.api.Session; + +import java.util.ArrayList; +import java.util.List; + +/** + * <code>MockSession</code> + */ +public class MockSession implements Session { + + String _user = null; + boolean _isAuthenticated = false; + List<UserListener> _userListeners = new ArrayList<UserListener>(); + List<AuthenticationListener> _authenticationListeners = new ArrayList<AuthenticationListener>(); + + public MockSession() + { + super(); + } + + public void addAuthenticationListener(AuthenticationListener listener) { + _authenticationListeners.add(listener); + } + + public void addUserListener(UserListener listener) { + _userListeners.add(listener); + } + + public String getUser() { + return _user; + } + + public boolean isAuthenticated() { + return _isAuthenticated; + } + + public void removeAuthenticationListener(AuthenticationListener listener) { + _authenticationListeners.remove(listener); + } + + public void removeUserListener(UserListener listener) { + _userListeners.remove(listener); + } + + public void setAuthentication(boolean isAuthenticated) { + _isAuthenticated = isAuthenticated; + for(AuthenticationListener listener : _authenticationListeners) + { + listener.notifyChange(isAuthenticated); + } + } + + public void setUser(String user) { + _user = user; + for(UserListener listener : _userListeners) + { + listener.notifyChange(user); + } + } + +} Added: james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveParser.java URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveParser.java?rev=1716451&view=auto ============================================================================== --- james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveParser.java (added) +++ james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveParser.java Wed Nov 25 16:01:15 2015 @@ -0,0 +1,56 @@ +package org.apache.james.managesieve.mock; + +import org.apache.james.managesieve.api.SieveParser; +import org.apache.james.managesieve.api.SyntaxException; + +import java.util.Arrays; +import java.util.List; + +/* + * 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. + * + */ + +/** + * <code>MockSieveParser</code> + */ +public class MockSieveParser implements SieveParser { + + private List<String> _extensions = null; + + public MockSieveParser() { + super(); + } + + public List<String> getExtensions() { + return _extensions; + } + + public void setExtensions(List<String> extensions) { + _extensions = extensions; + } + + public List<String> parse(String content) throws SyntaxException { + if (content.equals("SyntaxException")) + { + throw new SyntaxException("Ouch!"); + } + return Arrays.asList("warning1", "warning2"); + } + +} Added: 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=1716451&view=auto ============================================================================== --- james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveRepository.java (added) +++ james/project/trunk/protocols/managesieve/src/test/java/org/apache/james/managesieve/mock/MockSieveRepository.java Wed Nov 25 16:01:15 2015 @@ -0,0 +1,369 @@ +/* + * 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.james.managesieve.mock; + +import org.apache.james.sieverepository.api.ScriptSummary; +import org.apache.james.sieverepository.api.SieveRepository; +import org.apache.james.sieverepository.api.exception.DuplicateException; +import org.apache.james.sieverepository.api.exception.DuplicateUserException; +import org.apache.james.sieverepository.api.exception.IsActiveException; +import org.apache.james.sieverepository.api.exception.QuotaExceededException; +import org.apache.james.sieverepository.api.exception.QuotaNotFoundException; +import org.apache.james.sieverepository.api.exception.ScriptNotFoundException; +import org.apache.james.sieverepository.api.exception.StorageException; +import org.apache.james.sieverepository.api.exception.UserNotFoundException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + + +/** + * <code>MockSieveRepository</code> + */ +public class MockSieveRepository implements SieveRepository { + + public class SieveScript + { + private String _name = null; + private String _content = null; + private boolean _isActive = false; + + /** + * Creates a new instance of SieveScript. + * + */ + private SieveScript() { + super(); + } + + /** + * Creates a new instance of SieveScript. + * + */ + public SieveScript(String content, boolean isActive) { + this(); + setContent(content); + setActive(isActive); + } + + /** + * @return the name + */ + public String getName() { + return _name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + _name = name; + } + + /** + * @return the content + */ + public String getContent() { + return _content; + } + + /** + * @param content the content to set + */ + public void setContent(String content) { + _content = content; + } + + /** + * @return the isActive + */ + public boolean isActive() { + return _isActive; + } + + /** + * @param isActive the isActive to set + */ + public void setActive(boolean isActive) { + _isActive = isActive; + } + } + + Map<String,Map<String, SieveScript>> _repository = null; + + /** + * Creates a new instance of MockSieveRepository. + * + */ + public MockSieveRepository() { + _repository = new HashMap<String,Map<String, SieveScript>>(); + } + + /** + * @see SieveRepository#addUser(String) + */ + public void addUser(String user) throws DuplicateUserException, StorageException { + if (_repository.containsKey(user)) + { + throw new DuplicateUserException(user); + } + _repository.put(user, new HashMap<String, SieveScript>()); + } + + /** + * @see SieveRepository#deleteScript(String, String) + */ + public void deleteScript(String user, String name) throws UserNotFoundException, + ScriptNotFoundException, IsActiveException, StorageException { + if (!_repository.containsKey(user)) + { + throw new UserNotFoundException(user); + } + SieveScript script = _repository.get(user).get(name); + if (null == script) + { + throw new ScriptNotFoundException(name); + } + if (script.isActive()) + { + throw new IsActiveException(name); + } + _repository.get(user).remove(name); + } + + /** + * @see SieveRepository#getActive(String) + */ + public String getActive(String user) throws UserNotFoundException, ScriptNotFoundException { + if (!_repository.containsKey(user)) + { + throw new UserNotFoundException(user); + } + Set<Entry<String, SieveScript>> scripts = _repository.get(user).entrySet(); + String content = null; + for (final Entry<String, SieveScript> entry : scripts) + { + if (entry.getValue().isActive()) + { + content = entry.getValue().getContent(); + break; + } + } + if (null == content) + { + throw new ScriptNotFoundException(); + } + return content; + } + + /** + * @see SieveRepository#getQuota() + */ + public long getQuota() throws QuotaNotFoundException { + // TODO Auto-generated method stub + return 0; + } + + /** + * @see SieveRepository#getQuota(String) + */ + public long getQuota(String user) throws UserNotFoundException, QuotaNotFoundException { + // TODO Auto-generated method stub + return 0; + } + + /** + * @see SieveRepository#getScript(String, String) + */ + public String getScript(String user, String name) throws UserNotFoundException, + ScriptNotFoundException { + if (!_repository.containsKey(user)) + { + throw new UserNotFoundException(user); + } + SieveScript script = _repository.get(user).get(name); + if (null == script) + { + throw new ScriptNotFoundException(name); + } + return script.getContent(); + } + + /** + * @see SieveRepository#hasQuota() + */ + public boolean hasQuota() { + // TODO Auto-generated method stub + return false; + } + + /** + * @see SieveRepository#hasQuota(String) + */ + public boolean hasQuota(String user) throws UserNotFoundException { + // TODO Auto-generated method stub + return false; + } + + /** + * @see SieveRepository#hasUser(String) + */ + public boolean hasUser(String user) { + return _repository.containsKey(user); + } + + /** + * @see SieveRepository#haveSpace(String, String, long) + */ + public void haveSpace(String user, String name, long size) throws UserNotFoundException, + QuotaExceededException { + if (!_repository.containsKey(user)) + { + throw new UserNotFoundException(user); + } + } + + /** + * @see SieveRepository#listScripts(String) + */ + public List<ScriptSummary> listScripts(String user) throws UserNotFoundException { + if (!_repository.containsKey(user)) + { + throw new UserNotFoundException(user); + } + Set<Entry<String, SieveScript>> scripts = _repository.get(user).entrySet(); + List<ScriptSummary> summaries = new ArrayList<ScriptSummary>(scripts.size()); + for (final Entry<String, SieveScript> entry : scripts) { + summaries.add(new ScriptSummary(entry.getKey(), entry.getValue().isActive())); + } + return summaries; + } + + /** + * @see SieveRepository#putScript(String, String, String) + */ + public void putScript(String user, String name, String content) throws UserNotFoundException, + StorageException, QuotaExceededException { + if (!_repository.containsKey(user)) + { + throw new UserNotFoundException(user); + } + Map<String,SieveScript> scripts = _repository.get(user); + scripts.put(name, new SieveScript(content, false)); + } + + /** + * @see SieveRepository#removeQuota() + */ + public void removeQuota() throws QuotaNotFoundException, StorageException { + // TODO Auto-generated method stub + + } + + /** + * @see SieveRepository#removeQuota(String) + */ + public void removeQuota(String user) throws UserNotFoundException, QuotaNotFoundException, + StorageException { + // TODO Auto-generated method stub + + } + + /** + * @see SieveRepository#removeUser(String) + */ + public void removeUser(String user) throws UserNotFoundException, StorageException { + // TODO Auto-generated method stub + + } + + /** + * @see SieveRepository#renameScript(String, String, String) + */ + public void renameScript(String user, String oldName, String newName) + throws UserNotFoundException, ScriptNotFoundException, + DuplicateException, StorageException { + // TODO Auto-generated method stub + + } + + /** + * @see SieveRepository#setActive(String, String) + */ + public void setActive(String user, String name) throws UserNotFoundException, + ScriptNotFoundException, StorageException { + + // Turn off currently active script, if any + Entry<String, SieveScript> oldActive = null; + oldActive = getActiveEntry(user); + if (null != oldActive) { + oldActive.getValue().setActive(false); + } + + // Turn on the new active script if not an empty name + if ((null != name) && (!name.trim().isEmpty())) { + if (_repository.get(user).containsKey(name)) { + _repository.get(user).get(name).setActive(true); + } else { + if (null != oldActive) { + oldActive.getValue().setActive(true); + } + throw new ScriptNotFoundException(); + } + } + } + + protected Entry<String, SieveScript> getActiveEntry(String user) + { + Set<Entry<String, SieveScript>> scripts = _repository.get(user).entrySet(); + Entry<String, SieveScript> activeEntry = null; + for (final Entry<String, SieveScript> entry : scripts) + { + if (entry.getValue().isActive()) + { + activeEntry = entry; + break; + } + } + return activeEntry; + } + + /** + * @see SieveRepository#setQuota(long) + */ + public void setQuota(long quota) throws StorageException { + // TODO Auto-generated method stub + + } + + /** + * @see SieveRepository#setQuota(String, long) + */ + public void setQuota(String user, long quota) throws UserNotFoundException, StorageException { + // TODO Auto-generated method stub + + } + +} Modified: james/project/trunk/protocols/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/pom.xml?rev=1716451&r1=1716450&r2=1716451&view=diff ============================================================================== --- james/project/trunk/protocols/pom.xml (original) +++ james/project/trunk/protocols/pom.xml Wed Nov 25 16:01:15 2015 @@ -37,11 +37,12 @@ <modules> <module>api</module> - <module>smtp</module> + <module>imap</module> <module>lmtp</module> + <module>managesieve</module> <module>netty</module> <module>pop3</module> - <module>imap</module> + <module>smtp</module> </modules> <properties> @@ -60,6 +61,8 @@ <junit.version>4.10</junit.version> <geronimo-javamail.version>1.8.3</geronimo-javamail.version> <slf4j.version>1.6.3</slf4j.version> + <james-server-data-api.version>3.0.0-beta5-SNAPSHOT</james-server-data-api.version> + <jsieve.version>0.6-SNAPSHOT</jsieve.version> </properties> <dependencyManagement> @@ -163,6 +166,16 @@ <type>test-jar</type> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-data-api</artifactId> + <version>${james-server-data-api.version}</version> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>apache-jsieve-core</artifactId> + <version>${jsieve.version}</version> + </dependency> <!-- START Mail @@ -218,7 +231,7 @@ <groupId>org.apache.james</groupId> <artifactId>apache-james-mailbox-store</artifactId> <scope>test</scope> - <version>0.6-SNAPSHOT</version> + <version>${mailbox.version}</version> </dependency> <dependency> <groupId>org.apache.geronimo.javamail</groupId> Modified: james/project/trunk/server/app/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/server/app/pom.xml?rev=1716451&r1=1716450&r2=1716451&view=diff ============================================================================== --- james/project/trunk/server/app/pom.xml (original) +++ james/project/trunk/server/app/pom.xml Wed Nov 25 16:01:15 2015 @@ -324,6 +324,10 @@ </exclusions> </dependency> <dependency> + <groupId>org.apache.james.protocols</groupId> + <artifactId>protocols-managesieve</artifactId> + </dependency> + <dependency> <groupId>org.apache.james</groupId> <artifactId>james-server-fetchmail</artifactId> <scope>runtime</scope> Modified: james/project/trunk/server/container/spring/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/server/container/spring/pom.xml?rev=1716451&r1=1716450&r2=1716451&view=diff ============================================================================== --- james/project/trunk/server/container/spring/pom.xml (original) +++ james/project/trunk/server/container/spring/pom.xml Wed Nov 25 16:01:15 2015 @@ -83,26 +83,6 @@ </dependency> <dependency> <groupId>org.apache.james</groupId> - <artifactId>apache-jsieve-manager-mailet</artifactId> - <version>0.6-SNAPSHOT</version> - </dependency> - <dependency> - <groupId>org.apache.james</groupId> - <artifactId>apache-jsieve-manager-jsieve</artifactId> - <version>0.6-SNAPSHOT</version> - </dependency> - <dependency> - <groupId>org.apache.james</groupId> - <artifactId>apache-jsieve-manager-mailet</artifactId> - <version>0.6-SNAPSHOT</version> - </dependency> - <dependency> - <groupId>org.apache.james</groupId> - <artifactId>apache-jsieve-manager-jsieve</artifactId> - <version>0.6-SNAPSHOT</version> - </dependency> - <dependency> - <groupId>org.apache.james</groupId> <artifactId>apache-mailet-api</artifactId> </dependency> <dependency> Modified: james/project/trunk/server/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/server/pom.xml?rev=1716451&r1=1716450&r2=1716451&view=diff ============================================================================== --- james/project/trunk/server/pom.xml (original) +++ james/project/trunk/server/pom.xml Wed Nov 25 16:01:15 2015 @@ -405,6 +405,11 @@ <artifactId>protocols-lmtp</artifactId> <version>${protocols.version}</version> </dependency> + <dependency> + <groupId>org.apache.james.protocols</groupId> + <artifactId>protocols-managesieve</artifactId> + <version>${protocols.version}</version> + </dependency> <dependency> <groupId>org.apache.james</groupId> @@ -712,21 +717,6 @@ </exclusion> </exclusions> </dependency> - <dependency> - <groupId>org.apache.james</groupId> - <artifactId>apache-jsieve-manager-jsieve</artifactId> - <version>${jsieve.version}</version> - </dependency> - <dependency> - <groupId>org.apache.james</groupId> - <artifactId>apache-jsieve-manager-mailet</artifactId> - <version>${jsieve.version}</version> - </dependency> - <dependency> - <groupId>org.apache.james</groupId> - <artifactId>apache-jsieve-manager-api</artifactId> - <version>${jsieve.version}</version> - </dependency> <!-- Declare javamail as provided to be able to easily switch --> <!-- to different implementations (Geronimo) --> <dependency> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
