From 54057b1332c95b01e3dddf2be33ed7144f5d5ddd Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 17 Oct 2011 07:45:18 +0000 Subject: [PATCH] SWS-710 - Make it possible to retrieve SoapHeaderElement from SoapHeader by QName --- .../springframework/ws/soap/SoapHeader.java | 34 ++++++++++++------- .../ws/soap/saaj/SaajSoapHeader.java | 15 ++++++-- .../ws/soap/AbstractSoapHeaderTestCase.java | 18 ++++++++-- .../ws/soap/stroap/StroapHeader.java | 14 ++++++-- 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/springframework/ws/soap/SoapHeader.java b/core/src/main/java/org/springframework/ws/soap/SoapHeader.java index e1e9d687..04f8a2ea 100644 --- a/core/src/main/java/org/springframework/ws/soap/SoapHeader.java +++ b/core/src/main/java/org/springframework/ws/soap/SoapHeader.java @@ -1,11 +1,11 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -21,7 +21,7 @@ import javax.xml.namespace.QName; import javax.xml.transform.Result; /** - * Represents the Header element in a SOAP message. A SOAP header contains SoapHeaderElements, + * Represents the {@code Header} element in a SOAP message. A SOAP header contains {@code SoapHeaderElement}s, * which represent the individual headers. * * @author Arjen Poutsma @@ -32,25 +32,25 @@ import javax.xml.transform.Result; public interface SoapHeader extends SoapElement { /** - * Returns a Result that represents the contents of the header. + * Returns a {@code Result} that represents the contents of the header. *

* The result can be used for marshalling. * - * @return the Result of this element + * @return the {@code Result} of this element */ Result getResult(); /** - * Adds a new SoapHeaderElement with the specified qualified name to this header. + * Adds a new {@code SoapHeaderElement} with the specified qualified name to this header. * * @param name the qualified name of the new header element - * @return the created SoapHeaderElement + * @return the created {@code SoapHeaderElement} * @throws SoapHeaderException if the header cannot be created */ SoapHeaderElement addHeaderElement(QName name) throws SoapHeaderException; /** - * Removes the SoapHeaderElement with the specified qualified name from this header. + * Removes the {@code SoapHeaderElement} with the specified qualified name from this header. *

* This method will only remove the first child element with the specified name. If no element is found with the * specified name, this method has no effect. @@ -61,24 +61,34 @@ public interface SoapHeader extends SoapElement { void removeHeaderElement(QName name) throws SoapHeaderException; /** - * Returns an Iterator over all the SoapHeaderElements that have the specified actor or - * role and that have a MustUnderstand attribute whose value is equivalent to true. + * Returns an {@code Iterator} over all the {@code SoapHeaderElement}s that have the specified actor or + * role and that have a {@code MustUnderstand} attribute whose value is equivalent to {@code true}. * * @param actorOrRole the actor (SOAP 1.1) or role (SOAP 1.2) for which to search * @return an iterator over all the header elements that contain the specified actor/role and are marked as - * MustUnderstand + * {@code MustUnderstand} * @throws SoapHeaderException if the headers cannot be returned * @see SoapHeaderElement */ Iterator examineMustUnderstandHeaderElements(String actorOrRole) throws SoapHeaderException; /** - * Returns an Iterator over all the SoapHeaderElements in this header. + * Returns an {@code Iterator} over all the {@code SoapHeaderElement}s in this header. * * @return an iterator over all the header elements * @throws SoapHeaderException if the header cannot be returned * @see SoapHeaderElement */ Iterator examineAllHeaderElements() throws SoapHeaderException; + + /** + * Returns an {@code Iterator} over all the {@code SoapHeaderElement}s with the given qualified name in this header. + * + * @param name the qualified name for which to search + * @return an iterator over all the header elements + * @throws SoapHeaderException if the header cannot be returned + * @see SoapHeaderElement + */ + Iterator examineHeaderElements(QName name) throws SoapHeaderException; } diff --git a/core/src/main/java/org/springframework/ws/soap/saaj/SaajSoapHeader.java b/core/src/main/java/org/springframework/ws/soap/saaj/SaajSoapHeader.java index 2e29ec39..bea30637 100644 --- a/core/src/main/java/org/springframework/ws/soap/saaj/SaajSoapHeader.java +++ b/core/src/main/java/org/springframework/ws/soap/saaj/SaajSoapHeader.java @@ -1,11 +1,11 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -46,6 +46,17 @@ abstract class SaajSoapHeader extends SaajSoapElement implements Soa return new SaajSoapHeaderElementIterator(iterator); } + @SuppressWarnings("unchecked") + 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); diff --git a/core/src/test/java/org/springframework/ws/soap/AbstractSoapHeaderTestCase.java b/core/src/test/java/org/springframework/ws/soap/AbstractSoapHeaderTestCase.java index 4b62ae57..b66faa64 100644 --- a/core/src/test/java/org/springframework/ws/soap/AbstractSoapHeaderTestCase.java +++ b/core/src/test/java/org/springframework/ws/soap/AbstractSoapHeaderTestCase.java @@ -1,11 +1,11 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -88,6 +88,20 @@ public abstract class AbstractSoapHeaderTestCase extends AbstractSoapElementTest assertFalse("header element iterator has too many elements", iterator.hasNext()); } + @Test + public void testExamineHeaderElementWithName() throws Exception { + QName name1 = new QName(NAMESPACE, "name1", PREFIX); + QName name2 = new QName(NAMESPACE, "name2", PREFIX); + soapHeader.addHeaderElement(name1); + soapHeader.addHeaderElement(name2); + Iterator iterator = soapHeader.examineHeaderElements(name1); + assertNotNull("header element iterator is null", iterator); + assertTrue("header element iterator has no elements", iterator.hasNext()); + SoapHeaderElement headerElement = iterator.next(); + assertEquals("Invalid qName for element", name1, headerElement.getName()); + assertFalse("header element iterator has too many elements", iterator.hasNext()); + } + @Test public void testExamineMustUnderstandHeaderElements() throws Exception { QName qName1 = new QName(NAMESPACE, "localName1", PREFIX); diff --git a/sandbox/src/main/java/org/springframework/ws/soap/stroap/StroapHeader.java b/sandbox/src/main/java/org/springframework/ws/soap/stroap/StroapHeader.java index 97a7aaf5..dbd82791 100644 --- a/sandbox/src/main/java/org/springframework/ws/soap/stroap/StroapHeader.java +++ b/sandbox/src/main/java/org/springframework/ws/soap/stroap/StroapHeader.java @@ -1,11 +1,11 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -84,6 +84,16 @@ abstract class StroapHeader extends StroapElement implements SoapHeader { return headerElements.iterator(); } + public Iterator examineHeaderElements(QName name) throws SoapHeaderException { + List result = new LinkedList(); + for (StroapHeaderElement headerElement : this.headerElements) { + if (headerElement.getName().equals(name)) { + result.add(headerElement); + } + } + return result.iterator(); + } + public Iterator examineMustUnderstandHeaderElements(String actorOrRole) throws SoapHeaderException { List result = new LinkedList();