Author: btellier Date: Thu Dec 17 14:54:32 2015 New Revision: 1720570 URL: http://svn.apache.org/viewvc?rev=1720570&view=rev Log: JAMES-1618 Add a MPT implementation for ManageSieve and demonstrate it on SieveFileRepository
Added: james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferInputStream.java james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferOutputStream.java james/project/trunk/mpt/impl/managesieve/ james/project/trunk/mpt/impl/managesieve/core/ james/project/trunk/mpt/impl/managesieve/core/pom.xml james/project/trunk/mpt/impl/managesieve/core/src/ james/project/trunk/mpt/impl/managesieve/core/src/main/ james/project/trunk/mpt/impl/managesieve/core/src/main/java/ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveHostSystem.java james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveSession.java james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ManageSieveMPTTest.java james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/NoopTest.java james/project/trunk/mpt/impl/managesieve/core/src/main/resources/ james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/ james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/ james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/ james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/ james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/ james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/noop.test james/project/trunk/mpt/impl/managesieve/file/ james/project/trunk/mpt/impl/managesieve/file/pom.xml james/project/trunk/mpt/impl/managesieve/file/src/ james/project/trunk/mpt/impl/managesieve/file/src/test/ james/project/trunk/mpt/impl/managesieve/file/src/test/java/ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/FileModule.java james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java james/project/trunk/mpt/impl/managesieve/pom.xml Modified: james/project/trunk/mpt/impl/imap-mailbox/core/src/main/java/org/apache/james/mpt/host/JamesImapHostSystem.java james/project/trunk/mpt/pom.xml james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/core/CoreProcessor.java james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/ManageSieveProcessor.java james/project/trunk/server/protocols/protocols-managesieve/pom.xml Added: james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferInputStream.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferInputStream.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferInputStream.java (added) +++ james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferInputStream.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,58 @@ +/**************************************************************** + * 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.mpt.helper; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; + + +public class ByteBufferInputStream extends InputStream { + private ByteBuffer buffer = ByteBuffer.allocate(16384); + + private CharsetEncoder encoder = Charset.forName("ASCII").newEncoder(); + + private boolean readLast = true; + + public int read() throws IOException { + if (!readLast) { + readLast = true; + buffer.flip(); + } + int result = -1; + if (buffer.hasRemaining()) { + result = buffer.get(); + } + return result; + } + + public void nextLine(String line) { + if (buffer.position() > 0 && readLast) { + buffer.compact(); + } + encoder.encode(CharBuffer.wrap(line), buffer, true); + buffer.put((byte) '\r'); + buffer.put((byte) '\n'); + readLast = false; + } +} Added: james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferOutputStream.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferOutputStream.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferOutputStream.java (added) +++ james/project/trunk/mpt/core/src/main/java/org/apache/james/mpt/helper/ByteBufferOutputStream.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,91 @@ +/**************************************************************** + * 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.mpt.helper; + +import org.apache.james.mpt.api.Continuation; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; + +public class ByteBufferOutputStream extends OutputStream { + + private ByteBuffer buffer = ByteBuffer.allocate(65536); + private Charset ascii = Charset.forName("ASCII"); + private Continuation continuation; + private boolean matchPlus = false; + private boolean matchCR = false; + private boolean matchLF = false; + + public ByteBufferOutputStream(Continuation continuation) { + this.continuation = continuation; + } + + public void write(String message) throws IOException { + ascii.newEncoder().encode(CharBuffer.wrap(message), buffer, true); + } + + public void write(int b) throws IOException { + buffer.put((byte) b); + if (b == '\n' && matchPlus && matchCR && matchLF) { + matchPlus = false; + matchCR = false; + matchLF = false; + continuation.doContinue(); + } else if (b == '\n') { + matchLF = true; + matchPlus = false; + matchCR = false; + } else if (b == '+' && matchLF) { + matchPlus = true; + matchCR = false; + } else if (b == '\r' && matchPlus && matchLF) { + matchCR = true; + } else { + matchPlus = false; + matchCR = false; + matchLF = false; + } + } + + public String nextLine() throws Exception { + buffer.flip(); + byte last = 0; + while (buffer.hasRemaining()) { + byte next = buffer.get(); + if (last == '\r' && next == '\n') { + break; + } + last = next; + } + final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer(); + readOnlyBuffer.flip(); + int limit = readOnlyBuffer.limit() - 2; + if (limit < 0) { + limit = 0; + } + readOnlyBuffer.limit(limit); + String result = ascii.decode(readOnlyBuffer).toString(); + buffer.compact(); + return result; + } +} Modified: james/project/trunk/mpt/impl/imap-mailbox/core/src/main/java/org/apache/james/mpt/host/JamesImapHostSystem.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/imap-mailbox/core/src/main/java/org/apache/james/mpt/host/JamesImapHostSystem.java?rev=1720570&r1=1720569&r2=1720570&view=diff ============================================================================== --- james/project/trunk/mpt/impl/imap-mailbox/core/src/main/java/org/apache/james/mpt/host/JamesImapHostSystem.java (original) +++ james/project/trunk/mpt/impl/imap-mailbox/core/src/main/java/org/apache/james/mpt/host/JamesImapHostSystem.java Thu Dec 17 14:54:32 2015 @@ -19,16 +19,6 @@ package org.apache.james.mpt.host; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; -import java.util.HashSet; -import java.util.Set; - import org.apache.james.imap.api.process.ImapProcessor; import org.apache.james.imap.decode.ImapDecoder; import org.apache.james.imap.decode.main.ImapRequestStreamHandler; @@ -37,9 +27,14 @@ import org.apache.james.mailbox.MailboxS import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mpt.api.Continuation; import org.apache.james.mpt.api.ImapHostSystem; +import org.apache.james.mpt.helper.ByteBufferInputStream; +import org.apache.james.mpt.helper.ByteBufferOutputStream; import org.apache.james.mpt.session.ImapSessionImpl; import org.slf4j.LoggerFactory; +import java.util.HashSet; +import java.util.Set; + public abstract class JamesImapHostSystem implements ImapHostSystem { private ImapDecoder decoder; @@ -77,39 +72,8 @@ public abstract class JamesImapHostSyste protected abstract void resetData() throws Exception; - /* (non-Javadoc) - * @see org.apache.james.mpt.host.ImapHostSystem#createMailbox(org.apache.james.mailbox.model.MailboxPath) - */ public abstract void createMailbox(MailboxPath mailboxPath) throws Exception; - public String getHelloName() { - return "JAMES"; - } - - public ImapDecoder getImapDecoder() { - return decoder; - } - - public ImapEncoder getImapEncoder() { - return encoder; - } - - public ImapProcessor getImapProcessor() { - return processor; - } - - public int getResetLength() { - return Integer.MAX_VALUE; - } - - public int countUsers() { - return users.size(); - } - - public String getRealName(String name) { - return name; - } - class Session implements org.apache.james.mpt.api.Session { ByteBufferOutputStream out; @@ -151,112 +115,6 @@ public abstract class JamesImapHostSyste in.nextLine(line); } - public void forceConnectionClose(String byeMessage) { - try { - out.write(byeMessage); - session.deselect(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } - - static class ByteBufferInputStream extends InputStream { - - ByteBuffer buffer = ByteBuffer.allocate(16384); - - CharsetEncoder encoder = Charset.forName("ASCII").newEncoder(); - - boolean readLast = true; - - public int read() throws IOException { - if (!readLast) { - readLast = true; - buffer.flip(); - } - int result = -1; - if (buffer.hasRemaining()) { - result = buffer.get(); - } - return result; - } - - public void nextLine(String line) { - if (buffer.position() > 0 && readLast) { - buffer.compact(); - } - encoder.encode(CharBuffer.wrap(line), buffer, true); - buffer.put((byte) '\r'); - buffer.put((byte) '\n'); - readLast = false; - } - } - - static class ByteBufferOutputStream extends OutputStream { - ByteBuffer buffer = ByteBuffer.allocate(65536); - - Charset ascii = Charset.forName("ASCII"); - - Continuation continuation; - - boolean matchPlus = false; - - boolean matchCR = false; - - boolean matchLF = false; - - public ByteBufferOutputStream(Continuation continuation) { - this.continuation = continuation; - } - - public void write(String message) throws IOException { - ascii.newEncoder().encode(CharBuffer.wrap(message), buffer, true); - } - - public void write(int b) throws IOException { - buffer.put((byte) b); - if (b == '\n' && matchPlus && matchCR && matchLF) { - matchPlus = false; - matchCR = false; - matchLF = false; - continuation.doContinue(); - } else if (b == '\n') { - matchLF = true; - matchPlus = false; - matchCR = false; - } else if (b == '+' && matchLF) { - matchPlus = true; - matchCR = false; - } else if (b == '\r' && matchPlus && matchLF) { - matchCR = true; - } else { - matchPlus = false; - matchCR = false; - matchLF = false; - } - } - - public String nextLine() throws Exception { - buffer.flip(); - byte last = 0; - while (buffer.hasRemaining()) { - byte next = buffer.get(); - if (last == '\r' && next == '\n') { - break; - } - last = next; - } - final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer(); - readOnlyBuffer.flip(); - int limit = readOnlyBuffer.limit() - 2; - if (limit < 0) { - limit = 0; - } - readOnlyBuffer.limit(limit); - String result = ascii.decode(readOnlyBuffer).toString(); - buffer.compact(); - return result; - } } public void afterTests() throws Exception { Added: james/project/trunk/mpt/impl/managesieve/core/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/core/pom.xml?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/core/pom.xml (added) +++ james/project/trunk/mpt/impl/managesieve/core/pom.xml Thu Dec 17 14:54:32 2015 @@ -0,0 +1,85 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>apache-james-mpt-managesieve</artifactId> + <groupId>org.apache.james</groupId> + <version>0.2-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>apache-james-mpt-managesieve-core</artifactId> + + <name>Apache James MPT ManageSieve Core</name> + + <dependencies> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>apache-james-mpt-core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-data-api</artifactId> + <version>${james.version}</version> + </dependency> + <dependency> + <groupId>org.apache.james.protocols</groupId> + <artifactId>protocols-managesieve</artifactId> + <version>1.6.4-SNAPSHOT</version> + </dependency> + + + <dependency> + <groupId>com.google.inject</groupId> + <artifactId>guice</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + <dependency> + <groupId>org.apache.onami</groupId> + <artifactId>org.apache.onami.test</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <excludes> + <exclude>**/suite/*.java</exclude> + <exclude>**/suite/**/*.java</exclude> + </excludes> + </configuration> + </plugin> + </plugins> + </build> + + +</project> \ No newline at end of file Added: james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java (added) +++ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/JamesManageSieveHostSystem.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,71 @@ +/**************************************************************** + * 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.mpt.host; + +import org.apache.james.managesieve.core.CoreProcessor; +import org.apache.james.managesieve.jsieve.Parser; +import org.apache.james.managesieve.transcode.LineToCore; +import org.apache.james.managesieve.transcode.LineToCoreToLine; +import org.apache.james.managesieve.transcode.ManageSieveProcessor; +import org.apache.james.mpt.api.Continuation; +import org.apache.james.mpt.api.Session; +import org.apache.james.sieverepository.api.SieveRepository; +import org.apache.james.user.api.UsersRepository; +import org.apache.jsieve.ConfigurationManager; + +public abstract class JamesManageSieveHostSystem implements ManageSieveHostSystem { + + private final UsersRepository usersRepository; + protected final SieveRepository sieveRepository; + private final ManageSieveProcessor processor; + + public JamesManageSieveHostSystem(UsersRepository usersRepository, SieveRepository sieveRepository) throws Exception { + this.usersRepository = usersRepository; + this.sieveRepository = sieveRepository; + this.processor = new ManageSieveProcessor(new LineToCoreToLine(new LineToCore(new CoreProcessor(sieveRepository, usersRepository, new Parser(new ConfigurationManager()))))); + } + + @Override + public boolean addUser(String user, String password) throws Exception { + usersRepository.addUser(user, password); + return true; + } + + @Override + public Session newSession(Continuation continuation) throws Exception { + return new ManageSieveSession(processor, continuation); + } + + @Override + public void beforeTests() throws Exception {} + + @Override + public void afterTests() throws Exception {} + + @Override + public void beforeTest() throws Exception {} + + @Override + public void afterTest() throws Exception { + resetData(); + } + + protected abstract void resetData() throws Exception; +} Added: james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveHostSystem.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveHostSystem.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveHostSystem.java (added) +++ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveHostSystem.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,26 @@ +/**************************************************************** + * 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.mpt.host; + +import org.apache.james.mpt.api.HostSystem; + +public interface ManageSieveHostSystem extends HostSystem { + +} Added: james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveSession.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveSession.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveSession.java (added) +++ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/host/ManageSieveSession.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,73 @@ +/**************************************************************** + * 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.mpt.host; + +import org.apache.commons.io.IOUtils; +import org.apache.james.managesieve.transcode.ManageSieveProcessor; +import org.apache.james.managesieve.util.SettableSession; +import org.apache.james.mpt.api.Continuation; +import org.apache.james.mpt.api.Session; +import org.apache.james.mpt.helper.ByteBufferInputStream; +import org.apache.james.mpt.helper.ByteBufferOutputStream; + +import java.io.StringWriter; + +public class ManageSieveSession implements Session { + + private ByteBufferOutputStream out; + private ByteBufferInputStream in; + private ManageSieveProcessor manageSieveProcessor; + private SettableSession settableSession; + private boolean isReadLast = true; + + public ManageSieveSession(ManageSieveProcessor manageSieveProcessor, Continuation continuation) { + this.manageSieveProcessor = manageSieveProcessor; + this.out = new ByteBufferOutputStream(continuation); + this.in = new ByteBufferInputStream(); + this.settableSession = new SettableSession(); + } + + @Override + public String readLine() throws Exception { + if (!isReadLast) { + StringWriter stringWriter = new StringWriter(); + IOUtils.copy(in, stringWriter); + String request = stringWriter.toString(); + String response = manageSieveProcessor.handleRequest(settableSession, request); + out.write(response); + isReadLast = true; + } + return out.nextLine(); + } + + @Override + public void start() throws Exception { + } + + @Override + public void stop() throws Exception { + } + + @Override + public void writeLine(String line) throws Exception { + isReadLast = false; + in.nextLine(line); + } +} Added: james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ManageSieveMPTTest.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ManageSieveMPTTest.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ManageSieveMPTTest.java (added) +++ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/ManageSieveMPTTest.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,33 @@ +/**************************************************************** + * 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.mpt.testsuite; + +import org.apache.james.mpt.api.HostSystem; +import org.apache.james.mpt.script.AbstractSimpleScriptedTestProtocol; + +public class ManageSieveMPTTest extends AbstractSimpleScriptedTestProtocol { + + public static final String USER = "user"; + public static final String PASSWORD = "password"; + + public ManageSieveMPTTest(HostSystem hostSystem) throws Exception { + super(hostSystem, USER, PASSWORD, "/org/apache/james/managesieve/scripts/"); + } +} Added: james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/NoopTest.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/NoopTest.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/NoopTest.java (added) +++ james/project/trunk/mpt/impl/managesieve/core/src/main/java/org/apache/james/mpt/testsuite/NoopTest.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,42 @@ +/**************************************************************** + * 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.mpt.testsuite; + +import com.google.inject.Inject; +import org.apache.james.mpt.host.ManageSieveHostSystem; +import org.junit.Test; + +import java.util.Locale; + +public class NoopTest extends ManageSieveMPTTest { + + @Inject + private static ManageSieveHostSystem hostSystem; + + public NoopTest() throws Exception { + super(hostSystem); + } + + @Test + public void noopShouldWork() throws Exception { + scriptTest("noop", Locale.US); + } + +} Added: james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/noop.test URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/noop.test?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/noop.test (added) +++ james/project/trunk/mpt/impl/managesieve/core/src/main/resources/org/apache/james/managesieve/scripts/noop.test Thu Dec 17 14:54:32 2015 @@ -0,0 +1,21 @@ +################################################################ +# 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. # +################################################################ + +C: NOOP +S: NO NOOP command not yet implemented \ No newline at end of file Added: james/project/trunk/mpt/impl/managesieve/file/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/file/pom.xml?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/file/pom.xml (added) +++ james/project/trunk/mpt/impl/managesieve/file/pom.xml Thu Dec 17 14:54:32 2015 @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>apache-james-mpt-managesieve</artifactId> + <groupId>org.apache.james</groupId> + <version>0.2-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>apache-james-mpt-managesieve-file</artifactId> + + <name>Apache James MPT ManageSieve File</name> + + <dependencies> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>apache-james-mpt-managesieve-core</artifactId> + <version>0.2-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-data-file</artifactId> + <version>${james.version}</version> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-data-memory</artifactId> + <version>${james.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <excludes> + <exclude>**/suite/*.java</exclude> + <exclude>**/suite/**/*.java</exclude> + </excludes> + </configuration> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file Added: james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/FileModule.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/FileModule.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/FileModule.java (added) +++ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/FileModule.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,32 @@ +/**************************************************************** + * 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.mpt.managesieve.file; + +import com.google.inject.AbstractModule; +import org.apache.james.mpt.host.ManageSieveHostSystem; +import org.apache.james.mpt.managesieve.file.host.FileHostSystem; + +public class FileModule extends AbstractModule { + + @Override + protected void configure() { + bind(ManageSieveHostSystem.class).to(FileHostSystem.class); + } +} Added: james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java (added) +++ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/ManageSieveFileTest.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,34 @@ +/**************************************************************** + * 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.mpt.managesieve.file; + +import org.apache.james.mpt.testsuite.NoopTest; +import org.apache.onami.test.OnamiSuite; +import org.apache.onami.test.annotation.GuiceModules; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@GuiceModules({ FileModule.class }) +@RunWith(OnamiSuite.class) +@Suite.SuiteClasses({ + NoopTest.class +}) +public class ManageSieveFileTest { +} Added: james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java (added) +++ james/project/trunk/mpt/impl/managesieve/file/src/test/java/org/apache/james/mpt/managesieve/file/host/FileHostSystem.java Thu Dec 17 14:54:32 2015 @@ -0,0 +1,54 @@ +package org.apache.james.mpt.managesieve.file.host; + +import org.apache.commons.io.FileUtils; +import org.apache.james.filesystem.api.FileSystem; +import org.apache.james.mpt.host.JamesManageSieveHostSystem; +import org.apache.james.sieverepository.api.SieveRepository; +import org.apache.james.sieverepository.file.SieveFileRepository; +import org.apache.james.user.api.UsersRepository; +import org.apache.james.user.memory.MemoryUsersRepository; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +public class FileHostSystem extends JamesManageSieveHostSystem { + + private static final String SIEVE_ROOT = FileSystem.FILE_PROTOCOL + "sieve"; + private static final FileSystem fileSystem = getFileSystem(); + + private static FileSystem getFileSystem() { + return new FileSystem() { + public File getBasedir() throws FileNotFoundException { + return new File(System.getProperty("java.io.tmpdir")); + } + public InputStream getResource(String url) throws IOException { + return new FileInputStream(getFile(url)); + } + public File getFile(String fileURL) throws FileNotFoundException { + return new File(getBasedir(), fileURL.substring(FileSystem.FILE_PROTOCOL.length())); + } + }; + } + + protected static SieveRepository createSieveRepository() throws Exception { + File root = getFileSystem().getFile(SIEVE_ROOT); + FileUtils.forceMkdir(root); + return new SieveFileRepository(fileSystem); + } + + public FileHostSystem() throws Exception { + super(new MemoryUsersRepository(), createSieveRepository()); + } + + @Override + protected void resetData() throws Exception { + File root = fileSystem.getFile(SIEVE_ROOT); + // Remove files from the previous test, if any + if (root.exists()) { + FileUtils.forceDelete(root); + } + } +} Added: james/project/trunk/mpt/impl/managesieve/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/impl/managesieve/pom.xml?rev=1720570&view=auto ============================================================================== --- james/project/trunk/mpt/impl/managesieve/pom.xml (added) +++ james/project/trunk/mpt/impl/managesieve/pom.xml Thu Dec 17 14:54:32 2015 @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>apache-james-mpt</artifactId> + <groupId>org.apache.james</groupId> + <version>0.2-SNAPSHOT</version> + <relativePath>../../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <name>Apache James MPT ManageSieve</name> + + <artifactId>apache-james-mpt-managesieve</artifactId> + <packaging>pom</packaging> + <modules> + <module>core</module> + <module>file</module> + </modules> + + +</project> \ No newline at end of file Modified: james/project/trunk/mpt/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/mpt/pom.xml?rev=1720570&r1=1720569&r2=1720570&view=diff ============================================================================== --- james/project/trunk/mpt/pom.xml (original) +++ james/project/trunk/mpt/pom.xml Thu Dec 17 14:54:32 2015 @@ -45,6 +45,7 @@ <module>all</module> <module>core</module> <module>impl/imap-mailbox</module> + <module>impl/managesieve</module> <module>mavenplugin</module> </modules> @@ -114,6 +115,7 @@ <javax.mail.groupId>javax.mail</javax.mail.groupId> <javax.mail.artifactId>mail</javax.mail.artifactId> <backends.version>1.8.3-SNAPSHOT</backends.version> + <james.version>3.0.0-beta5-SNAPSHOT</james.version> </properties> 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=1720570&r1=1720569&r2=1720570&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 Thu Dec 17 14:54:32 2015 @@ -183,8 +183,10 @@ public class CoreProcessor implements Co private String buildExtensions(SieveParser parser) { StringBuilder builder = new StringBuilder(); - for (String extension : parser.getExtensions()) { - builder.append(extension).append(' '); + if (parser.getExtensions() != null) { + for (String extension : parser.getExtensions()) { + builder.append(extension).append(' '); + } } return builder.toString().trim(); } Modified: james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/ManageSieveProcessor.java URL: http://svn.apache.org/viewvc/james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/ManageSieveProcessor.java?rev=1720570&r1=1720569&r2=1720570&view=diff ============================================================================== --- james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/ManageSieveProcessor.java (original) +++ james/project/trunk/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/ManageSieveProcessor.java Thu Dec 17 14:54:32 2015 @@ -22,12 +22,7 @@ package org.apache.james.managesieve.tra import org.apache.james.managesieve.api.ManageSieveException; import org.apache.james.managesieve.api.Session; -import org.apache.james.managesieve.api.SieveParser; -import org.apache.james.managesieve.core.CoreProcessor; -import org.apache.james.managesieve.jsieve.Parser; -import org.apache.james.sieverepository.api.SieveRepository; import org.apache.james.sieverepository.api.exception.SieveRepositoryException; -import org.apache.james.user.api.UsersRepository; public class ManageSieveProcessor { @@ -55,8 +50,36 @@ public class ManageSieveProcessor { public String handleRequest(Session session, String request) throws ManageSieveException, SieveRepositoryException { int firstWordEndIndex = request.indexOf(' '); - String command = request.substring(0, firstWordEndIndex); - String arguments = request.substring(firstWordEndIndex); + String arguments = parseArguments(request, firstWordEndIndex); + String command = parseCommand(request, firstWordEndIndex); + return matchCommandWithImplementation(session, arguments, command) + "\r\n"; + } + + private String parseCommand(String request, int firstWordEndIndex) { + String command; + if (request.contains(" ")) { + command = request.substring(0, firstWordEndIndex); + } else { + command = request; + } + if (command.endsWith("\n")) { + command = command.substring(0, command.length()-1); + } + if (command.endsWith("\r")) { + command = command.substring(0, command.length()-1); + } + return command; + } + + private String parseArguments(String request, int firstWordEndIndex) { + if (request.contains(" ")) { + return request.substring(firstWordEndIndex); + } else { + return ""; + } + } + + private String matchCommandWithImplementation(Session session, String arguments, String command) { if (command.equals(AUTHENTICATE)) { return "NO AUTHENTICATE command not yet implemented"; } else if (command.equals(CAPABILITY)) { @@ -76,7 +99,7 @@ public class ManageSieveProcessor { } else if (command.equals(LOGOUT)) { return "NO LOGOUT command not yet implemented"; } else if (command.equals(NOOP)) { - return "NO NOOP command not yet implemented"; + return "NO NOOP command not yet implemented\r\n"; } else if (command.equals(PUTSCRIPT)) { return lineToCoreToLine.putScript(session, arguments); } else if (command.equals(RENAMESCRIPT)) { Modified: james/project/trunk/server/protocols/protocols-managesieve/pom.xml URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/protocols-managesieve/pom.xml?rev=1720570&r1=1720569&r2=1720570&view=diff ============================================================================== --- james/project/trunk/server/protocols/protocols-managesieve/pom.xml (original) +++ james/project/trunk/server/protocols/protocols-managesieve/pom.xml Thu Dec 17 14:54:32 2015 @@ -12,6 +12,8 @@ <artifactId>james-server-protocols-managesieve</artifactId> + <name>Apache James :: Server :: ManageSieve</name> + <dependencies> <dependency> <groupId>org.apache.james</groupId> --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org