Author: norman
Date: Wed Nov 24 11:35:25 2010
New Revision: 1038560

URL: http://svn.apache.org/viewvc?rev=1038560&view=rev
Log:
Make sure that RetrCmdHandler and TopCmdHandler will not block the I/O Thread. 
This is done by using session.writeStream(..). See JAMES-1140

Added:
    
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ExtraDotInputStream.java
    
james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java
Modified:
    
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/POP3Session.java
    
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/RetrCmdHandler.java
    
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/TopCmdHandler.java

Modified: 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/POP3Session.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/POP3Session.java?rev=1038560&r1=1038559&r2=1038560&view=diff
==============================================================================
--- 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/POP3Session.java
 (original)
+++ 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/POP3Session.java
 Wed Nov 24 11:35:25 2010
@@ -22,8 +22,6 @@
 package org.apache.james.pop3server;
 
 
-import java.io.OutputStream;
-
 import org.apache.james.mailbox.MessageManager;
 import org.apache.james.protocols.api.TLSSupportedSession;
 
@@ -84,12 +82,5 @@ public interface POP3Session extends TLS
      * @param userMailbox mailbox
      */
     void setUserMailbox(MessageManager mailbox);
-    
-    /**
-     * Get output stream which connects to the underlying socket. This can be 
used to write big bunch of data without keep it all in memory
-     * 
-     * @return out
-     */
-    OutputStream getOutputStream();
 }
 

Added: 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ExtraDotInputStream.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ExtraDotInputStream.java?rev=1038560&view=auto
==============================================================================
--- 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ExtraDotInputStream.java
 (added)
+++ 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ExtraDotInputStream.java
 Wed Nov 24 11:35:25 2010
@@ -0,0 +1,110 @@
+/****************************************************************
+ * 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.pop3server.core;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Adds extra dot if dot occurs in message body at beginning of line 
(according to RFC1939)
+ */
+public class ExtraDotInputStream extends FilterInputStream{
+
+    byte[] buf = new byte[3];
+    int pos = 0;
+    boolean end = false;
+    boolean extraDot = false;
+    boolean startLine;
+    int last;
+    
+    public ExtraDotInputStream(InputStream in) {
+        super(in);
+        startLine = true;
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        int i;
+        for (i = 0; i < len; i++) {
+            int a = read();
+            if (i == 0 && a == - 1) {
+                return -1;
+            } else {
+                if (a == -1) {
+                    break;
+                } else {
+                    b[off++] =  (byte) a;
+                }
+            }
+        }
+        return i;
+        
+    }
+
+    @Override
+    public int read(byte[] b) throws IOException {
+        return read(b, 0, b.length);
+    }
+
+    @Override
+    public synchronized int read() throws IOException {
+        if (end) return -1;
+        
+        if (startLine) {
+            int i = 0;
+            while (i < 3) {
+                buf[i++] = (byte) in.read();
+            }
+            if (buf[0] == '.' && buf[1] == '\r' && buf[2] ==  '\n') {
+                extraDot = true;
+            }
+            startLine = false;
+            pos = 0;
+        }
+        
+        int a;
+        if (pos == -1) {
+            a = in.read();
+        } else {
+            if (extraDot) {
+                extraDot = false;
+                return '.';
+            } else {
+                a = buf[pos++];
+                
+                if (pos == buf.length) {
+                    pos = -1;
+                }
+                if (a == -1) {
+                    end = true;
+                }
+            }
+            
+        }
+        if (last == '\r' && a == '\n') {
+            startLine = true;
+        }
+        last = a;
+        return a;
+       
+    }
+
+}

Modified: 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/RetrCmdHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/RetrCmdHandler.java?rev=1038560&r1=1038559&r2=1038560&view=diff
==============================================================================
--- 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/RetrCmdHandler.java
 (original)
+++ 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/RetrCmdHandler.java
 Wed Nov 24 11:35:25 2010
@@ -19,8 +19,10 @@
 
 package org.apache.james.pop3server.core;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
+import java.io.InputStream;
 import java.nio.channels.Channels;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -29,6 +31,8 @@ import java.util.List;
 
 import javax.mail.MessagingException;
 
+import org.apache.james.mailbox.Content;
+import org.apache.james.mailbox.InputStreamContent;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageRange;
 import org.apache.james.mailbox.MessageResult;
