View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.mime4j.message;
21  
22  /**
23   * Utility class for copying message bodies.
24   */
25  public class BodyCopier {
26  
27      private BodyCopier() {
28      }
29  
30      /**
31       * Returns a copy of the given {@link Body} that can be used (and modified)
32       * independently of the original. The copy should be
33       * {@link Disposable#dispose() disposed of} when it is no longer needed.
34       * <p>
35       * The {@link Body#getParent() parent} of the returned copy is
36       * <code>null</code>, that is, the copy is detached from the parent
37       * entity of the original.
38       * 
39       * @param body
40       *            body to copy.
41       * @return a copy of the given body.
42       * @throws UnsupportedOperationException
43       *             if <code>body</code> is an instance of {@link SingleBody}
44       *             that does not support the {@link SingleBody#copy() copy()}
45       *             operation (or contains such a <code>SingleBody</code>).
46       * @throws IllegalArgumentException
47       *             if <code>body</code> is <code>null</code> or
48       *             <code>body</code> is a <code>Body</code> that is neither
49       *             a {@link Message}, {@link Multipart} or {@link SingleBody}
50       *             (or contains such a <code>Body</code>).
51       */
52      public static Body copy(Body body) {
53          if (body == null)
54              throw new IllegalArgumentException("Body is null");
55  
56          if (body instanceof Message)
57              return new Message((Message) body);
58  
59          if (body instanceof Multipart)
60              return new Multipart((Multipart) body);
61  
62          if (body instanceof SingleBody)
63              return ((SingleBody) body).copy();
64  
65          throw new IllegalArgumentException("Unsupported body class");
66      }
67  
68  }