Author: bago Date: Fri Sep 16 15:32:17 2011 New Revision: 1171625 URL: http://svn.apache.org/viewvc?rev=1171625&view=rev Log: Introduce ProtocolTransport a service implemented by the transport implementation (e.g: netty) and used by generic Protocol sessions (e.g: SmtpSession). This way the session does need to depend on the transport implementation but depends on it at runtime.
Added: james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java (with props) james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/NettyProtocolTransport.java (with props) Modified: james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractSession.java james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/LineHandlerUpstreamHandler.java james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPChannelUpstreamHandler.java james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPNettySession.java Added: james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java?rev=1171625&view=auto ============================================================================== --- james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java (added) +++ james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java Fri Sep 16 15:32:17 2011 @@ -0,0 +1,46 @@ +/**************************************************************** + * 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.protocols.api; + +import java.net.InetSocketAddress; + +/** + * ProtocolTransport is used by each ProtocolSession to communicate with the underlying transport. + * Transport implementations will provide their own implementation of the transport. + */ +public interface ProtocolTransport { + + InetSocketAddress getRemoteAddress(); + + String getId(); + + boolean isTLSStarted(); + + boolean isStartTLSSupported(); + + void writeResponse(Response response, ProtocolSession session); + + void popLineHandler(); + + <T extends ProtocolSession> void pushLineHandler(LineHandler<T> overrideCommandHandler, T smtpNettySession); + + int getPushedLineHandlerCount(); + +} Propchange: james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/ProtocolTransport.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractSession.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractSession.java?rev=1171625&r1=1171624&r2=1171625&view=diff ============================================================================== --- james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractSession.java (original) +++ james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/AbstractSession.java Fri Sep 16 15:32:17 2011 @@ -21,15 +21,9 @@ package org.apache.james.protocols.impl; import java.net.InetSocketAddress; -import javax.net.ssl.SSLEngine; - +import org.apache.james.protocols.api.ProtocolTransport; import org.apache.james.protocols.api.Response; -import org.apache.james.protocols.api.StartTlsResponse; import org.apache.james.protocols.api.TLSSupportedSession; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelFuture; -import org.jboss.netty.channel.ChannelFutureListener; -import org.jboss.netty.handler.ssl.SslHandler; import org.slf4j.Logger; /** @@ -38,27 +32,21 @@ import org.slf4j.Logger; * */ public abstract class AbstractSession implements TLSSupportedSession { - protected Channel channel; protected InetSocketAddress socketAddress; private Logger logger; private SessionLog pLog = null; - protected SSLEngine engine; protected String user; private String id; + protected ProtocolTransport transport; - public AbstractSession(Logger logger, Channel channel, SSLEngine engine) { - this.channel = channel; - this.socketAddress = (InetSocketAddress) channel.getRemoteAddress(); + public AbstractSession(Logger logger, ProtocolTransport transport) { + this.transport = transport; + this.socketAddress = transport.getRemoteAddress(); this.logger = logger; - this.engine = engine; - this.id = channel.getId() + ""; - } - - public AbstractSession(Logger logger, Channel channel) { - this(logger, channel, null); + this.id = transport.getId(); } /** @@ -90,31 +78,26 @@ public abstract class AbstractSession im } /** - * Return underlying {@link Channel} + * Return underlying {@link ProtocolTransport} * * @return session */ - public Channel getChannel() { - return channel; + public ProtocolTransport getProtocolTransport() { + return transport; } /** * @see org.apache.james.api.protocol.TLSSupportedSession#isStartTLSSupported() */ public boolean isStartTLSSupported() { - return engine != null; + return transport.isStartTLSSupported(); } /** * @see org.apache.james.api.protocol.TLSSupportedSession#isTLSStarted() */ public boolean isTLSStarted() { - - if (isStartTLSSupported()) { - return channel.getPipeline().get("sslHandler") != null; - } - - return false; + return transport.isTLSStarted(); } /** @@ -133,23 +116,7 @@ public abstract class AbstractSession im * @see org.apache.james.api.protocol.ProtocolSession#writeResponse(org.apache.james.api.protocol.Response) */ public void writeResponse(final Response response) { - if (response != null && channel.isConnected()) { - ChannelFuture cf = channel.write(response); - if (response.isEndSession()) { - // close the channel if needed after the message was written out - cf.addListener(ChannelFutureListener.CLOSE); - } - if (response instanceof StartTlsResponse) { - if (isStartTLSSupported()) { - channel.setReadable(false); - SslHandler filter = new SslHandler(engine); - filter.getEngine().setUseClientMode(false); - resetState(); - channel.getPipeline().addFirst("sslHandler", filter); - channel.setReadable(true); - } - } - } + transport.writeResponse(response, this); } /* Modified: james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/LineHandlerUpstreamHandler.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/LineHandlerUpstreamHandler.java?rev=1171625&r1=1171624&r2=1171625&view=diff ============================================================================== --- james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/LineHandlerUpstreamHandler.java (original) +++ james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/LineHandlerUpstreamHandler.java Fri Sep 16 15:32:17 2011 @@ -29,14 +29,14 @@ import org.jboss.netty.channel.SimpleCha /** * {@link ChannelUpstreamHandler} implementation which will call a given {@link LineHandler} implementation * - * @param <Session> + * @param <S> */ -public class LineHandlerUpstreamHandler<Session extends ProtocolSession> extends SimpleChannelUpstreamHandler { +public class LineHandlerUpstreamHandler<S extends ProtocolSession> extends SimpleChannelUpstreamHandler { - private final LineHandler<Session> handler; - private final Session session; + private final LineHandler<S> handler; + private final S session; - public LineHandlerUpstreamHandler(Session session, LineHandler<Session> handler) { + public LineHandlerUpstreamHandler(S session, LineHandler<S> handler) { this.handler = handler; this.session = session; } Added: james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/NettyProtocolTransport.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/NettyProtocolTransport.java?rev=1171625&view=auto ============================================================================== --- james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/NettyProtocolTransport.java (added) +++ james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/NettyProtocolTransport.java Fri Sep 16 15:32:17 2011 @@ -0,0 +1,113 @@ +/**************************************************************** + * 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.protocols.impl; + +import java.net.InetSocketAddress; + +import javax.net.ssl.SSLEngine; + +import org.apache.james.protocols.api.LineHandler; +import org.apache.james.protocols.api.ProtocolSession; +import org.apache.james.protocols.api.ProtocolTransport; +import org.apache.james.protocols.api.Response; +import org.apache.james.protocols.api.StartTlsResponse; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelFuture; +import org.jboss.netty.channel.ChannelFutureListener; +import org.jboss.netty.handler.ssl.SslHandler; + +/** + * A Netty implementation of a ProtocolTransport + */ +public class NettyProtocolTransport implements ProtocolTransport { + + private Channel channel; + private SSLEngine engine; + private int lineHandlerCount = 0; + + public NettyProtocolTransport(Channel channel, SSLEngine engine) { + this.channel = channel; + this.engine = engine; + } + + public InetSocketAddress getRemoteAddress() { + return (InetSocketAddress) channel.getRemoteAddress(); + } + + public String getId() { + return channel.getId() + ""; + } + + public boolean isTLSStarted() { + if (isStartTLSSupported()) { + return channel.getPipeline().get("sslHandler") != null; + } + return false; + } + + /** + * @see org.apache.james.api.protocol.TLSSupportedSession#isStartTLSSupported() + */ + public boolean isStartTLSSupported() { + return engine != null; + } + + public void writeResponse(Response response, ProtocolSession session) { + if (response != null && channel.isConnected()) { + ChannelFuture cf = channel.write(response); + if (response.isEndSession()) { + // close the channel if needed after the message was written out + cf.addListener(ChannelFutureListener.CLOSE); + } + if (response instanceof StartTlsResponse) { + if (isStartTLSSupported()) { + channel.setReadable(false); + SslHandler filter = new SslHandler(engine); + filter.getEngine().setUseClientMode(false); + session.resetState(); + channel.getPipeline().addFirst("sslHandler", filter); + channel.setReadable(true); + } + } + } + } + + public void popLineHandler() { + if (lineHandlerCount > 0) { + channel.getPipeline().remove("lineHandler" + lineHandlerCount); + lineHandlerCount--; + } + } + + public <T extends ProtocolSession> void pushLineHandler(LineHandler<T> overrideCommandHandler, + T session) { + lineHandlerCount++; + // Add the linehandler in front of the coreHandler so we can be sure + // it is executed with the same ExecutorHandler as the coreHandler (if one exist) + // + // See JAMES-1277 + channel.getPipeline().addBefore("coreHandler", "lineHandler" + lineHandlerCount, new LineHandlerUpstreamHandler<T>(session, overrideCommandHandler)); + } + + public int getPushedLineHandlerCount() { + return lineHandlerCount; + } + +} Propchange: james/protocols/trunk/impl/src/main/java/org/apache/james/protocols/impl/NettyProtocolTransport.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPChannelUpstreamHandler.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPChannelUpstreamHandler.java?rev=1171625&r1=1171624&r2=1171625&view=diff ============================================================================== --- james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPChannelUpstreamHandler.java (original) +++ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPChannelUpstreamHandler.java Fri Sep 16 15:32:17 2011 @@ -24,6 +24,7 @@ import javax.net.ssl.SSLEngine; import org.apache.james.protocols.api.ProtocolHandlerChain; import org.apache.james.protocols.api.ProtocolSession; import org.apache.james.protocols.impl.AbstractChannelUpstreamHandler; +import org.apache.james.protocols.impl.NettyProtocolTransport; import org.apache.james.protocols.smtp.SMTPConfiguration; import org.apache.james.protocols.smtp.SMTPResponse; import org.apache.james.protocols.smtp.SMTPRetCode; @@ -67,7 +68,8 @@ public class SMTPChannelUpstreamHandler engine.setEnabledCipherSuites(enabledCipherSuites); } } - return new SMTPNettySession(conf, logger, ctx.getChannel(), engine); + + return new SMTPNettySession(conf, logger, new NettyProtocolTransport(ctx.getChannel(), engine)); } @Override Modified: james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPNettySession.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPNettySession.java?rev=1171625&r1=1171624&r2=1171625&view=diff ============================================================================== --- james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPNettySession.java (original) +++ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPNettySession.java Fri Sep 16 15:32:17 2011 @@ -22,14 +22,11 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; -import javax.net.ssl.SSLEngine; - import org.apache.james.protocols.api.LineHandler; +import org.apache.james.protocols.api.ProtocolTransport; import org.apache.james.protocols.impl.AbstractSession; -import org.apache.james.protocols.impl.LineHandlerUpstreamHandler; import org.apache.james.protocols.smtp.SMTPConfiguration; import org.apache.james.protocols.smtp.SMTPSession; -import org.jboss.netty.channel.Channel; import org.slf4j.Logger; /** @@ -44,10 +41,8 @@ public class SMTPNettySession extends Ab private SMTPConfiguration theConfigData; - private int lineHandlerCount = 0; - - public SMTPNettySession(SMTPConfiguration theConfigData, Logger logger, Channel channel, SSLEngine engine) { - super(logger, channel, engine); + public SMTPNettySession(SMTPConfiguration theConfigData, Logger logger, ProtocolTransport transport) { + super(logger, transport); this.theConfigData = theConfigData; connectionState = new HashMap<String, Object>(); @@ -100,22 +95,14 @@ public class SMTPNettySession extends Ab * @see org.apache.james.protocols.smtp.SMTPSession#popLineHandler() */ public void popLineHandler() { - if (lineHandlerCount > 0) { - getChannel().getPipeline().remove("lineHandler" + lineHandlerCount); - lineHandlerCount--; - } + transport.popLineHandler(); } /** * @see org.apache.james.protocols.smtp.SMTPSession#pushLineHandler(org.apache.james.smtpserver.protocol.LineHandler) */ public void pushLineHandler(LineHandler<SMTPSession> overrideCommandHandler) { - lineHandlerCount++; - // Add the linehandler in front of the coreHandler so we can be sure - // it is executed with the same ExecutorHandler as the coreHandler (if one exist) - // - // See JAMES-1277 - getChannel().getPipeline().addBefore("coreHandler", "lineHandler" + lineHandlerCount, new LineHandlerUpstreamHandler<SMTPSession>(this, overrideCommandHandler)); + transport.pushLineHandler(overrideCommandHandler, this); } /** @@ -197,7 +184,7 @@ public class SMTPNettySession extends Ab * org.apache.james.protocols.smtp.SMTPSession#getPushedLineHandlerCount() */ public int getPushedLineHandlerCount() { - return lineHandlerCount; + return transport.getPushedLineHandlerCount(); } } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org