@@ -39,7 +43,6 @@ import org.apache.james.pop3server.POP3S
 import org.apache.james.protocols.api.CommandHandler;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.james.util.stream.ExtraDotOutputStream;
 
 /**
  * Handles RETR command
@@ -72,27 +75,23 @@ public class RetrCmdHandler implements C
                 Long uid = uidList.get(num - 1);
                 if (deletedUidList.contains(uid) == false) {
                     Iterator<MessageResult> results = 
session.getUserMailbox().getMessages(MessageRange.one(uid), new 
FetchGroupImpl(FetchGroup.FULL_CONTENT), mailboxSession);
-                    OutputStream out = session.getOutputStream();
-                    OutputStream extraDotOut = new ExtraDotOutputStream(out);
-                    
-                    out.write((POP3Response.OK_RESPONSE + " Message 
follows\r\n").getBytes());
-                    out.flush();
                     
+                    session.writeStream(new 
ByteArrayInputStream((POP3Response.OK_RESPONSE + " Message 
follows\r\n").getBytes()));
                     // response = new POP3Response(POP3Response.OK_RESPONSE,
                     // "Message follows");
                     try {
                         MessageResult result = results.next();
-                        
result.getFullContent().writeTo(Channels.newChannel(extraDotOut));
-
-                    } finally { 
-                        extraDotOut.flush();
-                        
+                        Content content = result.getFullContent();
+                        InputStream in;
+                        if (content instanceof InputStreamContent) {
+                            in =((InputStreamContent) 
content).getInputStream();
+                        } else {
+                            in = createInputStream(content);
+                        }
+                        session.writeStream(new ExtraDotInputStream(in));
+                    } finally {                         
                         // write a single dot to mark message as complete
-                        out.write((".\r\n").getBytes());
-                        out.flush();
-                        
-                        extraDotOut.close();
-                        out.close();
+                        session.writeStream(new 
ByteArrayInputStream(".\r\n".getBytes()));
                     }
 
                     return null;
@@ -115,6 +114,12 @@ public class RetrCmdHandler implements C
         return response;
     }
 
+    protected InputStream createInputStream(Content content) throws 
IOException {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        content.writeTo(Channels.newChannel(out));
+        return new ByteArrayInputStream(out.toByteArray());
+    }
+    
     /*
      * (non-Javadoc)
      * 

Modified: 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/TopCmdHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/TopCmdHandler.java?rev=1038560&r1=1038559&r2=1038560&view=diff
==============================================================================
--- 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/TopCmdHandler.java
 (original)
+++ 
james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/TopCmdHandler.java
 Wed Nov 24 11:35:25 2010
@@ -19,9 +19,12 @@
 
 package org.apache.james.pop3server.core;
 
-import java.io.FilterOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FilterInputStream;
 import java.io.IOException;
-import java.io.OutputStream;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
 import java.nio.channels.Channels;
 import java.nio.channels.WritableByteChannel;
 import java.util.ArrayList;
@@ -31,6 +34,8 @@ import java.util.List;
 
 import javax.mail.MessagingException;
 
+import org.apache.james.mailbox.Content;
+import org.apache.james.mailbox.InputStreamContent;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageRange;
 import org.apache.james.mailbox.MessageResult;
@@ -41,7 +46,6 @@ import org.apache.james.pop3server.POP3R
 import org.apache.james.pop3server.POP3Session;
 import org.apache.james.protocols.api.Request;
 import org.apache.james.protocols.api.Response;
-import org.apache.james.util.stream.ExtraDotOutputStream;
 
 /**
  * Handles TOP command
@@ -95,38 +99,41 @@ public class TopCmdHandler extends RetrC
                     FetchGroupImpl fetchGroup = new 
FetchGroupImpl(FetchGroup.BODY_CONTENT);
                     fetchGroup.or(FetchGroup.HEADERS);
                     Iterator<MessageResult> results = 
session.getUserMailbox().getMessages(MessageRange.one(uid), fetchGroup, 
mailboxSession);
-                    OutputStream out = session.getOutputStream();
-                    OutputStream extraDotOut = new ExtraDotOutputStream(out);
                     
-                    out.write((POP3Response.OK_RESPONSE + " Message 
follows\r\n").getBytes());
+                    session.writeStream(new 
ByteArrayInputStream((POP3Response.OK_RESPONSE + " Message 
follows\r\n").getBytes()));
                     try {
                         MessageResult result = results.next();
 
-                        WritableByteChannel outChannel = 
Channels.newChannel(extraDotOut);
-
+                        ByteArrayOutputStream headersOut = new 
ByteArrayOutputStream();
+                        WritableByteChannel headersChannel = 
Channels.newChannel(headersOut);
+                        
                         // write headers
                         Iterator<Header> headers = result.headers();
                         while (headers.hasNext()) {
-                            headers.next().writeTo(outChannel);
+                            headers.next().writeTo(headersChannel);
 
                             // we need to write out the CRLF after each header
-                            extraDotOut.write("\r\n".getBytes());
+                            
headersChannel.write(ByteBuffer.wrap("\r\n".getBytes()));
 
                         }
                         // headers and body are seperated by a CRLF
-                        extraDotOut.write("\r\n".getBytes());
-
+                        
headersChannel.write(ByteBuffer.wrap("\r\n".getBytes()));
+                        session.writeStream(new 
ByteArrayInputStream(headersOut.toByteArray()));
+                        
+                        InputStream bodyIn;
+                        Content content = result.getBody();
+                        if (content instanceof InputStreamContent) {
+                            bodyIn = ((InputStreamContent) 
content).getInputStream();
+                        } else {
+                            bodyIn = createInputStream(content);
+                        }
                         // write body
-                        result.getBody().writeTo(Channels.newChannel(new 
CountingBodyOutputStream(extraDotOut, lines)));
+                        session.writeStream(new 
CountingBodyInputStream(bodyIn, lines));
 
                     } finally {
-                        extraDotOut.flush();
                         // write a single dot to mark message as complete
-                        out.write((".\r\n").getBytes());
-                        out.flush();
-                        
-                        extraDotOut.close();
-                        out.close();
+                        session.writeStream(new 
ByteArrayInputStream(".\r\n".getBytes()));
+                       
                     }
 
                     return null;
@@ -174,62 +181,86 @@ public class TopCmdHandler extends RetrC
     }
 
     /**
-     * This OutputStream implementation can be used to limit the body lines
-     * which will be written to the wrapped OutputStream
+     * This {...@link InputStream} implementation can be used to limit the 
body lines
+     * which will be read from the wrapped {...@link InputStream}
      * 
      * 
      * 
      */
