Author: norman
Date: Tue Jan 18 20:30:22 2011
New Revision: 1060562

URL: http://svn.apache.org/viewvc?rev=1060562&view=rev
Log:
Some more perfomant way to write the Literal
Add some javadocs

Added:
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelWritableByteChannel.java
Modified:
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelImapResponseWriter.java
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapLineHandlerAdapter.java
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoder.java
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapRequestLineReader.java
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java
    
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NotEnoughDataException.java

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelImapResponseWriter.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelImapResponseWriter.java?rev=1060562&r1=1060561&r2=1060562&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelImapResponseWriter.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelImapResponseWriter.java
 Tue Jan 18 20:30:22 2011
@@ -21,21 +21,25 @@ package org.apache.james.imapserver.nett
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
-import java.nio.channels.Channels;
 import java.nio.channels.WritableByteChannel;
 
 import org.apache.james.imap.main.AbstractImapResponseWriter;
 import org.apache.james.imap.message.response.Literal;
-import org.jboss.netty.buffer.ChannelBuffer;
-import org.jboss.netty.buffer.ChannelBufferOutputStream;
 import org.jboss.netty.buffer.ChannelBuffers;
 import org.jboss.netty.channel.Channel;
 
+/**
+ * {@link AbstractImapResponseWriter} implementation which writes the data to 
a {@link Channel}
+ * 
+ *
+ */
 public class ChannelImapResponseWriter extends AbstractImapResponseWriter{
 
     private Channel channel;
+    private WritableByteChannel wChannel;
     public ChannelImapResponseWriter(Channel channel) {
         this.channel = channel;
+        this.wChannel = new ChannelWritableByteChannel(channel);
     }
     
     /*
@@ -51,31 +55,8 @@ public class ChannelImapResponseWriter e
      * @see 
org.apache.james.imap.main.AbstractImapResponseWriter#write(org.apache.james.imap.message.response.Literal)
      */
     protected void write(Literal literal) throws IOException {
-        ChannelBuffer buf = ChannelBuffers.buffer((int)literal.size());
-        literal.writeTo(Channels.newChannel(new 
ChannelBufferOutputStream(buf)));
-        channel.write(buf);
+        literal.writeTo(wChannel);
     }
 
-    
-    private final class ChannelWritableByteChannel implements 
WritableByteChannel {
-
-        public void close() throws IOException {
-            // do nothing
-        }
-
-        public boolean isOpen() {
-            return channel.isOpen();
-        }
-
-        /*
-         * (non-Javadoc)
-         * @see 
java.nio.channels.WritableByteChannel#write(java.nio.ByteBuffer)
-         */
-        public int write(ByteBuffer src) throws IOException {
-            int size = src.limit();
-            channel.write(ChannelBuffers.wrappedBuffer(src));
-            return size;
-        }
-        
-    }
+   
 }

Added: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelWritableByteChannel.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelWritableByteChannel.java?rev=1060562&view=auto
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelWritableByteChannel.java
 (added)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ChannelWritableByteChannel.java
 Tue Jan 18 20:30:22 2011
@@ -0,0 +1,63 @@
+/****************************************************************
+ * 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.imapserver.netty;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.WritableByteChannel;
+
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.jboss.netty.channel.Channel;
+
+/**
+ * Some adapter class which allows to write to {@link Channel} via a {@link 
WritableByteChannel} interface
+ * 
+ *
+ */
+public class ChannelWritableByteChannel implements WritableByteChannel {
+
+    private final Channel channel;
+
+    public ChannelWritableByteChannel(Channel channel) {
+        this.channel = channel;
+    }
+    
+    public void close() throws IOException {
+        // do nothing
+    }
+
+    public boolean isOpen() {
+        return channel.isOpen();
+    }
+
+    /*
+     * (non-Javadoc)
+     * @see java.nio.channels.WritableByteChannel#write(java.nio.ByteBuffer)
+     */
+    public int write(ByteBuffer src) throws IOException {
+        if (src.remaining() == 0) return 0;
+        byte data[] = new byte[src.remaining()];
+        src.get(data);
+        
+        channel.write(ChannelBuffers.wrappedBuffer(data));
+        return data.length;
+    }
+   
+
+}

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java?rev=1060562&r1=1060561&r2=1060562&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
 Tue Jan 18 20:30:22 2011
