1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.wss4j;
18  
19  import java.io.InputStream;
20  import java.util.HashMap;
21  import java.util.Map;
22  import javax.xml.soap.MessageFactory;
23  import javax.xml.soap.MimeHeaders;
24  import javax.xml.soap.SOAPConstants;
25  import javax.xml.soap.SOAPMessage;
26  import javax.xml.stream.XMLInputFactory;
27  import javax.xml.stream.XMLStreamReader;
28  import javax.xml.transform.dom.DOMSource;
29  
30  import org.springframework.core.io.ClassPathResource;
31  import org.springframework.core.io.Resource;
32  import org.springframework.ws.WebServiceMessage;
33  import org.springframework.ws.context.DefaultMessageContext;
34  import org.springframework.ws.context.MessageContext;
35  import org.springframework.ws.soap.SoapMessage;
36  import org.springframework.ws.soap.SoapMessageFactory;
37  import org.springframework.ws.soap.SoapVersion;
38  import org.springframework.ws.soap.axiom.AxiomSoapMessage;
39  import org.springframework.ws.soap.axiom.AxiomSoapMessageFactory;
40  import org.springframework.ws.soap.axiom.support.AxiomUtils;
41  import org.springframework.ws.soap.saaj.SaajSoapMessage;
42  import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
43  import org.springframework.xml.transform.StringSource;
44  import org.springframework.xml.xpath.Jaxp13XPathTemplate;
45  
46  import org.apache.axiom.soap.SOAP12Constants;
47  import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
48  import org.junit.Assert;
49  import org.junit.Before;
50  import org.w3c.dom.Document;
51  import org.w3c.dom.Node;
52  
53  import static org.junit.Assert.assertTrue;
54  
55  public abstract class Wss4jTestCase {
56  
57      protected MessageFactory saajSoap11MessageFactory;
58  
59      protected MessageFactory saajSoap12MessageFactory;
60  
61      protected final boolean axiomTest = this.getClass().getSimpleName().startsWith("Axiom");
62  
63      protected final boolean saajTest = this.getClass().getSimpleName().startsWith("Saaj");
64  
65      protected Jaxp13XPathTemplate xpathTemplate = new Jaxp13XPathTemplate();
66  
67      @Before
68      public final void setUp() throws Exception {
69          if (!axiomTest && !saajTest) {
70              throw new IllegalArgumentException("test class name must start with either Axiom or Saaj");
71          }
72          saajSoap11MessageFactory = MessageFactory.newInstance();
73          saajSoap12MessageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
74          Map<String, String> namespaces = new HashMap<String, String>();
75          namespaces.put("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
76          namespaces.put("wsse",
77                  "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
78          namespaces.put("ds", "http://www.w3.org/2000/09/xmldsig#");
79          namespaces.put("xenc", "http://www.w3.org/2001/04/xmlenc#");
80          namespaces.put("wsse11", "http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd");
81          namespaces.put("echo", "http://www.springframework.org/spring-ws/samples/echo");
82          namespaces.put("wsu",
83                  "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
84          namespaces.put("test", "http://test");
85          xpathTemplate.setNamespaces(namespaces);
86          onSetup();
87      }
88  
89      protected void assertXpathEvaluatesTo(String message,
90                                            String expectedValue,
91                                            String xpathExpression,
92                                            Document document) {
93          String actualValue = xpathTemplate.evaluateAsString(xpathExpression, new DOMSource(document));
94          Assert.assertEquals(message, expectedValue, actualValue);
95      }
96  
97      protected void assertXpathEvaluatesTo(String message,
98                                            String expectedValue,
99                                            String xpathExpression,
100                                           String document) {
101         String actualValue = xpathTemplate.evaluateAsString(xpathExpression, new StringSource(document));
102         Assert.assertEquals(message, expectedValue, actualValue);
103     }
104 
105     protected void assertXpathExists(String message, String xpathExpression, Document document) {
106         Node node = xpathTemplate.evaluateAsNode(xpathExpression, new DOMSource(document));
107         Assert.assertNotNull(message, node);
108     }
109 
110     protected void assertXpathNotExists(String message, String xpathExpression, Document document) {
111         Node node = xpathTemplate.evaluateAsNode(xpathExpression, new DOMSource(document));
112         Assert.assertNull(message, node);
113     }
114 
115     protected void assertXpathNotExists(String message, String xpathExpression, String document) {
116         Node node = xpathTemplate.evaluateAsNode(xpathExpression, new StringSource(document));
117         Assert.assertNull(message, node);
118     }
119 
120     protected SaajSoapMessage loadSaaj11Message(String fileName) throws Exception {
121         MimeHeaders mimeHeaders = new MimeHeaders();
122         mimeHeaders.addHeader("Content-Type", "text/xml");
123         Resource resource = new ClassPathResource(fileName, getClass());
124         InputStream is = resource.getInputStream();
125         try {
126             assertTrue("Could not load SAAJ message [" + resource + "]", resource.exists());
127             is = resource.getInputStream();
128             return new SaajSoapMessage(saajSoap11MessageFactory.createMessage(mimeHeaders, is), saajSoap11MessageFactory);
129         }
130         finally {
131             is.close();
132         }
133     }
134     
135     protected SaajSoapMessage loadSaaj12Message(String fileName) throws Exception {
136         MimeHeaders mimeHeaders = new MimeHeaders();
137         mimeHeaders.addHeader("Content-Type", "application/soap+xml");
138         Resource resource = new ClassPathResource(fileName, getClass());
139         InputStream is = resource.getInputStream();
140         try {
141             assertTrue("Could not load SAAJ message [" + resource + "]", resource.exists());
142             is = resource.getInputStream();
143             return new SaajSoapMessage(saajSoap12MessageFactory.createMessage(mimeHeaders, is), saajSoap12MessageFactory);
144         }
145         finally {
146             is.close();
147         }
148     }
149 
150     protected AxiomSoapMessage loadAxiom11Message(String fileName) throws Exception {
151         Resource resource = new ClassPathResource(fileName, getClass());
152         InputStream is = resource.getInputStream();
153         try {
154             assertTrue("Could not load Axiom message [" + resource + "]", resource.exists());
155             is = resource.getInputStream();
156 
157             XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(is);
158             StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(parser, null);
159             org.apache.axiom.soap.SOAPMessage soapMessage = builder.getSoapMessage();
160             return new AxiomSoapMessage(soapMessage, "", true, true);
161         }
162         finally {
163             is.close();
164         }
165     }
166 
167      @SuppressWarnings("Since15")
168      protected AxiomSoapMessage loadAxiom12Message(String fileName) throws Exception {
169         Resource resource = new ClassPathResource(fileName, getClass());
170         InputStream is = resource.getInputStream();
171         try {
172             assertTrue("Could not load Axiom message [" + resource + "]", resource.exists());
173             is = resource.getInputStream();
174 
175             XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(is);
176             StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(parser, SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
177             org.apache.axiom.soap.SOAPMessage soapMessage = builder.getSoapMessage();
178             return new AxiomSoapMessage(soapMessage, "", true, true);
179         }
180         finally {
181             is.close();
182         }
183     }
184 
185     protected Object getMessage(SoapMessage soapMessage) {
186         if (soapMessage instanceof SaajSoapMessage) {
187             return ((SaajSoapMessage) soapMessage).getSaajMessage();
188         }
189         if (soapMessage instanceof AxiomSoapMessage) {
190             return ((AxiomSoapMessage) soapMessage).getAxiomMessage();
191 
192         }
193         throw new IllegalArgumentException("Illegal message: " + soapMessage);
194     }
195 
196     protected void setMessage(SoapMessage soapMessage, Object message) {
197         if (soapMessage instanceof SaajSoapMessage) {
198             ((SaajSoapMessage) soapMessage).setSaajMessage((SOAPMessage) message);
199             return;
200         }
201         if (soapMessage instanceof AxiomSoapMessage) {
202             ((AxiomSoapMessage) soapMessage).setAxiomMessage((org.apache.axiom.soap.SOAPMessage) message);
203             return;
204         }
205         throw new IllegalArgumentException("Illegal message: " + message);
206     }
207 
208     protected void onSetup() throws Exception {
209     }
210 
211     protected SoapMessage loadSoap11Message(String fileName) throws Exception {
212         if (axiomTest) {
213             return loadAxiom11Message(fileName);
214         }
215         if (saajTest) {
216             return loadSaaj11Message(fileName);
217         }
218         throw new IllegalArgumentException();
219     }
220 
221     protected SoapMessage loadSoap12Message(String fileName) throws Exception {
222         if (axiomTest) {
223             return loadAxiom12Message(fileName);
224         }
225         if (saajTest) {
226             return loadSaaj12Message(fileName);
227         }
228         throw new IllegalArgumentException();
229     }
230 
231     protected SoapMessageFactory getSoap11MessageFactory() throws Exception {
232         if (axiomTest) {
233             return new AxiomSoapMessageFactory();
234         }
235         if (saajTest) {
236             return new SaajSoapMessageFactory(saajSoap11MessageFactory);
237         }
238         throw new IllegalArgumentException();
239     }
240 
241     protected SoapMessageFactory getSoap12MessageFactory() throws Exception {
242         SoapMessageFactory messageFactory;
243         if (axiomTest) {
244             messageFactory = new AxiomSoapMessageFactory();
245         } else if (saajTest) {
246             messageFactory = new SaajSoapMessageFactory(saajSoap12MessageFactory);
247         } else
248             throw new IllegalArgumentException();
249         messageFactory.setSoapVersion(SoapVersion.SOAP_12);
250         return messageFactory;
251     }
252     
253     protected Document getDocument(SoapMessage message) throws Exception {
254         if (axiomTest) {
255             return AxiomUtils.toDocument(((AxiomSoapMessage) message).getAxiomMessage().getSOAPEnvelope());
256         }
257         if (saajTest) {
258             return ((SaajSoapMessage) message).getSaajMessage().getSOAPPart();
259         }
260         throw new IllegalArgumentException();
261     }
262 
263     protected MessageContext getSoap11MessageContext(final SoapMessage response) throws Exception {
264         return new DefaultMessageContext(response, getSoap11MessageFactory()) {
265             @Override
266             public WebServiceMessage getResponse() {
267                 return response;
268             }
269         };
270     }
271 
272     protected MessageContext getSoap12MessageContext(final SoapMessage response) throws Exception {
273         return new DefaultMessageContext(response, getSoap12MessageFactory()) {
274             @Override
275             public WebServiceMessage getResponse() {
276                 return response;
277             }
278         };
279     }
280 
281 }