[6785] | 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 | import java.util.Collections; |
---|
| 23 | import java.util.LinkedList; |
---|
| 24 | import java.util.List; |
---|
| 25 | |
---|
| 26 | import org.apache.james.mime4j.dom.Entity; |
---|
| 27 | import org.apache.james.mime4j.dom.Multipart; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Represents a MIME multipart body (see RFC 2045).A multipart body has a |
---|
| 31 | * ordered list of body parts. The multipart body also has a preamble and |
---|
| 32 | * epilogue. The preamble consists of whatever characters appear before the |
---|
| 33 | * first body part while the epilogue consists of whatever characters come after |
---|
| 34 | * the last body part. |
---|
| 35 | */ |
---|
| 36 | public abstract class AbstractMultipart implements Multipart { |
---|
| 37 | |
---|
| 38 | protected List<Entity> bodyParts = new LinkedList<Entity>(); |
---|
| 39 | private Entity parent = null; |
---|
| 40 | |
---|
| 41 | private String subType; |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Creates a new empty <code>Multipart</code> instance. |
---|
| 45 | */ |
---|
| 46 | public AbstractMultipart(String subType) { |
---|
| 47 | this.subType = subType; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Gets the multipart sub-type. E.g. <code>alternative</code> (the |
---|
| 52 | * default) or <code>parallel</code>. See RFC 2045 for common sub-types |
---|
| 53 | * and their meaning. |
---|
| 54 | * |
---|
| 55 | * @return the multipart sub-type. |
---|
| 56 | */ |
---|
| 57 | public String getSubType() { |
---|
| 58 | return subType; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * Sets the multipart sub-type. E.g. <code>alternative</code> or |
---|
| 63 | * <code>parallel</code>. See RFC 2045 for common sub-types and their |
---|
| 64 | * meaning. |
---|
| 65 | * |
---|
| 66 | * @param subType |
---|
| 67 | * the sub-type. |
---|
| 68 | */ |
---|
| 69 | public void setSubType(String subType) { |
---|
| 70 | this.subType = subType; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * @see org.apache.james.mime4j.dom.Body#getParent() |
---|
| 75 | */ |
---|
| 76 | public Entity getParent() { |
---|
| 77 | return parent; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * @see org.apache.james.mime4j.dom.Body#setParent(org.apache.james.mime4j.dom.Entity) |
---|
| 82 | */ |
---|
| 83 | public void setParent(Entity parent) { |
---|
| 84 | this.parent = parent; |
---|
| 85 | for (Entity bodyPart : bodyParts) { |
---|
| 86 | bodyPart.setParent(parent); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Returns the number of body parts. |
---|
| 92 | * |
---|
| 93 | * @return number of <code>Entity</code> objects. |
---|
| 94 | */ |
---|
| 95 | public int getCount() { |
---|
| 96 | return bodyParts.size(); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Gets the list of body parts. The list is immutable. |
---|
| 101 | * |
---|
| 102 | * @return the list of <code>Entity</code> objects. |
---|
| 103 | */ |
---|
| 104 | public List<Entity> getBodyParts() { |
---|
| 105 | return Collections.unmodifiableList(bodyParts); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Sets the list of body parts. |
---|
| 110 | * |
---|
| 111 | * @param bodyParts |
---|
| 112 | * the new list of <code>Entity</code> objects. |
---|
| 113 | */ |
---|
| 114 | public void setBodyParts(List<Entity> bodyParts) { |
---|
| 115 | this.bodyParts = bodyParts; |
---|
| 116 | for (Entity bodyPart : bodyParts) { |
---|
| 117 | bodyPart.setParent(parent); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * Adds a body part to the end of the list of body parts. |
---|
| 123 | * |
---|
| 124 | * @param bodyPart |
---|
| 125 | * the body part. |
---|
| 126 | */ |
---|
| 127 | public void addBodyPart(Entity bodyPart) { |
---|
| 128 | if (bodyPart == null) |
---|
| 129 | throw new IllegalArgumentException(); |
---|
| 130 | |
---|
| 131 | bodyParts.add(bodyPart); |
---|
| 132 | bodyPart.setParent(parent); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * Inserts a body part at the specified position in the list of body parts. |
---|
| 137 | * |
---|
| 138 | * @param bodyPart |
---|
| 139 | * the body part. |
---|
| 140 | * @param index |
---|
| 141 | * index at which the specified body part is to be inserted. |
---|
| 142 | * @throws IndexOutOfBoundsException |
---|
| 143 | * if the index is out of range (index < 0 || index > |
---|
| 144 | * getCount()). |
---|
| 145 | */ |
---|
| 146 | public void addBodyPart(Entity bodyPart, int index) { |
---|
| 147 | if (bodyPart == null) |
---|
| 148 | throw new IllegalArgumentException(); |
---|
| 149 | |
---|
| 150 | bodyParts.add(index, bodyPart); |
---|
| 151 | bodyPart.setParent(parent); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * Removes the body part at the specified position in the list of body |
---|
| 156 | * parts. |
---|
| 157 | * |
---|
| 158 | * @param index |
---|
| 159 | * index of the body part to be removed. |
---|
| 160 | * @return the removed body part. |
---|
| 161 | * @throws IndexOutOfBoundsException |
---|
| 162 | * if the index is out of range (index < 0 || index >= |
---|
| 163 | * getCount()). |
---|
| 164 | */ |
---|
| 165 | public Entity removeBodyPart(int index) { |
---|
| 166 | Entity bodyPart = bodyParts.remove(index); |
---|
| 167 | bodyPart.setParent(null); |
---|
| 168 | return bodyPart; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * Replaces the body part at the specified position in the list of body |
---|
| 173 | * parts with the specified body part. |
---|
| 174 | * |
---|
| 175 | * @param bodyPart |
---|
| 176 | * body part to be stored at the specified position. |
---|
| 177 | * @param index |
---|
| 178 | * index of body part to replace. |
---|
| 179 | * @return the replaced body part. |
---|
| 180 | * @throws IndexOutOfBoundsException |
---|
| 181 | * if the index is out of range (index < 0 || index >= |
---|
| 182 | * getCount()). |
---|
| 183 | */ |
---|
| 184 | public Entity replaceBodyPart(Entity bodyPart, int index) { |
---|
| 185 | if (bodyPart == null) |
---|
| 186 | throw new IllegalArgumentException(); |
---|
| 187 | |
---|
| 188 | Entity replacedEntity = bodyParts.set(index, bodyPart); |
---|
| 189 | if (bodyPart == replacedEntity) |
---|
| 190 | throw new IllegalArgumentException( |
---|
| 191 | "Cannot replace body part with itself"); |
---|
| 192 | |
---|
| 193 | bodyPart.setParent(parent); |
---|
| 194 | replacedEntity.setParent(null); |
---|
| 195 | |
---|
| 196 | return replacedEntity; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * Gets the preamble or null if the message has no preamble. |
---|
| 201 | * |
---|
| 202 | * @return the preamble. |
---|
| 203 | */ |
---|
| 204 | public abstract String getPreamble(); |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * Sets the preamble with a value or null to remove the preamble. |
---|
| 208 | * |
---|
| 209 | * @param preamble |
---|
| 210 | * the preamble. |
---|
| 211 | */ |
---|
| 212 | public abstract void setPreamble(String preamble); |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | * Gets the epilogue or null if the message has no epilogue |
---|
| 216 | * |
---|
| 217 | * @return the epilogue. |
---|
| 218 | */ |
---|
| 219 | public abstract String getEpilogue(); |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | * Sets the epilogue value, or remove it if the value passed is null. |
---|
| 223 | * |
---|
| 224 | * @param epilogue |
---|
| 225 | * the epilogue. |
---|
| 226 | */ |
---|
| 227 | public abstract void setEpilogue(String epilogue); |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * Disposes of the BodyParts of this Multipart. Note that the dispose call |
---|
| 231 | * does not get forwarded to the parent entity of this Multipart. |
---|
| 232 | * |
---|
| 233 | * @see org.apache.james.mime4j.dom.Disposable#dispose() |
---|
| 234 | */ |
---|
| 235 | public void dispose() { |
---|
| 236 | for (Entity bodyPart : bodyParts) { |
---|
| 237 | bodyPart.dispose(); |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | } |
---|