package com.sun.xml.internal.ws.api.message.saaj;
import com.sun.xml.internal.ws.api.SOAPVersion;
import com.sun.xml.internal.ws.api.WSBinding;
import com.sun.xml.internal.ws.api.message.Header;
import com.sun.xml.internal.ws.api.message.MessageHeaders;
import com.sun.xml.internal.ws.binding.SOAPBindingImpl;
import com.sun.xml.internal.ws.message.saaj.SAAJHeader;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class implements MessageHeaders {
SOAPMessage ;
Map<SOAPHeaderElement, Header> ;
Map<QName, Integer> ;
SOAPVersion ;
private Set<QName> ;
public (SOAPMessage sm, SOAPVersion version) {
this.sm = sm;
this.soapVersion = version;
initHeaderUnderstanding();
}
private void initHeaderUnderstanding() {
SOAPHeader soapHeader = ensureSOAPHeader();
if (soapHeader == null) {
return;
}
Iterator allHeaders = soapHeader.examineAllHeaderElements();
while(allHeaders.hasNext()) {
SOAPHeaderElement nextHdrElem = (SOAPHeaderElement) allHeaders.next();
if (nextHdrElem == null) {
continue;
}
if (nextHdrElem.getMustUnderstand()) {
notUnderstood(nextHdrElem.getElementQName());
}
}
}
@Override
public void (Header header) {
understood(header.getNamespaceURI(), header.getLocalPart());
}
@Override
public void (String nsUri, String localName) {
understood(new QName(nsUri, localName));
}
@Override
public void (QName qName) {
if (notUnderstoodCount == null) {
notUnderstoodCount = new HashMap<QName, Integer>();
}
Integer count = notUnderstoodCount.get(qName);
if (count != null && count.intValue() > 0) {
count = count.intValue() - 1;
if (count <= 0) {
notUnderstoodCount.remove(qName);
} else {
notUnderstoodCount.put(qName, count);
}
}
if (understoodHeaders == null) {
understoodHeaders = new HashSet<QName>();
}
understoodHeaders.add(qName);
}
@Override
public boolean (Header header) {
return isUnderstood(header.getNamespaceURI(), header.getLocalPart());
}
@Override
public boolean (String nsUri, String localName) {
return isUnderstood(new QName(nsUri, localName));
}
@Override
public boolean (QName name) {
if (understoodHeaders == null) {
return false;
}
return understoodHeaders.contains(name);
}
public boolean (int index) {
return false;
}
@Override
public Header (String nsUri, String localName, boolean markAsUnderstood) {
SOAPHeaderElement h = find(nsUri, localName);
if (h != null) {
if (markAsUnderstood) {
understood(nsUri, localName);
}
return new SAAJHeader(h);
}
return null;
}
@Override
public Header (QName name, boolean markAsUnderstood) {
return get(name.getNamespaceURI(), name.getLocalPart(), markAsUnderstood);
}
@Override
public Iterator<Header> (QName headerName,
boolean markAsUnderstood) {
return getHeaders(headerName.getNamespaceURI(), headerName.getLocalPart(), markAsUnderstood);
}
@Override
public Iterator<Header> (final String nsUri, final String localName,
final boolean markAsUnderstood) {
SOAPHeader soapHeader = ensureSOAPHeader();
if (soapHeader == null) {
return null;
}
Iterator allHeaders = soapHeader.examineAllHeaderElements();
if (markAsUnderstood) {
List<Header> headers = new ArrayList<Header>();
while (allHeaders.hasNext()) {
SOAPHeaderElement nextHdr = (SOAPHeaderElement) allHeaders.next();
if (nextHdr != null &&
nextHdr.getNamespaceURI().equals(nsUri)) {
if (localName == null ||
nextHdr.getLocalName().equals(localName)) {
understood(nextHdr.getNamespaceURI(), nextHdr.getLocalName());
headers.add(new SAAJHeader(nextHdr));
}
}
}
return headers.iterator();
}
return new HeaderReadIterator(allHeaders, nsUri, localName);
}
@Override
public Iterator<Header> (String nsUri, boolean markAsUnderstood) {
return getHeaders(nsUri, null, markAsUnderstood);
}
@Override
public boolean (Header header) {
try {
header.writeTo(sm);
} catch (SOAPException e) {
return false;
}
notUnderstood(new QName(header.getNamespaceURI(), header.getLocalPart()));
if (isNonSAAJHeader(header)) {
addNonSAAJHeader(find(header.getNamespaceURI(), header.getLocalPart()),
header);
}
return true;
}
@Override
public Header (QName name) {
return remove(name.getNamespaceURI(), name.getLocalPart());
}
@Override
public Header (String nsUri, String localName) {
SOAPHeader soapHeader = ensureSOAPHeader();
if (soapHeader == null) {
return null;
}
SOAPHeaderElement headerElem = find(nsUri, localName);
if (headerElem == null) {
return null;
}
headerElem = (SOAPHeaderElement) soapHeader.removeChild(headerElem);
removeNonSAAJHeader(headerElem);
QName hdrName = (nsUri == null) ? new QName(localName) : new QName(nsUri, localName);
if (understoodHeaders != null) {
understoodHeaders.remove(hdrName);
}
removeNotUnderstood(hdrName);
return new SAAJHeader(headerElem);
}
private void (QName hdrName) {
if (notUnderstoodCount == null) {
return;
}
Integer notUnderstood = notUnderstoodCount.get(hdrName);
if (notUnderstood != null) {
int intNotUnderstood = notUnderstood;
intNotUnderstood--;
if (intNotUnderstood <= 0) {
notUnderstoodCount.remove(hdrName);
}
}
}
private SOAPHeaderElement (QName qName) {
return find(qName.getNamespaceURI(), qName.getLocalPart());
}
private SOAPHeaderElement (String nsUri, String localName) {
SOAPHeader soapHeader = ensureSOAPHeader();
if (soapHeader == null) {
return null;
}
Iterator allHeaders = soapHeader.examineAllHeaderElements();
while(allHeaders.hasNext()) {
SOAPHeaderElement nextHdrElem = (SOAPHeaderElement) allHeaders.next();
if (nextHdrElem.getNamespaceURI().equals(nsUri) &&
nextHdrElem.getLocalName().equals(localName)) {
return nextHdrElem;
}
}
return null;
}
private void (QName qName) {
if (notUnderstoodCount == null) {
notUnderstoodCount = new HashMap<QName, Integer>();
}
Integer count = notUnderstoodCount.get(qName);
if (count == null) {
notUnderstoodCount.put(qName, 1);
} else {
notUnderstoodCount.put(qName, count + 1);
}
if (understoodHeaders != null) {
understoodHeaders.remove(qName);
}
}
private SOAPHeader () {
SOAPHeader header;
try {
header = sm.getSOAPPart().getEnvelope().getHeader();
if (header != null) {
return header;
} else {
return sm.getSOAPPart().getEnvelope().addHeader();
}
} catch (Exception e) {
return null;
}
}
private boolean (Header header) {
return !(header instanceof SAAJHeader);
}
private void (SOAPHeaderElement headerElem, Header header) {
if (nonSAAJHeaders == null) {
nonSAAJHeaders = new HashMap<>();
}
nonSAAJHeaders.put(headerElem, header);
}
private void (SOAPHeaderElement headerElem) {
if (nonSAAJHeaders != null) {
nonSAAJHeaders.remove(headerElem);
}
}
@Override
public boolean (Header header) {
remove(header.getNamespaceURI(), header.getLocalPart());
return add(header);
}
@Override
public void (Header old, Header header) {
if (remove(old.getNamespaceURI(), old.getLocalPart()) == null)
throw new IllegalArgumentException();
add(header);
}
@Override
public Set<QName> () {
return understoodHeaders;
}
@Override
public Set<QName> (Set<String> roles,
Set<QName> knownHeaders, WSBinding binding) {
Set<QName> notUnderstoodHeaderNames = new HashSet<QName>();
if (notUnderstoodCount == null) {
return notUnderstoodHeaderNames;
}
for (QName headerName : notUnderstoodCount.keySet()) {
int count = notUnderstoodCount.get(headerName);
if (count <= 0) {
continue;
}
SOAPHeaderElement hdrElem = find(headerName);
if (!hdrElem.getMustUnderstand()) {
continue;
}
SAAJHeader hdr = new SAAJHeader(hdrElem);
boolean understood = false;
if (roles != null) {
understood = !roles.contains(hdr.getRole(soapVersion));
}
if (understood) {
continue;
}
if (binding != null && binding instanceof SOAPBindingImpl) {
understood = ((SOAPBindingImpl) binding).understandsHeader(headerName);
if (!understood) {
if (knownHeaders != null && knownHeaders.contains(headerName)) {
understood = true;
}
}
}
if (!understood) {
notUnderstoodHeaderNames.add(headerName);
}
}
return notUnderstoodHeaderNames;
}
@Override
public Iterator<Header> () {
SOAPHeader soapHeader = ensureSOAPHeader();
if (soapHeader == null) {
return null;
}
Iterator allHeaders = soapHeader.examineAllHeaderElements();
return new HeaderReadIterator(allHeaders, null, null);
}
private static class implements Iterator<Header> {
SOAPHeaderElement ;
Iterator ;
String ;
String ;
public (Iterator allHeaders, String nsUri,
String localName) {
this.soapHeaders = allHeaders;
this.myNsUri = nsUri;
this.myLocalName = localName;
}
@Override
public boolean () {
if (current == null) {
advance();
}
return (current != null);
}
@Override
public Header () {
if (!hasNext()) {
return null;
}
if (current == null) {
return null;
}
SAAJHeader ret = new SAAJHeader(current);
current = null;
return ret;
}
@Override
public void () {
throw new UnsupportedOperationException();
}
private void () {
while (soapHeaders.hasNext()) {
SOAPHeaderElement nextHdr = (SOAPHeaderElement) soapHeaders.next();
if (nextHdr != null &&
(myNsUri == null || nextHdr.getNamespaceURI().equals(myNsUri)) &&
(myLocalName == null || nextHdr.getLocalName().equals(myLocalName))) {
current = nextHdr;
return;
}
}
current = null;
}
}
@Override
public boolean () {
SOAPHeader soapHeader = ensureSOAPHeader();
if (soapHeader == null) {
return false;
}
Iterator allHeaders = soapHeader.examineAllHeaderElements();
return allHeaders.hasNext();
}
@Override
public List<Header> () {
SOAPHeader soapHeader = ensureSOAPHeader();
if (soapHeader == null) {
return Collections.emptyList();
}
Iterator allHeaders = soapHeader.examineAllHeaderElements();
List<Header> headers = new ArrayList<Header>();
while (allHeaders.hasNext()) {
SOAPHeaderElement nextHdr = (SOAPHeaderElement) allHeaders.next();
headers.add(new SAAJHeader(nextHdr));
}
return headers;
}
}