Author: norman Date: Fri Sep 16 10:57:30 2011 New Revision: 1171501 URL: http://svn.apache.org/viewvc?rev=1171501&view=rev Log: Add simple SMTPServer by using netty. See PROTOCOLS-31
Added: james/protocols/trunk/smtp-netty/ (with props) james/protocols/trunk/smtp-netty/.classpath (with props) james/protocols/trunk/smtp-netty/.project (with props) james/protocols/trunk/smtp-netty/pom.xml (with props) james/protocols/trunk/smtp-netty/src/ james/protocols/trunk/smtp-netty/src/main/ james/protocols/trunk/smtp-netty/src/main/java/ james/protocols/trunk/smtp-netty/src/main/java/org/ james/protocols/trunk/smtp-netty/src/main/java/org/apache/ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPChannelUpstreamHandler.java (with props) james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPNettySession.java (with props) james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPResponseEncoder.java (with props) james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPServer.java (with props) Propchange: james/protocols/trunk/smtp-netty/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Fri Sep 16 10:57:30 2011 @@ -0,0 +1 @@ +target Added: james/protocols/trunk/smtp-netty/.classpath URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp-netty/.classpath?rev=1171501&view=auto ============================================================================== --- james/protocols/trunk/smtp-netty/.classpath (added) +++ james/protocols/trunk/smtp-netty/.classpath Fri Sep 16 10:57:30 2011 @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src/main/java"/> + <classpathentry kind="src" path="src/test/java"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> + <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/> + <classpathentry kind="output" path="target/classes"/> +</classpath> Propchange: james/protocols/trunk/smtp-netty/.classpath ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/protocols/trunk/smtp-netty/.project URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp-netty/.project?rev=1171501&view=auto ============================================================================== --- james/protocols/trunk/smtp-netty/.project (added) +++ james/protocols/trunk/smtp-netty/.project Fri Sep 16 10:57:30 2011 @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>protocols-smtp-netty</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.m2e.core.maven2Builder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + <nature>org.eclipse.m2e.core.maven2Nature</nature> + </natures> +</projectDescription> Propchange: james/protocols/trunk/smtp-netty/.project ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/protocols/trunk/smtp-netty/pom.xml URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp-netty/pom.xml?rev=1171501&view=auto ============================================================================== --- james/protocols/trunk/smtp-netty/pom.xml (added) +++ james/protocols/trunk/smtp-netty/pom.xml Fri Sep 16 10:57:30 2011 @@ -0,0 +1,103 @@ +<?xml version="1.0" encoding="ISO-8859-15"?> +<!-- + 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/maven-v4_0_0.xsd"> + <parent> + <artifactId>protocols</artifactId> + <groupId>org.apache.james</groupId> + <version>1.6-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.james.protocols</groupId> + <artifactId>protocols-smtp-netty</artifactId> + <name>Apache James Protocols SMTP Implementation using netty</name> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <!-- This configuration is used by all goals --> + <configuration> + <archive> + <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile> + <manifest> + <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> + <addDefaultImplementationEntries>true</addDefaultImplementationEntries> + </manifest> + </archive> + </configuration> + <executions> + <execution> + <goals> + <goal>test-jar</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <executions> + <execution> + <id>bundle-manifest</id> + <phase>process-classes</phase> + <goals> + <goal>manifest</goal> + </goals> + </execution> + </executions> + <extensions>true</extensions> + <configuration> + <instructions> + <Export-Package>org.apache.james.protocols.impl.*, org.jboss.netty.handler.connection</Export-Package> + <Embed-Dependency>*;scope=runtime</Embed-Dependency> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>org.apache.james.protocols</groupId> + <artifactId>protocols-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.james.protocols</groupId> + <artifactId>protocols-smtp</artifactId> + </dependency> + <dependency> + <groupId>org.apache.james.protocols</groupId> + <artifactId>protocols-impl</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>org.jboss.netty</groupId> + <artifactId>netty</artifactId> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> +</project> \ No newline at end of file Propchange: james/protocols/trunk/smtp-netty/pom.xml ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: 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=1171501&view=auto ============================================================================== --- james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPChannelUpstreamHandler.java (added) +++ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPChannelUpstreamHandler.java Fri Sep 16 10:57:30 2011 @@ -0,0 +1,94 @@ +/**************************************************************** + * 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.smtp.netty; + +import javax.net.ssl.SSLContext; +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.smtp.SMTPConfiguration; +import org.apache.james.protocols.smtp.SMTPResponse; +import org.apache.james.protocols.smtp.SMTPRetCode; +import org.apache.james.protocols.smtp.SMTPSession; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelFutureListener; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelUpstreamHandler; +import org.jboss.netty.channel.ExceptionEvent; +import org.jboss.netty.channel.ChannelHandler.Sharable; +import org.jboss.netty.handler.codec.frame.TooLongFrameException; +import org.slf4j.Logger; + +/** + * {@link ChannelUpstreamHandler} which is used by the SMTPServer + */ +@Sharable +public class SMTPChannelUpstreamHandler extends AbstractChannelUpstreamHandler { + private final Logger logger; + private final SMTPConfiguration conf; + private final SSLContext context; + private String[] enabledCipherSuites; + + public SMTPChannelUpstreamHandler(ProtocolHandlerChain chain, SMTPConfiguration conf, Logger logger) { + this(chain, conf, logger, null, null); + } + + public SMTPChannelUpstreamHandler(ProtocolHandlerChain chain, SMTPConfiguration conf, Logger logger, SSLContext context, String[] enabledCipherSuites) { + super(chain); + this.conf = conf; + this.logger = logger; + this.context = context; + this.enabledCipherSuites = enabledCipherSuites; + } + + @Override + protected ProtocolSession createSession(ChannelHandlerContext ctx) throws Exception { + if (context != null) { + SSLEngine engine = context.createSSLEngine(); + if (enabledCipherSuites != null && enabledCipherSuites.length > 0) { + engine.setEnabledCipherSuites(enabledCipherSuites); + } + return new SMTPNettySession(conf, logger, ctx.getChannel(), engine); + } else { + return new SMTPNettySession(conf, logger, ctx.getChannel()); + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { + Channel channel = ctx.getChannel(); + if (e.getCause() instanceof TooLongFrameException) { + ctx.getChannel().write(new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_COMMAND_UNRECOGNIZED, "Line length exceeded. See RFC 2821 #4.5.3.1.")); + } else { + if (channel.isConnected()) { + ctx.getChannel().write(new SMTPResponse(SMTPRetCode.LOCAL_ERROR, "Unable to process request")).addListener(ChannelFutureListener.CLOSE); + } + SMTPSession smtpSession = (SMTPSession) ctx.getAttachment(); + if (smtpSession != null) { + smtpSession.getLogger().debug("Unable to process request", e.getCause()); + } else { + logger.debug("Unable to process request", e.getCause()); + } + cleanup(ctx); + } + } + +} Propchange: james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPChannelUpstreamHandler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: 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=1171501&view=auto ============================================================================== --- james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPNettySession.java (added) +++ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPNettySession.java Fri Sep 16 10:57:30 2011 @@ -0,0 +1,207 @@ +/**************************************************************** + * 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.smtp.netty; + +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.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; + +/** + * {@link SMTPSession} implementation for use with Netty + */ +public class SMTPNettySession extends AbstractSession implements SMTPSession { + public final static String SMTP_SESSION = "SMTP_SESSION"; + + private boolean relayingAllowed; + + private Map<String, Object> connectionState; + + private SMTPConfiguration theConfigData; + + private int lineHandlerCount = 0; + + public SMTPNettySession(SMTPConfiguration theConfigData, Logger logger, Channel channel, SSLEngine engine) { + super(logger, channel, engine); + this.theConfigData = theConfigData; + connectionState = new HashMap<String, Object>(); + + relayingAllowed = theConfigData.isRelayingAllowed(getRemoteIPAddress()); + } + + public SMTPNettySession(SMTPConfiguration theConfigData, Logger logger, Channel channel) { + this(theConfigData, logger, channel, null); + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#getConnectionState() + */ + public Map<String, Object> getConnectionState() { + return connectionState; + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#getState() + */ + @SuppressWarnings("unchecked") + public Map<String, Object> getState() { + Map<String, Object> res = (Map<String, Object>) getConnectionState().get(SMTPSession.SESSION_STATE_MAP); + if (res == null) { + res = new HashMap<String, Object>(); + getConnectionState().put(SMTPSession.SESSION_STATE_MAP, res); + } + return res; + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#isRelayingAllowed() + */ + public boolean isRelayingAllowed() { + return relayingAllowed; + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#resetState() + */ + public void resetState() { + // remember the ehlo mode between resets + Object currentHeloMode = getState().get(CURRENT_HELO_MODE); + + getState().clear(); + + // start again with the old helo mode + if (currentHeloMode != null) { + getState().put(CURRENT_HELO_MODE, currentHeloMode); + } + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#popLineHandler() + */ + public void popLineHandler() { + if (lineHandlerCount > 0) { + getChannel().getPipeline().remove("lineHandler" + lineHandlerCount); + lineHandlerCount--; + } + } + + /** + * @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)); + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#getHelloName() + */ + public String getHelloName() { + return theConfigData.getHelloName(); + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#getMaxMessageSize() + */ + public long getMaxMessageSize() { + return theConfigData.getMaxMessageSize(); + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#getRcptCount() + */ + @SuppressWarnings("unchecked") + public int getRcptCount() { + int count = 0; + + // check if the key exists + if (getState().get(SMTPSession.RCPT_LIST) != null) { + count = ((Collection) getState().get(SMTPSession.RCPT_LIST)).size(); + } + + return count; + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#getSMTPGreeting() + */ + public String getSMTPGreeting() { + return theConfigData.getSMTPGreeting(); + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#isAuthSupported() + */ + public boolean isAuthSupported() { + return theConfigData.isAuthRequired(socketAddress.getAddress().getHostAddress()); + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#setRelayingAllowed(boolean) + */ + public void setRelayingAllowed(boolean relayingAllowed) { + this.relayingAllowed = relayingAllowed; + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#sleep(long) + */ + public void sleep(long ms) { + // session.getFilterChain().addAfter("connectionFilter", + // "tarpitFilter",new TarpitFilter(ms)); + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#useAddressBracketsEnforcement() + */ + public boolean useAddressBracketsEnforcement() { + return theConfigData.useAddressBracketsEnforcement(); + } + + /** + * @see org.apache.james.protocols.smtp.SMTPSession#useHeloEhloEnforcement() + */ + public boolean useHeloEhloEnforcement() { + return theConfigData.useHeloEhloEnforcement(); + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.james.protocols.smtp.SMTPSession#getPushedLineHandlerCount() + */ + public int getPushedLineHandlerCount() { + return lineHandlerCount; + } + +} Propchange: james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPNettySession.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPResponseEncoder.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPResponseEncoder.java?rev=1171501&view=auto ============================================================================== --- james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPResponseEncoder.java (added) +++ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPResponseEncoder.java Fri Sep 16 10:57:30 2011 @@ -0,0 +1,61 @@ +/**************************************************************** + * 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.smtp.netty; + +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +import org.apache.james.protocols.impl.AbstractResponseEncoder; +import org.apache.james.protocols.smtp.SMTPResponse; +import org.jboss.netty.channel.ChannelHandler.Sharable; + +/** + * {@link AbstractResponseEncoder} which encode {@link SMTPResponse} objects + */ +@Sharable +public class SMTPResponseEncoder extends AbstractResponseEncoder<SMTPResponse> { + + public SMTPResponseEncoder() { + super(SMTPResponse.class, Charset.forName("US-ASCII")); + } + + @Override + protected List<String> getResponse(SMTPResponse response) { + List<String> responseList = new ArrayList<String>(); + + for (int k = 0; k < response.getLines().size(); k++) { + StringBuffer respBuff = new StringBuffer(256); + respBuff.append(response.getRetCode()); + if (k == response.getLines().size() - 1) { + respBuff.append(" "); + respBuff.append(response.getLines().get(k)); + + } else { + respBuff.append("-"); + respBuff.append(response.getLines().get(k)); + + } + responseList.add(respBuff.toString()); + } + + return responseList; + } + +} Propchange: james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPResponseEncoder.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPServer.java URL: http://svn.apache.org/viewvc/james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPServer.java?rev=1171501&view=auto ============================================================================== --- james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPServer.java (added) +++ james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPServer.java Fri Sep 16 10:57:30 2011 @@ -0,0 +1,205 @@ +/**************************************************************** + * 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.smtp.netty; + + +import javax.net.ssl.SSLContext; + +import org.apache.james.protocols.impl.AbstractAsyncServer; +import org.apache.james.protocols.impl.AbstractSSLAwareChannelPipelineFactory; +import org.apache.james.protocols.smtp.SMTPConfiguration; +import org.apache.james.protocols.smtp.SMTPProtocolHandlerChain; +import org.apache.james.protocols.smtp.SMTPServerMBean; +import org.jboss.netty.channel.ChannelPipelineFactory; +import org.jboss.netty.channel.ChannelUpstreamHandler; +import org.jboss.netty.channel.group.ChannelGroup; +import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; +import org.jboss.netty.handler.execution.ExecutionHandler; +import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * NIO SMTPServer which use Netty + */ +public class SMTPServer extends AbstractAsyncServer implements SMTPServerMBean { + + private SMTPProtocolHandlerChain chain; + + private Logger logger = LoggerFactory.getLogger(SMTPServer.class); + + private SSLContext context; + + private boolean starttls; + + private ExecutionHandler eHandler; + + + /** + * The configuration data to be passed to the handler + */ + private final SMTPConfiguration theConfigData; + + + private final static SMTPResponseEncoder SMTP_RESPONSE_ENCODER = new SMTPResponseEncoder(); + private SMTPChannelUpstreamHandler coreHandler; + + + + public SMTPServer(SMTPConfiguration theConfigData, SMTPProtocolHandlerChain chain) { + this(theConfigData, chain, null, false); + } + + + public SMTPServer(SMTPConfiguration theConfigData, SMTPProtocolHandlerChain chain, SSLContext context, boolean starttls) { + super(); + this.chain = chain; + this.context = context; + this.starttls = starttls; + if (context != null && starttls) { + this.theConfigData = new StartTLSSMTPConfiguration(theConfigData); + } else { + this.theConfigData = theConfigData; + } + } + + protected ExecutionHandler createExecutionHandler(int size) { + return new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(size, 0, 0)); + } + + + public void setUseExecutionHandler(boolean useHandler, int size) { + if (isBound()) throw new IllegalStateException("Server running already"); + if (useHandler) { + eHandler =createExecutionHandler(size); + } else { + if (eHandler != null) { + eHandler.releaseExternalResources(); + } + eHandler = null; + } + } + + + protected ChannelUpstreamHandler createCoreHandler() { + return coreHandler; + } + + + /* + * (non-Javadoc) + * @see org.apache.james.protocols.smtp.SMTPServerMBean#isEnabled() + */ + public boolean isEnabled() { + return isBound(); + } + + /* + * (non-Javadoc) + * @see org.apache.james.protocols.smtp.SMTPServerMBean#getSocketType() + */ + public String getSocketType() { + if (context != null && !starttls) { + return "ssl"; + } else { + return "plain"; + } + } + + + @Override + public synchronized void bind() throws Exception { + coreHandler = new SMTPChannelUpstreamHandler(chain, theConfigData, logger, context, null); + super.bind(); + } + + + @Override + protected ChannelPipelineFactory createPipelineFactory(ChannelGroup group) { + return new AbstractSSLAwareChannelPipelineFactory(getTimeout(), 0, getBacklog(), group, eHandler) { + + @Override + protected ChannelUpstreamHandler createHandler() { + return coreHandler; + } + + @Override + protected OneToOneEncoder createEncoder() { + return SMTP_RESPONSE_ENCODER; + } + + @Override + protected boolean isSSLSocket() { + return context != null && !starttls; + } + + @Override + protected SSLContext getSSLContext() { + return context; + } + }; + + } + + private final static class StartTLSSMTPConfiguration implements SMTPConfiguration { + + private SMTPConfiguration config; + + public StartTLSSMTPConfiguration(SMTPConfiguration config) { + this.config = config; + } + + public String getHelloName() { + return config.getHelloName(); + } + + public int getResetLength() { + return config.getResetLength(); + } + + public long getMaxMessageSize() { + return config.getMaxMessageSize(); + } + + public boolean isRelayingAllowed(String remoteIP) { + return config.isRelayingAllowed(remoteIP); + } + + public boolean isAuthRequired(String remoteIP) { + return config.isAuthRequired(remoteIP); + } + + public boolean useHeloEhloEnforcement() { + return config.useHeloEhloEnforcement(); + } + + public String getSMTPGreeting() { + return config.getSMTPGreeting(); + } + + public boolean useAddressBracketsEnforcement() { + return config.useAddressBracketsEnforcement(); + } + + public boolean isStartTLSSupported() { + return true; + } + + } +} Propchange: james/protocols/trunk/smtp-netty/src/main/java/org/apache/james/protocols/smtp/netty/SMTPServer.java ------------------------------------------------------------------------------ svn:mime-type = text/plain --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org