001 /*
002 * $Id: MimeHeaders.java,v 1.4 2004/04/02 01:24:17 ofung Exp $
003 * $Revision: 1.4 $
004 * $Date: 2004/04/02 01:24:17 $
005 */
006
007 /*
008 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
009 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
010 *
011 * This code is free software; you can redistribute it and/or modify it
012 * under the terms of the GNU General Public License version 2 only, as
013 * published by the Free Software Foundation. Sun designates this
014 * particular file as subject to the "Classpath" exception as provided
015 * by Sun in the LICENSE file that accompanied this code.
016 *
017 * This code is distributed in the hope that it will be useful, but WITHOUT
018 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
019 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
020 * version 2 for more details (a copy is included in the LICENSE file that
021 * accompanied this code).
022 *
023 * You should have received a copy of the GNU General Public License version
024 * 2 along with this work; if not, write to the Free Software Foundation,
025 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
026 *
027 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
028 * CA 95054 USA or visit www.sun.com if you need additional information or
029 * have any questions.
030 */
031 package javax.xml.soap;
032
033 import java.util.Iterator;
034 import java.util.Vector;
035
036 /**
037 * A container for <code>MimeHeader</code> objects, which represent
038 * the MIME headers present in a MIME part of a message.
039 *
040 * <p>This class is used primarily when an application wants to
041 * retrieve specific attachments based on certain MIME headers and
042 * values. This class will most likely be used by implementations of
043 * <code>AttachmentPart</code> and other MIME dependent parts of the SAAJ
044 * API.
045 * @see SOAPMessage#getAttachments
046 * @see AttachmentPart
047 */
048 public class MimeHeaders {
049 private Vector headers;
050
051 /**
052 * Constructs a default <code>MimeHeaders</code> object initialized with
053 * an empty <code>Vector</code> object.
054 */
055 public MimeHeaders() {
056 headers = new Vector();
057 }
058
059 /**
060 * Returns all of the values for the specified header as an array of
061 * <code>String</code> objects.
062 *
063 * @param name the name of the header for which values will be returned
064 * @return a <code>String</code> array with all of the values for the
065 * specified header
066 * @see #setHeader
067 */
068 public String[] getHeader(String name) {
069 Vector values = new Vector();
070
071 for (int i = 0; i < headers.size(); i++) {
072 MimeHeader hdr = (MimeHeader) headers.elementAt(i);
073 if (hdr.getName().equalsIgnoreCase(name)
074 && hdr.getValue() != null)
075 values.addElement(hdr.getValue());
076 }
077
078 if (values.size() == 0)
079 return null;
080
081 String r[] = new String[values.size()];
082 values.copyInto(r);
083 return r;
084 }
085
086 /**
087 * Replaces the current value of the first header entry whose name matches
088 * the given name with the given value, adding a new header if no existing header
089 * name matches. This method also removes all matching headers after the first one.
090 * <P>
091 * Note that RFC822 headers can contain only US-ASCII characters.
092 *
093 * @param name a <code>String</code> with the name of the header for
094 * which to search
095 * @param value a <code>String</code> with the value that will replace the
096 * current value of the specified header
097 *
098 * @exception IllegalArgumentException if there was a problem in the
099 * mime header name or the value being set
100 * @see #getHeader
101 */
102 public void setHeader(String name, String value) {
103 boolean found = false;
104
105 if ((name == null) || name.equals(""))
106 throw new IllegalArgumentException(
107 "Illegal MimeHeader name");
108
109 for (int i = 0; i < headers.size(); i++) {
110 MimeHeader hdr = (MimeHeader) headers.elementAt(i);
111 if (hdr.getName().equalsIgnoreCase(name)) {
112 if (!found) {
113 headers.setElementAt(new MimeHeader(hdr.getName(),
114 value), i);
115 found = true;
116 } else
117 headers.removeElementAt(i--);
118 }
119 }
120
121 if (!found)
122 addHeader(name, value);
123 }
124
125 /**
126 * Adds a <code>MimeHeader</code> object with the specified name and value
127 * to this <code>MimeHeaders</code> object's list of headers.
128 * <P>
129 * Note that RFC822 headers can contain only US-ASCII characters.
130 *
131 * @param name a <code>String</code> with the name of the header to
132 * be added
133 * @param value a <code>String</code> with the value of the header to
134 * be added
135 *
136 * @exception IllegalArgumentException if there was a problem in the
137 * mime header name or value being added
138 */
139 public void addHeader(String name, String value) {
140 if ((name == null) || name.equals(""))
141 throw new IllegalArgumentException(
142 "Illegal MimeHeader name");
143
144 int pos = headers.size();
145
146 for (int i = pos - 1; i >= 0; i--) {
147 MimeHeader hdr = (MimeHeader) headers.elementAt(i);
148 if (hdr.getName().equalsIgnoreCase(name)) {
149 headers.insertElementAt(new MimeHeader(name, value),
150 i + 1);
151 return;
152 }
153 }
154 headers.addElement(new MimeHeader(name, value));
155 }
156
157 /**
158 * Remove all <code>MimeHeader</code> objects whose name matches the
159 * given name.
160 *
161 * @param name a <code>String</code> with the name of the header for
162 * which to search
163 */
164 public void removeHeader(String name) {
165 for (int i = 0; i < headers.size(); i++) {
166 MimeHeader hdr = (MimeHeader) headers.elementAt(i);
167 if (hdr.getName().equalsIgnoreCase(name))
168 headers.removeElementAt(i--);
169 }
170 }
171
172 /**
173 * Removes all the header entries from this <code>MimeHeaders</code> object.
174 */
175 public void removeAllHeaders() {
176 headers.removeAllElements();
177 }
178
179 /**
180 * Returns all the <code>MimeHeader</code>s in this <code>MimeHeaders</code> object.
181 *
182 * @return an <code>Iterator</code> object over this <code>MimeHeaders</code>
183 * object's list of <code>MimeHeader</code> objects
184 */
185 public Iterator getAllHeaders() {
186 return headers.iterator();
187 }
188
189 class MatchingIterator implements Iterator {
190 private boolean match;
191 private Iterator iterator;
192 private String[] names;
193 private Object nextHeader;
194
195 MatchingIterator(String[] names, boolean match) {
196 this .match = match;
197 this .names = names;
198 this .iterator = headers.iterator();
199 }
200
201 private Object nextMatch() {
202 next: while (iterator.hasNext()) {
203 MimeHeader hdr = (MimeHeader) 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 public boolean hasNext() {
221 if (nextHeader == null)
222 nextHeader = nextMatch();
223 return nextHeader != null;
224 }
225
226 public Object next() {
227 // hasNext should've prefetched the header for us,
228 // return it.
229 if (nextHeader != null) {
230 Object ret = nextHeader;
231 nextHeader = null;
232 return ret;
233 }
234 if (hasNext())
235 return nextHeader;
236 return null;
237 }
238
239 public void remove() {
240 iterator.remove();
241 }
242 }
243
244 /**
245 * Returns all the <code>MimeHeader</code> objects whose name matches
246 * a name in the given array of names.
247 *
248 * @param names an array of <code>String</code> objects with the names
249 * for which to search
250 * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
251 * objects whose name matches one of the names in the given list
252 */
253 public Iterator getMatchingHeaders(String[] names) {
254 return new MatchingIterator(names, true);
255 }
256
257 /**
258 * Returns all of the <code>MimeHeader</code> objects whose name does not
259 * match a name in the given array of names.
260 *
261 * @param names an array of <code>String</code> objects with the names
262 * for which to search
263 * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
264 * objects whose name does not match one of the names in the given list
265 */
266 public Iterator getNonMatchingHeaders(String[] names) {
267 return new MatchingIterator(names, false);
268 }
269 }
|