Skip to content

Commit ec15995

Browse files
committed
SWS-483: Wss4j 1.5.5 stripping custom SOAP headers after 1.5.6 upgrade
1 parent fd996a7 commit ec15995

File tree

4 files changed

+103
-23
lines changed

4 files changed

+103
-23
lines changed

core/src/main/java/org/springframework/ws/soap/axiom/support/AxiomUtils.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.ByteArrayOutputStream;
21+
import java.io.IOException;
22+
import java.io.OutputStream;
2123
import java.util.Iterator;
2224
import java.util.Locale;
2325
import javax.xml.namespace.QName;
@@ -164,7 +166,12 @@ public static SOAPEnvelope toEnvelope(Document document) {
164166

165167
StAXSOAPModelBuilder stAXSOAPModelBuilder =
166168
new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(bis), null);
167-
return stAXSOAPModelBuilder.getSOAPEnvelope();
169+
SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();
170+
171+
// Necessary to build a correct Axiom tree, see SWS-483
172+
envelope.serialize(new NullOutputStream());
173+
174+
return envelope;
168175
}
169176
catch (Exception ex) {
170177
IllegalArgumentException iaex =
@@ -174,4 +181,17 @@ public static SOAPEnvelope toEnvelope(Document document) {
174181
}
175182
}
176183

184+
/** OutputStream that does nothing. */
185+
private static class NullOutputStream extends OutputStream {
186+
187+
public void write(int b) throws IOException {
188+
}
189+
190+
public void write(byte[] b) throws IOException {
191+
}
192+
193+
public void write(byte[] b, int off, int len) throws IOException {
194+
}
195+
}
196+
177197
}

security/src/test/java/org/springframework/ws/soap/security/wss4j/Wss4jMessageInterceptorHeaderTestCase.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.ws.soap.security.wss4j;
1818

19+
import java.io.ByteArrayOutputStream;
1920
import java.util.Iterator;
2021
import java.util.Properties;
2122
import javax.xml.namespace.QName;
@@ -79,4 +80,40 @@ public void testEmptySecurityHeader() throws Exception {
7980
// expected
8081
}
8182
}
83+
84+
public void testPreserveCustomHeaders() throws Exception {
85+
interceptor.setSecurementActions("UsernameToken");
86+
interceptor.setSecurementUsername("Bert");
87+
interceptor.setSecurementPassword("Ernie");
88+
89+
ByteArrayOutputStream os = new ByteArrayOutputStream();
90+
SoapMessage message = loadMessage("customHeader-soap.xml");
91+
MessageContext messageContext = new DefaultMessageContext(message, getMessageFactory());
92+
message.writeTo(os);
93+
String document = os.toString("UTF-8");
94+
assertXpathEvaluatesTo("Header 1 does not exist", "test1", "/SOAP-ENV:Envelope/SOAP-ENV:Header/test:header1",
95+
document);
96+
assertXpathNotExists("Header 2 exist", "/SOAP-ENV:Envelope/SOAP-ENV:Header/test:header2", document);
97+
98+
interceptor.secureMessage(message, messageContext);
99+
100+
SoapHeaderElement element = message.getSoapHeader().addHeaderElement(new QName("http://test", "header2"));
101+
element.setText("test2");
102+
103+
os = new ByteArrayOutputStream();
104+
message.writeTo(os);
105+
document = os.toString("UTF-8");
106+
assertXpathEvaluatesTo("Header 1 does not exist", "test1", "/SOAP-ENV:Envelope/SOAP-ENV:Header/test:header1",
107+
document);
108+
assertXpathEvaluatesTo("Header 2 does not exist", "test2", "/SOAP-ENV:Envelope/SOAP-ENV:Header/test:header2",
109+
document);
110+
111+
os = new ByteArrayOutputStream();
112+
message.writeTo(os);
113+
document = os.toString("UTF-8");
114+
assertXpathEvaluatesTo("Header 1 does not exist", "test1", "/SOAP-ENV:Envelope/SOAP-ENV:Header/test:header1",
115+
document);
116+
assertXpathEvaluatesTo("Header 2 does not exist", "test2", "/SOAP-ENV:Envelope/SOAP-ENV:Header/test:header2",
117+
document);
118+
}
82119
}

security/src/test/java/org/springframework/ws/soap/security/wss4j/Wss4jTestCase.java

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,13 @@
1616
package org.springframework.ws.soap.security.wss4j;
1717

1818
import java.io.InputStream;
19-
import java.util.HashMap;
20-
import java.util.Map;
19+
import java.util.Properties;
2120
import javax.xml.soap.MessageFactory;
2221
import javax.xml.soap.MimeHeaders;
2322
import javax.xml.soap.SOAPMessage;
2423
import javax.xml.stream.XMLInputFactory;
2524
import javax.xml.stream.XMLStreamReader;
25+
import javax.xml.transform.dom.DOMSource;
2626

