geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jboy...@apache.org
Subject svn commit: r154539 - in geronimo/trunk/specs/javamail/src: java/javax/mail/internet/InternetHeaders.java test/javax/mail/internet/InternetHeadersTest.java
Date Sun, 20 Feb 2005 17:59:49 GMT
Author: jboynes
Date: Sun Feb 20 09:59:48 2005
New Revision: 154539

URL: http://svn.apache.org/viewcvs?view=rev&rev=154539
Log:
support load and nonmatching headers

Added:
    geronimo/trunk/specs/javamail/src/test/javax/mail/internet/InternetHeadersTest.java
Modified:
    geronimo/trunk/specs/javamail/src/java/javax/mail/internet/InternetHeaders.java

Modified: geronimo/trunk/specs/javamail/src/java/javax/mail/internet/InternetHeaders.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/specs/javamail/src/java/javax/mail/internet/InternetHeaders.java?view=diff&r1=154538&r2=154539
==============================================================================
--- geronimo/trunk/specs/javamail/src/java/javax/mail/internet/InternetHeaders.java (original)
+++ geronimo/trunk/specs/javamail/src/java/javax/mail/internet/InternetHeaders.java Sun Feb
20 09:59:48 2005
@@ -28,6 +28,8 @@
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.HashSet;
+import java.util.Set;
 import javax.mail.Address;
 import javax.mail.Header;
 import javax.mail.MessagingException;
@@ -97,8 +99,66 @@
      * @throws MessagingException if there is a problem pasring the stream
      */
     public void load(InputStream in) throws MessagingException {
-        // todo implement
-        throw new UnsupportedOperationException();
+        try {
+            StringBuffer name = new StringBuffer(32);
+            StringBuffer value = new StringBuffer(128);
+            done: while (true) {
+                int c = in.read();
+                char ch = (char) c;
+                if (c == -1) {
+                    break;
+                } else if (c == 13) {
+                    // empty line terminates header
+                    in.read(); // skip LF
+                    break;
+                } else if (Character.isWhitespace(ch)) {
+                    // handle continuation
+                    do {
+                        c = in.read();
+                        if (c == -1) {
+                            break done;
+                        }
+                        ch = (char) c;
+                    } while (Character.isWhitespace(ch));
+                }  else {
+                    // new header
+                    if (name.length() > 0) {
+                        addHeader(name.toString().trim(), value.toString().trim());
+                    }
+                    name.setLength(0);
+                    value.setLength(0);
+                    while (true) {
+                        name.append((char)c);
+                        c = in.read();
+                        if (c == -1) {
+                            break done;
+                        } else if (c == ':') {
+                            break;
+                        }
+                    }
+                    c = in.read();
+                    if (c == -1) {
+                        break done;
+                    }
+                }
+
+                while (c != 13) {
+                    ch = (char) c;
+                    value.append(ch);
+                    c = in.read();
+                    if (c == -1) {
+                        break done;
+                    }
+                }
+                // skip LF
+                in.read();
+            }
+            if (name.length() > 0) {
+                addHeader(name.toString().trim(), value.toString().trim());
+            }
+        } catch (IOException e) {
+            throw new MessagingException("Error loading headers", e);
+        }
     }
 
     /**
@@ -172,6 +232,7 @@
         List list = getHeaderList(name);
         if (list == null) {
             list = new ArrayList();
+            headers.put(name.toLowerCase(), list);
         }
         list.add(new InternetHeader(name, value));
     }
@@ -209,8 +270,19 @@
     }
 
     public Enumeration getNonMatchingHeaders(String[] names) {
-        // todo implement
-        throw new UnsupportedOperationException();
+        Set exclude = new HashSet(names.length);
+        for (int i = 0; i < names.length; i++) {
+            String name = names[i];
+            exclude.add(name.toLowerCase());
+        }
+        List result = new ArrayList(headers.size());
+        for (Iterator i = headers.entrySet().iterator(); i.hasNext();) {
+            Map.Entry entry = (Map.Entry) i.next();
+            if (!exclude.contains(((String)entry.getKey()).toLowerCase())) {
+                result.addAll((List)entry.getValue());
+            }
+        }
+        return Collections.enumeration(result);
     }
 
     public void addHeaderLine(String line) {

Added: geronimo/trunk/specs/javamail/src/test/javax/mail/internet/InternetHeadersTest.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/specs/javamail/src/test/javax/mail/internet/InternetHeadersTest.java?view=auto&rev=154539
==============================================================================
--- geronimo/trunk/specs/javamail/src/test/javax/mail/internet/InternetHeadersTest.java (added)
+++ geronimo/trunk/specs/javamail/src/test/javax/mail/internet/InternetHeadersTest.java Sun
Feb 20 09:59:48 2005
@@ -0,0 +1,48 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed 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.
+ */
+
+//
+// This source code implements specifications defined by the Java
+// Community Process. In order to remain compliant with the specification
+// DO NOT add / change / or delete method signatures!
+//
+package javax.mail.internet;
+
+import java.io.ByteArrayInputStream;
+
+import javax.mail.MessagingException;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class InternetHeadersTest extends TestCase {
+    private InternetHeaders headers;
+
+    public void testLoadSingleHeader() throws MessagingException {
+        String stream = "content-type: text/plain\r\n\r\n";
+        headers.load(new ByteArrayInputStream(stream.getBytes()));
+        String[] header = headers.getHeader("content-type");
+        assertNotNull(header);
+        assertEquals("text/plain", header[0]);
+    }
+
+    protected void setUp() throws Exception {
+        headers = new InternetHeaders();
+    }
+}



Mime
View raw message