001: /*
002: * $Id: MimeHeaders.java,v 1.6 2006/03/30 00:59:38 ofung Exp $
003: * $Revision: 1.6 $
004: * $Date: 2006/03/30 00:59:38 $
005: */
006:
007: /*
008: * The contents of this file are subject to the terms
009: * of the Common Development and Distribution License
010: * (the License). You may not use this file except in
011: * compliance with the License.
012: *
013: * You can obtain a copy of the license at
014: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * See the License for the specific language governing
016: * permissions and limitations under the License.
017: *
018: * When distributing Covered Code, include this CDDL
019: * Header Notice in each file and include the License file
020: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
021: * If applicable, add the following below the CDDL Header,
022: * with the fields enclosed by brackets [] replaced by
023: * you own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028: package javax.xml.soap;
029:
030: import java.util.Iterator;
031: import java.util.Vector;
032:
033: /**
034: * A container for <code>MimeHeader</code> objects, which represent
035: * the MIME headers present in a MIME part of a message.
036: *
037: * <p>This class is used primarily when an application wants to
038: * retrieve specific attachments based on certain MIME headers and
039: * values. This class will most likely be used by implementations of
040: * <code>AttachmentPart</code> and other MIME dependent parts of the SAAJ
041: * API.
042: * @see SOAPMessage#getAttachments
043: * @see AttachmentPart
044: */
045: public class MimeHeaders {
046: private Vector headers;
047:
048: /**
049: * Constructs a default <code>MimeHeaders</code> object initialized with
050: * an empty <code>Vector</code> object.
051: */
052: public MimeHeaders() {
053: headers = new Vector();
054: }
055:
056: /**
057: * Returns all of the values for the specified header as an array of
058: * <code>String</code> objects.
059: *
060: * @param name the name of the header for which values will be returned
061: * @return a <code>String</code> array with all of the values for the
062: * specified header
063: * @see #setHeader
064: */
065: public String[] getHeader(String name) {
066: Vector values = new Vector();
067:
068: for (int i = 0; i < headers.size(); i++) {
069: MimeHeader hdr = (MimeHeader) headers.elementAt(i);
070: if (hdr.getName().equalsIgnoreCase(name)
071: && hdr.getValue() != null)
072: values.addElement(hdr.getValue());
073: }
074:
075: if (values.size() == 0)
076: return null;
077:
078: String r[] = new String[values.size()];
079: values.copyInto(r);
080: return r;
081: }
082:
083: /**
084: * Replaces the current value of the first header entry whose name matches
085: * the given name with the given value, adding a new header if no existing header
086: * name matches. This method also removes all matching headers after the first one.
087: * <P>
088: * Note that RFC822 headers can contain only US-ASCII characters.
089: *
090: * @param name a <code>String</code> with the name of the header for
091: * which to search
092: * @param value a <code>String</code> with the value that will replace the
093: * current value of the specified header
094: *
095: * @exception IllegalArgumentException if there was a problem in the
096: * mime header name or the value being set
097: * @see #getHeader
098: */
099: public void setHeader(String name, String value) {
100: boolean found = false;
101:
102: if ((name == null) || name.equals(""))
103: throw new IllegalArgumentException(
104: "Illegal MimeHeader name");
105:
106: for (int i = 0; i < headers.size(); i++) {
107: MimeHeader hdr = (MimeHeader) headers.elementAt(i);
108: if (hdr.getName().equalsIgnoreCase(name)) {
109: if (!found) {
110: headers.setElementAt(new MimeHeader(hdr.getName(),
111: value), i);
112: found = true;
113: } else
114: headers.removeElementAt(i--);
115: }
116: }
117:
118: if (!found)
119: addHeader(name, value);
120: }
121:
122: /**
123: * Adds a <code>MimeHeader</code> object with the specified name and value
124: * to this <code>MimeHeaders</code> object's list of headers.
125: * <P>
126: * Note that RFC822 headers can contain only US-ASCII characters.
127: *
128: * @param name a <code>String</code> with the name of the header to
129: * be added
130: * @param value a <code>String</code> with the value of the header to
131: * be added
132: *
133: * @exception IllegalArgumentException if there was a problem in the
134: * mime header name or value being added
135: */
136: public void addHeader(String name, String value) {
137: if ((name == null) || name.equals(""))
138: throw new IllegalArgumentException(
139: "Illegal MimeHeader name");
140:
141: int pos = headers.size();
142:
143: for (int i = pos - 1; i >= 0; i--) {
144: MimeHeader hdr = (MimeHeader) headers.elementAt(i);
145: if (hdr.getName().equalsIgnoreCase(name)) {
146: headers.insertElementAt(new MimeHeader(name, value),
147: i + 1);
148: return;
149: }
150: }
151: headers.addElement(new MimeHeader(name, value));
152: }
153:
154: /**
155: * Remove all <code>MimeHeader</code> objects whose name matches the
156: * given name.
157: *
158: * @param name a <code>String</code> with the name of the header for
159: * which to search
160: */
161: public void removeHeader(String name) {
162: for (int i = 0; i < headers.size(); i++) {
163: MimeHeader hdr = (MimeHeader) headers.elementAt(i);
164: if (hdr.getName().equalsIgnoreCase(name))
165: headers.removeElementAt(i--);
166: }
167: }
168:
169: /**
170: * Removes all the header entries from this <code>MimeHeaders</code> object.
171: */
172: public void removeAllHeaders() {
173: headers.removeAllElements();
174: }
175:
176: /**
177: * Returns all the <code>MimeHeader</code>s in this <code>MimeHeaders</code> object.
178: *
179: * @return an <code>Iterator</code> object over this <code>MimeHeaders</code>
180: * object's list of <code>MimeHeader</code> objects
181: */
182: public Iterator getAllHeaders() {
183: return headers.iterator();
184: }
185:
186: class MatchingIterator implements Iterator {
187: private boolean match;
188: private Iterator iterator;
189: private String[] names;
190: private Object nextHeader;
191:
192: MatchingIterator(String[] names, boolean match) {
193: this .match = match;
194: this .names = names;
195: this .iterator = headers.iterator();
196: }
197:
198: private Object nextMatch() {
199: next: while (iterator.hasNext()) {
200: MimeHeader hdr = (MimeHeader) iterator.next();
201:
202: if (names == null)
203: return match ? null : hdr;
204:
205: for (int i = 0; i < names.length; i++)
206: if (hdr.getName().equalsIgnoreCase(names[i]))
207: if (match)
208: return hdr;
209: else
210: continue next;
211: if (!match)
212: return hdr;
213: }
214: return null;
215: }
216:
217: public boolean hasNext() {
218: if (nextHeader == null)
219: nextHeader = nextMatch();
220: return nextHeader != null;
221: }
222:
223: public Object next() {
224: // hasNext should've prefetched the header for us,
225: // return it.
226: if (nextHeader != null) {
227: Object ret = nextHeader;
228: nextHeader = null;
229: return ret;
230: }
231: if (hasNext())
232: return nextHeader;
233: return null;
234: }
235:
236: public void remove() {
237: iterator.remove();
238: }
239: }
240:
241: /**
242: * Returns all the <code>MimeHeader</code> objects whose name matches
243: * a name in the given array of names.
244: *
245: * @param names an array of <code>String</code> objects with the names
246: * for which to search
247: * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
248: * objects whose name matches one of the names in the given list
249: */
250: public Iterator getMatchingHeaders(String[] names) {
251: return new MatchingIterator(names, true);
252: }
253:
254: /**
255: * Returns all of the <code>MimeHeader</code> objects whose name does not
256: * match a name in the given array of names.
257: *
258: * @param names an array of <code>String</code> objects with the names
259: * for which to search
260: * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
261: * objects whose name does not match one of the names in the given list
262: */
263: public Iterator getNonMatchingHeaders(String[] names) {
264: return new MatchingIterator(names, false);
265: }
266: }
|