4444import org .mortbay .jetty .Server ;
4545import org .mortbay .jetty .servlet .Context ;
4646import org .mortbay .jetty .servlet .ServletHolder ;
47+ import org .w3c .dom .Document ;
48+ import org .xml .sax .SAXException ;
49+
4750import org .springframework .util .StringUtils ;
4851import org .springframework .ws .client .WebServiceTransportException ;
4952import org .springframework .ws .pox .dom .DomPoxMessageFactory ;
5457import org .springframework .ws .transport .http .CommonsHttpMessageSender ;
5558import org .springframework .xml .transform .StringResult ;
5659import org .springframework .xml .transform .StringSource ;
57- import org .w3c .dom .Document ;
58- import org .xml .sax .SAXException ;
5960
6061public class WebServiceTemplateIntegrationTest extends XMLTestCase {
6162
@@ -68,6 +69,9 @@ protected void setUp() throws Exception {
6869 Context jettyContext = new Context (jettyServer , "/" );
6970 jettyContext .addServlet (new ServletHolder (new EchoSoapServlet ()), "/soap/echo" );
7071 jettyContext .addServlet (new ServletHolder (new SoapFaultServlet ()), "/soap/fault" );
72+ SoapFaultServlet badRequestFault = new SoapFaultServlet ();
73+ badRequestFault .setSc (400 );
74+ jettyContext .addServlet (new ServletHolder (badRequestFault ), "/soap/badRequestFault" );
7175 jettyContext .addServlet (new ServletHolder (new NoResponseSoapServlet ()), "/soap/noResponse" );
7276 jettyContext .addServlet (new ServletHolder (new PoxServlet ()), "/pox" );
7377 jettyContext .addServlet (new ServletHolder (new ErrorServlet (404 )), "/errors/notfound" );
@@ -83,6 +87,10 @@ public void testAxiom() throws Exception {
8387 testSoap (new AxiomSoapMessageFactory ());
8488 }
8589
90+ public void testWithSaaj () throws Exception {
91+ testSoap (new SaajSoapMessageFactory (MessageFactory .newInstance ()));
92+ }
93+
8694 public void testPox () throws Exception {
8795 template = new WebServiceTemplate (new DomPoxMessageFactory ());
8896 template .setMessageSender (new CommonsHttpMessageSender ());
@@ -99,18 +107,15 @@ public void testPox() throws Exception {
99107 //expected
100108 }
101109 try {
102- template .sendSourceAndReceiveToResult ("http://localhost:8888/errors/server" , new StringSource (content ), result );
110+ template .sendSourceAndReceiveToResult ("http://localhost:8888/errors/server" , new StringSource (content ),
111+ result );
103112 fail ("WebServiceTransportException expected" );
104113 }
105114 catch (WebServiceTransportException ex ) {
106115 //expected
107116 }
108117 }
109118
110- public void testWithSaaj () throws Exception {
111- testSoap (new SaajSoapMessageFactory (MessageFactory .newInstance ()));
112- }
113-
114119 private void testSoap (SoapMessageFactory messageFactory )
115120 throws SAXException , IOException , ParserConfigurationException {
116121 template = new WebServiceTemplate (messageFactory );
@@ -119,8 +124,8 @@ private void testSoap(SoapMessageFactory messageFactory)
119124 StringResult result = new StringResult ();
120125 template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/echo" , new StringSource (content ), result );
121126 assertXMLEqual (content , result .toString ());
122- boolean b = template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/noResponse" , new StringSource ( content ),
123- new StringResult ());
127+ boolean b = template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/noResponse" ,
128+ new StringSource ( content ), new StringResult ());
124129 assertFalse ("Invalid result" , b );
125130 try {
126131 template .sendSourceAndReceiveToResult ("http://localhost:8888/errors/notfound" , new StringSource (content ),
@@ -131,20 +136,31 @@ private void testSoap(SoapMessageFactory messageFactory)
131136 //expected
132137 }
133138 try {
134- template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/fault" , new StringSource (content ), result );
139+ template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/fault" , new StringSource (content ),
140+ result );
135141 fail ("SoapFaultClientException expected" );
136142 }
137143 catch (SoapFaultClientException ex ) {
138144 //expected
139145 }
146+ template .setCheckConnectionForFault (false );
147+ try {
148+ template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/badRequestFault" ,
149+ new StringSource (content ), result );
150+ fail ("SoapFaultClientException expected" );
151+ }
152+ catch (SoapFaultClientException ex ) {
153+ //expected
154+ }
155+ template .setCheckConnectionForFault (true );
140156 }
141157
142158 /** Servlet that returns and error message for a given status code. */
143159 private static class ErrorServlet extends HttpServlet {
144160
145161 private int sc ;
146162
147- public ErrorServlet (int sc ) {
163+ private ErrorServlet (int sc ) {
148164 this .sc = sc ;
149165 }
150166
@@ -185,6 +201,12 @@ private abstract static class AbstractSoapServlet extends HttpServlet {
185201
186202 protected MessageFactory msgFactory = null ;
187203
204+ private int sc = -1 ;
205+
206+ public void setSc (int sc ) {
207+ this .sc = sc ;
208+ }
209+
188210 public void init (ServletConfig servletConfig ) throws ServletException {
189211 super .init (servletConfig );
190212 try {
@@ -200,16 +222,21 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws Serv
200222 MimeHeaders headers = getHeaders (req );
201223 SOAPMessage request = msgFactory .createMessage (headers , req .getInputStream ());
202224 SOAPMessage reply = onMessage (request );
225+ if (sc != -1 ) {
226+ resp .setStatus (sc );
227+ }
203228 if (reply != null ) {
204229 if (reply .saveRequired ()) {
205230 reply .saveChanges ();
206231 }
207- resp .setStatus (!reply .getSOAPBody ().hasFault () ? HttpServletResponse .SC_OK :
208- HttpServletResponse .SC_INTERNAL_SERVER_ERROR );
232+ if (sc == -1 ) {
233+ resp .setStatus (!reply .getSOAPBody ().hasFault () ? HttpServletResponse .SC_OK :
234+ HttpServletResponse .SC_INTERNAL_SERVER_ERROR );
235+ }
209236 putHeaders (reply .getMimeHeaders (), resp );
210237 reply .writeTo (resp .getOutputStream ());
211238 }
212- else {
239+ else if ( sc == - 1 ) {
213240 resp .setStatus (HttpServletResponse .SC_ACCEPTED );
214241 }
215242 }
@@ -267,4 +294,5 @@ protected SOAPMessage onMessage(SOAPMessage message) throws SOAPException {
267294 return response ;
268295 }
269296 }
297+
270298}
0 commit comments