package com.sun.xml.internal.messaging.saaj.soap.impl;
import java.util.logging.Level;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;
import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
public abstract class extends ElementImpl implements SOAPHeader {
protected static final boolean MUST_UNDERSTAND_ONLY = false;
protected (SOAPDocumentImpl ownerDoc, NameImpl name) {
super(ownerDoc, name);
}
public (SOAPDocumentImpl ownerDoc, Element domElement) {
super(ownerDoc, domElement);
}
protected abstract SOAPHeaderElement (Name name)
throws SOAPException;
protected abstract SOAPHeaderElement (QName name)
throws SOAPException;
protected abstract NameImpl ();
protected abstract NameImpl ();
protected abstract NameImpl ();
@Override
public SOAPHeaderElement (Name name) throws SOAPException {
SOAPElement newHeaderElement =
ElementFactory.createNamedElement(
((SOAPDocument) getOwnerDocument()).getDocument(),
name.getLocalName(),
name.getPrefix(),
name.getURI());
if (newHeaderElement == null
|| !(newHeaderElement instanceof SOAPHeaderElement)) {
newHeaderElement = createHeaderElement(name);
}
String uri = newHeaderElement.getElementQName().getNamespaceURI();
if ((uri == null) || ("").equals(uri)) {
log.severe("SAAJ0131.impl.header.elems.ns.qualified");
throw new SOAPExceptionImpl("HeaderElements must be namespace qualified");
}
addNode(newHeaderElement);
return (SOAPHeaderElement) newHeaderElement;
}
@Override
public SOAPHeaderElement (QName name) throws SOAPException {
SOAPElement newHeaderElement =
ElementFactory.createNamedElement(
((SOAPDocument) getOwnerDocument()).getDocument(),
name.getLocalPart(),
name.getPrefix(),
name.getNamespaceURI());
if (newHeaderElement == null
|| !(newHeaderElement instanceof SOAPHeaderElement)) {
newHeaderElement = createHeaderElement(name);
}
String uri = newHeaderElement.getElementQName().getNamespaceURI();
if ((uri == null) || ("").equals(uri)) {
log.severe("SAAJ0131.impl.header.elems.ns.qualified");
throw new SOAPExceptionImpl("HeaderElements must be namespace qualified");
}
addNode(newHeaderElement);
return (SOAPHeaderElement) newHeaderElement;
}
@Override
protected SOAPElement (Name name) throws SOAPException {
return addHeaderElement(name);
}
@Override
protected SOAPElement (QName name) throws SOAPException {
return addHeaderElement(name);
}
@Override
public Iterator<SOAPHeaderElement> (String actor) {
return getHeaderElementsForActor(actor, false, false);
}
@Override
public Iterator<SOAPHeaderElement> (String actor) {
return getHeaderElementsForActor(actor, true, false);
}
protected Iterator<SOAPHeaderElement> (
String actor,
boolean detach,
boolean mustUnderstand) {
if (actor == null || actor.equals("")) {
log.severe("SAAJ0132.impl.invalid.value.for.actor.or.role");
throw new IllegalArgumentException("Invalid value for actor or role");
}
return getHeaderElements(actor, detach, mustUnderstand);
}
protected Iterator<SOAPHeaderElement> (
String actor,
boolean detach,
boolean mustUnderstand) {
List<SOAPHeaderElement> elementList = new ArrayList<>();
Iterator<javax.xml.soap.Node> eachChild = getChildElements();
org.w3c.dom.Node currentChild = iterate(eachChild);
while (currentChild != null) {
if (!(currentChild instanceof SOAPHeaderElement)) {
currentChild = iterate(eachChild);
} else {
HeaderElementImpl currentElement =
(HeaderElementImpl) currentChild;
currentChild = iterate(eachChild);
boolean isMustUnderstandMatching =
(!mustUnderstand || currentElement.getMustUnderstand());
boolean doAdd = false;
if (actor == null && isMustUnderstandMatching) {
doAdd = true;
} else {
String currentActor = currentElement.getActorOrRole();
if (currentActor == null) {
currentActor = "";
}
if (currentActor.equalsIgnoreCase(actor)
&& isMustUnderstandMatching) {
doAdd = true;
}
}
if (doAdd) {
elementList.add(currentElement);
if (detach) {
currentElement.detachNode();
}
}
}
}
return elementList.listIterator();
}
private <T> T (Iterator<T> each) {
return each.hasNext() ? each.next() : null;
}
@Override
public void (SOAPElement element) throws SOAPException {
if (!(element instanceof SOAPEnvelope)) {
log.severe("SAAJ0133.impl.header.parent.mustbe.envelope");
throw new SOAPException("Parent of SOAPHeader has to be a SOAPEnvelope");
}
super.setParentElement(element);
}
@Override
public SOAPElement (String localName) throws SOAPException {
SOAPElement element = super.addChildElement(localName);
String uri = element.getElementName().getURI();
if ((uri == null) || ("").equals(uri)) {
log.severe("SAAJ0134.impl.header.elems.ns.qualified");
throw new SOAPExceptionImpl("HeaderElements must be namespace qualified");
}
return element;
}
@Override
public Iterator<SOAPHeaderElement> () {
return getHeaderElements(null, false, MUST_UNDERSTAND_ONLY);
}
@Override
public Iterator<SOAPHeaderElement> examineMustUnderstandHeaderElements(String actor) {
return getHeaderElements(actor, false, true);
}
@Override
public Iterator<SOAPHeaderElement> () {
return getHeaderElements(null, true, false);
}
@Override
public SOAPHeaderElement (Iterator supportedSoapUris)
throws SOAPException {
if (supportedSoapUris == null) {
log.severe("SAAJ0411.ver1_2.no.null.supportedURIs");
throw new SOAPException("Argument cannot be null; iterator of supportedURIs cannot be null");
}
if (!supportedSoapUris.hasNext()) {
log.severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs");
throw new SOAPException("List of supported URIs cannot be empty");
}
Name upgradeName = getUpgradeName();
SOAPHeaderElement upgradeHeaderElement =
(SOAPHeaderElement) addChildElement(upgradeName);
Name supportedEnvelopeName = getSupportedEnvelopeName();
int i = 0;
while (supportedSoapUris.hasNext()) {
SOAPElement subElement =
upgradeHeaderElement.addChildElement(supportedEnvelopeName);
String ns = "ns" + Integer.toString(i);
subElement.addAttribute(
NameImpl.createFromUnqualifiedName("qname"),
ns + ":Envelope");
subElement.addNamespaceDeclaration(
ns, (String) supportedSoapUris.next());
i ++;
}
return upgradeHeaderElement;
}
@Override
public SOAPHeaderElement (String supportedSoapUri)
throws SOAPException {
return addUpgradeHeaderElement(new String[] {supportedSoapUri});
}
@Override
public SOAPHeaderElement (String[] supportedSoapUris)
throws SOAPException {
if (supportedSoapUris == null) {
log.severe("SAAJ0411.ver1_2.no.null.supportedURIs");
throw new SOAPException("Argument cannot be null; array of supportedURIs cannot be null");
}
if (supportedSoapUris.length == 0) {
log.severe("SAAJ0412.ver1_2.no.empty.list.of.supportedURIs");
throw new SOAPException("List of supported URIs cannot be empty");
}
Name upgradeName = getUpgradeName();
SOAPHeaderElement upgradeHeaderElement =
(SOAPHeaderElement) addChildElement(upgradeName);
Name supportedEnvelopeName = getSupportedEnvelopeName();
for (int i = 0; i < supportedSoapUris.length; i ++) {
SOAPElement subElement =
upgradeHeaderElement.addChildElement(supportedEnvelopeName);
String ns = "ns" + Integer.toString(i);
subElement.addAttribute(
NameImpl.createFromUnqualifiedName("qname"),
ns + ":Envelope");
subElement.addNamespaceDeclaration(ns, supportedSoapUris[i]);
}
return upgradeHeaderElement;
}
@Override
protected SOAPElement (Element element) {
final org.w3c.dom.Node soapNode = getSoapDocument().findIfPresent(element);
if (soapNode instanceof SOAPHeaderElement) {
return (SOAPElement) soapNode;
} else {
SOAPHeaderElement headerElement;
try {
headerElement =
createHeaderElement(NameImpl.copyElementName(element));
} catch (SOAPException e) {
throw new ClassCastException("Could not convert Element to SOAPHeaderElement: " + e.getMessage());
}
return replaceElementWithSOAPElement(
element,
(ElementImpl) headerElement);
}
}
@Override
public SOAPElement (QName newName) throws SOAPException {
log.log(Level.SEVERE,
"SAAJ0146.impl.invalid.name.change.requested",
new Object[] {elementQName.getLocalPart(),
newName.getLocalPart()});
throw new SOAPException("Cannot change name for "
+ elementQName.getLocalPart() + " to "
+ newName.getLocalPart());
}
}