1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.marre.mime;
36
37 import java.util.*;
38
39 /***
40 * Represents a MIME body part.
41 *
42 * A body part can contain headers and a body.
43 *
44 * @author Markus Eriksson
45 * @version $Id: MimeBodyPart.java,v 1.9 2004/11/02 17:59:47 c95men Exp $
46 */
47 public class MimeBodyPart
48 {
49 protected byte[] myBody;
50 protected MimeContentType myContentType;
51
52 protected List myHeaders;
53
54 /***
55 * Creates a new empty MimeBodyPart.
56 */
57 public MimeBodyPart()
58 {
59 myHeaders = new LinkedList();
60 }
61
62 /***
63 * Creates a new empty MimeBodyPart.
64 */
65 public MimeBodyPart(byte[] body, MimeContentType contentType)
66 {
67 this();
68 setContent(body, contentType);
69 }
70
71 /***
72 * Creates a new empty MimeBodyPart.
73 */
74 public MimeBodyPart(byte[] body, String contentType)
75 {
76 this();
77 setContent(body, contentType);
78 }
79
80 /***
81 * Adds a mime header to this body part.
82 *
83 * @param theHeader The header to add
84 */
85 public void addHeader(MimeHeader theHeader)
86 {
87 myHeaders.add(theHeader);
88 }
89
90 /***
91 * Adds a eader to this body part.
92 *
93 * @param theHeaderName The name of the header
94 * @param theHeaderValue The value
95 */
96 public void addHeader(String theHeaderName, String theHeaderValue)
97 {
98 MimeHeader header = getHeader(theHeaderName);
99 if (header != null)
100 {
101 myHeaders.remove(header);
102 header = null;
103 }
104 addHeader(new MimeHeader(theHeaderName, theHeaderValue));
105 }
106
107 /***
108 * Retrieves a header with the given index.
109 *
110 * @param theIndex Index of header to retrieve
111 * @return The header, or null if not found
112 */
113 public MimeHeader getHeader(int theIndex)
114 {
115 return (MimeHeader) myHeaders.get(theIndex);
116 }
117
118 /***
119 * Retrieves a header with the given name.
120 *
121 * @param headerName The name of the header to find
122 * @return The header, or null if not found
123 */
124 public MimeHeader getHeader(String headerName)
125 {
126 Iterator iter = myHeaders.iterator();
127
128 while (iter.hasNext())
129 {
130 MimeHeader header = (MimeHeader) iter.next();
131
132 if (header.getName().equalsIgnoreCase(headerName))
133 {
134 return header;
135 }
136 }
137
138 return null;
139 }
140
141 /***
142 * Returns the number of headers.
143 *
144 * @return The number of headers
145 */
146 public int getHeaderCount()
147 {
148 return myHeaders.size();
149 }
150
151 /***
152 * Sets the main content of this body part.
153 *
154 * @param theContent The main content
155 * @param theContentType The content-type of the content
156 */
157 public void setContent(byte[] theContent, String theContentType)
158 {
159 myBody = new byte[theContent.length];
160 System.arraycopy(theContent, 0, myBody, 0, theContent.length);
161 myContentType = new MimeContentType(theContentType);
162 }
163
164 /***
165 * Sets the main content of this body part.
166 *
167 * @param theContent The main content
168 * @param theContentType The content type
169 */
170 public void setContent(byte[] theContent, MimeContentType theContentType)
171 {
172 myBody = new byte[theContent.length];
173 System.arraycopy(theContent, 0, myBody, 0, theContent.length);
174 myContentType = theContentType;
175 }
176
177 /***
178 * Sets the "Content-Id" header.
179 *
180 * @param theContentId The content-id
181 */
182 public void setContentId(String theContentId)
183 {
184 addHeader("Content-Id", theContentId);
185 }
186
187 /***
188 * Sets the "Content-Location" header.
189 *
190 * @param theContentLocation The content-location
191 */
192 public void setContentLocation(String theContentLocation)
193 {
194 addHeader("Content-Location", theContentLocation);
195 }
196
197 /***
198 * Returns the content of this body part.
199 *
200 * @return The content
201 */
202 public byte[] getBody()
203 {
204 byte[] bodyCopy = null;
205
206 if (myBody != null)
207 {
208 bodyCopy = new byte[myBody.length];
209 System.arraycopy(myBody, 0, bodyCopy, 0, myBody.length);
210 }
211
212 return bodyCopy;
213 }
214
215 /***
216 * Returns the size of the body in this body part.
217 *
218 * @return The size of the body
219 */
220 public int getBodySize()
221 {
222 return myBody.length;
223 }
224
225 /***
226 * Returns the content type.
227 *
228 * @return The content type
229 */
230 public MimeContentType getContentType()
231 {
232 return myContentType;
233 }
234 }