Author: norman
Date: Mon Jan 17 19:06:28 2011
New Revision: 1060046

URL: http://svn.apache.org/viewvc?rev=1060046&view=rev
Log:
Add support for IMAP COMPRESS extension. See IMAP-247

Modified:
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelOutputStream.java
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/IMAPServer.java
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapStreamChannelUpstreamHandler.java
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/StreamHandler.java

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelOutputStream.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelOutputStream.java?rev=1060046&r1=1060045&r2=1060046&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelOutputStream.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelOutputStream.java
 Mon Jan 17 19:06:28 2011
@@ -90,7 +90,7 @@ public class ChannelOutputStream extends
         lastChannelFuture.awaitUninterruptibly();
         if (!lastChannelFuture.isSuccess()) {
             throw new IOException(
-                    "The bytes could not be written to the session");
+                    "The bytes could not be written to the session", 
lastChannelFuture.getCause());
         }
     }
     

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/IMAPServer.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/IMAPServer.java?rev=1060046&r1=1060045&r2=1060046&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/IMAPServer.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/IMAPServer.java
 Mon Jan 17 19:06:28 2011
@@ -36,6 +36,8 @@ import org.apache.james.protocols.lib.ne
 import org.jboss.netty.channel.ChannelPipeline;
 import org.jboss.netty.channel.ChannelPipelineFactory;
 import org.jboss.netty.channel.group.ChannelGroup;
+import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
+import org.jboss.netty.handler.codec.frame.Delimiters;
 import org.jboss.netty.handler.connection.ConnectionLimitUpstreamHandler;
 import org.jboss.netty.handler.connection.ConnectionPerIpLimitUpstreamHandler;
 import org.jboss.netty.handler.ssl.SslHandler;
@@ -102,7 +104,8 @@ public class IMAPServer extends Abstract
             
             // Timeout of 30 minutes See rfc2060 5.4 for details
             private final static int TIMEOUT = 30 * 60;
