Added a SOAPEnvelope#getOrCreateHeader method (which is more in line with what one usually wants to do).
diff --git a/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPEnvelope.java b/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPEnvelope.java
index b2646e6..fc57612 100644
--- a/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPEnvelope.java
+++ b/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPEnvelope.java
@@ -35,6 +35,15 @@
SOAPHeader getHeader();
/**
+ * Returns the existing {@link SOAPHeader} object for this envelope or creates a new one if
+ * there is none. Similarly to {@link #getHeader()}, this method avoids building the
+ * {@link SOAPBody}.
+ *
+ * @return the existing or newly created {@link SOAPHeader} object for this envelope
+ */
+ SOAPHeader getOrCreateHeader();
+
+ /**
* Returns the <CODE>SOAPBody</CODE> object associated with this <CODE>SOAPEnvelope</CODE>
* object. <P> This SOAPBody will just be a container for all the BodyElements in the
* <CODE>OMMessage</CODE> </P>
diff --git a/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPFactory.java b/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPFactory.java
index 1a20d5a..9989637 100644
--- a/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPFactory.java
+++ b/modules/axiom-api/src/main/java/org/apache/axiom/soap/SOAPFactory.java
@@ -63,8 +63,14 @@
SOAPEnvelope createSOAPEnvelope(OMNamespace ns);
/**
+ * Create a {@link SOAPHeader} as a child of the given {@link SOAPEnvelope}.
+ * <p>
+ * Note that for most use cases, it is preferable to use
+ * {@link SOAPEnvelope#getOrCreateHeader()} instead of this method.
+ *
* @param envelope
- * @return Returns SOAPHeader.
+ * the parent of the {@link SOAPHeader}
+ * @return the newly created {@link SOAPHeader}
*/
SOAPHeader createSOAPHeader(SOAPEnvelope envelope) throws SOAPProcessingException;
diff --git a/modules/axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/SOAPEnvelopeImpl.java b/modules/axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/SOAPEnvelopeImpl.java
index 1fd6909..07a4c7b 100644
--- a/modules/axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/SOAPEnvelopeImpl.java
+++ b/modules/axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/SOAPEnvelopeImpl.java
@@ -72,6 +72,11 @@
return (SOAPHeader) header;
}
+ public SOAPHeader getOrCreateHeader() {
+ SOAPHeader header = getHeader();
+ return header != null ? header : ((SOAPFactory)factory).createSOAPHeader(this);
+ }
+
/**
* Check that a node is allowed as a child of a SOAP envelope.
*
diff --git a/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPEnvelopeImpl.java b/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPEnvelopeImpl.java
index d12a5a3..58d7a8f 100644
--- a/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPEnvelopeImpl.java
+++ b/modules/axiom-impl/src/main/java/org/apache/axiom/soap/impl/llom/SOAPEnvelopeImpl.java
@@ -88,6 +88,11 @@
return null;
}
+ public SOAPHeader getOrCreateHeader() {
+ SOAPHeader header = getHeader();
+ return header != null ? header : ((SOAPFactory)factory).createSOAPHeader(this);
+ }
+
/**
* Check that a node is allowed as a child of a SOAP envelope.
*
diff --git a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/SOAPTestSuiteBuilder.java b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/SOAPTestSuiteBuilder.java
index 0e3f557..60e11c6 100644
--- a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/SOAPTestSuiteBuilder.java
+++ b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/SOAPTestSuiteBuilder.java
@@ -124,6 +124,8 @@
addTest(new org.apache.axiom.ts.soap.envelope.TestGetHeader(metaFactory, spec));
addTest(new org.apache.axiom.ts.soap.envelope.TestGetHeaderWithParser(metaFactory, spec));
addTest(new org.apache.axiom.ts.soap.envelope.TestGetHeaderWithParserNoHeader(metaFactory, spec));
+ addTest(new org.apache.axiom.ts.soap.envelope.TestGetOrCreateHeader(metaFactory, spec));
+ addTest(new org.apache.axiom.ts.soap.envelope.TestGetOrCreateHeaderWithParserNoHeader(metaFactory, spec));
for (int i=0; i<generalQNames.length; i++) {
QName qname = generalQNames[i];
addTest(new org.apache.axiom.ts.soap.envelope.TestGetSOAPBodyFirstElementLocalNameAndNS(metaFactory, spec, qname));
diff --git a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/envelope/TestGetOrCreateHeader.java b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/envelope/TestGetOrCreateHeader.java
new file mode 100644
index 0000000..3e6a29b
--- /dev/null
+++ b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/envelope/TestGetOrCreateHeader.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.ts.soap.envelope;
+
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAPHeader;
+import org.apache.axiom.ts.soap.SOAPSpec;
+import org.apache.axiom.ts.soap.SOAPTestCase;
+
+public class TestGetOrCreateHeader extends SOAPTestCase {
+ public TestGetOrCreateHeader(OMMetaFactory metaFactory, SOAPSpec spec) {
+ super(metaFactory, spec);
+ }
+
+ protected void runTest() throws Throwable {
+ SOAPEnvelope envelope = soapFactory.getDefaultEnvelope();
+ SOAPHeader header = (SOAPHeader)envelope.getFirstOMChild();
+ assertSame(header, envelope.getOrCreateHeader());
+ }
+}
diff --git a/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/envelope/TestGetOrCreateHeaderWithParserNoHeader.java b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/envelope/TestGetOrCreateHeaderWithParserNoHeader.java
new file mode 100644
index 0000000..83d1cac
--- /dev/null
+++ b/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap/envelope/TestGetOrCreateHeaderWithParserNoHeader.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.ts.soap.envelope;
+
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.soap.SOAPBody;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAPHeader;
+import org.apache.axiom.ts.soap.SOAPSpec;
+import org.apache.axiom.ts.soap.SOAPTestCase;
+
+/**
+ * Tests the behavior of {@link SOAPEnvelope#getOrCreateHeader()} on a message that has no header.
+ * It checks that the call creates a new {@link SOAPHeader} and that it doesn't build the
+ * {@link SOAPBody}.
+ */
+public class TestGetOrCreateHeaderWithParserNoHeader extends SOAPTestCase {
+ public TestGetOrCreateHeaderWithParserNoHeader(OMMetaFactory metaFactory, SOAPSpec spec) {
+ super(metaFactory, spec);
+ }
+
+ protected void runTest() throws Throwable {
+ SOAPEnvelope envelope = getTestMessage(MESSAGE_WITHOUT_HEADER);
+ SOAPHeader header = envelope.getOrCreateHeader();
+ assertNotNull(header);
+ assertSame(envelope.getFirstElement(), header);
+ assertFalse(envelope.getBody().isComplete());
+ }
+}