1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.security.xwss;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.HashMap;
22 import java.util.Map;
23 import javax.xml.soap.MessageFactory;
24 import javax.xml.soap.MimeHeaders;
25 import javax.xml.soap.SOAPException;
26 import javax.xml.soap.SOAPMessage;
27
28 import org.springframework.core.io.ClassPathResource;
29 import org.springframework.core.io.Resource;
30 import org.springframework.ws.soap.saaj.SaajSoapMessage;
31 import org.springframework.xml.xpath.XPathExpression;
32 import org.springframework.xml.xpath.XPathExpressionFactory;
33
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Node;
38
39 import static org.junit.Assert.assertTrue;
40
41 public abstract class AbstractXwssMessageInterceptorTestCase {
42
43 protected XwsSecurityInterceptor interceptor;
44
45 private MessageFactory messageFactory;
46
47 private Map<String, String> namespaces;
48
49 @Before
50 public final void setUp() throws Exception {
51 interceptor = new XwsSecurityInterceptor();
52 messageFactory = MessageFactory.newInstance();
53 namespaces = new HashMap<String, String>(4);
54 namespaces.put("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
55 namespaces.put("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
56 namespaces.put("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
57 namespaces.put("ds", "http://www.w3.org/2000/09/xmldsig#");
58 namespaces.put("xenc", "http://www.w3.org/2001/04/xmlenc#");
59 onSetup();
60 }
61
62 protected void assertXpathEvaluatesTo(String message,
63 String expectedValue,
64 String xpathExpression,
65 SOAPMessage soapMessage) {
66 XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
67 Document document = soapMessage.getSOAPPart();
68 String actualValue = expression.evaluateAsString(document);
69 Assert.assertEquals(message, expectedValue, actualValue);
70 }
71
72 protected void assertXpathExists(String message, String xpathExpression, SOAPMessage soapMessage) {
73 XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
74 Document document = soapMessage.getSOAPPart();
75 Node node = expression.evaluateAsNode(document);
76 Assert.assertNotNull(message, node);
77 }
78
79 protected void assertXpathNotExists(String message, String xpathExpression, SOAPMessage soapMessage) {
80 XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
81 Document document = soapMessage.getSOAPPart();
82 Node node = expression.evaluateAsNode(document);
83 Assert.assertNull(message, node);
84 }
85
86 protected SaajSoapMessage loadSaajMessage(String fileName) throws SOAPException, IOException {
87 MimeHeaders mimeHeaders = new MimeHeaders();
88 mimeHeaders.addHeader("Content-Type", "text/xml");
89 Resource resource = new ClassPathResource(fileName, getClass());
90 InputStream is = resource.getInputStream();
91 try {
92 assertTrue("Could not load SAAJ message [" + resource + "]", resource.exists());
93 is = resource.getInputStream();
94 return new SaajSoapMessage(messageFactory.createMessage(mimeHeaders, is));
95 }
96 finally {
97 is.close();
98 }
99 }
100
101 protected void onSetup() throws Exception {
102 }
103 }