-    private final class CountingBodyOutputStream extends FilterOutputStream {
+    private final class CountingBodyInputStream extends FilterInputStream {
 
         private int count = 0;
         private int limit = -1;
-        private char lastChar;
+        private int lastChar;
 
         /**
          * 
-         * @param out
-         *            OutputStream to write to
+         * @param in
+         *            InputStream to read from
          * @param limit
-         *            the lines to write to the outputstream. -1 is used for no
+         *            the lines to read. -1 is used for no
          *            limits
          */
-        public CountingBodyOutputStream(OutputStream out, int limit) {
-            super(out);
+        public CountingBodyInputStream(InputStream in, int limit) {
+            super(in);
             this.limit = limit;
         }
 
         @Override
-        public void write(byte[] b, int off, int len) throws IOException {
-            for (int i = off; i < len; i++) {
-                write(b[i]);
+        public synchronized int read() throws IOException {
+            if (limit != -1) {
+                if (count <= limit) {
+                    int a  = in.read();
+                    
+                    if (lastChar == '\r' && a == '\n') {
+                        count++;
+                    }
+                    lastChar = a;
+                    
+                    return a;
+                } else {
+                    return -1;
+                }
+            } else {
+                return in.read();
             }
+
+            
+            
         }
 
         @Override
-        public void write(byte[] b) throws IOException {
-            for (int i = 0; i < b.length; i++) {
-                write(b[i]);
+        public int read(byte[] b, int off, int len) throws IOException {
+            if (limit == -1) {
+                return in.read(b, off, len);
+            }  else {
+                int i;
+                for (i = 0; i < len; i++) {
+                    int a = read();
+                    if (i == 0 && a == - 1) {
+                        return -1;
+                    } else {
+                        if (a == -1) {
+                            break;
+                        } else {
+                            b[off++] =  (byte) a;
+                        }
+                    }
+                }
+                return i;
             }
         }
 
         @Override
-        public void write(int b) throws IOException {
-
-            if (limit != -1) {
-                if (count <= limit) {
-                    super.write(b);
-                }
+        public int read(byte[] b) throws IOException {
+            if (limit == -1) {
+                return in.read(b);
             } else {
-                super.write(b);
-            }
-
-            if (lastChar == '\r' && b == '\n') {
-                count++;
+                return read(b, 0 , b.length); 
             }
-            lastChar = (char) b;
-
         }
 
+     
+
     }
 }

Added: 
james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java?rev=1038560&view=auto
==============================================================================
--- 
james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java
 (added)
+++ 
james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/ExtraDotInputStreamTest.java
 Wed Nov 24 11:35:25 2010
@@ -0,0 +1,49 @@
+/****************************************************************
+ * 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.pop3server;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.james.pop3server.core.ExtraDotInputStream;
+
+import junit.framework.TestCase;
+
+public class ExtraDotInputStreamTest extends TestCase{
+
+    public void testExtraDot() throws IOException {
+        String data = "This\r\n.\r\nThis.\r\n";
+        String expectedOutput = "This\r\n..\r\nThis.\r\n";
+        ExtraDotInputStream in = new ExtraDotInputStream(new 
ByteArrayInputStream(data.getBytes()));
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        
+        int i = -1;
+        while((i = in.read()) != -1) {
+            out.write(i);
+        }
+        in.close();
+        out.close();
+        
+        String output = new String(out.toByteArray());
+        assertEquals(expectedOutput, output);
+        
+    }
+}



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

Reply via email to