2020import java .util .Enumeration ;
2121import java .util .Iterator ;
2222import java .util .StringTokenizer ;
23+ import javax .activation .CommandMap ;
24+ import javax .activation .DataHandler ;
25+ import javax .activation .MailcapCommandMap ;
26+ import javax .mail .util .ByteArrayDataSource ;
2327import javax .servlet .ServletConfig ;
2428import javax .servlet .ServletException ;
2529import javax .servlet .http .HttpServlet ;
5761import org .springframework .oxm .Unmarshaller ;
5862import org .springframework .oxm .XmlMappingException ;
5963import org .springframework .util .StringUtils ;
64+ import org .springframework .ws .WebServiceMessage ;
6065import org .springframework .ws .client .WebServiceTransportException ;
6166import org .springframework .ws .pox .dom .DomPoxMessageFactory ;
67+ import org .springframework .ws .soap .SoapMessage ;
6268import org .springframework .ws .soap .SoapMessageFactory ;
6369import org .springframework .ws .soap .axiom .AxiomSoapMessageFactory ;
6470import org .springframework .ws .soap .client .SoapFaultClientException ;
@@ -72,12 +78,18 @@ public class WebServiceTemplateIntegrationTest extends XMLTestCase {
7278
7379 private static Server jettyServer ;
7480
75- private String messagePayload ;
81+ private String messagePayload = "<root xmlns='http://springframework.org/spring-ws'><child/></root>" ;
7682
7783 public static Test suite () {
7884 return new ServerTestSetup (new TestSuite (WebServiceTemplateIntegrationTest .class ));
7985 }
8086
87+ /*
88+ public void testSaaj() throws Exception {
89+ doSoap(new SaajSoapMessageFactory(MessageFactory.newInstance()));
90+ }
91+ */
92+
8193 public void testAxiom () throws Exception {
8294 doSoap (new AxiomSoapMessageFactory ());
8395 }
@@ -123,10 +135,10 @@ private void doSoap(SoapMessageFactory messageFactory) throws Exception {
123135 notFound ();
124136 fault ();
125137 faultNonCompliant ();
138+ attachment ();
126139 }
127140
128141 private void sendSourceAndReceiveToResult () throws SAXException , IOException {
129- messagePayload = "<root xmlns='http://springframework.org/spring-ws'><child/></root>" ;
130142 StringResult result = new StringResult ();
131143 boolean b = template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/echo" ,
132144 new StringSource (messagePayload ), result );
@@ -240,6 +252,19 @@ private void faultNonCompliant() {
240252 }
241253 }
242254
255+ private void attachment () {
256+ template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/attachment" , new StringSource (messagePayload ),
257+ new WebServiceMessageCallback () {
258+
259+ public void doWithMessage (WebServiceMessage message ) throws IOException , TransformerException {
260+ SoapMessage soapMessage = (SoapMessage ) message ;
261+ final String attachmentContent = "content" ;
262+ soapMessage .addAttachment ("attachment-1" ,
263+ new DataHandler (new ByteArrayDataSource (attachmentContent , "text/plain" )));
264+ }
265+ }, new StringResult ());
266+ }
267+
243268 /** Servlet that returns and error message for a given status code. */
244269 private static class ErrorServlet extends HttpServlet {
245270
@@ -284,7 +309,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws Serv
284309 /** Abstract SOAP Servlet */
285310 private abstract static class AbstractSoapServlet extends HttpServlet {
286311
287- protected MessageFactory msgFactory = null ;
312+ protected MessageFactory messageFactory = null ;
288313
289314 private int sc = -1 ;
290315
@@ -295,7 +320,7 @@ public void setSc(int sc) {
295320 public void init (ServletConfig servletConfig ) throws ServletException {
296321 super .init (servletConfig );
297322 try {
298- msgFactory = MessageFactory .newInstance ();
323+ messageFactory = MessageFactory .newInstance ();
299324 }
300325 catch (SOAPException ex ) {
301326 throw new ServletException ("Unable to create message factory" + ex .getMessage ());
@@ -305,7 +330,7 @@ public void init(ServletConfig servletConfig) throws ServletException {
305330 public void doPost (HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
306331 try {
307332 MimeHeaders headers = getHeaders (req );
308- SOAPMessage request = msgFactory .createMessage (headers , req .getInputStream ());
333+ SOAPMessage request = messageFactory .createMessage (headers , req .getInputStream ());
309334 SOAPMessage reply = onMessage (request );
310335 if (sc != -1 ) {
311336 resp .setStatus (sc );
@@ -326,7 +351,7 @@ else if (sc == -1) {
326351 }
327352 }
328353 catch (Exception ex ) {
329- throw new ServletException ("SAAJ POST failed " + ex .getMessage ());
354+ throw new ServletException ("SAAJ POST failed " + ex .getMessage (), ex );
330355 }
331356 }
332357
@@ -373,13 +398,21 @@ protected SOAPMessage onMessage(SOAPMessage message) throws SOAPException {
373398 private static class SoapFaultServlet extends AbstractSoapServlet {
374399
375400 protected SOAPMessage onMessage (SOAPMessage message ) throws SOAPException {
376- SOAPMessage response = msgFactory .createMessage ();
401+ SOAPMessage response = messageFactory .createMessage ();
377402 SOAPBody body = response .getSOAPBody ();
378403 body .addFault (new QName ("http://schemas.xmlsoap.org/soap/envelope/" , "Server" ), "Server fault" );
379404 return response ;
380405 }
381406 }
382407
408+ private static class AttachmentsServlet extends AbstractSoapServlet {
409+
410+ protected SOAPMessage onMessage (SOAPMessage message ) throws SOAPException {
411+ assertEquals ("No attachments found" , 1 , message .countAttachments ());
412+ return null ;
413+ }
414+ }
415+
383416 /** A custom TestSetup to make sure that setUp() and tearDown() are only called once. */
384417 private static class ServerTestSetup extends TestSetup {
385418
@@ -388,6 +421,8 @@ private ServerTestSetup(Test test) {
388421 }
389422
390423 protected void setUp () throws Exception {
424+ removeXmlDataContentHandler ();
425+
391426 jettyServer = new Server (8888 );
392427 Context jettyContext = new Context (jettyServer , "/" );
393428 jettyContext .addServlet (new ServletHolder (new EchoSoapServlet ()), "/soap/echo" );
@@ -396,12 +431,24 @@ protected void setUp() throws Exception {
396431 badRequestFault .setSc (400 );
397432 jettyContext .addServlet (new ServletHolder (badRequestFault ), "/soap/badRequestFault" );
398433 jettyContext .addServlet (new ServletHolder (new NoResponseSoapServlet ()), "/soap/noResponse" );
434+ jettyContext .addServlet (new ServletHolder (new AttachmentsServlet ()), "/soap/attachment" );
399435 jettyContext .addServlet (new ServletHolder (new PoxServlet ()), "/pox" );
400436 jettyContext .addServlet (new ServletHolder (new ErrorServlet (404 )), "/errors/notfound" );
401437 jettyContext .addServlet (new ServletHolder (new ErrorServlet (500 )), "/errors/server" );
402438 jettyServer .start ();
403439 }
404440
441+ /**
442+ * A workaround for the faulty XmlDataContentHandler in the SAAJ RI, which cannot handle mime types such as
443+ * "text/xml; charset=UTF-8", causing issues with Axiom. We basically reset the command map
444+ */
445+ private void removeXmlDataContentHandler () throws SOAPException {
446+ MessageFactory messageFactory = MessageFactory .newInstance ();
447+ SOAPMessage message = messageFactory .createMessage ();
448+ message .createAttachmentPart ();
449+ CommandMap .setDefaultCommandMap (new MailcapCommandMap ());
450+ }
451+
405452 protected void tearDown () throws Exception {
406453 jettyServer .stop ();
407454 }
0 commit comments