@@ -18,9 +18,7 @@
  ****************************************************************/
 package org.apache.james.imapserver.netty;
 
-import java.io.FilterOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.net.InetSocketAddress;
 
 import javax.net.ssl.SSLContext;
@@ -36,18 +34,15 @@ import org.apache.james.imap.encode.base
 import org.apache.james.imap.main.ResponseEncoder;
 import org.apache.james.protocols.impl.ChannelAttributeSupport;
 import org.apache.james.protocols.impl.SessionLog;
-import org.jboss.netty.buffer.ChannelBuffer;
-import org.jboss.netty.buffer.ChannelBuffers;
 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.channel.MessageEvent;
 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
-import org.jboss.netty.handler.ssl.SslHandler;
 
 /**
- * {@link StreamHandler} which handles IMAP
+ * {@link SimpleChannelUpstreamHandler} which handles IMAP
  * 
  *
  */
@@ -165,65 +160,5 @@ public class ImapChannelUpstreamHandler 
         super.messageReceived(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). 
-     * 
-     * Once this was done it just pass the data to the wrapped {@link 
OutputStream} without doing any more buffering
-     *
-     */
-    private final class StartTLSOutputStream extends FilterOutputStream {
-        private int lastChar;
-        private boolean bufferData = false;
-        private final ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
-        
-        public StartTLSOutputStream(OutputStream out) {
-            super(out);   
-        }
-        
-        /**
-         * Buffer the data till the next CLRF was found
-         */
-        public synchronized final void bufferTillCRLF() {
-            bufferData = true;
-        }
-
-        @Override
-        public synchronized void write(byte[] b, int off, int len) throws 
IOException {
-            if (bufferData) {
-                for (int i = off; i < len; i++) {
-                    write(b[i]);
-                }
-            } else {
-                out.write(b, off, len);
-            }
-        }
-
-        @Override
-        public void write(byte[] b) throws IOException {
-            write(b, 0, b.length);
-        }
-
-        @Override
-        public synchronized void write(int b) throws IOException {
-            if (bufferData) {
-                buffer.writeByte((byte)b);
-                // check for CLRF and if found write the data and disable 
buffering
-                if (b == '\n' && lastChar == '\r') {
-                    byte[] line = new byte[buffer.capacity()];
-                    buffer.getBytes(0, line);
-                    out.write(line);
-                    bufferData = false;
-                }
-                lastChar = b;
-
-            } else {
-                out.write(b);
-            }
-        }
-        
-        
-    }
-
 
 }

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapLineHandlerAdapter.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapLineHandlerAdapter.java?rev=1060562&r1=1060561&r2=1060562&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapLineHandlerAdapter.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapLineHandlerAdapter.java
 Tue Jan 18 20:30:22 2011
@@ -26,6 +26,10 @@ import org.jboss.netty.channel.ChannelHa
 import org.jboss.netty.channel.MessageEvent;
 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
 
+/**
+ * {@link SimpleChannelUpstreamHandler} implementation which will delegate the 
data received on {@link #messageReceived(ChannelHandlerContext, MessageEvent)} 
to a {@link ImapLineHandler#onLine(ImapSession, byte[])}
+ *
+ */
 public class ImapLineHandlerAdapter extends SimpleChannelUpstreamHandler 
implements ChannelAttributeSupport{
 
     private ImapLineHandler lineHandler;
@@ -36,7 +40,15 @@ public class ImapLineHandlerAdapter exte
     
     @Override
     public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) 
throws Exception {
-        lineHandler.onLine((ImapSession) attributes.get(ctx.getChannel()), 
((ChannelBuffer)e.getMessage()).array());
+        ChannelBuffer buf = (ChannelBuffer)e.getMessage();
+        byte data[];
+        if (buf.hasArray()) {
+            data = buf.array();
+        } else {
+            data = new byte[buf.readableBytes()];
+            buf.readBytes(data);
+        }
+        lineHandler.onLine((ImapSession) attributes.get(ctx.getChannel()), 
data);
     }
 
 }

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoder.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoder.java?rev=1060562&r1=1060561&r2=1060562&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoder.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/ImapRequestFrameDecoder.java
 Tue Jan 18 20:30:22 2011
