james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r731084 - in /james/mime4j/trunk/src: main/java/org/apache/james/mime4j/message/ test/java/org/apache/james/mime4j/message/
Date Sat, 03 Jan 2009 20:37:16 GMT
Author: mwiederkehr
Date: Sat Jan  3 12:37:16 2009
New Revision: 731084

URL: http://svn.apache.org/viewvc?rev=731084&view=rev
Log:
Added copy constructors for BodyPart, Header, Message and Multipart (MIME4J-94).

Added:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyCopier.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/CopyConstructorTest.java
Modified:
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyPart.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Multipart.java

Added: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyCopier.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyCopier.java?rev=731084&view=auto
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyCopier.java (added)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyCopier.java Sat Jan
 3 12:37:16 2009
@@ -0,0 +1,68 @@
+/****************************************************************
+ * 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.mime4j.message;
+
+/**
+ * Utility class for copying message bodies.
+ */
+public class BodyCopier {
+
+    private BodyCopier() {
+    }
+
+    /**
+     * Returns a copy of the given {@link Body} that can be used (and modified)
+     * independently of the original. The copy should be
+     * {@link Disposable#dispose() disposed of} when it is no longer needed.
+     * <p>
+     * The {@link Body#getParent() parent} of the returned copy is
+     * <code>null</code>, that is, the copy is detached from the parent
+     * entity of the original.
+     * 
+     * @param body
+     *            body to copy.
+     * @return a copy of the given body.
+     * @throws UnsupportedOperationException
+     *             if <code>body</code> is an instance of {@link SingleBody}
+     *             that does not support the {@link SingleBody#copy() copy()}
+     *             operation (or contains such a <code>SingleBody</code>).
+     * @throws IllegalArgumentException
+     *             if <code>body</code> is <code>null</code> or
+     *             <code>body</code> is a <code>Body</code> that
is neither
+     *             a {@link Message}, {@link Multipart} or {@link SingleBody}
+     *             (or contains such a <code>Body</code>).
+     */
+    public static Body copy(Body body) {
+        if (body == null)
+            throw new IllegalArgumentException("Body is null");
+
+        if (body instanceof Message)
+            return new Message((Message) body);
+
+        if (body instanceof Multipart)
+            return new Multipart((Multipart) body);
+
+        if (body instanceof SingleBody)
+            return ((SingleBody) body).copy();
+
+        throw new IllegalArgumentException("Unsupported body class");
+    }
+
+}

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyPart.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyPart.java?rev=731084&r1=731083&r2=731084&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyPart.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/BodyPart.java Sat Jan
 3 12:37:16 2009