-            
+            public final static int MAX_LINE_LENGTH = 8192;
+
             public ChannelPipeline getPipeline() throws Exception {
                 ChannelPipeline pipeline = pipeline();
                 pipeline.addLast("groupHandler", groupHandler);
@@ -111,6 +114,12 @@ public class IMAPServer extends Abstract
 
                 pipeline.addLast("connectionPerIpLimit", new 
ConnectionPerIpLimitUpstreamHandler(IMAPServer.this.connPerIP));
 
+
+                
+                // Add the text line decoder which limit the max line length, 
don't strip the delimiter and use CRLF as delimiter
+                pipeline.addLast("framer", new 
DelimiterBasedFrameDecoder(MAX_LINE_LENGTH, false, Delimiters.lineDelimiter()));
+               
+                
                 if (isSSLSocket()) {
                     // We need to set clientMode to false.
                     // See https://issues.apache.org/jira/browse/JAMES-1025

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapStreamChannelUpstreamHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapStreamChannelUpstreamHandler.java?rev=1060046&r1=1060045&r2=1060046&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapStreamChannelUpstreamHandler.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapStreamChannelUpstreamHandler.java
 Mon Jan 17 19:06:28 2011
@@ -29,7 +29,6 @@ import javax.net.ssl.SSLContext;
 
 import org.apache.commons.logging.Log;
 import org.apache.james.imap.api.ImapConstants;
-import org.apache.james.imap.api.process.ImapSession;
 import org.apache.james.imap.main.ImapRequestStreamHandler;
 import org.apache.james.imap.main.ImapSessionImpl;
 import org.apache.james.protocols.impl.SessionLog;
@@ -39,6 +38,9 @@ import org.jboss.netty.channel.Channel;
 import org.jboss.netty.channel.ChannelHandlerContext;
 import org.jboss.netty.channel.ChannelStateEvent;
 import org.jboss.netty.channel.ExceptionEvent;
+import org.jboss.netty.handler.codec.compression.ZlibDecoder;
+import org.jboss.netty.handler.codec.compression.ZlibEncoder;
+import org.jboss.netty.handler.codec.compression.ZlibWrapper;
 import org.jboss.netty.handler.ssl.SslHandler;
 import org.jboss.netty.util.Timer;
 
@@ -83,10 +85,11 @@ public class ImapStreamChannelUpstreamHa
     protected void processStreamIo(final ChannelHandlerContext ctx, final 
InputStream in, final OutputStream out) {
         final ImapSessionImpl imapSession = (ImapSessionImpl) 
getAttachment(ctx).get(IMAP_SESSION);
         Channel channel = ctx.getChannel();
-        
+       
         // Store the stream as attachment
         OutputStream bufferedOut = new StartTLSOutputStream(out);
         getAttachment(ctx).put(BUFFERED_OUT, bufferedOut);
+
         
         // handle requests in a loop
         while (channel.isConnected() && handler.handleRequest(in, bufferedOut, 
imapSession));
@@ -109,6 +112,16 @@ public class ImapStreamChannelUpstreamHa
             public boolean startTLS() {
                 if (supportStartTLS() == false) return false; 
                 
+              
+                OutputStream out =  
(OutputStream)getAttachment(ctx).get(KEY_OUT);
+                try {
+                    out.flush();
+                } catch (IOException e) {
+                    getLog().info("Unable to start TLS", e);
+                    return false;
+                }
+                
+                
                 // enable buffering of the stream
                 
((StartTLSOutputStream)getAttachment(ctx).get(BUFFERED_OUT)).bufferTillCRLF();
 
@@ -117,7 +130,12 @@ public class ImapStreamChannelUpstreamHa
                 if (enabledCipherSuites != null && enabledCipherSuites.length 
> 0) {
                     
filter.getEngine().setEnabledCipherSuites(enabledCipherSuites);
                 }
-                ctx.getPipeline().addFirst("sslHandler", filter);
+                if (ctx.getPipeline().get("zlibDecoder") == null) {
+                    ctx.getPipeline().addFirst("sslHandler", filter);
+                } else {
+                    ctx.getPipeline().addAfter("zlibDecoder", "sslHandler", 
filter);
+
+                }
 
                 return true;
             }
@@ -126,6 +144,29 @@ public class ImapStreamChannelUpstreamHa
             public boolean supportStartTLS() {
                  return context != null;
             }
+
+            public boolean isCompressionSupported() {
+                return true;
+            }
+
+            @Override
+            public boolean startCompression() {
+                ctx.getChannel().setReadable(false);
+                // enable buffering of the stream
+                OutputStream out =  
(OutputStream)getAttachment(ctx).get(KEY_OUT);
+                try {
+                    out.flush();
+                } catch (IOException e) {
+                    getLog().info("Unable to start compression", e);
+                    return false;
+                }                
+                ctx.getPipeline().addFirst("zlibDecoder", new 
ZlibDecoder(ZlibWrapper.NONE));
+                ctx.getPipeline().addFirst("zlibEncoder", new 
ZlibEncoder(ZlibWrapper.NONE, 5));
+
+                ctx.getChannel().setReadable(true);
+
+                return true;
+            }
             
         };
         imapsession.setLog(getLogger(ctx.getChannel()));
@@ -161,7 +202,7 @@ public class ImapStreamChannelUpstreamHa
         getLogger(ctx.getChannel()).debug("Error while processing imap 
request" ,e.getCause());
         
         // logout on error not sure if that is the best way to handle it
-        final ImapSession imapSession = (ImapSessionImpl) 
getAttachment(ctx).get(IMAP_SESSION);     
+        final ImapSessionImpl imapSession = (ImapSessionImpl) 
getAttachment(ctx).get(IMAP_SESSION);     
         if (imapSession != null) imapSession.logout();
 
         // just close the channel now!
@@ -169,7 +210,7 @@ public class ImapStreamChannelUpstreamHa
         
         super.exceptionCaught(ctx, e);
     }
-
+    
     /**
      * Because Netty {@link SslHandler} need to NOT encrypt the first response 
send to client this {@link FilterOutputStream} is needed. It
      * buffer the data till the complete response was written to the stream 
(searching for the CRLF). 
@@ -229,4 +270,6 @@ public class ImapStreamChannelUpstreamHa
         
         
     }
+
+
 }

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/StreamHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/StreamHandler.java?rev=1060046&r1=1060045&r2=1060046&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/StreamHandler.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/StreamHandler.java
 Mon Jan 17 19:06:28 2011
@@ -48,8 +48,8 @@ public abstract class StreamHandler exte
     
     private final ExecutorService executor;
     
-    private static final String KEY_IN = "stream.in";
-    private static final String KEY_OUT = "stream.out";
+    public static final String KEY_IN = "stream.in";
+    public static final String KEY_OUT = "stream.out";
     
     /**
      *  Create a new Instance which use a cached ThreadPool with no limit to 
perform the stream handling



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to