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