KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > soap > MimeHeaders


1 /*
2  * $Id: MimeHeaders.java,v 1.5 2005/04/05 20:49:49 mk125090 Exp $
3  * $Revision: 1.5 $
4  * $Date: 2005/04/05 20:49:49 $
5  */

6
7 /*
8  * The contents of this file are subject to the terms
9  * of the Common Development and Distribution License
10  * (the License). You may not use this file except in
11  * compliance with the License.
12  *
13  * You can obtain a copy of the license at
14  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
15  * See the License for the specific language governing
16  * permissions and limitations under the License.
17  *
18  * When distributing Covered Code, include this CDDL
19  * Header Notice in each file and include the License file
20  * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
21  * If applicable, add the following below the CDDL Header,
22  * with the fields enclosed by brackets [] replaced by
23  * you own identifying information:
24  * "Portions Copyrighted [year] [name of copyright owner]"
25  *
26  * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
27  */

28 package javax.xml.soap;
29
30 import java.util.Iterator JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 /**
34  * A container for <code>MimeHeader</code> objects, which represent
35  * the MIME headers present in a MIME part of a message.
36  *
37  * <p>This class is used primarily when an application wants to
38  * retrieve specific attachments based on certain MIME headers and
39  * values. This class will most likely be used by implementations of
40  * <code>AttachmentPart</code> and other MIME dependent parts of the SAAJ
41  * API.
42  * @see SOAPMessage#getAttachments
43  * @see AttachmentPart
44  */

