Fixed SWS-128: A SOAP:Fault does not set the the response code to 500

This commit is contained in:
Arjen Poutsma
2007-05-27 09:22:38 +00:00
parent f22b47e0d0
commit b003a6f848
11 changed files with 289 additions and 21 deletions

View File

@@ -101,10 +101,6 @@ public abstract class AbstractReceiverConnection extends AbstractWebServiceConne
return getResponseOutputStream();
}
public void close() throws IOException {
super.close();
}
}

View File

@@ -29,6 +29,7 @@ import org.springframework.ws.WebServiceMessageFactory;
public abstract class AbstractWebServiceConnection implements WebServiceConnection {
public final void send(WebServiceMessage message) throws IOException {
onSendBeforeWrite(message);
TransportOutputStream tos = createTransportOutputStream();
try {
message.writeTo(tos);
@@ -37,10 +38,11 @@ public abstract class AbstractWebServiceConnection implements WebServiceConnecti
finally {
tos.close();
}
onSend(message);
onSendAfterWrite(message);
}
public final WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException {
onReceiveBeforeRead();
TransportInputStream tis = createTransportInputStream();
if (tis == null) {
return null;
@@ -52,7 +54,7 @@ public abstract class AbstractWebServiceConnection implements WebServiceConnecti
finally {
tis.close();
}
onReceive(message);
onReceiveAfterRead(message);
return message;
}
@@ -66,7 +68,7 @@ public abstract class AbstractWebServiceConnection implements WebServiceConnecti
protected abstract TransportOutputStream createTransportOutputStream() throws IOException;
/**
* Called when the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
* Called before the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
* #send(WebServiceMessage)}.
* <p/>
* Default implementation does nothing.
@@ -74,8 +76,19 @@ public abstract class AbstractWebServiceConnection implements WebServiceConnecti
* @param message the message
* @throws IOException when an I/O exception occurs
*/
protected void onSend(WebServiceMessage message) throws IOException {
protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
}
/**
* Called after the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
* #send(WebServiceMessage)}.
* <p/>
* Default implementation does nothing.
*
* @param message the message
* @throws IOException when an I/O exception occurs
*/
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
}
/**
@@ -87,7 +100,18 @@ public abstract class AbstractWebServiceConnection implements WebServiceConnecti
protected abstract TransportInputStream createTransportInputStream() throws IOException;
/**
* Called when the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
* Called before a message has been read from the <code>TransportInputStream</code>. Called from {@link
* #receive(WebServiceMessageFactory)}.
* <p/>
* Default implementation does nothing.
*
* @throws IOException when an I/O exception occurs
*/
protected void onReceiveBeforeRead() throws IOException {
}
/**
* Called when the given message has been read from the <code>TransportInputStream</code>. Called from {@link
* #receive(WebServiceMessageFactory)}.
* <p/>
* Default implementation does nothing.
@@ -95,8 +119,7 @@ public abstract class AbstractWebServiceConnection implements WebServiceConnecti
* @param message the message
* @throws IOException when an I/O exception occurs
*/
protected void onReceive(WebServiceMessage message) throws IOException {
protected void onReceiveAfterRead(WebServiceMessage message) throws IOException {
}
}

View File

@@ -44,7 +44,7 @@ public class CommonsHttpConnection extends AbstractHttpSenderConnection {
private final PostMethod postMethod;
private ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream();
private ByteArrayOutputStream bufferedOutput;
public CommonsHttpConnection(HttpClient httpClient, PostMethod postMethod) {
Assert.notNull(httpClient, "httpClient must not be null");
@@ -61,6 +61,14 @@ public class CommonsHttpConnection extends AbstractHttpSenderConnection {
postMethod.releaseConnection();
}
/*
* Sending request
*/
protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
bufferedOutput = new ByteArrayOutputStream();
}
protected void addRequestHeader(String name, String value) throws IOException {
postMethod.addRequestHeader(name, value);
}
@@ -69,12 +77,16 @@ public class CommonsHttpConnection extends AbstractHttpSenderConnection {
return bufferedOutput;
}
protected void onSend(WebServiceMessage message) throws IOException {
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
postMethod.setRequestEntity(new ByteArrayRequestEntity(bufferedOutput.toByteArray()));
bufferedOutput = null;
httpClient.executeMethod(postMethod);
}
/*
* Receiving response
*/
protected int getResponseCode() throws IOException {
return postMethod.getStatusCode();
}

View File

@@ -53,12 +53,6 @@ public class HttpServletConnection extends AbstractReceiverConnection implements
this.httpServletResponse = httpServletResponse;
}
public void close() throws IOException {
if (!sentResponse && endpointFound) {
httpServletResponse.setStatus(HttpServletResponse.SC_ACCEPTED);
}
}
/** Returns the <code>HttpServletRequest</code> for this connection. */
public HttpServletRequest getHttpServletRequest() {
return httpServletRequest;
@@ -74,6 +68,16 @@ public class HttpServletConnection extends AbstractReceiverConnection implements
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
public void close() throws IOException {
if (!sentResponse && endpointFound) {
httpServletResponse.setStatus(HttpServletResponse.SC_ACCEPTED);
}
}
/*
* Receiving request
*/
protected Iterator getRequestHeaderNames() throws IOException {
return new EnumerationIterator(httpServletRequest.getHeaderNames());
}
@@ -86,6 +90,10 @@ public class HttpServletConnection extends AbstractReceiverConnection implements
return httpServletRequest.getInputStream();
}
/*
* Sending response
*/
protected void addResponseHeader(String name, String value) throws IOException {
httpServletResponse.addHeader(name, value);
}
@@ -94,7 +102,7 @@ public class HttpServletConnection extends AbstractReceiverConnection implements
return httpServletResponse.getOutputStream();
}
protected void onSend(WebServiceMessage message) throws IOException {
protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
sentResponse = true;
if (!message.hasFault()) {
httpServletResponse.setStatus(HttpServletResponse.SC_OK);

View File

@@ -58,6 +58,10 @@ public class HttpUrlConnection extends AbstractHttpSenderConnection {
connection.disconnect();
}
/*
* Sending request
*/
protected void addRequestHeader(String name, String value) throws IOException {
connection.addRequestProperty(name, value);
}
@@ -66,10 +70,14 @@ public class HttpUrlConnection extends AbstractHttpSenderConnection {
return connection.getOutputStream();
}
protected void onSend(WebServiceMessage message) throws IOException {
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
connection.connect();
}
/*
* Receiving response
*/
protected long getResponseContentLength() throws IOException {
return connection.getContentLength();
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.http;
import java.util.Locale;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.MessageEndpoint;
import org.springframework.ws.soap.SoapMessage;
public class FaultEndpoint implements MessageEndpoint {
public void invoke(MessageContext messageContext) throws Exception {
SoapMessage response = (SoapMessage) messageContext.getResponse();
response.getSoapBody().addServerOrReceiverFault("Something went wrong", Locale.ENGLISH);
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright 2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.http;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class MessageDispatcherServletIntegrationTest extends TestCase {
private Server jettyServer;
private HttpClient client;
private static final String SOAP_ACTION = "SOAPAction";
private static final String CONTENT_TYPE = "Content-Type";
protected final void setUp() throws Exception {
jettyServer = new Server(8888);
Context jettyContext = new Context(jettyServer, "/");
File dir = new File(getClass().getResource(".").toURI());
jettyContext.setResourceBase(dir.getAbsolutePath());
ServletHolder servletHolder = new ServletHolder(new MessageDispatcherServlet());
servletHolder.setName("spring-ws");
jettyContext.addServlet(servletHolder, "/*");
jettyServer.start();
client = new HttpClient();
}
protected void tearDown() throws Exception {
jettyServer.stop();
}
public void testNoResponse() throws IOException {
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
postMethod.addRequestHeader(CONTENT_TYPE, "text/xml");
postMethod.addRequestHeader(SOAP_ACTION, "http://springframework.org/spring-ws/NoResponse");
Resource soapRequest = new ClassPathResource("soapRequest.xml", MessageDispatcherServletIntegrationTest.class);
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
client.executeMethod(postMethod);
assertEquals("Invalid Response Code", HttpStatus.SC_ACCEPTED, postMethod.getStatusCode());
assertEquals("Response retrieved", 0, postMethod.getResponseContentLength());
}
public void testResponse() throws IOException {
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
postMethod.addRequestHeader(CONTENT_TYPE, "text/xml");
postMethod.addRequestHeader(SOAP_ACTION, "http://springframework.org/spring-ws/Response");
Resource soapRequest = new ClassPathResource("soapRequest.xml", MessageDispatcherServletIntegrationTest.class);
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
client.executeMethod(postMethod);
assertEquals("Invalid Response Code", HttpStatus.SC_OK, postMethod.getStatusCode());
assertTrue("No Response retrieved", postMethod.getResponseContentLength() > 0);
}
public void testNoEndpoint() throws IOException {
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
postMethod.addRequestHeader(CONTENT_TYPE, "text/xml");
postMethod.addRequestHeader(SOAP_ACTION, "http://springframework.org/spring-ws/NoEndpoint");
Resource soapRequest = new ClassPathResource("soapRequest.xml", MessageDispatcherServletIntegrationTest.class);
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
client.executeMethod(postMethod);
assertEquals("Invalid Response Code", HttpStatus.SC_NOT_FOUND, postMethod.getStatusCode());
assertEquals("Response retrieved", 0, postMethod.getResponseContentLength());
}
public void testFault() throws IOException {
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
postMethod.addRequestHeader(CONTENT_TYPE, "text/xml");
postMethod.addRequestHeader(SOAP_ACTION, "http://springframework.org/spring-ws/Fault");
Resource soapRequest = new ClassPathResource("soapRequest.xml", MessageDispatcherServletIntegrationTest.class);
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
client.executeMethod(postMethod);
assertEquals("Invalid Response Code", HttpStatus.SC_INTERNAL_SERVER_ERROR, postMethod.getStatusCode());
assertTrue("No Response retrieved", postMethod.getResponseContentLength() > 0);
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.http;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.MessageEndpoint;
/** @author Arjen Poutsma */
public class NoResponseEndpoint implements MessageEndpoint {
public void invoke(MessageContext messageContext) throws Exception {
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport.http;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.MessageEndpoint;
/** @author Arjen Poutsma */
public class ResponseEndpoint implements MessageEndpoint {
public void invoke(MessageContext messageContext) throws Exception {
messageContext.getResponse();
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="payloadMapping" class="org.springframework.ws.soap.server.endpoint.mapping.SoapActionEndpointMapping">
<property name="mappings">
<props>
<prop key="http://springframework.org/spring-ws/NoResponse">noResponseEndpoint</prop>
<prop key="http://springframework.org/spring-ws/Response">responseEndpoint</prop>
<prop key="http://springframework.org/spring-ws/Fault">faultEndpoint</prop>
</props>
</property>
</bean>
<bean id="noResponseEndpoint"
class="org.springframework.ws.transport.http.NoResponseEndpoint"/>
<bean id="responseEndpoint"
class="org.springframework.ws.transport.http.ResponseEndpoint"/>
<bean id="faultEndpoint"
class="org.springframework.ws.transport.http.FaultEndpoint"/>
</beans>

View File

@@ -0,0 +1,7 @@
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
<SOAP-ENV:Body>
<m:GetLastTradePrice xmlns:m='http://www.springframework.org/spring-ws'>
<symbol>DIS</symbol>
</m:GetLastTradePrice>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>