Skip to content

Commit

Permalink
Merge pull request spring-projects#685 from garyrussell/INT-2837a
Browse files Browse the repository at this point in the history
* INT-2837a:
  INT-2837 Fix Accept Hdr for Serializable Response
  • Loading branch information
markfisher committed Nov 30, 2012
2 parents 4ce08ad + 7ef66e2 commit 84f4bea
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 19 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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.
Expand Down Expand Up @@ -31,8 +31,9 @@

/**
* An {@link HttpMessageConverter} implementation for {@link Serializable} instances.
*
*
* @author Mark Fisher
* @author Gary Russell
* @since 2.0
*/
public class SerializingHttpMessageConverter extends AbstractHttpMessageConverter<Serializable> {
Expand All @@ -51,11 +52,6 @@ public boolean supports(Class<?> clazz) {
return Serializable.class.isAssignableFrom(clazz);
}

@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return (Serializable.class.isAssignableFrom(clazz) && APPLICATION_JAVA_SERIALIZED_OBJECT.includes(mediaType));
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return (Serializable.class.isAssignableFrom(clazz) && APPLICATION_JAVA_SERIALIZED_OBJECT.includes(mediaType));
Expand Down
Expand Up @@ -54,7 +54,6 @@
import org.springframework.integration.expression.ExpressionUtils;
import org.springframework.integration.gateway.MessagingGatewaySupport;
import org.springframework.integration.http.converter.MultipartAwareFormHttpMessageConverter;
import org.springframework.integration.http.converter.SerializingHttpMessageConverter;
import org.springframework.integration.http.multipart.MultipartHttpInputMessage;
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;
import org.springframework.integration.mapping.HeaderMapper;
Expand Down Expand Up @@ -147,7 +146,6 @@ public HttpRequestHandlingEndpointSupport() {
public HttpRequestHandlingEndpointSupport(boolean expectReply) {
this.expectReply = expectReply;
this.messageConverters.add(new MultipartAwareFormHttpMessageConverter());
this.messageConverters.add(new SerializingHttpMessageConverter());
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(new StringHttpMessageConverter());
this.messageConverters.add(new ResourceHttpMessageConverter());
Expand Down
Expand Up @@ -51,7 +51,6 @@
import org.springframework.integration.MessagingException;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.http.converter.SerializingHttpMessageConverter;
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;
import org.springframework.integration.mapping.HeaderMapper;
import org.springframework.integration.support.MessageBuilder;
Expand Down Expand Up @@ -152,7 +151,6 @@ public HttpRequestExecutingMessageHandler(String uri, RestTemplate restTemplate)
public HttpRequestExecutingMessageHandler(Expression uriExpression, RestTemplate restTemplate) {
Assert.notNull(uriExpression, "URI Expression is required");
this.restTemplate = (restTemplate == null ? new RestTemplate() : restTemplate);
this.restTemplate.getMessageConverters().add(0, new SerializingHttpMessageConverter());
this.uriExpression = uriExpression;
StandardEvaluationContext sec = new StandardEvaluationContext();
sec.addPropertyAccessor(new MapAccessor());
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand All @@ -40,16 +41,19 @@
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.http.converter.SerializingHttpMessageConverter;
import org.springframework.integration.http.inbound.HttpRequestHandlingController;
import org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway;
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.MultiValueMap;
Expand All @@ -60,6 +64,7 @@
* @author Gary Russell
* @author Gunnar Hillert
* @author Artem Bilan
* @author Gary Russell
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
Expand Down Expand Up @@ -239,7 +244,7 @@ public void postRequestWithTextContentOk() throws Exception {
assertEquals("test", message.getPayload());
}

@Test
@Test @DirtiesContext
public void postRequestWithSerializedObjectContentOk() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
Expand All @@ -253,6 +258,11 @@ public void postRequestWithSerializedObjectContentOk() throws Exception {
request.addHeader("Content-Type", "application/x-java-serialized-object");

MockHttpServletResponse response = new MockHttpServletResponse();

List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new SerializingHttpMessageConverter());
postOnlyAdapter.setMessageConverters(converters);

postOnlyAdapter.handleRequest(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
Message<?> message = requests.receive(0);
Expand Down
Expand Up @@ -25,6 +25,7 @@
import static org.springframework.integration.test.util.TestUtils.getPropertyValue;
import static org.springframework.integration.test.util.TestUtils.handlerExpecting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -40,17 +41,20 @@
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.core.SubscribableChannel;
import org.springframework.integration.http.converter.SerializingHttpMessageConverter;
import org.springframework.integration.http.inbound.HttpRequestHandlingController;
import org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway;
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Expand Down Expand Up @@ -103,7 +107,7 @@ public void checkConfig() {
assertEquals(Long.valueOf(4567), TestUtils.getPropertyValue(messagingTemplate, "receiveTimeout"));
}

@Test(timeout=1000)
@Test(timeout=1000) @DirtiesContext
public void checkFlow() throws Exception {
requests.subscribe(handlerExpecting(any(Message.class)));
MockHttpServletRequest request = new MockHttpServletRequest();
Expand All @@ -112,6 +116,10 @@ public void checkFlow() throws Exception {
request.setParameter("foo", "bar");

MockHttpServletResponse response = new MockHttpServletResponse();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new SerializingHttpMessageConverter());
gateway.setMessageConverters(converters);

gateway.handleRequest(request, response);
assertThat(response.getStatus(), is(HttpServletResponse.SC_OK));

Expand Down
Expand Up @@ -3,9 +3,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
Expand All @@ -27,6 +29,17 @@
reply-channel="replyChannel"
expected-response-type-expression="payload"/>

<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="resTypeExpressionSetSerializationChannel"
reply-channel="replyChannel"
message-converters="stringAndSerializingConverters"
expected-response-type-expression="payload"/>

<util:list id="stringAndSerializingConverters">
<bean class="org.springframework.integration.http.converter.SerializingHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
</util:list>

<int:channel id="replyChannel">
<int:queue/>
</int:channel>
Expand Down
Expand Up @@ -26,11 +26,9 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.MediaType;
Expand All @@ -39,6 +37,7 @@
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sun.net.httpserver.HttpExchange;
Expand All @@ -48,6 +47,7 @@
/**
* @author Oleg Zhurakousky
* @author Artem Bilan
* @author Gary Russell
* @since 2.2
*
* <p/>
Expand Down Expand Up @@ -76,6 +76,9 @@ public class OutboundResponseTypeTests {
@Autowired
private MessageChannel resTypeExpressionSetChannel;

@Autowired
private MessageChannel resTypeExpressionSetSerializationChannel;

@BeforeClass
public static void createServer() throws Exception {
httpHandler = new MyHandler();
Expand Down Expand Up @@ -115,7 +118,7 @@ public void testWithResponseTypeExpressionSet() throws Exception {

@Test
public void testWithResponseTypeExpressionSetAsClass() throws Exception {
this.resTypeExpressionSetChannel.send(new GenericMessage<Class<?>>(String.class));
this.resTypeExpressionSetSerializationChannel.send(new GenericMessage<Class<?>>(String.class));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof String);
Expand Down
Expand Up @@ -39,6 +39,7 @@
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.http.converter.SerializingHttpMessageConverter;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
Expand Down Expand Up @@ -198,6 +199,11 @@ public void serializableRequestBody() throws Exception {
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false);
gateway.setRequestPayloadType(TestBean.class);
gateway.setRequestChannel(channel);

List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new SerializingHttpMessageConverter());
gateway.setMessageConverters(converters);

MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test");

//request.setContentType("application/x-java-serialized-object"); //Works in Spring 3.1.2.RELEASE but NOT in 3.0.7.RELEASE
Expand Down

0 comments on commit 84f4bea

Please sign in to comment.