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("ds", "http://www.w3.org/2000/09/xmldsig#");
57 namespaces.put("xenc", "http://www.w3.org/2001/04/xmlenc#");
58 onSetup();
59 }
60
61 protected void assertXpathEvaluatesTo(String message,
62 String expectedValue,
63 String xpathExpression,
64 SOAPMessage soapMessage) {
65 XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
66 Document document = soapMessage.getSOAPPart();
67 String actualValue = expression.evaluateAsString(document);
68 Assert.assertEquals(message, expectedValue, actualValue);
69 }
70
71 protected void assertXpathExists(String message, String xpathExpression, SOAPMessage soapMessage) {
72 XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
73 Document document = soapMessage.getSOAPPart();
74 Node node = expression.evaluateAsNode(document);
75 Assert.assertNotNull(message, node);
76 }
77
78 protected void assertXpathNotExists(String message, String xpathExpression, SOAPMessage soapMessage) {
79 XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
80 Document document = soapMessage.getSOAPPart();
81 Node node = expression.evaluateAsNode(document);
82 Assert.assertNull(message, node);
83 }
84
85 protected SaajSoapMessage loadSaajMessage(String fileName) throws SOAPException, IOException {
86 MimeHeaders mimeHeaders = new MimeHeaders();
87 mimeHeaders.addHeader("Content-Type", "text/xml");
88 Resource resource = new ClassPathResource(fileName, getClass());
89 InputStream is = resource.getInputStream();
90 try {
91 assertTrue("Could not load SAAJ message [" + resource + "]", resource.exists());
92 is = resource.getInputStream();
93 return new SaajSoapMessage(messageFactory.createMessage(mimeHeaders, is));
94 }
95 finally {
96 is.close();
97 }
98 }
99
100 protected void onSetup() throws Exception {
101 }
102 }