Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxws/src/share/jaxws_classes/javax/xml/soap/MimeHeaders.java
38890 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.soap;2627import java.util.Iterator;28import java.util.Vector;2930/**31* A container for <code>MimeHeader</code> objects, which represent32* the MIME headers present in a MIME part of a message.33*34* <p>This class is used primarily when an application wants to35* retrieve specific attachments based on certain MIME headers and36* values. This class will most likely be used by implementations of37* <code>AttachmentPart</code> and other MIME dependent parts of the SAAJ38* API.39* @see SOAPMessage#getAttachments40* @see AttachmentPart41*/42public class MimeHeaders {43private Vector headers;4445/**46* Constructs a default <code>MimeHeaders</code> object initialized with47* an empty <code>Vector</code> object.48*/49public MimeHeaders() {50headers = new Vector();51}5253/**54* Returns all of the values for the specified header as an array of55* <code>String</code> objects.56*57* @param name the name of the header for which values will be returned58* @return a <code>String</code> array with all of the values for the59* specified header60* @see #setHeader61*/62public String[] getHeader(String name) {63Vector values = new Vector();6465for(int i = 0; i < headers.size(); i++) {66MimeHeader hdr = (MimeHeader) headers.elementAt(i);67if (hdr.getName().equalsIgnoreCase(name)68&& hdr.getValue() != null)69values.addElement(hdr.getValue());70}7172if (values.size() == 0)73return null;7475String r[] = new String[values.size()];76values.copyInto(r);77return r;78}7980/**81* Replaces the current value of the first header entry whose name matches82* the given name with the given value, adding a new header if no existing header83* name matches. This method also removes all matching headers after the first one.84* <P>85* Note that RFC822 headers can contain only US-ASCII characters.86*87* @param name a <code>String</code> with the name of the header for88* which to search89* @param value a <code>String</code> with the value that will replace the90* current value of the specified header91*92* @exception IllegalArgumentException if there was a problem in the93* mime header name or the value being set94* @see #getHeader95*/96public void setHeader(String name, String value)97{98boolean found = false;99100if ((name == null) || name.equals(""))101throw new IllegalArgumentException("Illegal MimeHeader name");102103for(int i = 0; i < headers.size(); i++) {104MimeHeader hdr = (MimeHeader) headers.elementAt(i);105if (hdr.getName().equalsIgnoreCase(name)) {106if (!found) {107headers.setElementAt(new MimeHeader(hdr.getName(),108value), i);109found = true;110}111else112headers.removeElementAt(i--);113}114}115116if (!found)117addHeader(name, value);118}119120/**121* Adds a <code>MimeHeader</code> object with the specified name and value122* to this <code>MimeHeaders</code> object's list of headers.123* <P>124* Note that RFC822 headers can contain only US-ASCII characters.125*126* @param name a <code>String</code> with the name of the header to127* be added128* @param value a <code>String</code> with the value of the header to129* be added130*131* @exception IllegalArgumentException if there was a problem in the132* mime header name or value being added133*/134public void addHeader(String name, String value)135{136if ((name == null) || name.equals(""))137throw new IllegalArgumentException("Illegal MimeHeader name");138139int pos = headers.size();140141for(int i = pos - 1 ; i >= 0; i--) {142MimeHeader hdr = (MimeHeader) headers.elementAt(i);143if (hdr.getName().equalsIgnoreCase(name)) {144headers.insertElementAt(new MimeHeader(name, value),145i+1);146return;147}148}149headers.addElement(new MimeHeader(name, value));150}151152/**153* Remove all <code>MimeHeader</code> objects whose name matches the154* given name.155*156* @param name a <code>String</code> with the name of the header for157* which to search158*/159public void removeHeader(String name) {160for(int i = 0; i < headers.size(); i++) {161MimeHeader hdr = (MimeHeader) headers.elementAt(i);162if (hdr.getName().equalsIgnoreCase(name))163headers.removeElementAt(i--);164}165}166167/**168* Removes all the header entries from this <code>MimeHeaders</code> object.169*/170public void removeAllHeaders() {171headers.removeAllElements();172}173174175/**176* Returns all the <code>MimeHeader</code>s in this <code>MimeHeaders</code> object.177*178* @return an <code>Iterator</code> object over this <code>MimeHeaders</code>179* object's list of <code>MimeHeader</code> objects180*/181public Iterator getAllHeaders() {182return headers.iterator();183}184185class MatchingIterator implements Iterator {186private boolean match;187private Iterator iterator;188private String[] names;189private Object nextHeader;190191MatchingIterator(String[] names, boolean match) {192this.match = match;193this.names = names;194this.iterator = headers.iterator();195}196197private Object nextMatch() {198next:199while (iterator.hasNext()) {200MimeHeader hdr = (MimeHeader) iterator.next();201202if (names == null)203return match ? null : hdr;204205for(int i = 0; i < names.length; i++)206if (hdr.getName().equalsIgnoreCase(names[i]))207if (match)208return hdr;209else210continue next;211if (!match)212return hdr;213}214return null;215}216217218public boolean hasNext() {219if (nextHeader == null)220nextHeader = nextMatch();221return nextHeader != null;222}223224public Object next() {225// hasNext should've prefetched the header for us,226// return it.227if (nextHeader != null) {228Object ret = nextHeader;229nextHeader = null;230return ret;231}232if (hasNext())233return nextHeader;234return null;235}236237public void remove() {238iterator.remove();239}240}241242243/**244* Returns all the <code>MimeHeader</code> objects whose name matches245* a name in the given array of names.246*247* @param names an array of <code>String</code> objects with the names248* for which to search249* @return an <code>Iterator</code> object over the <code>MimeHeader</code>250* objects whose name matches one of the names in the given list251*/252public Iterator getMatchingHeaders(String[] names) {253return new MatchingIterator(names, true);254}255256/**257* Returns all of the <code>MimeHeader</code> objects whose name does not258* match a name in the given array of names.259*260* @param names an array of <code>String</code> objects with the names261* for which to search262* @return an <code>Iterator</code> object over the <code>MimeHeader</code>263* objects whose name does not match one of the names in the given list264*/265public Iterator getNonMatchingHeaders(String[] names) {266return new MatchingIterator(names, false);267}268}269270271