@@ -29,4 +29,31 @@
  */
 public class BodyPart extends Entity {
 
+    /**
+     * Creates a new empty <code>BodyPart</code>.
+     */
+    public BodyPart() {
+    }
+
+    /**
+     * Creates a new <code>BodyPart</code> from the specified
+     * <code>BodyPart</code>. The <code>BodyPart</code> instance
is initialized
+     * with copies of header and body of the specified <code>BodyPart</code>.
+     * The parent entity of the new body part is <code>null</code>.
+     * 
+     * @param other
+     *            body part to copy.
+     * @throws UnsupportedOperationException
+     *             if <code>other</code> contains a {@link SingleBody} that
+     *             does not support the {@link SingleBody#copy() copy()}
+     *             operation.
+     * @throws IllegalArgumentException
+     *             if <code>other</code> contains a <code>Body</code>
that
+     *             is neither a {@link Message}, {@link Multipart} or
+     *             {@link SingleBody}.
+     */
+    public BodyPart(BodyPart other) {
+        super(other);
+    }
+
 }

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java?rev=731084&r1=731083&r2=731084&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Entity.java Sat Jan 
3 12:37:16 2009
@@ -41,6 +41,41 @@
     private Entity parent = null;
 
     /**
+     * Creates a new <code>Entity</code>. Typically invoked implicitly by a
+     * subclass constructor.
+     */
+    protected Entity() {
+    }
+
+    /**
+     * Creates a new <code>Entity</code> from the specified
+     * <code>Entity</code>. The <code>Entity</code> instance is initialized
+     * with copies of header and body of the specified <code>Entity</code>.
+     * The parent entity of the new entity is <code>null</code>.
+     * 
+     * @param other
+     *            entity to copy.
+     * @throws UnsupportedOperationException
+     *             if <code>other</code> contains a {@link SingleBody} that
+     *             does not support the {@link SingleBody#copy() copy()}
+     *             operation.
+     * @throws IllegalArgumentException
+     *             if <code>other</code> contains a <code>Body</code>
that
+     *             is neither a {@link Message}, {@link Multipart} or
+     *             {@link SingleBody}.
+     */
+    protected Entity(Entity other) {
+        if (other.header != null) {
+            header = new Header(other.header);
+        }
+
+        if (other.body != null) {
+            Body bodyCopy = BodyCopier.copy(other.body);
+            setBody(bodyCopy);
+        }
+    }
+
+    /**
      * Gets the parent entity of this entity.
      * Returns <code>null</code> if this is the root entity.
      * 

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java?rev=731084&r1=731083&r2=731084&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java Sat Jan 
3 12:37:16 2009
@@ -60,6 +60,22 @@
     }
 
     /**
+     * Creates a new <code>Header</code> from the specified
+     * <code>Header</code>. The <code>Header</code> instance is initialized
+     * with a copy of the list of {@link Field}s of the specified
+     * <code>Header</code>. The <code>Field</code> objects are not
copied
+     * because they are immutable and can safely be shared between headers.
+     * 
+     * @param other
+     *            header to copy.
+     */
+    public Header(Header other) {
+        for (Field otherField : other.fields) {
+            addField(otherField);
+        }
+    }
+
+    /**
      * Creates a new <code>Header</code> from the specified stream.
      * 
      * @param is the stream to read the header from.

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java?rev=731084&r1=731083&r2=731084&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Message.java Sat Jan
 3 12:37:16 2009
@@ -53,6 +53,27 @@
     }
 
     /**
+     * Creates a new <code>Message</code> from the specified
+     * <code>Message</code>. The <code>Message</code> instance is
initialized
+     * with copies of header and body of the specified <code>Message</code>.
+     * The parent entity of the new message is <code>null</code>.
+     * 
+     * @param other
+     *            message to copy.
+     * @throws UnsupportedOperationException
+     *             if <code>other</code> contains a {@link SingleBody} that
+     *             does not support the {@link SingleBody#copy() copy()}
+     *             operation.
+     * @throws IllegalArgumentException
+     *             if <code>other</code> contains a <code>Body</code>
that
+     *             is neither a {@link Message}, {@link Multipart} or
+     *             {@link SingleBody}.
+     */
+    public Message(Message other) {
+        super(other);
+    }
+
+    /**
      * Parses the specified MIME message stream into a <code>Message</code>
      * instance.
      * 

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Multipart.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Multipart.java?rev=731084&r1=731083&r2=731084&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Multipart.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Multipart.java Sat Jan
 3 12:37:16 2009
@@ -60,6 +60,36 @@
     }
 
     /**
+     * Creates a new <code>Multipart</code> from the specified
+     * <code>Multipart</code>. The <code>Multipart</code> instance
is
+     * initialized with copies of preamble, epilogue, sub type and the list of
+     * body parts of the specified <code>Multipart</code>. The parent entity
+     * of the new multipart is <code>null</code>.
+     * 
+     * @param other
+     *            multipart to copy.
+     * @throws UnsupportedOperationException
+     *             if <code>other</code> contains a {@link SingleBody} that
+     *             does not support the {@link SingleBody#copy() copy()}
+     *             operation.
+     * @throws IllegalArgumentException
+     *             if <code>other</code> contains a <code>Body</code>
that
+     *             is neither a {@link Message}, {@link Multipart} or
+     *             {@link SingleBody}.
+     */
+    public Multipart(Multipart other) {
+        preamble = other.preamble;
+        epilogue = other.epilogue;
+        
+        for (BodyPart otherBodyPart : other.bodyParts) {
+            BodyPart bodyPartCopy = new BodyPart(otherBodyPart);
+            addBodyPart(bodyPartCopy);
+        }
+        
+        subType = other.subType;
+    }
+
+    /**
      * Gets the multipart sub-type. E.g. <code>alternative</code> (the default)
      * or <code>parallel</code>. See RFC 2045 for common sub-types and their
      * meaning.

Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/CopyConstructorTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/CopyConstructorTest.java?rev=731084&view=auto
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/CopyConstructorTest.java
(added)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/CopyConstructorTest.java
Sat Jan  3 12:37:16 2009
@@ -0,0 +1,187 @@
+/****************************************************************
+ * 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.mime4j.message;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.james.mime4j.field.Field;
+
+import junit.framework.TestCase;
+
+public class CopyConstructorTest extends TestCase {
+
+    public void testCopyEmptyMessage() throws Exception {
+        Message original = new Message();
+
+        Message copy = new Message(original);
+
+        assertNull(copy.getHeader());
+        assertNull(copy.getBody());
+        assertNull(copy.getParent());
+    }
+
+    public void testCopyMessage() throws Exception {
+        Message parent = new Message();
+        Header header = new Header();
+        Body body = new BodyFactory().textBody("test");
+
+        Message original = new Message();
+        original.setHeader(header);
+        original.setBody(body);
+        original.setParent(parent);
+
+        Message copy = new Message(original);
+
+        assertNotNull(copy.getHeader());
+        assertNotSame(header, copy.getHeader());
+
+        assertNotNull(copy.getBody());
+        assertNotSame(body, copy.getBody());
+
+        assertSame(copy, copy.getBody().getParent());
+
+        assertNull(copy.getParent());
+    }
+
+    public void testCopyEmptyBodyPart() throws Exception {
+        BodyPart original = new BodyPart();
+
+        BodyPart copy = new BodyPart(original);
+
+        assertNull(copy.getHeader());
+        assertNull(copy.getBody());
+        assertNull(copy.getParent());
+    }
+
+    public void testCopyBodyPart() throws Exception {
+        Message parent = new Message();
+        Header header = new Header();
+        Body body = new BodyFactory().textBody("test");
+
+        BodyPart original = new BodyPart();
+        original.setHeader(header);
+        original.setBody(body);
+        original.setParent(parent);
+
+        BodyPart copy = new BodyPart(original);
+
+        assertNotNull(copy.getHeader());
+        assertNotSame(header, copy.getHeader());
+
+        assertNotNull(copy.getBody());
+        assertNotSame(body, copy.getBody());
+
+        assertSame(copy, copy.getBody().getParent());
+
+        assertNull(copy.getParent());
+    }
+
+    public void testCopyEmptyMultipart() throws Exception {
+        Multipart original = new Multipart("mixed");
+
+        Multipart copy = new Multipart(original);
+
+        assertSame(original.getPreamble(), copy.getPreamble());
+        assertSame(original.getEpilogue(), copy.getEpilogue());
+        assertSame(original.getSubType(), copy.getSubType());
+        assertTrue(copy.getBodyParts().isEmpty());
+        assertNull(copy.getParent());
+    }
+
+    public void testCopyMultipart() throws Exception {
+        Message parent = new Message();
+        BodyPart bodyPart = new BodyPart();
+
+        Multipart original = new Multipart("mixed");
+        original.setPreamble("preamble");
+        original.setEpilogue("epilogue");
+        original.setParent(parent);
+        original.addBodyPart(bodyPart);
+
+        Multipart copy = new Multipart(original);
+
+        assertSame(original.getPreamble(), copy.getPreamble());
+        assertSame(original.getEpilogue(), copy.getEpilogue());
+        assertSame(original.getSubType(), copy.getSubType());
+        assertEquals(1, copy.getBodyParts().size());
+        assertNull(copy.getParent());
+
+        BodyPart bodyPartCopy = copy.getBodyParts().iterator().next();
+        assertNotSame(bodyPart, bodyPartCopy);
+
+        assertSame(parent, bodyPart.getParent());
+        assertNull(bodyPartCopy.getParent());
+    }
+
+    public void testCopyMultipartMessage() throws Exception {
+        BodyPart bodyPart1 = new BodyPart();
+        BodyPart bodyPart2 = new BodyPart();
+
+        Multipart multipart = new Multipart("mixed");
+        multipart.addBodyPart(bodyPart1);
+        multipart.addBodyPart(bodyPart2);
+
+        Message original = new Message();
+        original.setHeader(new Header());
+        original.setBody(multipart);
+
+        Message copy = new Message(original);
+
+        Multipart multipartCopy = (Multipart) copy.getBody();
+        List<BodyPart> bodyParts = multipartCopy.getBodyParts();
+        BodyPart bodyPartCopy1 = bodyParts.get(0);
+        BodyPart bodyPartCopy2 = bodyParts.get(1);
+
+        assertNotSame(bodyPart1, bodyPartCopy1);
+        assertEquals(original, bodyPart1.getParent());
+        assertEquals(copy, bodyPartCopy1.getParent());
+
+        assertNotSame(bodyPart2, bodyPartCopy2);
+        assertEquals(original, bodyPart2.getParent());
+        assertEquals(copy, bodyPartCopy2.getParent());
+    }
+
+    public void testCopyHeader() throws Exception {
+        Field f1 = Field.parse("name1: value1");
+        Field f2 = Field.parse("name2: value");
+        Field f3 = Field.parse("name1: value2");
+
+        Header original = new Header();
+        original.addField(f1);
+        original.addField(f2);
+        original.addField(f3);
+
+        Header copy = new Header(original);
+
+        // copy must have same fields as original
+        assertEquals(Arrays.asList(f1, f2, f3), copy.getFields());
+        assertEquals(Arrays.asList(f1, f3), copy.getFields("name1"));
+
+        // modify original
+        original.removeFields("name1");
+        assertEquals(Arrays.asList(f2), original.getFields());
+
+        // copy may not be affected
+        assertEquals(Arrays.asList(f1, f2, f3), copy.getFields());
+        assertEquals(Arrays.asList(f1, f3), copy.getFields("name1"));
+    }
+
+}



---------------------------------------------------------------------
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