View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
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 }