44
44
import org .mortbay .jetty .Server ;
45
45
import org .mortbay .jetty .servlet .Context ;
46
46
import org .mortbay .jetty .servlet .ServletHolder ;
47
+ import org .w3c .dom .Document ;
48
+ import org .xml .sax .SAXException ;
49
+
47
50
import org .springframework .util .StringUtils ;
48
51
import org .springframework .ws .client .WebServiceTransportException ;
49
52
import org .springframework .ws .pox .dom .DomPoxMessageFactory ;
54
57
import org .springframework .ws .transport .http .CommonsHttpMessageSender ;
55
58
import org .springframework .xml .transform .StringResult ;
56
59
import org .springframework .xml .transform .StringSource ;
57
- import org .w3c .dom .Document ;
58
- import org .xml .sax .SAXException ;
59
60
60
61
public class WebServiceTemplateIntegrationTest extends XMLTestCase {
61
62
@@ -68,6 +69,9 @@ protected void setUp() throws Exception {
68
69
Context jettyContext = new Context (jettyServer , "/" );
69
70
jettyContext .addServlet (new ServletHolder (new EchoSoapServlet ()), "/soap/echo" );
70
71
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" );
71
75
jettyContext .addServlet (new ServletHolder (new NoResponseSoapServlet ()), "/soap/noResponse" );
72
76
jettyContext .addServlet (new ServletHolder (new PoxServlet ()), "/pox" );
73
77
jettyContext .addServlet (new ServletHolder (new ErrorServlet (404 )), "/errors/notfound" );
@@ -83,6 +87,10 @@ public void testAxiom() throws Exception {
83
87
testSoap (new AxiomSoapMessageFactory ());
84
88
}
85
89
90
+ public void testWithSaaj () throws Exception {
91
+ testSoap (new SaajSoapMessageFactory (MessageFactory .newInstance ()));
92
+ }
93
+
86
94
public void testPox () throws Exception {
87
95
template = new WebServiceTemplate (new DomPoxMessageFactory ());
88
96
template .setMessageSender (new CommonsHttpMessageSender ());
@@ -99,18 +107,15 @@ public void testPox() throws Exception {
99
107
//expected
100
108
}
101
109
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 );
103
112
fail ("WebServiceTransportException expected" );
104
113
}
105
114
catch (WebServiceTransportException ex ) {
106
115
//expected
107
116
}
108
117
}
109
118
110
- public void testWithSaaj () throws Exception {
111
- testSoap (new SaajSoapMessageFactory (MessageFactory .newInstance ()));
112
- }
113
-
114
119
private void testSoap (SoapMessageFactory messageFactory )
115
120
throws SAXException , IOException , ParserConfigurationException {
116
121
template = new WebServiceTemplate (messageFactory );
@@ -119,8 +124,8 @@ private void testSoap(SoapMessageFactory messageFactory)
119
124
StringResult result = new StringResult ();
120
125
template .sendSourceAndReceiveToResult ("http://localhost:8888/soap/echo" , new StringSource (content ), result );
121
126
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 ());
124
129
assertFalse ("Invalid result" , b );
125
130
try {
126
131
template .sendSourceAndReceiveToResult ("http://localhost:8888/errors/notfound" , new StringSource (content ),
@@ -131,20 +136,31 @@ private void testSoap(SoapMessageFactory messageFactory)
131
136
//expected
132
137
}
133
138
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 );
135
141
fail ("SoapFaultClientException expected" );
136
142
}
137
143
catch (SoapFaultClientException ex ) {
138
144
//expected
139
145
}
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 );
140
156
}
141
157
142
158
/** Servlet that returns and error message for a given status code. */
143
159
private static class ErrorServlet extends HttpServlet {
144
160
145
161
private int sc ;
146
162
147
- public ErrorServlet (int sc ) {
163
+ private ErrorServlet (int sc ) {
148
164
this .sc = sc ;
149
165
}
150
166
@@ -185,6 +201,12 @@ private abstract static class AbstractSoapServlet extends HttpServlet {
185
201
186
202
protected MessageFactory msgFactory = null ;
187
203
204
+ private int sc = -1 ;
205
+
206
+ public void setSc (int sc ) {
207
+ this .sc = sc ;
208
+ }
209
+
188
210
public void init (ServletConfig servletConfig ) throws ServletException {
189
211
super .init (servletConfig );
190
212
try {
@@ -200,16 +222,21 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws Serv
200
222
MimeHeaders headers = getHeaders (req );
201
223
SOAPMessage request = msgFactory .createMessage (headers , req .getInputStream ());
202
224
SOAPMessage reply = onMessage (request );
225
+ if (sc != -1 ) {
226
+ resp .setStatus (sc );
227
+ }
203
228
if (reply != null ) {
204
229
if (reply .saveRequired ()) {
205
230
reply .saveChanges ();
206
231
}
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
+ }
209
236
putHeaders (reply .getMimeHeaders (), resp );
210
237
reply .writeTo (resp .getOutputStream ());
211
238
}
212
- else {
239
+ else if ( sc == - 1 ) {
213
240
resp .setStatus (HttpServletResponse .SC_ACCEPTED );
214
241
}
215
242
}
@@ -267,4 +294,5 @@ protected SOAPMessage onMessage(SOAPMessage message) throws SOAPException {
267
294
return response ;
268
295
}
269
296
}
297
+
270
298
}
0 commit comments