added method for getting all header elements with a given name

This commit is contained in:
Arjen Poutsma
2007-05-13 12:32:17 +00:00
parent 393ecc6968
commit c45824d2d8
8 changed files with 59 additions and 1 deletions

View File

@@ -68,4 +68,14 @@ public interface SoapHeader extends SoapElement {
* @see SoapHeaderElement
*/
Iterator examineAllHeaderElements() throws SoapHeaderException;
/**
* Returns an <code>Iterator</code> over all the <code>SoapHeaderElement</code>s with the specified header.
*
* @param name the qualified name of header elements to look for
* @return an iterator over all the header elements
* @throws SoapHeaderException if the header cannot be returned
* @see SoapHeaderElement
*/
Iterator examineHeaderElements(QName name) throws SoapHeaderException;
}

View File

@@ -28,6 +28,7 @@ import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.SOAPHeaderBlock;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.SoapHeaderException;
import org.springframework.xml.namespace.QNameUtils;
/**
@@ -73,7 +74,15 @@ class AxiomSoapHeader extends AxiomSoapElement implements SoapHeader {
catch (OMException ex) {
throw new AxiomSoapHeaderException(ex);
}
}
public Iterator examineHeaderElements(QName name) throws SoapHeaderException {
try {
return new AxiomSoapHeaderElementIterator(getAxiomHeader().getChildrenWithName(name));
}
catch (OMException ex) {
throw new AxiomSoapHeaderException(ex);
}
}
protected SOAPHeader getAxiomHeader() {

View File

@@ -100,6 +100,11 @@ public class Saaj11Implementation implements SaajImplementation {
return results.iterator();
}
public Iterator getChildElements(SOAPElement element, QName name) throws SOAPException {
Name childName = SaajUtils.toName(name, element);
return element.getChildElements(childName);
}
public QName getFaultCode(SOAPFault fault) {
String code = fault.getFaultCode();
int idx = code.indexOf(':');

View File

@@ -123,6 +123,11 @@ public class Saaj12Implementation implements SaajImplementation {
return results.iterator();
}
public Iterator getChildElements(SOAPElement element, QName name) throws SOAPException {
Name childName = SaajUtils.toName(name, element);
return element.getChildElements(childName);
}
public SOAPEnvelope getEnvelope(SOAPMessage message) throws SOAPException {
return message.getSOAPPart().getEnvelope();
}

View File

@@ -151,6 +151,10 @@ public class Saaj13Implementation implements SaajImplementation {
return element.getAllAttributesAsQNames();
}
public Iterator getChildElements(SOAPElement element, QName name) throws SOAPException {
return element.getChildElements(name);
}
public SOAPEnvelope getEnvelope(SOAPMessage message) throws SOAPException {
return message.getSOAPPart().getEnvelope();
}

View File

@@ -62,6 +62,9 @@ public interface SaajImplementation {
/** Returns all attributes as an iterator of QNames. * */
Iterator getAllAttibutes(SOAPElement element);
/** Returns an iterator over the child elements with the given name. */
Iterator getChildElements(SOAPElement element, QName name) throws SOAPException;
/** Returns the envelope of the given message. */
SOAPEnvelope getEnvelope(SOAPMessage message) throws SOAPException;

View File

@@ -44,6 +44,16 @@ class SaajSoapHeader extends SaajSoapElement implements SoapHeader {
return new SaajSoapHeaderElementIterator(iterator);
}
public Iterator examineHeaderElements(QName name) throws SoapHeaderException {
try {
Iterator iterator = getImplementation().getChildElements(getSaajHeader(), name);
return new SaajSoapHeaderElementIterator(iterator);
}
catch (SOAPException ex) {
throw new SaajSoapHeaderException(ex);
}
}
public Iterator examineMustUnderstandHeaderElements(String actorOrRole) throws SoapHeaderException {
Iterator iterator = getImplementation().examineMustUnderstandHeaderElements(getSaajHeader(), actorOrRole);
return new SaajSoapHeaderElementIterator(iterator);

View File

@@ -36,8 +36,8 @@ public abstract class AbstractSoapHeaderTestCase extends AbstractSoapElementTest
public void testAddHeaderElement() throws Exception {
QName qName = new QName("http://www.springframework.org", "localName", "spring");
SoapHeaderElement headerElement = soapHeader.addHeaderElement(qName);
assertEquals("Invalid qName for element", qName, headerElement.getName());
assertNotNull("No SoapHeaderElement returned", headerElement);
assertEquals("Invalid qName for element", qName, headerElement.getName());
String payload = "<content xmlns='http://www.springframework.org'/>";
transformer.transform(new StringSource(payload), headerElement.getResult());
assertHeaderElementEqual(headerElement,
@@ -94,10 +94,22 @@ public abstract class AbstractSoapHeaderTestCase extends AbstractSoapElementTest
assertHeaderElementEqual(headerElement, content);
}
public void testExamineHeaderElements() throws Exception {
QName qName = new QName("http://www.springframework.org", "localName", "spring");
soapHeader.addHeaderElement(qName);
Iterator iterator = soapHeader.examineHeaderElements(qName);
assertNotNull("No Iterator returned", iterator);
assertTrue("header element iterator has no elements", iterator.hasNext());
SoapHeaderElement headerElement = (SoapHeaderElement) iterator.next();
assertNotNull("No SoapHeaderElement returned", headerElement);
assertEquals("Invalid qName for element", qName, headerElement.getName());
}
protected void assertHeaderElementEqual(SoapHeaderElement headerElement, String expected) throws Exception {
StringResult result = new StringResult();
transformer.transform(headerElement.getSource(), result);
assertXMLEqual("Invalid contents of header element", expected, result.toString());
}
}