james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1055588 - in /james/server/trunk/pop3server/src: main/java/org/apache/james/pop3server/core/ test/java/org/apache/james/pop3server/
Date Wed, 05 Jan 2011 19:15:21 GMT
Author: norman
Date: Wed Jan  5 19:15:21 2011
New Revision: 1055588

URL: http://svn.apache.org/viewvc?rev=1055588&view=rev
Log:
Start to work on JAMES-1174 which will ensure that every message ends with CRLF when send
for TOP and RETR command 

Added:
    james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/CRLFTerminatedInputStream.java
    james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ReadByteFilterInputStream.java
    james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java
Modified:
    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/CRLFTerminatedInputStream.java
URL: http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/CRLFTerminatedInputStream.java?rev=1055588&view=auto
==============================================================================
--- james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/CRLFTerminatedInputStream.java
(added)
+++ james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/CRLFTerminatedInputStream.java
Wed Jan  5 19:15:21 2011
@@ -0,0 +1,72 @@
+/****************************************************************
+ * 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;
+
+/**
+ * This {@link FilterInputStream} makes sure that the last chars of the stream are \r\n
+ *
+ */
+public class CRLFTerminatedInputStream extends ReadByteFilterInputStream{
+
+    private int last;
+    private byte[] extraData;
+    private int pos = 0;
+
+    private boolean endOfStream = false;
+    
+    public CRLFTerminatedInputStream(InputStream in) {
+        super(in);
+    }
+
+    @Override
+    public int read() throws IOException {
+        if (endOfStream == false) {
+            int i = super.read();
+            if (i == -1) {
+                endOfStream = true;
+                if (last != '\r') {
+                    extraData = new byte[2];
+                    extraData[0] = '\r';
+                    extraData[1] = '\n';
+                } else if (last != '\n') {
+                    extraData = new byte[1];
+                    extraData[0] = '\r';
+                } else {
+                    extraData = null;
+                }
+
+            } else {
+                last = i;
+            }
+            return i;
+
+        } else {
+            if (extraData == null || extraData.length == pos) {
+                return -1;
+            } else {
+                return extraData[pos++];
+            }
+        }
+    }
+}

Modified: 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=1055588&r1=1055587&r2=1055588&view=diff
==============================================================================
--- james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ExtraDotInputStream.java
(original)
+++ james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ExtraDotInputStream.java
Wed Jan  5 19:15:21 2011
@@ -19,14 +19,13 @@
 
 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{
+public class ExtraDotInputStream extends ReadByteFilterInputStream{
 
     byte[] buf = new byte[3];
     int pos = 0;
@@ -40,30 +39,7 @@ public class ExtraDotInputStream extends
         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;

Added: james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ReadByteFilterInputStream.java
URL: http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ReadByteFilterInputStream.java?rev=1055588&view=auto
==============================================================================
--- james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ReadByteFilterInputStream.java
(added)
+++ james/server/trunk/pop3server/src/main/java/org/apache/james/pop3server/core/ReadByteFilterInputStream.java
Wed Jan  5 19:15:21 2011
@@ -0,0 +1,56 @@
+/****************************************************************
+ * 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;
+
+public class ReadByteFilterInputStream extends FilterInputStream{
+
+    protected ReadByteFilterInputStream(InputStream in) {
+        super(in);
+    }
+
+    @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);
+    }
+
+}

Added: james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java?rev=1055588&view=auto
==============================================================================
--- james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java
(added)
+++ james/server/trunk/pop3server/src/test/java/org/apache/james/pop3server/CRLFTerminatedInputStreamTest.java
Wed Jan  5 19:15:21 2011
@@ -0,0 +1,48 @@
+/****************************************************************
+ * 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.CRLFTerminatedInputStream;
+
+import junit.framework.TestCase;
+
+public class CRLFTerminatedInputStreamTest extends TestCase{
+
+    public void testCRLFPresent() throws IOException {
+        String data = "Subject: test\r\n\r\ndata\r\n";
+        CRLFTerminatedInputStream in = new CRLFTerminatedInputStream(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(data, output);
+    }
+}



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


Mime
View raw message