2727
import junit.framework.TestCase;
2828
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
@@ -41,8 +41,8 @@
4141
import org.springframework.ws.soap.axiom.support.AxiomUtils;
4242
import org.springframework.ws.soap.saaj.SaajSoapMessage;
4343
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
44-
import org.springframework.xml.xpath.XPathExpression;
45-
import org.springframework.xml.xpath.XPathExpressionFactory;
44+
import org.springframework.xml.transform.StringSource;
45+
import org.springframework.xml.xpath.Jaxp13XPathTemplate;
4646

4747
public abstract class Wss4jTestCase extends TestCase {
4848

@@ -52,43 +52,57 @@ public abstract class Wss4jTestCase extends TestCase {
5252

5353
protected final boolean saajTest = this.getClass().getSimpleName().startsWith("Saaj");
5454

55-
protected Map namespaces;
55+
protected Jaxp13XPathTemplate xpathTemplate = new Jaxp13XPathTemplate();
5656

5757
protected final void setUp() throws Exception {
5858
if (!axiomTest && !saajTest) {
5959
throw new IllegalArgumentException("test class name must statrt with either Axiom or Saaj");
6060
}
6161
messageFactory = MessageFactory.newInstance();
62-
namespaces = new HashMap();
63-
namespaces.put("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
64-
namespaces.put("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
65-
namespaces.put("ds", "http://www.w3.org/2000/09/xmldsig#");
66-
namespaces.put("xenc", "http://www.w3.org/2001/04/xmlenc#");
62+
Properties namespaces = new Properties();
63+
namespaces.setProperty("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
64+
namespaces.setProperty("wsse",
65+
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
66+
namespaces.setProperty("ds", "http://www.w3.org/2000/09/xmldsig#");
67+
namespaces.setProperty("xenc", "http://www.w3.org/2001/04/xmlenc#");
6768
// namespaces.put("wsse11", "http://docs.oasis-open.org/wss/2005/xx/oasis-2005xx-wss-wssecurity-secext-1.1.xsd");
68-
namespaces.put("wsse11", "http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd");
69-
namespaces.put("echo", "http://www.springframework.org/spring-ws/samples/echo");
70-
namespaces.put("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
69+
namespaces.setProperty("wsse11", "http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd");
70+
namespaces.setProperty("echo", "http://www.springframework.org/spring-ws/samples/echo");
71+
namespaces.setProperty("wsu",
72+
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
73+
namespaces.setProperty("test", "http://test");
74+
xpathTemplate.setNamespaces(namespaces);
7175
onSetup();
7276
}
7377

7478
protected void assertXpathEvaluatesTo(String message,
7579
String expectedValue,
7680
String xpathExpression,
7781
Document document) {
78-
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
79-
String actualValue = expression.evaluateAsString(document);
82+
String actualValue = xpathTemplate.evaluateAsString(xpathExpression, new DOMSource(document));
83+
assertEquals(message, expectedValue, actualValue);
84+
}
85+
86+
protected void assertXpathEvaluatesTo(String message,
87+
String expectedValue,
88+
String xpathExpression,
89+
String document) {
90+
String actualValue = xpathTemplate.evaluateAsString(xpathExpression, new StringSource(document));
8091
assertEquals(message, expectedValue, actualValue);
8192
}
8293

8394
protected void assertXpathExists(String message, String xpathExpression, Document document) {
84-
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
85-
Node node = expression.evaluateAsNode(document);
95+
Node node = xpathTemplate.evaluateAsNode(xpathExpression, new DOMSource(document));
8696
assertNotNull(message, node);
8797
}
8898

8999
protected void assertXpathNotExists(String message, String xpathExpression, Document document) {
90-
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
91-
Node node = expression.evaluateAsNode(document);
100+
Node node = xpathTemplate.evaluateAsNode(xpathExpression, new DOMSource(document));
101+
assertNull(message, node);
102+
}
103+
104+
protected void assertXpathNotExists(String message, String xpathExpression, String document) {
105+
Node node = xpathTemplate.evaluateAsNode(xpathExpression, new StringSource(document));
92106
assertNull(message, node);
93107
}
94108

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
3+
<SOAP-ENV:Header>
4+
<header1 xmlns="http://test">test1</header1>
5+
</SOAP-ENV:Header>
6+
<SOAP-ENV:Body>
7+
<tru:StockSymbol xmlns:tru="http://fabrikam123.com/payloads">QQQ</tru:StockSymbol>
8+
</SOAP-ENV:Body>
9+
</SOAP-ENV:Envelope>

0 commit comments

Comments
 (0)