You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2011/12/16 07:48:58 UTC

svn commit: r1215046 - in /james/protocols/trunk: api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java netty/src/main/java/org/apache/james/protocols/netty/NettyProtocolTransport.java

Author: norman
Date: Fri Dec 16 06:48:58 2011
New Revision: 1215046

URL: http://svn.apache.org/viewvc?rev=1215046&view=rev
Log:
Make sure StartTls is started at the right time in all cases. See PROTOCOLS-54

Modified:
    james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java
    james/protocols/trunk/netty/src/main/java/org/apache/james/protocols/netty/NettyProtocolTransport.java

Modified: james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java?rev=1215046&r1=1215045&r2=1215046&view=diff
==============================================================================
--- james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java (original)
+++ james/protocols/trunk/api/src/main/java/org/apache/james/protocols/api/AbstractProtocolTransport.java Fri Dec 16 06:48:58 2011
@@ -106,19 +106,22 @@ public abstract class AbstractProtocolTr
      */
     protected void writeResponseToClient(Response response, ProtocolSession session) {
         if (response != null) {
-            writeToClient(toBytes(response), session);
-            if (response instanceof StreamResponse) {
-                writeToClient(((StreamResponse) response).getStream(), session);
-            }
-           
+            boolean startTLS = false;
             if (response instanceof StartTlsResponse) {
                 if (isStartTLSSupported()) {
-                    startTLS(session);
-                    session.resetState();
-
+                    startTLS = true;
                 }
             }
             
+            
+            if (response instanceof StreamResponse) {
+                writeToClient(toBytes(response), session, false);
+                writeToClient(((StreamResponse) response).getStream(), session, startTLS);
+            } else {
+                writeToClient(toBytes(response), session, startTLS);
+            }
+            session.resetState();
+            
             if (response.isEndSession()) {
                 // close the channel if needed after the message was written out
                 close();
@@ -153,25 +156,21 @@ public abstract class AbstractProtocolTr
     /**
      * Write the given <code>byte's</code> to the remote peer
      * 
-     * @param bytes
-     * @param session
+     * @param bytes    the bytes to write 
+     * @param session  the {@link ProtocolSession} for the write request
+     * @param startTLS true if startTLS should be started after the bytes were written to the client
      */
-    protected abstract void writeToClient(byte[] bytes, ProtocolSession session);
+    protected abstract void writeToClient(byte[] bytes, ProtocolSession session, boolean startTLS);
     
     /**
      * Write the given {@link InputStream} to the remote peer
      * 
-     * @param in
-     * @param session
+     * @param in       the {@link InputStream} which should be written back to the client
+     * @param session  the {@link ProtocolSession} for the write request
+     * @param startTLS true if startTLS should be started after the {@link InputStream} was written to the client
      */
-    protected abstract void writeToClient(InputStream in, ProtocolSession session);
+    protected abstract void writeToClient(InputStream in, ProtocolSession session, boolean startTLS);
 
-    /**
-     * Start the TLS encrpytion
-     * 
-     * @param session
-     */
-    protected abstract void startTLS(ProtocolSession session);
     
     /**
      * Close the Transport

Modified: james/protocols/trunk/netty/src/main/java/org/apache/james/protocols/netty/NettyProtocolTransport.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/netty/src/main/java/org/apache/james/protocols/netty/NettyProtocolTransport.java?rev=1215046&r1=1215045&r2=1215046&view=diff
==============================================================================
--- james/protocols/trunk/netty/src/main/java/org/apache/james/protocols/netty/NettyProtocolTransport.java (original)
+++ james/protocols/trunk/netty/src/main/java/org/apache/james/protocols/netty/NettyProtocolTransport.java Fri Dec 16 06:48:58 2011
@@ -106,20 +106,22 @@ public class NettyProtocolTransport exte
         return lineHandlerCount;
     }
 
-    
-
-    @Override
-    protected void startTLS(ProtocolSession session) {
-        channel.setReadable(false);
-        SslHandler filter = new SslHandler(engine);
+    /**
+     * Add the {@link SslHandler} to the pipeline and start encrypting after the next written message
+     */
+    private void prepareStartTLS() {
+        SslHandler filter = new SslHandler(engine, true);
         filter.getEngine().setUseClientMode(false);
         channel.getPipeline().addFirst(HandlerConstants.SSL_HANDLER, filter);
-        channel.setReadable(true);        
     }
 
     @Override
-    protected void writeToClient(byte[] bytes, ProtocolSession session) {
+    protected void writeToClient(byte[] bytes, ProtocolSession session, boolean startTLS) {
+        if (startTLS) {
+            prepareStartTLS();
+        }
         channel.write(ChannelBuffers.wrappedBuffer(bytes));
+        
     }
 
     @Override
@@ -129,7 +131,10 @@ public class NettyProtocolTransport exte
 
 
     @Override
-    protected void writeToClient(InputStream in, ProtocolSession session) {
+    protected void writeToClient(InputStream in, ProtocolSession session, boolean startTLS) {
+        if (startTLS) {
+            prepareStartTLS();
+        }
         channel.write(new ChunkedStream(in));
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org