src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/HeaderImpl.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, 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 com.sun.xml.internal.messaging.saaj.soap.impl;
ohair@286 27
ohair@286 28 import java.util.*;
ohair@286 29 import java.util.logging.Level;
ohair@286 30
ohair@286 31 import javax.xml.namespace.QName;
ohair@286 32 import javax.xml.soap.*;
ohair@286 33
ohair@286 34 import org.w3c.dom.Element;
ohair@286 35
ohair@286 36 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
ohair@286 37 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
ohair@286 38 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
ohair@286 39 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
ohair@286 40
ohair@286 41 public abstract class HeaderImpl extends ElementImpl implements SOAPHeader {
ohair@286 42 protected static final boolean MUST_UNDERSTAND_ONLY = false;
ohair@286 43
ohair@286 44 protected HeaderImpl(SOAPDocumentImpl ownerDoc, NameImpl name) {
ohair@286 45 super(ownerDoc, name);
ohair@286 46 }
ohair@286 47
ohair@286 48 protected abstract SOAPHeaderElement createHeaderElement(Name name)
ohair@286 49 throws SOAPException;
ohair@286 50 protected abstract SOAPHeaderElement createHeaderElement(QName name)
ohair@286 51 throws SOAPException;
ohair@286 52 protected abstract NameImpl getNotUnderstoodName();
ohair@286 53 protected abstract NameImpl getUpgradeName();
ohair@286 54 protected abstract NameImpl getSupportedEnvelopeName();
ohair@286 55
ohair@286 56 public SOAPHeaderElement addHeaderElement(Name name) throws SOAPException {
ohair@286 57 SOAPElement newHeaderElement =
ohair@286 58 ElementFactory.createNamedElement(
ohair@286 59 ((SOAPDocument) getOwnerDocument()).getDocument(),
ohair@286 60 name.getLocalName(),
ohair@286 61 name.getPrefix(),
ohair@286 62 name.getURI());
ohair@286 63 if (newHeaderElement == null
ohair@286 64 || !(newHeaderElement instanceof SOAPHeaderElement)) {
ohair@286 65 newHeaderElement = createHeaderElement(name);
ohair@286 66 }
ohair@286 67
ohair@286 68 // header elements must be namespace qualified
ohair@286 69 // check that URI is not empty, ensuring that the element is NS qualified.
ohair@286 70 String uri = newHeaderElement.getElementQName().getNamespaceURI();
ohair@286 71 if ((uri == null) || ("").equals(uri)) {
ohair@286 72 log.severe("SAAJ0131.impl.header.elems.ns.qualified");
ohair@286 73 throw new SOAPExceptionImpl("HeaderElements must be namespace qualified");
ohair@286 74 }
ohair@286 75 addNode(newHeaderElement);
ohair@286 76 return (SOAPHeaderElement) newHeaderElement;
ohair@286 77 }
ohair@286 78
ohair@286 79 public SOAPHeaderElement addHeaderElement(QName name) throws SOAPException {
ohair@286 80 SOAPElement newHeaderElement =
ohair@286 81 ElementFactory.createNamedElement(
ohair@286 82 ((SOAPDocument) getOwnerDocument()).getDocument(),
ohair@286 83 name.getLocalPart(),
ohair@286 84 name.getPrefix(),
ohair@286 85 name.getNamespaceURI());
ohair@286 86 if (newHeaderElement == null
ohair@286 87 || !(newHeaderElement instanceof SOAPHeaderElement)) {
ohair@286 88 newHeaderElement = createHeaderElement(name);
ohair@286 89 }
ohair@286 90
ohair@286 91 // header elements must be namespace qualified
ohair@286 92 // check that URI is not empty, ensuring that the element is NS qualified.
ohair@286 93 String uri = newHeaderElement.getElementQName().getNamespaceURI();
ohair@286 94 if ((uri == null) || ("").equals(uri)) {
ohair@286 95 log.severe("SAAJ0131.impl.header.elems.ns.qualified");
ohair@286 96 throw new SOAPExceptionImpl("HeaderElements must be namespace qualified");
ohair@286 97 }
ohair@286 98 addNode(newHeaderElement);
ohair@286 99 return (SOAPHeaderElement) newHeaderElement;
ohair@286 100 }
ohair@286 101
ohair@286 102 protected SOAPElement addElement(Name name) throws SOAPException {
ohair@286 103 return addHeaderElement(name);
ohair@286 104 }
ohair@286 105
ohair@286 106 protected SOAPElement addElement(QName name) throws SOAPException {
ohair@286 107 return addHeaderElement(name);
ohair@286 108 }
ohair@286 109
ohair@286 110 public Iterator examineHeaderElements(String actor) {
ohair@286 111 return getHeaderElementsForActor(actor, false, false);
ohair@286 112 }
ohair@286 113
ohair@286 114 public Iterator extractHeaderElements(String actor) {
ohair@286 115 return getHeaderElementsForActor(actor, true, false);
ohair@286 116 }
ohair@286 117
ohair@286 118 protected Iterator getHeaderElementsForActor(
ohair@286 119 String actor,
ohair@286 120 boolean detach,
ohair@286 121 boolean mustUnderstand) {
ohair@286 122 if (actor == null || actor.equals("")) {
ohair@286 123 log.severe("SAAJ0132.impl.invalid.value.for.actor.or.role");
ohair@286 124 throw new IllegalArgumentException("Invalid value for actor or role");
ohair@286 125 }
ohair@286 126 return getHeaderElements(actor, detach, mustUnderstand);
ohair@286 127 }
ohair@286 128
ohair@286 129 protected Iterator getHeaderElements(
ohair@286 130 String actor,
ohair@286 131 boolean detach,
ohair@286 132 boolean mustUnderstand) {
ohair@286 133 List elementList = new ArrayList();
ohair@286 134
ohair@286 135 Iterator eachChild = getChildElements();
ohair@286 136
ohair@286 137 Object currentChild = iterate(eachChild);
ohair@286 138 while (currentChild != null) {
ohair@286 139 if (!(currentChild instanceof SOAPHeaderElement)) {
ohair@286 140 currentChild = iterate(eachChild);
ohair@286 141 } else {
ohair@286 142 HeaderElementImpl currentElement =
ohair@286 143 (HeaderElementImpl) currentChild;
ohair@286 144 currentChild = iterate(eachChild);
ohair@286 145
ohair@286 146 boolean isMustUnderstandMatching =
ohair@286 147 (!mustUnderstand || currentElement.getMustUnderstand());
ohair@286 148 boolean doAdd = false;
ohair@286 149 if (actor == null && isMustUnderstandMatching) {
ohair@286 150 doAdd = true;
ohair@286 151 } else {
ohair@286 152 String currentActor = currentElement.getActorOrRole();
ohair@286 153 if (currentActor == null) {
ohair@286 154 currentActor = "";
ohair@286 155 }
ohair@286 156
ohair@286 157 if (currentActor.equalsIgnoreCase(actor)
ohair@286 158 && isMustUnderstandMatching) {
ohair@286 159 doAdd = true;
ohair@286 160 }
ohair@286 161 }
ohair@286 162
ohair@286 163 if (doAdd) {
ohair@286 164 elementList.add(currentElement);
ohair@286 165 if (detach) {
ohair@286 166 currentElement.detachNode();
ohair@286 167 }
ohair@286 168 }
ohair@286 169 }
ohair@286 170 }
ohair@286 171
ohair@286 172 return elementList.listIterator();
ohair@286 173 }
ohair@286 174
ohair@286 175 private Object iterate(Iterator each) {
ohair@286 176 return each.hasNext() ? each.next() : null;
ohair@286 177 }
ohair@286 178
ohair@286 179 public void setParentElement(SOAPElement element) throws SOAPException {
ohair@286 180 if (!(element instanceof SOAPEnvelope)) {
ohair@286 181 log.severe("SAAJ0133.impl.header.parent.mustbe.envelope");
ohair@286 182 throw new SOAPException("Parent of SOAPHeader has to be a SOAPEnvelope");
ohair@286 183 }
ohair@286 184 super.setParentElement(element);
ohair@286 185 }
ohair@286 186
ohair@286 187 // overriding ElementImpl's method to ensure that HeaderElements are
ohair@286 188 // namespace qualified. Holds for both SOAP versions.
ohair@286 189 // TODO - This check needs to be made for other addChildElement() methods
ohair@286 190 // as well.
ohair@286 191 public SOAPElement addChildElement(String localName) throws SOAPException {
ohair@286 192
ohair@286 193 SOAPElement element = super.addChildElement(localName);
ohair@286 194 // check that URI is not empty, ensuring that the element is NS qualified.
ohair@286 195 String uri = element.getElementName().getURI();
ohair@286 196 if ((uri == null) || ("").equals(uri)) {
ohair@286 197 log.severe("SAAJ0134.impl.header.elems.ns.qualified");
ohair@286 198 throw new SOAPExceptionImpl("HeaderElements must be namespace qualified");
ohair@286 199 }
ohair@286 200 return element;
ohair@286 201 }
ohair@286 202
ohair@286 203 public Iterator examineAllHeaderElements() {
ohair@286 204 return getHeaderElements(null, false, MUST_UNDERSTAND_ONLY);
ohair@286 205 }
ohair@286 206
ohair@286 207 public Iterator examineMustUnderstandHeaderElements(String actor) {
ohair@286 208 return getHeaderElements(actor, false, true);
ohair@286 209
ohair@286 210 }
ohair@286 211
ohair@286 212 public Iterator extractAllHeaderElements() {
ohair@286 213 return getHeaderElements(null, true, false);
ohair@286 214 }
ohair@286 215
ohair@286 216 public SOAPHeaderElement addUpgradeHeaderElement(Iterator supportedSoapUris)
ohair@286 217 throws SOAPException {
ohair@286 218 if (supportedSoapUris == null) {
ohair@286 219 log.severe("SAAJ0411.ver1_2.no.null.supportedURIs");
ohair@286 220 throw new SOAPException("Argument cannot be null; iterator of supportedURIs cannot be null");
ohair@286 221 }
ohair@286 222 if (!supportedSoapUris.hasNext()) {
ohair@286 223 log.severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs");
ohair@286 224 throw new SOAPException("List of supported URIs cannot be empty");
ohair@286 225 }
ohair@286 226 Name upgradeName = getUpgradeName();
ohair@286 227 SOAPHeaderElement upgradeHeaderElement =
ohair@286 228 (SOAPHeaderElement) addChildElement(upgradeName);
ohair@286 229 Name supportedEnvelopeName = getSupportedEnvelopeName();
ohair@286 230 int i = 0;
ohair@286 231 while (supportedSoapUris.hasNext()) {
ohair@286 232 SOAPElement subElement =
ohair@286 233 upgradeHeaderElement.addChildElement(supportedEnvelopeName);
ohair@286 234 String ns = "ns" + Integer.toString(i);
ohair@286 235 subElement.addAttribute(
ohair@286 236 NameImpl.createFromUnqualifiedName("qname"),
ohair@286 237 ns + ":Envelope");
ohair@286 238 subElement.addNamespaceDeclaration(
ohair@286 239 ns,
ohair@286 240 (String) supportedSoapUris.next());
ohair@286 241 i ++;
ohair@286 242 }
ohair@286 243 return upgradeHeaderElement;
ohair@286 244 }
ohair@286 245
ohair@286 246 public SOAPHeaderElement addUpgradeHeaderElement(String supportedSoapUri)
ohair@286 247 throws SOAPException {
ohair@286 248 return addUpgradeHeaderElement(new String[] {supportedSoapUri});
ohair@286 249 }
ohair@286 250
ohair@286 251 public SOAPHeaderElement addUpgradeHeaderElement(String[] supportedSoapUris)
ohair@286 252 throws SOAPException {
ohair@286 253
ohair@286 254 if (supportedSoapUris == null) {
ohair@286 255 log.severe("SAAJ0411.ver1_2.no.null.supportedURIs");
ohair@286 256 throw new SOAPException("Argument cannot be null; array of supportedURIs cannot be null");
ohair@286 257 }
ohair@286 258 if (supportedSoapUris.length == 0) {
ohair@286 259 log.severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs");
ohair@286 260 throw new SOAPException("List of supported URIs cannot be empty");
ohair@286 261 }
ohair@286 262 Name upgradeName = getUpgradeName();
ohair@286 263 SOAPHeaderElement upgradeHeaderElement =
ohair@286 264 (SOAPHeaderElement) addChildElement(upgradeName);
ohair@286 265 Name supportedEnvelopeName = getSupportedEnvelopeName();
ohair@286 266 for (int i = 0; i < supportedSoapUris.length; i ++) {
ohair@286 267 SOAPElement subElement =
ohair@286 268 upgradeHeaderElement.addChildElement(supportedEnvelopeName);
ohair@286 269 String ns = "ns" + Integer.toString(i);
ohair@286 270 subElement.addAttribute(
ohair@286 271 NameImpl.createFromUnqualifiedName("qname"),
ohair@286 272 ns + ":Envelope");
ohair@286 273 subElement.addNamespaceDeclaration(ns, supportedSoapUris[i]);
ohair@286 274 }
ohair@286 275 return upgradeHeaderElement;
ohair@286 276 }
ohair@286 277
ohair@286 278 protected SOAPElement convertToSoapElement(Element element) {
ohair@286 279 if (element instanceof SOAPHeaderElement) {
ohair@286 280 return (SOAPElement) element;
ohair@286 281 } else {
ohair@286 282 SOAPHeaderElement headerElement;
ohair@286 283 try {
ohair@286 284 headerElement =
ohair@286 285 createHeaderElement(NameImpl.copyElementName(element));
ohair@286 286 } catch (SOAPException e) {
ohair@286 287 throw new ClassCastException("Could not convert Element to SOAPHeaderElement: " + e.getMessage());
ohair@286 288 }
ohair@286 289 return replaceElementWithSOAPElement(
ohair@286 290 element,
ohair@286 291 (ElementImpl) headerElement);
ohair@286 292 }
ohair@286 293 }
ohair@286 294
ohair@286 295 public SOAPElement setElementQName(QName newName) throws SOAPException {
ohair@286 296 log.log(Level.SEVERE,
ohair@286 297 "SAAJ0146.impl.invalid.name.change.requested",
ohair@286 298 new Object[] {elementQName.getLocalPart(),
ohair@286 299 newName.getLocalPart()});
ohair@286 300 throw new SOAPException("Cannot change name for "
ohair@286 301 + elementQName.getLocalPart() + " to "
ohair@286 302 + newName.getLocalPart());
ohair@286 303 }
ohair@286 304
ohair@286 305 }

mercurial