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.commons.io.IOUtils; 023import org.apache.james.managesieve.api.SessionTerminatedException; 024import org.apache.james.managesieve.transcode.ManageSieveProcessor; 025import org.apache.james.managesieve.util.SettableSession; 026import org.apache.james.mpt.api.Continuation; 027import org.apache.james.mpt.api.Session; 028import org.apache.james.mpt.helper.ByteBufferInputStream; 029import org.apache.james.mpt.helper.ByteBufferOutputStream; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032 033import java.io.StringWriter; 034 035public class ManageSieveSession implements Session { 036 037 private static final Logger LOGGER = LoggerFactory.getLogger(ManageSieveSession.class); 038 039 private final ByteBufferOutputStream out; 040 private final ByteBufferInputStream in; 041 private final ManageSieveProcessor manageSieveProcessor; 042 private final SettableSession settableSession; 043 private boolean isReadLast = true; 044 045 public ManageSieveSession(ManageSieveProcessor manageSieveProcessor, Continuation continuation) { 046 this.manageSieveProcessor = manageSieveProcessor; 047 this.out = new ByteBufferOutputStream(continuation); 048 this.in = new ByteBufferInputStream(); 049 this.settableSession = new SettableSession(); 050 } 051 052 @Override 053 public String readLine() throws Exception { 054 if (!isReadLast) { 055 String response; 056 StringWriter stringWriter = new StringWriter(); 057 IOUtils.copy(in, stringWriter); 058 String request = stringWriter.toString(); 059 try { 060 response = manageSieveProcessor.handleRequest(settableSession, request); 061 } catch (SessionTerminatedException e) { 062 LOGGER.info("Session is terminated"); 063 response = "OK channel is closing\r\n"; 064 } 065 out.write(response); 066 isReadLast = true; 067 } 068 if (settableSession.getState() == org.apache.james.managesieve.api.Session.State.SSL_NEGOCIATION) { 069 settableSession.setState(org.apache.james.managesieve.api.Session.State.UNAUTHENTICATED); 070 settableSession.setSslEnabled(true); 071 } 072 return out.nextLine(); 073 } 074 075 @Override 076 public void start() throws Exception { 077 } 078 079 @Override 080 public void stop() throws Exception { 081 } 082 083 @Override 084 public void restart() throws Exception { 085 } 086 087 @Override 088 public void writeLine(String line) throws Exception { 089 isReadLast = false; 090 in.nextLine(line); 091 } 092}