@@ -67,7 +67,7 @@ public class ImapRequestFrameDecoder ext
             return message;
         } catch (NotEnoughDataException e) {
             // this exception was thrown because we don't have enough data yet 
-            int neededData = e.getDataSize();
+            int neededData = e.getNeededSize();
             ctx.setAttachment(neededData);
             buffer.resetReaderIndex();
             return null;

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapRequestLineReader.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapRequestLineReader.java?rev=1060562&r1=1060561&r2=1060562&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapRequestLineReader.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapRequestLineReader.java
 Tue Jan 18 20:30:22 2011
@@ -28,6 +28,11 @@ import org.jboss.netty.buffer.ChannelBuf
 import org.jboss.netty.buffer.ChannelBuffers;
 import org.jboss.netty.channel.Channel;
 
+/**
+ * {@link ImapRequestLineReader} implementation which will write to a {@link 
Channel} and read from a {@link ChannelBuffer}. Please
+ * see the docs on {@link #nextChar()} and {@link #read(int)} to understand 
the special behavoir of this implementation
+ *
+ */
 public class NettyImapRequestLineReader extends ImapRequestLineReader{
 
     private ChannelBuffer buffer;
@@ -39,9 +44,12 @@ public class NettyImapRequestLineReader 
         this.channel = channel;
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.imap.decode.ImapRequestLineReader#nextChar()
+
+    /**
+     * Return the next char to read. This will return the same char on every 
call till {@link #consume()} was called.
+     * 
+     * This implementation will throw a {@link NotEnoughDataException} if the 
wrapped {@link ChannelBuffer} contains not enough
+     * data to read the next char
      */
     public char nextChar() throws DecodingException {
         if (!nextSeen) {
@@ -59,9 +67,9 @@ public class NettyImapRequestLineReader 
         return nextChar;
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.james.imap.decode.ImapRequestLineReader#read(int)
+    /**
+     * Return a {@link ChannelBufferInputStream} if the wrapped {@link 
ChannelBuffer} contains enough data. If not
+     * it will throw a {@link NotEnoughDataException} 
      */
     public InputStream read(int size) throws DecodingException {
         if (size > buffer.readableBytes()) {

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java?rev=1060562&r1=1060561&r2=1060562&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NettyImapSession.java
 Tue Jan 18 20:30:22 2011
@@ -141,21 +141,8 @@ public class NettyImapSession implements
      */
     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();
-*/
+        context.getChannel().setReadable(false);           
+
         SslHandler filter = new SslHandler(sslContext.createSSLEngine(), 
false);
         filter.getEngine().setUseClientMode(false);
         if (enabledCipherSuites != null && enabledCipherSuites.length > 0) {
@@ -167,6 +154,7 @@ public class NettyImapSession implements
             context.getPipeline().addAfter("zlibDecoder", "sslHandler", 
filter);
 
         }
+        context.getChannel().setReadable(true);           
 
         return true;
     }
@@ -192,17 +180,9 @@ public class NettyImapSession implements
      * @see org.apache.james.imap.api.process.ImapSession#startCompression()
      */
     public boolean startCompression() {
-        context.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;
-        }  
-        */              
+        if (isCompressionSupported() == false) return false;
+        
+        context.getChannel().setReadable(false);           
         context.getPipeline().addFirst("zlibDecoder", new 
ZlibDecoder(ZlibWrapper.NONE));
         context.getPipeline().addFirst("zlibEncoder", new 
ZlibEncoder(ZlibWrapper.NONE, 5));
 

Modified: 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NotEnoughDataException.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NotEnoughDataException.java?rev=1060562&r1=1060561&r2=1060562&view=diff
==============================================================================
--- 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NotEnoughDataException.java
 (original)
+++ 
james/server/trunk/imapserver/src/main/java/org/apache/james/imapserver/netty/NotEnoughDataException.java
 Tue Jan 18 20:30:22 2011
@@ -19,6 +19,10 @@
 
 package org.apache.james.imapserver.netty;
 
+/**
+ *
+ */
+@SuppressWarnings("serial")
 public class NotEnoughDataException extends RuntimeException{
 
     public final static int UNKNOWN_SIZE = -1;
@@ -31,7 +35,13 @@ public class NotEnoughDataException exte
     public NotEnoughDataException() {
         this(UNKNOWN_SIZE);
     }
-    public int getDataSize() {
+    
+    /**
+     * Return the size of the data which is needed
+     * 
+     * @return size
+     */
+    public int getNeededSize() {
         return size;
     }
 }



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

Reply via email to