325N/A/*
325N/A * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage javax.xml.soap;
325N/A
325N/Aimport java.util.Iterator;
325N/Aimport java.util.Vector;
325N/A
325N/A/**
325N/A * A container for <code>MimeHeader</code> objects, which represent
325N/A * the MIME headers present in a MIME part of a message.
325N/A *
325N/A * <p>This class is used primarily when an application wants to
325N/A * retrieve specific attachments based on certain MIME headers and
325N/A * values. This class will most likely be used by implementations of
325N/A * <code>AttachmentPart</code> and other MIME dependent parts of the SAAJ
325N/A * API.
325N/A * @see SOAPMessage#getAttachments
325N/A * @see AttachmentPart
325N/A */
325N/Apublic class MimeHeaders {
325N/A private Vector headers;
325N/A
325N/A /**
325N/A * Constructs a default <code>MimeHeaders</code> object initialized with
325N/A * an empty <code>Vector</code> object.
325N/A */
325N/A public MimeHeaders() {
325N/A headers = new Vector();
325N/A }
325N/A
325N/A /**
325N/A * Returns all of the values for the specified header as an array of
325N/A * <code>String</code> objects.
325N/A *
325N/A * @param name the name of the header for which values will be returned
325N/A * @return a <code>String</code> array with all of the values for the
325N/A * specified header
325N/A * @see #setHeader
325N/A */
325N/A public String[] getHeader(String name) {
325N/A Vector values = new Vector();
325N/A
325N/A for(int i = 0; i < headers.size(); i++) {
325N/A MimeHeader hdr = (MimeHeader) headers.elementAt(i);
325N/A if (hdr.getName().equalsIgnoreCase(name)
325N/A && hdr.getValue() != null)
325N/A values.addElement(hdr.getValue());
325N/A }
325N/A
325N/A if (values.size() == 0)
325N/A return null;
325N/A
325N/A String r[] = new String[values.size()];
325N/A values.copyInto(r);
325N/A return r;
325N/A }
325N/A
325N/A /**
325N/A * Replaces the current value of the first header entry whose name matches
325N/A * the given name with the given value, adding a new header if no existing header
325N/A * name matches. This method also removes all matching headers after the first one.
325N/A * <P>
325N/A * Note that RFC822 headers can contain only US-ASCII characters.
325N/A *
325N/A * @param name a <code>String</code> with the name of the header for
325N/A * which to search
325N/A * @param value a <code>String</code> with the value that will replace the
325N/A * current value of the specified header
325N/A *
325N/A * @exception IllegalArgumentException if there was a problem in the
325N/A * mime header name or the value being set
325N/A * @see #getHeader
325N/A */
325N/A public void setHeader(String name, String value)
325N/A {
325N/A boolean found = false;
325N/A
325N/A if ((name == null) || name.equals(""))
325N/A throw new IllegalArgumentException("Illegal MimeHeader name");
325N/A
325N/A for(int i = 0; i < headers.size(); i++) {
325N/A MimeHeader hdr = (MimeHeader) headers.elementAt(i);
325N/A if (hdr.getName().equalsIgnoreCase(name)) {
325N/A if (!found) {
325N/A headers.setElementAt(new MimeHeader(hdr.getName(),
325N/A value), i);
325N/A found = true;
325N/A }
325N/A else
325N/A headers.removeElementAt(i--);
325N/A }
325N/A }
325N/A
325N/A if (!found)
325N/A addHeader(name, value);
325N/A }
325N/A
325N/A /**
325N/A * Adds a <code>MimeHeader</code> object with the specified name and value
325N/A * to this <code>MimeHeaders</code> object's list of headers.
325N/A * <P>
325N/A * Note that RFC822 headers can contain only US-ASCII characters.
325N/A *
325N/A * @param name a <code>String</code> with the name of the header to
325N/A * be added
325N/A * @param value a <code>String</code> with the value of the header to
325N/A * be added
325N/A *
325N/A * @exception IllegalArgumentException if there was a problem in the
325N/A * mime header name or value being added
325N/A */
325N/A public void addHeader(String name, String value)
325N/A {
325N/A if ((name == null) || name.equals(""))
325N/A throw new IllegalArgumentException("Illegal MimeHeader name");
325N/A
325N/A int pos = headers.size();
325N/A
325N/A for(int i = pos - 1 ; i >= 0; i--) {
325N/A MimeHeader hdr = (MimeHeader) headers.elementAt(i);
325N/A if (hdr.getName().equalsIgnoreCase(name)) {
325N/A headers.insertElementAt(new MimeHeader(name, value),
325N/A i+1);
325N/A return;
325N/A }
325N/A }
325N/A headers.addElement(new MimeHeader(name, value));
325N/A }
325N/A
325N/A /**
325N/A * Remove all <code>MimeHeader</code> objects whose name matches the
325N/A * given name.
325N/A *
325N/A * @param name a <code>String</code> with the name of the header for
325N/A * which to search
325N/A */
325N/A public void removeHeader(String name) {
325N/A for(int i = 0; i < headers.size(); i++) {
325N/A MimeHeader hdr = (MimeHeader) headers.elementAt(i);
325N/A if (hdr.getName().equalsIgnoreCase(name))
325N/A headers.removeElementAt(i--);
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Removes all the header entries from this <code>MimeHeaders</code> object.
325N/A */
325N/A public void removeAllHeaders() {
325N/A headers.removeAllElements();
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Returns all the <code>MimeHeader</code>s in this <code>MimeHeaders</code> object.
325N/A *
325N/A * @return an <code>Iterator</code> object over this <code>MimeHeaders</code>
325N/A * object's list of <code>MimeHeader</code> objects
325N/A */
325N/A public Iterator getAllHeaders() {
325N/A return headers.iterator();
325N/A }
325N/A
325N/A class MatchingIterator implements Iterator {
325N/A private boolean match;
325N/A private Iterator iterator;
325N/A private String[] names;
325N/A private Object nextHeader;
325N/A
325N/A MatchingIterator(String[] names, boolean match) {
325N/A this.match = match;
325N/A this.names = names;
325N/A this.iterator = headers.iterator();
325N/A }
325N/A
325N/A private Object nextMatch() {
325N/A next:
325N/A while (iterator.hasNext()) {
325N/A MimeHeader hdr = (MimeHeader) iterator.next();
325N/A
325N/A if (names == null)
325N/A return match ? null : hdr;
325N/A
325N/A for(int i = 0; i < names.length; i++)
325N/A if (hdr.getName().equalsIgnoreCase(names[i]))
325N/A if (match)
325N/A return hdr;
325N/A else
325N/A continue next;
325N/A if (!match)
325N/A return hdr;
325N/A }
325N/A return null;
325N/A }
325N/A
325N/A
325N/A public boolean hasNext() {
325N/A if (nextHeader == null)
325N/A nextHeader = nextMatch();
325N/A return nextHeader != null;
325N/A }
325N/A
325N/A public Object next() {
325N/A // hasNext should've prefetched the header for us,
325N/A // return it.
325N/A if (nextHeader != null) {
325N/A Object ret = nextHeader;
325N/A nextHeader = null;
325N/A return ret;
325N/A }
325N/A if (hasNext())
325N/A return nextHeader;
325N/A return null;
325N/A }
325N/A
325N/A public void remove() {
325N/A iterator.remove();
325N/A }
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Returns all the <code>MimeHeader</code> objects whose name matches
325N/A * a name in the given array of names.
325N/A *
325N/A * @param names an array of <code>String</code> objects with the names
325N/A * for which to search
325N/A * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
325N/A * objects whose name matches one of the names in the given list
325N/A */
325N/A public Iterator getMatchingHeaders(String[] names) {
325N/A return new MatchingIterator(names, true);
325N/A }
325N/A
325N/A /**
325N/A * Returns all of the <code>MimeHeader</code> objects whose name does not
325N/A * match a name in the given array of names.
325N/A *
325N/A * @param names an array of <code>String</code> objects with the names
325N/A * for which to search
325N/A * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
325N/A * objects whose name does not match one of the names in the given list
325N/A */
325N/A public Iterator getNonMatchingHeaders(String[] names) {
325N/A return new MatchingIterator(names, false);
325N/A }
325N/A}