src/share/jaxws_classes/javax/xml/soap/MimeHeaders.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package javax.xml.soap;
ohair@286 27
ohair@286 28 import java.util.Iterator;
ohair@286 29 import java.util.Vector;
ohair@286 30
ohair@286 31 /**
ohair@286 32 * A container for <code>MimeHeader</code> objects, which represent
ohair@286 33 * the MIME headers present in a MIME part of a message.
ohair@286 34 *
ohair@286 35 * <p>This class is used primarily when an application wants to
ohair@286 36 * retrieve specific attachments based on certain MIME headers and
ohair@286 37 * values. This class will most likely be used by implementations of
ohair@286 38 * <code>AttachmentPart</code> and other MIME dependent parts of the SAAJ
ohair@286 39 * API.
ohair@286 40 * @see SOAPMessage#getAttachments
ohair@286 41 * @see AttachmentPart
ohair@286 42 */
ohair@286 43 public class MimeHeaders {
ohair@286 44 private Vector headers;
ohair@286 45
ohair@286 46 /**
ohair@286 47 * Constructs a default <code>MimeHeaders</code> object initialized with
ohair@286 48 * an empty <code>Vector</code> object.
ohair@286 49 */
ohair@286 50 public MimeHeaders() {
ohair@286 51 headers = new Vector();
ohair@286 52 }
ohair@286 53
ohair@286 54 /**
ohair@286 55 * Returns all of the values for the specified header as an array of
ohair@286 56 * <code>String</code> objects.
ohair@286 57 *
ohair@286 58 * @param name the name of the header for which values will be returned
ohair@286 59 * @return a <code>String</code> array with all of the values for the
ohair@286 60 * specified header
ohair@286 61 * @see #setHeader
ohair@286 62 */
ohair@286 63 public String[] getHeader(String name) {
ohair@286 64 Vector values = new Vector();
ohair@286 65
ohair@286 66 for(int i = 0; i < headers.size(); i++) {
ohair@286 67 MimeHeader hdr = (MimeHeader) headers.elementAt(i);
ohair@286 68 if (hdr.getName().equalsIgnoreCase(name)
ohair@286 69 && hdr.getValue() != null)
ohair@286 70 values.addElement(hdr.getValue());
ohair@286 71 }
ohair@286 72
ohair@286 73 if (values.size() == 0)
ohair@286 74 return null;
ohair@286 75
ohair@286 76 String r[] = new String[values.size()];
ohair@286 77 values.copyInto(r);
ohair@286 78 return r;
ohair@286 79 }
ohair@286 80
ohair@286 81 /**
ohair@286 82 * Replaces the current value of the first header entry whose name matches
ohair@286 83 * the given name with the given value, adding a new header if no existing header
ohair@286 84 * name matches. This method also removes all matching headers after the first one.
ohair@286 85 * <P>
ohair@286 86 * Note that RFC822 headers can contain only US-ASCII characters.
ohair@286 87 *
ohair@286 88 * @param name a <code>String</code> with the name of the header for
ohair@286 89 * which to search
ohair@286 90 * @param value a <code>String</code> with the value that will replace the
ohair@286 91 * current value of the specified header
ohair@286 92 *
ohair@286 93 * @exception IllegalArgumentException if there was a problem in the
ohair@286 94 * mime header name or the value being set
ohair@286 95 * @see #getHeader
ohair@286 96 */
ohair@286 97 public void setHeader(String name, String value)
ohair@286 98 {
ohair@286 99 boolean found = false;
ohair@286 100
ohair@286 101 if ((name == null) || name.equals(""))
ohair@286 102 throw new IllegalArgumentException("Illegal MimeHeader name");
ohair@286 103
ohair@286 104 for(int i = 0; i < headers.size(); i++) {
ohair@286 105 MimeHeader hdr = (MimeHeader) headers.elementAt(i);
ohair@286 106 if (hdr.getName().equalsIgnoreCase(name)) {
ohair@286 107 if (!found) {
ohair@286 108 headers.setElementAt(new MimeHeader(hdr.getName(),
ohair@286 109 value), i);
ohair@286 110 found = true;
ohair@286 111 }
ohair@286 112 else
ohair@286 113 headers.removeElementAt(i--);
ohair@286 114 }
ohair@286 115 }
ohair@286 116
ohair@286 117 if (!found)
ohair@286 118 addHeader(name, value);
ohair@286 119 }
ohair@286 120
ohair@286 121 /**
ohair@286 122 * Adds a <code>MimeHeader</code> object with the specified name and value
ohair@286 123 * to this <code>MimeHeaders</code> object's list of headers.
ohair@286 124 * <P>
ohair@286 125 * Note that RFC822 headers can contain only US-ASCII characters.
ohair@286 126 *
ohair@286 127 * @param name a <code>String</code> with the name of the header to
ohair@286 128 * be added
ohair@286 129 * @param value a <code>String</code> with the value of the header to
ohair@286 130 * be added
ohair@286 131 *
ohair@286 132 * @exception IllegalArgumentException if there was a problem in the
ohair@286 133 * mime header name or value being added
ohair@286 134 */
ohair@286 135 public void addHeader(String name, String value)
ohair@286 136 {
ohair@286 137 if ((name == null) || name.equals(""))
ohair@286 138 throw new IllegalArgumentException("Illegal MimeHeader name");
ohair@286 139
ohair@286 140 int pos = headers.size();
ohair@286 141
ohair@286 142 for(int i = pos - 1 ; i >= 0; i--) {
ohair@286 143 MimeHeader hdr = (MimeHeader) headers.elementAt(i);
ohair@286 144 if (hdr.getName().equalsIgnoreCase(name)) {
ohair@286 145 headers.insertElementAt(new MimeHeader(name, value),
ohair@286 146 i+1);
ohair@286 147 return;
ohair@286 148 }
ohair@286 149 }
ohair@286 150 headers.addElement(new MimeHeader(name, value));
ohair@286 151 }
ohair@286 152
ohair@286 153 /**
ohair@286 154 * Remove all <code>MimeHeader</code> objects whose name matches the
ohair@286 155 * given name.
ohair@286 156 *
ohair@286 157 * @param name a <code>String</code> with the name of the header for
ohair@286 158 * which to search
ohair@286 159 */
ohair@286 160 public void removeHeader(String name) {
ohair@286 161 for(int i = 0; i < headers.size(); i++) {
ohair@286 162 MimeHeader hdr = (MimeHeader) headers.elementAt(i);
ohair@286 163 if (hdr.getName().equalsIgnoreCase(name))
ohair@286 164 headers.removeElementAt(i--);
ohair@286 165 }
ohair@286 166 }
ohair@286 167
ohair@286 168 /**
ohair@286 169 * Removes all the header entries from this <code>MimeHeaders</code> object.
ohair@286 170 */
ohair@286 171 public void removeAllHeaders() {
ohair@286 172 headers.removeAllElements();
ohair@286 173 }
ohair@286 174
ohair@286 175
ohair@286 176 /**
ohair@286 177 * Returns all the <code>MimeHeader</code>s in this <code>MimeHeaders</code> object.
ohair@286 178 *
ohair@286 179 * @return an <code>Iterator</code> object over this <code>MimeHeaders</code>
ohair@286 180 * object's list of <code>MimeHeader</code> objects
ohair@286 181 */
ohair@286 182 public Iterator getAllHeaders() {
ohair@286 183 return headers.iterator();
ohair@286 184 }
ohair@286 185
ohair@286 186 class MatchingIterator implements Iterator {
ohair@286 187 private boolean match;
ohair@286 188 private Iterator iterator;
ohair@286 189 private String[] names;
ohair@286 190 private Object nextHeader;
ohair@286 191
ohair@286 192 MatchingIterator(String[] names, boolean match) {
ohair@286 193 this.match = match;
ohair@286 194 this.names = names;
ohair@286 195 this.iterator = headers.iterator();
ohair@286 196 }
ohair@286 197
ohair@286 198 private Object nextMatch() {
ohair@286 199 next:
ohair@286 200 while (iterator.hasNext()) {
ohair@286 201 MimeHeader hdr = (MimeHeader) iterator.next();
ohair@286 202
ohair@286 203 if (names == null)
ohair@286 204 return match ? null : hdr;
ohair@286 205
ohair@286 206 for(int i = 0; i < names.length; i++)
ohair@286 207 if (hdr.getName().equalsIgnoreCase(names[i]))
ohair@286 208 if (match)
ohair@286 209 return hdr;
ohair@286 210 else
ohair@286 211 continue next;
ohair@286 212 if (!match)
ohair@286 213 return hdr;
ohair@286 214 }
ohair@286 215 return null;
ohair@286 216 }
ohair@286 217
ohair@286 218
ohair@286 219 public boolean hasNext() {
ohair@286 220 if (nextHeader == null)
ohair@286 221 nextHeader = nextMatch();
ohair@286 222 return nextHeader != null;
ohair@286 223 }
ohair@286 224
ohair@286 225 public Object next() {
ohair@286 226 // hasNext should've prefetched the header for us,
ohair@286 227 // return it.
ohair@286 228 if (nextHeader != null) {
ohair@286 229 Object ret = nextHeader;
ohair@286 230 nextHeader = null;
ohair@286 231 return ret;
ohair@286 232 }
ohair@286 233 if (hasNext())
ohair@286 234 return nextHeader;
ohair@286 235 return null;
ohair@286 236 }
ohair@286 237
ohair@286 238 public void remove() {
ohair@286 239 iterator.remove();
ohair@286 240 }
ohair@286 241 }
ohair@286 242
ohair@286 243
ohair@286 244 /**
ohair@286 245 * Returns all the <code>MimeHeader</code> objects whose name matches
ohair@286 246 * a name in the given array of names.
ohair@286 247 *
ohair@286 248 * @param names an array of <code>String</code> objects with the names
ohair@286 249 * for which to search
ohair@286 250 * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
ohair@286 251 * objects whose name matches one of the names in the given list
ohair@286 252 */
ohair@286 253 public Iterator getMatchingHeaders(String[] names) {
ohair@286 254 return new MatchingIterator(names, true);
ohair@286 255 }
ohair@286 256
ohair@286 257 /**
ohair@286 258 * Returns all of the <code>MimeHeader</code> objects whose name does not
ohair@286 259 * match a name in the given array of names.
ohair@286 260 *
ohair@286 261 * @param names an array of <code>String</code> objects with the names
ohair@286 262 * for which to search
ohair@286 263 * @return an <code>Iterator</code> object over the <code>MimeHeader</code>
ohair@286 264 * objects whose name does not match one of the names in the given list
ohair@286 265 */
ohair@286 266 public Iterator getNonMatchingHeaders(String[] names) {
ohair@286 267 return new MatchingIterator(names, false);
ohair@286 268 }
ohair@286 269 }

mercurial