45 public class MimeHeaders {
46     private Vector JavaDoc headers;
47
48    /**
49     * Constructs a default <code>MimeHeaders</code> object initialized with
50     * an empty <code>Vector</code> object.
51     */

52     public MimeHeaders() {
53         headers = new Vector JavaDoc();
54     }
55
56     /**
57      * Returns all of the values for the specified header as an array of
58      * <code>String</code> objects.
59      *
60      * @param name the name of the header for which values will be returned
61      * @return a <code>String</code> array with all of the values for the
62      * specified header
63      * @see #setHeader
64      */

65     public String JavaDoc[] getHeader(String JavaDoc name) {
66         Vector JavaDoc values = new Vector JavaDoc();
67
68         for(int i = 0; i < headers.size(); i++) {
69             MimeHeader JavaDoc hdr = (MimeHeader JavaDoc) headers.elementAt(i);
70             if (hdr.getName().equalsIgnoreCase(name)
71                 && hdr.getValue() != null)
72                 values.addElement(hdr.getValue());
73         }
74
75         if (values.size() == 0)
76             return null;
77
78         String JavaDoc r[] = new String JavaDoc[values.size()];
79         values.copyInto(r);
80         return r;
81     }
82
83     /**
84      * Replaces the current value of the first header entry whose name matches
85      * the given name with the given value, adding a new header if no existing header
86      * name matches. This method also removes all matching headers after the first one.
87      * <P>
88      * Note that RFC822 headers can contain only US-ASCII characters.
89      *
90      * @param name a <code>String</code> with the name of the header for
91      * which to search
92      * @param value a <code>String</code> with the value that will replace the
93      * current value of the specified header
94      *
95      * @exception IllegalArgumentException if there was a problem in the
96      * mime header name or the value being set
97      * @see #getHeader
98      */

99     public void setHeader(String JavaDoc name, String JavaDoc value)
100     {
101         boolean found = false;
102
103         if ((name == null) || name.equals(""))
104             throw new IllegalArgumentException JavaDoc("Illegal MimeHeader name");
105
106         for(int i = 0; i < headers.size(); i++) {
107             MimeHeader JavaDoc hdr = (MimeHeader JavaDoc) headers.elementAt(i);
108             if (hdr.getName().equalsIgnoreCase(name)) {
109                 if (!found) {
110                     headers.setElementAt(new MimeHeader JavaDoc(hdr.getName(),
111                                                         value), i);
112                     found = true;
113                 }
114                 else
115                     headers.removeElementAt(i--);
116             }
117         }
118
119         if (!found)
120             addHeader(name, value);
121     }
122
123     /**
124      * Adds a <code>MimeHeader</code> object with the specified name and value
125      * to this <code>MimeHeaders</code> object's list of headers.
126      * <P>
127      * Note that RFC822 headers can contain only US-ASCII characters.
128      *
129      * @param name a <code>String</code> with the name of the header to
130      * be added
131      * @param value a <code>String</code> with the value of the header to
132      * be added
133      *
134      * @exception IllegalArgumentException if there was a problem in the
135      * mime header name or value being added
136      */

137     public void addHeader(String JavaDoc name, String JavaDoc value)
138     {
139         if ((name == null) || name.equals(""))
140             throw new IllegalArgumentException JavaDoc("Illegal MimeHeader name");
141
142         int pos = headers.size();
143
144         for(int i = pos - 1 ; i >= 0; i--) {
145             MimeHeader JavaDoc hdr = (MimeHeader JavaDoc) headers.elementAt(i);
146             if (hdr.getName().equalsIgnoreCase(name)) {
147                 headers.insertElementAt(new MimeHeader JavaDoc(name, value),
148                                         i+1);
149                 return;
150             }
151         }
152         headers.addElement(new MimeHeader JavaDoc(name, value));
153     }
154
155     /**
156      * Remove all <code>MimeHeader</code> objects whose name matches the
157      * given name.
158      *
159      * @param name a <code>String</code> with the name of the header for
160      * which to search
161      */

162     public void removeHeader(String JavaDoc name) {
163         for(int i = 0; i < headers.size(); i++) {
164             MimeHeader JavaDoc hdr = (MimeHeader JavaDoc) headers.elementAt(i);
165             if (hdr.getName().equalsIgnoreCase(name))
166                 headers.removeElementAt(i--);
167         }
168     }
169
170     /**
171      * Removes all the header entries from this <code>MimeHeaders</code> object.
172      */

173     public void removeAllHeaders() {
174         headers.removeAllElements();
175     }
176
177
178     /**
179      * Returns all the <code>MimeHeader</code>s in this <code>MimeHeaders</code> object.
180      *
181      * @return an <code>Iterator</code> object over this <code>MimeHeaders</code>
182      * object's list of <code>MimeHeader</code> objects
183      */

184     public Iterator JavaDoc getAllHeaders() {
185         return headers.iterator();
186     }
187
188     class MatchingIterator implements Iterator JavaDoc {
189         private boolean match;
190         private Iterator JavaDoc iterator;
191         private String JavaDoc[] names;
192         private Object JavaDoc nextHeader;
193
194         MatchingIterator(String JavaDoc[] names, boolean match) {
195             this.match = match;
196             this.names = names;
197             this.iterator = headers.iterator();
198         }
199
200         private Object JavaDoc nextMatch() {
201         next:
202             while (iterator.hasNext()) {
203                 MimeHeader JavaDoc hdr = (MimeHeader JavaDoc) iterator.next();
204
205                 if (names == null)
206                     return match ? null : hdr;
207
208                 for(int i = 0; i < names.length; i++)
209                     if (hdr.getName().equalsIgnoreCase(names[i]))
210                         if (match)
211                             return hdr;
212                         else
213                             continue next;
214                 if (!match)
215                     return hdr;
216             }
217             return null;
218         }
219
220
221         public boolean hasNext() {
222             if (nextHeader == null)
223                 nextHeader = nextMatch();
224             return nextHeader != null;
225         }
226
227         public Object JavaDoc next() {
228             // hasNext should've prefetched the header for us,
229
// return it.
230
if (nextHeader != null) {
231                 Object JavaDoc ret = nextHeader;
232                 nextHeader = null;
233                 return ret;
234             }
235             if (hasNext())
236                 return nextHeader;
237             return null;
238         }
239
240         public void remove() {
241             iterator.remove();
242         }
243     }
244
245
246     /**
247      * Returns all the <code>MimeHeader</code> objects whose name matches
248      * a name in the given array of names.
249      *
250      * @param names an array of <code>String</code> objects with the names
251      * for which to search
252      * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
253      * objects whose name matches one of the names in the given list
254      */

255     public Iterator JavaDoc getMatchingHeaders(String JavaDoc[] names) {
256         return new MatchingIterator(names, true);
257     }
258
259     /**
260      * Returns all of the <code>MimeHeader</code> objects whose name does not
261      * match a name in the given array of names.
262      *
263      * @param names an array of <code>String</code> objects with the names
264      * for which to search
265      * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
266      * objects whose name does not match one of the names in the given list
267      */

268     public Iterator JavaDoc getNonMatchingHeaders(String JavaDoc[] names) {
269         return new MatchingIterator(names, false);
270     }
271 }
272
Popular Tags