001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package javax.xml.soap;
020:
021: import java.util.Iterator;
022: import java.util.Vector;
023:
024: /**
025: * A container for <CODE>MimeHeader</CODE> objects, which represent the MIME headers present in a
026: * MIME part of a message.</P>
027: * <p/>
028: * <P>This class is used primarily when an application wants to retrieve specific attachments based
029: * on certain MIME headers and values. This class will most likely be used by implementations of
030: * <CODE>AttachmentPart</CODE> and other MIME dependent parts of the JAXM API.
031: *
032: * @see SOAPMessage#getAttachments() SOAPMessage.getAttachments()
033: * @see AttachmentPart AttachmentPart
034: */
035: public class MimeHeaders {
036:
037: class MatchingIterator implements Iterator {
038:
039: private Object nextMatch() {
040:
041: label0: while (iterator.hasNext()) {
042: MimeHeader mimeheader = (MimeHeader) iterator.next();
043:
044: if (names == null) {
045: return match ? null : mimeheader;
046: }
047:
048: for (int i = 0; i < names.length; i++) {
049: if (!mimeheader.getName()
050: .equalsIgnoreCase(names[i])) {
051: continue;
052: }
053:
054: if (match) {
055: return mimeheader;
056: }
057:
058: continue label0;
059: }
060:
061: if (!match) {
062: return mimeheader;
063: }
064: }
065:
066: return null;
067: }
068:
069: public boolean hasNext() {
070:
071: if (nextHeader == null) {
072: nextHeader = nextMatch();
073: }
074:
075: return nextHeader != null;
076: }
077:
078: public Object next() {
079:
080: if (nextHeader != null) {
081: Object obj = nextHeader;
082:
083: nextHeader = null;
084:
085: return obj;
086: }
087:
088: if (hasNext()) {
089: return nextHeader;
090: } else {
091: return null;
092: }
093: }
094:
095: public void remove() {
096: iterator.remove();
097: }
098:
099: private boolean match;
100:
101: private Iterator iterator;
102:
103: private String names[];
104:
105: private Object nextHeader;
106:
107: MatchingIterator(String as[], boolean flag) {
108:
109: match = flag;
110: names = as;
111: iterator = headers.iterator();
112: }
113: }
114:
115: /**
116: * Constructs a default <CODE>MimeHeaders</CODE> object initialized with an empty
117: * <CODE>Vector</CODE> object.
118: */
119: public MimeHeaders() {
120: headers = new Vector();
121: }
122:
123: /**
124: * Returns all of the values for the specified header as an array of <CODE>String</CODE>
125: * objects.
126: *
127: * @param name the name of the header for which values will be returned
128: * @return a <CODE>String</CODE> array with all of the values for the specified header
129: * @see #setHeader(String, String) setHeader(java.lang.String,
130: * java.lang.String)
131: */
132: public String[] getHeader(String name) {
133:
134: Vector vector = new Vector();
135:
136: for (int i = 0; i < headers.size(); i++) {
137: MimeHeader mimeheader = (MimeHeader) headers.elementAt(i);
138:
139: if (mimeheader.getName().equalsIgnoreCase(name)
140: && (mimeheader.getValue() != null)) {
141: vector.addElement(mimeheader.getValue());
142: }
143: }
144:
145: if (vector.size() == 0) {
146: return null;
147: } else {
148: String as[] = new String[vector.size()];
149:
150: vector.copyInto(as);
151:
152: return as;
153: }
154: }
155:
156: /**
157: * Replaces the current value of the first header entry whose name matches the given name with
158: * the given value, adding a new header if no existing header name matches. This method also
159: * removes all matching headers after the first one.
160: * <p/>
161: * <P>Note that RFC822 headers can contain only US-ASCII characters.</P>
162: *
163: * @param name a <CODE>String</CODE> with the name of the header for which to search
164: * @param value a <CODE>String</CODE> with the value that will replace the current value of the
165: * specified header
166: * @throws IllegalArgumentException
167: * if there was a problem in the mime header name or the value being set
168: * @see #getHeader(String) getHeader(java.lang.String)
169: */
170: public void setHeader(String name, String value) {
171:
172: boolean flag = false;
173:
174: if ((name == null) || "".equals(name)) {
175: throw new IllegalArgumentException(
176: "Illegal MimeHeader name");
177: }
178:
179: for (int i = 0; i < headers.size(); i++) {
180: MimeHeader mimeheader = (MimeHeader) headers.elementAt(i);
181:
182: if (mimeheader.getName().equalsIgnoreCase(name)) {
183: if (!flag) {
184: headers.setElementAt(new MimeHeader(mimeheader
185: .getName(), value), i);
186:
187: flag = true;
188: } else {
189: headers.removeElementAt(i--);
190: }
191: }
192: }
193:
194: if (!flag) {
195: addHeader(name, value);
196: }
197: }
198:
199: /**
200: * Adds a <CODE>MimeHeader</CODE> object with the specified name and value to this
201: * <CODE>MimeHeaders</CODE> object's list of headers.
202: * <p/>
203: * <P>Note that RFC822 headers can contain only US-ASCII characters.</P>
204: *
205: * @param name a <CODE>String</CODE> with the name of the header to be added
206: * @param value a <CODE>String</CODE> with the value of the header to be added
207: * @throws IllegalArgumentException
208: * if there was a problem in the mime header name or value being added
209: */
210: public void addHeader(String name, String value) {
211:
212: if ((name == null) || "".equals(name)) {
213: throw new IllegalArgumentException(
214: "Illegal MimeHeader name");
215: }
216:
217: int i = headers.size();
218:
219: for (int j = i - 1; j >= 0; j--) {
220: MimeHeader mimeheader = (MimeHeader) headers.elementAt(j);
221:
222: if (mimeheader.getName().equalsIgnoreCase(name)) {
223: headers.insertElementAt(new MimeHeader(name, value),
224: j + 1);
225:
226: return;
227: }
228: }
229:
230: headers.addElement(new MimeHeader(name, value));
231: }
232:
233: /**
234: * Remove all <CODE>MimeHeader</CODE> objects whose name matches the the given name.
235: *
236: * @param name a <CODE>String</CODE> with the name of the header for which to search
237: */
238: public void removeHeader(String name) {
239:
240: for (int i = 0; i < headers.size(); i++) {
241: MimeHeader mimeheader = (MimeHeader) headers.elementAt(i);
242:
243: if (mimeheader.getName().equalsIgnoreCase(name)) {
244: headers.removeElementAt(i--);
245: }
246: }
247: }
248:
249: /** Removes all the header entries from this <CODE> MimeHeaders</CODE> object. */
250: public void removeAllHeaders() {
251: headers.removeAllElements();
252: }
253:
254: /**
255: * Returns all the headers in this <CODE>MimeHeaders</CODE> object.
256: *
257: * @return an <CODE>Iterator</CODE> object over this <CODE> MimeHeaders</CODE> object's list of
258: * <CODE> MimeHeader</CODE> objects
259: */
260: public Iterator getAllHeaders() {
261: return headers.iterator();
262: }
263:
264: /**
265: * Returns all the <CODE>MimeHeader</CODE> objects whose name matches a name in the given array
266: * of names.
267: *
268: * @param names an array of <CODE>String</CODE> objects with the names for which to search
269: * @return an <CODE>Iterator</CODE> object over the <CODE> MimeHeader</CODE> objects whose name
270: * matches one of the names in the given list
271: */
272: public Iterator getMatchingHeaders(String names[]) {
273: return new MatchingIterator(names, true);
274: }
275:
276: /**
277: * Returns all of the <CODE>MimeHeader</CODE> objects whose name does not match a name in the
278: * given array of names.
279: *
280: * @param names an array of <CODE>String</CODE> objects with the names for which to search
281: * @return an <CODE>Iterator</CODE> object over the <CODE> MimeHeader</CODE> objects whose name
282: * does not match one of the names in the given list
283: */
284: public Iterator getNonMatchingHeaders(String names[]) {
285: return new MatchingIterator(names, false);
286: }
287:
288: // fixme: does this need to be a Vector? Will a non-synchronized impl of
289: // List do?
290: /** A <code>Vector</code> containing the headers as <code>MimeHeader</code> instances. */
291: private Vector headers;
292: }
|