1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/HeaderImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,305 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.messaging.saaj.soap.impl; 1.30 + 1.31 +import java.util.*; 1.32 +import java.util.logging.Level; 1.33 + 1.34 +import javax.xml.namespace.QName; 1.35 +import javax.xml.soap.*; 1.36 + 1.37 +import org.w3c.dom.Element; 1.38 + 1.39 +import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; 1.40 +import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument; 1.41 +import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl; 1.42 +import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl; 1.43 + 1.44 +public abstract class HeaderImpl extends ElementImpl implements SOAPHeader { 1.45 + protected static final boolean MUST_UNDERSTAND_ONLY = false; 1.46 + 1.47 + protected HeaderImpl(SOAPDocumentImpl ownerDoc, NameImpl name) { 1.48 + super(ownerDoc, name); 1.49 + } 1.50 + 1.51 + protected abstract SOAPHeaderElement createHeaderElement(Name name) 1.52 + throws SOAPException; 1.53 + protected abstract SOAPHeaderElement createHeaderElement(QName name) 1.54 + throws SOAPException; 1.55 + protected abstract NameImpl getNotUnderstoodName(); 1.56 + protected abstract NameImpl getUpgradeName(); 1.57 + protected abstract NameImpl getSupportedEnvelopeName(); 1.58 + 1.59 + public SOAPHeaderElement addHeaderElement(Name name) throws SOAPException { 1.60 + SOAPElement newHeaderElement = 1.61 + ElementFactory.createNamedElement( 1.62 + ((SOAPDocument) getOwnerDocument()).getDocument(), 1.63 + name.getLocalName(), 1.64 + name.getPrefix(), 1.65 + name.getURI()); 1.66 + if (newHeaderElement == null 1.67 + || !(newHeaderElement instanceof SOAPHeaderElement)) { 1.68 + newHeaderElement = createHeaderElement(name); 1.69 + } 1.70 + 1.71 + // header elements must be namespace qualified 1.72 + // check that URI is not empty, ensuring that the element is NS qualified. 1.73 + String uri = newHeaderElement.getElementQName().getNamespaceURI(); 1.74 + if ((uri == null) || ("").equals(uri)) { 1.75 + log.severe("SAAJ0131.impl.header.elems.ns.qualified"); 1.76 + throw new SOAPExceptionImpl("HeaderElements must be namespace qualified"); 1.77 + } 1.78 + addNode(newHeaderElement); 1.79 + return (SOAPHeaderElement) newHeaderElement; 1.80 + } 1.81 + 1.82 + public SOAPHeaderElement addHeaderElement(QName name) throws SOAPException { 1.83 + SOAPElement newHeaderElement = 1.84 + ElementFactory.createNamedElement( 1.85 + ((SOAPDocument) getOwnerDocument()).getDocument(), 1.86 + name.getLocalPart(), 1.87 + name.getPrefix(), 1.88 + name.getNamespaceURI()); 1.89 + if (newHeaderElement == null 1.90 + || !(newHeaderElement instanceof SOAPHeaderElement)) { 1.91 + newHeaderElement = createHeaderElement(name); 1.92 + } 1.93 + 1.94 + // header elements must be namespace qualified 1.95 + // check that URI is not empty, ensuring that the element is NS qualified. 1.96 + String uri = newHeaderElement.getElementQName().getNamespaceURI(); 1.97 + if ((uri == null) || ("").equals(uri)) { 1.98 + log.severe("SAAJ0131.impl.header.elems.ns.qualified"); 1.99 + throw new SOAPExceptionImpl("HeaderElements must be namespace qualified"); 1.100 + } 1.101 + addNode(newHeaderElement); 1.102 + return (SOAPHeaderElement) newHeaderElement; 1.103 + } 1.104 + 1.105 + protected SOAPElement addElement(Name name) throws SOAPException { 1.106 + return addHeaderElement(name); 1.107 + } 1.108 + 1.109 + protected SOAPElement addElement(QName name) throws SOAPException { 1.110 + return addHeaderElement(name); 1.111 + } 1.112 + 1.113 + public Iterator examineHeaderElements(String actor) { 1.114 + return getHeaderElementsForActor(actor, false, false); 1.115 + } 1.116 + 1.117 + public Iterator extractHeaderElements(String actor) { 1.118 + return getHeaderElementsForActor(actor, true, false); 1.119 + } 1.120 + 1.121 + protected Iterator getHeaderElementsForActor( 1.122 + String actor, 1.123 + boolean detach, 1.124 + boolean mustUnderstand) { 1.125 + if (actor == null || actor.equals("")) { 1.126 + log.severe("SAAJ0132.impl.invalid.value.for.actor.or.role"); 1.127 + throw new IllegalArgumentException("Invalid value for actor or role"); 1.128 + } 1.129 + return getHeaderElements(actor, detach, mustUnderstand); 1.130 + } 1.131 + 1.132 + protected Iterator getHeaderElements( 1.133 + String actor, 1.134 + boolean detach, 1.135 + boolean mustUnderstand) { 1.136 + List elementList = new ArrayList(); 1.137 + 1.138 + Iterator eachChild = getChildElements(); 1.139 + 1.140 + Object currentChild = iterate(eachChild); 1.141 + while (currentChild != null) { 1.142 + if (!(currentChild instanceof SOAPHeaderElement)) { 1.143 + currentChild = iterate(eachChild); 1.144 + } else { 1.145 + HeaderElementImpl currentElement = 1.146 + (HeaderElementImpl) currentChild; 1.147 + currentChild = iterate(eachChild); 1.148 + 1.149 + boolean isMustUnderstandMatching = 1.150 + (!mustUnderstand || currentElement.getMustUnderstand()); 1.151 + boolean doAdd = false; 1.152 + if (actor == null && isMustUnderstandMatching) { 1.153 + doAdd = true; 1.154 + } else { 1.155 + String currentActor = currentElement.getActorOrRole(); 1.156 + if (currentActor == null) { 1.157 + currentActor = ""; 1.158 + } 1.159 + 1.160 + if (currentActor.equalsIgnoreCase(actor) 1.161 + && isMustUnderstandMatching) { 1.162 + doAdd = true; 1.163 + } 1.164 + } 1.165 + 1.166 + if (doAdd) { 1.167 + elementList.add(currentElement); 1.168 + if (detach) { 1.169 + currentElement.detachNode(); 1.170 + } 1.171 + } 1.172 + } 1.173 + } 1.174 + 1.175 + return elementList.listIterator(); 1.176 + } 1.177 + 1.178 + private Object iterate(Iterator each) { 1.179 + return each.hasNext() ? each.next() : null; 1.180 + } 1.181 + 1.182 + public void setParentElement(SOAPElement element) throws SOAPException { 1.183 + if (!(element instanceof SOAPEnvelope)) { 1.184 + log.severe("SAAJ0133.impl.header.parent.mustbe.envelope"); 1.185 + throw new SOAPException("Parent of SOAPHeader has to be a SOAPEnvelope"); 1.186 + } 1.187 + super.setParentElement(element); 1.188 + } 1.189 + 1.190 + // overriding ElementImpl's method to ensure that HeaderElements are 1.191 + // namespace qualified. Holds for both SOAP versions. 1.192 + // TODO - This check needs to be made for other addChildElement() methods 1.193 + // as well. 1.194 + public SOAPElement addChildElement(String localName) throws SOAPException { 1.195 + 1.196 + SOAPElement element = super.addChildElement(localName); 1.197 + // check that URI is not empty, ensuring that the element is NS qualified. 1.198 + String uri = element.getElementName().getURI(); 1.199 + if ((uri == null) || ("").equals(uri)) { 1.200 + log.severe("SAAJ0134.impl.header.elems.ns.qualified"); 1.201 + throw new SOAPExceptionImpl("HeaderElements must be namespace qualified"); 1.202 + } 1.203 + return element; 1.204 + } 1.205 + 1.206 + public Iterator examineAllHeaderElements() { 1.207 + return getHeaderElements(null, false, MUST_UNDERSTAND_ONLY); 1.208 + } 1.209 + 1.210 + public Iterator examineMustUnderstandHeaderElements(String actor) { 1.211 + return getHeaderElements(actor, false, true); 1.212 + 1.213 + } 1.214 + 1.215 + public Iterator extractAllHeaderElements() { 1.216 + return getHeaderElements(null, true, false); 1.217 + } 1.218 + 1.219 + public SOAPHeaderElement addUpgradeHeaderElement(Iterator supportedSoapUris) 1.220 + throws SOAPException { 1.221 + if (supportedSoapUris == null) { 1.222 + log.severe("SAAJ0411.ver1_2.no.null.supportedURIs"); 1.223 + throw new SOAPException("Argument cannot be null; iterator of supportedURIs cannot be null"); 1.224 + } 1.225 + if (!supportedSoapUris.hasNext()) { 1.226 + log.severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs"); 1.227 + throw new SOAPException("List of supported URIs cannot be empty"); 1.228 + } 1.229 + Name upgradeName = getUpgradeName(); 1.230 + SOAPHeaderElement upgradeHeaderElement = 1.231 + (SOAPHeaderElement) addChildElement(upgradeName); 1.232 + Name supportedEnvelopeName = getSupportedEnvelopeName(); 1.233 + int i = 0; 1.234 + while (supportedSoapUris.hasNext()) { 1.235 + SOAPElement subElement = 1.236 + upgradeHeaderElement.addChildElement(supportedEnvelopeName); 1.237 + String ns = "ns" + Integer.toString(i); 1.238 + subElement.addAttribute( 1.239 + NameImpl.createFromUnqualifiedName("qname"), 1.240 + ns + ":Envelope"); 1.241 + subElement.addNamespaceDeclaration( 1.242 + ns, 1.243 + (String) supportedSoapUris.next()); 1.244 + i ++; 1.245 + } 1.246 + return upgradeHeaderElement; 1.247 + } 1.248 + 1.249 + public SOAPHeaderElement addUpgradeHeaderElement(String supportedSoapUri) 1.250 + throws SOAPException { 1.251 + return addUpgradeHeaderElement(new String[] {supportedSoapUri}); 1.252 + } 1.253 + 1.254 + public SOAPHeaderElement addUpgradeHeaderElement(String[] supportedSoapUris) 1.255 + throws SOAPException { 1.256 + 1.257 + if (supportedSoapUris == null) { 1.258 + log.severe("SAAJ0411.ver1_2.no.null.supportedURIs"); 1.259 + throw new SOAPException("Argument cannot be null; array of supportedURIs cannot be null"); 1.260 + } 1.261 + if (supportedSoapUris.length == 0) { 1.262 + log.severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs"); 1.263 + throw new SOAPException("List of supported URIs cannot be empty"); 1.264 + } 1.265 + Name upgradeName = getUpgradeName(); 1.266 + SOAPHeaderElement upgradeHeaderElement = 1.267 + (SOAPHeaderElement) addChildElement(upgradeName); 1.268 + Name supportedEnvelopeName = getSupportedEnvelopeName(); 1.269 + for (int i = 0; i < supportedSoapUris.length; i ++) { 1.270 + SOAPElement subElement = 1.271 + upgradeHeaderElement.addChildElement(supportedEnvelopeName); 1.272 + String ns = "ns" + Integer.toString(i); 1.273 + subElement.addAttribute( 1.274 + NameImpl.createFromUnqualifiedName("qname"), 1.275 + ns + ":Envelope"); 1.276 + subElement.addNamespaceDeclaration(ns, supportedSoapUris[i]); 1.277 + } 1.278 + return upgradeHeaderElement; 1.279 + } 1.280 + 1.281 + protected SOAPElement convertToSoapElement(Element element) { 1.282 + if (element instanceof SOAPHeaderElement) { 1.283 + return (SOAPElement) element; 1.284 + } else { 1.285 + SOAPHeaderElement headerElement; 1.286 + try { 1.287 + headerElement = 1.288 + createHeaderElement(NameImpl.copyElementName(element)); 1.289 + } catch (SOAPException e) { 1.290 + throw new ClassCastException("Could not convert Element to SOAPHeaderElement: " + e.getMessage()); 1.291 + } 1.292 + return replaceElementWithSOAPElement( 1.293 + element, 1.294 + (ElementImpl) headerElement); 1.295 + } 1.296 + } 1.297 + 1.298 + public SOAPElement setElementQName(QName newName) throws SOAPException { 1.299 + log.log(Level.SEVERE, 1.300 + "SAAJ0146.impl.invalid.name.change.requested", 1.301 + new Object[] {elementQName.getLocalPart(), 1.302 + newName.getLocalPart()}); 1.303 + throw new SOAPException("Cannot change name for " 1.304 + + elementQName.getLocalPart() + " to " 1.305 + + newName.getLocalPart()); 1.306 + } 1.307 + 1.308 +}