INT-1767 using MediaType.ALL as the default Accept when writing a response in HttpRequestHandlingMessagingGateway

This commit is contained in:
Mark Fisher
2011-01-26 18:56:07 -05:00
parent 403855fea6
commit 1857d5b6c0
2 changed files with 27 additions and 2 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.integration.http.inbound;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import javax.servlet.ServletException;
@@ -30,6 +31,7 @@ import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.integration.MessagingException;
import org.springframework.integration.http.converter.MultipartAwareFormHttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.HttpRequestHandler;
@@ -98,12 +100,12 @@ public class HttpRequestHandlingMessagingGateway extends HttpRequestHandlingEndp
if (responseContent != null) {
ServletServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
ServletServerHttpResponse response = new ServletServerHttpResponse(servletResponse);
if (responseContent instanceof HttpStatus){
if (responseContent instanceof HttpStatus) {
response.setStatusCode((HttpStatus) responseContent);
}
else {
this.writeResponse(responseContent, response, request.getHeaders().getAccept());
}
}
}
}
@@ -126,6 +128,9 @@ public class HttpRequestHandlingMessagingGateway extends HttpRequestHandlingEndp
@SuppressWarnings({"unchecked", "rawtypes"})
private void writeResponse(Object content, ServletServerHttpResponse response, List<MediaType> acceptTypes) throws IOException {
if (CollectionUtils.isEmpty(acceptTypes)) {
acceptTypes = Collections.singletonList(MediaType.ALL);
}
for (HttpMessageConverter converter : this.getMessageConverters()) {
for (MediaType acceptType : acceptTypes) {
if (converter.canWrite(content.getClass(), acceptType)) {

View File

@@ -108,6 +108,26 @@ public class HttpRequestHandlingMessagingGatewayTests {
assertEquals("HELLO", response.getContentAsString());
}
@Test // INT-1767
public void noAcceptHeaderOnRequest() throws Exception {
DirectChannel requestChannel = new DirectChannel();
requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {
protected Object handleRequestMessage(Message<?> requestMessage) {
return requestMessage.getPayload().toString().toUpperCase();
}
});
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
gateway.setRequestPayloadType(String.class);
gateway.setRequestChannel(requestChannel);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType("text/plain");
request.setContent("hello".getBytes());
MockHttpServletResponse response = new MockHttpServletResponse();
gateway.handleRequest(request, response);
assertEquals("HELLO", response.getContentAsString());
}
@Test
public void testExceptionConversion() throws Exception {
QueueChannel requestChannel = new QueueChannel() {