001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.mpt.host;
021
022import org.apache.james.managesieve.core.CoreProcessor;
023import org.apache.james.managesieve.jsieve.Parser;
024import org.apache.james.managesieve.transcode.ArgumentParser;
025import org.apache.james.managesieve.transcode.ManageSieveProcessor;
026import org.apache.james.mpt.api.Continuation;
027import org.apache.james.mpt.api.Session;
028import org.apache.james.sieverepository.api.SieveRepository;
029import org.apache.james.user.api.UsersRepository;
030import org.apache.jsieve.ConfigurationManager;
031
032public abstract class JamesManageSieveHostSystem implements ManageSieveHostSystem {
033
034    private final UsersRepository usersRepository;
035    protected final SieveRepository sieveRepository;
036    private final ManageSieveProcessor processor;
037
038    public JamesManageSieveHostSystem(UsersRepository usersRepository, SieveRepository sieveRepository) throws Exception {
039        this.usersRepository = usersRepository;
040        this.sieveRepository = sieveRepository;
041        this.processor = new ManageSieveProcessor(new ArgumentParser(new CoreProcessor(sieveRepository, usersRepository, new Parser(new ConfigurationManager()))));
042    }
043
044    @Override
045    public boolean addUser(String user, String password) throws Exception {
046        usersRepository.addUser(user, password);
047        return true;
048    }
049
050    @Override
051    public void setMaxQuota(String user, long value) throws Exception {
052        sieveRepository.setQuota(user, value);
053    }
054
055    @Override
056    public Session newSession(Continuation continuation) throws Exception {
057        return new ManageSieveSession(processor, continuation);
058    }
059
060    @Override
061    public void beforeTests() throws Exception {}
062
063    @Override
064    public void afterTests() throws Exception {}
065
066    @Override
067    public void beforeTest() throws Exception {}
068
069    @Override
070    public void afterTest() throws Exception {
071        resetData();
072    }
073
074    protected abstract void resetData() throws Exception;
075}