Skip to content

Commit

Permalink
INT-4172: canWrite(mediaType) in SerializingHttpMC
Browse files Browse the repository at this point in the history
JIRA: https://jira.spring.io/browse/INT-4172

The `SerializingHttpMessageConverter` which can be customized via `setSupportedMediaTypes()`, but at the same time `canWrite(Class<?> clazz, MediaType mediaType)` consult only default `APPLICATION_JAVA_SERIALIZED_OBJECT`

The target application really may face different media types for the `Serialized` objects not just built-in `application/x-java-serialized-object`

Since we can customize `SerializingHttpMessageConverter` via  `setSupportedMediaTypes()` that will be fully consistent to consult `canWrite(mediaType)`
  • Loading branch information
artembilan committed Nov 28, 2016
1 parent 3e8ed94 commit b60e1dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Expand Up @@ -34,11 +34,14 @@
*
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*/
public class SerializingHttpMessageConverter extends AbstractHttpMessageConverter<Serializable> {

private static final MediaType APPLICATION_JAVA_SERIALIZED_OBJECT = new MediaType("application", "x-java-serialized-object");
private static final MediaType APPLICATION_JAVA_SERIALIZED_OBJECT =
new MediaType("application", "x-java-serialized-object");


/** Creates a new instance of the {@code SerializingHttpMessageConverter}. */
Expand All @@ -54,7 +57,7 @@ public boolean supports(Class<?> clazz) {

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

@Override
Expand Down
Expand Up @@ -28,6 +28,7 @@
import static org.springframework.integration.test.util.TestUtils.getPropertyValue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -37,14 +38,14 @@
import org.hamcrest.Matcher;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.converter.Converter;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.http.converter.SerializingHttpMessageConverter;
Expand Down Expand Up @@ -140,31 +141,35 @@ public void checkConfig() {
assertEquals(1001, TestUtils.getPropertyValue(this.gateway, "phase"));
}

@Test @DirtiesContext
@Test
@DirtiesContext
public void checkFlow() throws Exception {
this.requests.subscribe(handlerExpecting(any(Message.class)));
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.addHeader("Accept", "application/x-java-serialized-object");
request.addHeader("Accept", "application/my-serialized");
request.setParameter("foo", "bar");

MockHttpServletResponse response = new MockHttpServletResponse();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new SerializingHttpMessageConverter());
List<HttpMessageConverter<?>> converters = new ArrayList<>();
SerializingHttpMessageConverter serializingHttpMessageConverter = new SerializingHttpMessageConverter();
serializingHttpMessageConverter.setSupportedMediaTypes(
Collections.singletonList(new MediaType("application", "my-serialized")));
converters.add(serializingHttpMessageConverter);
this.gateway.setMessageConverters(converters);
this.gateway.afterPropertiesSet();
this.gateway.start();

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

assertEquals(response.getContentType(), "application/x-java-serialized-object");
assertEquals(response.getContentType(), "application/my-serialized");
}

@Test
public void testController() throws Exception {
DirectFieldAccessor accessor = new DirectFieldAccessor(inboundController);
String errorCode = (String) accessor.getPropertyValue("errorCode");
String errorCode = (String) accessor.getPropertyValue("errorCode");
assertEquals("oops", errorCode);
LiteralExpression viewExpression = (LiteralExpression) accessor.getPropertyValue("viewExpression");
assertEquals("foo", viewExpression.getValue());
Expand All @@ -173,7 +178,7 @@ public void testController() throws Exception {
@Test
public void testControllerViewExp() throws Exception {
DirectFieldAccessor accessor = new DirectFieldAccessor(inboundControllerViewExp);
String errorCode = (String) accessor.getPropertyValue("errorCode");
String errorCode = (String) accessor.getPropertyValue("errorCode");
assertEquals("oops", errorCode);
SpelExpression viewExpression = (SpelExpression) accessor.getPropertyValue("viewExpression");
assertNotNull(viewExpression);
Expand All @@ -183,7 +188,7 @@ public void testControllerViewExp() throws Exception {
@Test
public void requestWithHeaders() throws Exception {
DefaultHttpHeaderMapper headerMapper =
(DefaultHttpHeaderMapper) TestUtils.getPropertyValue(withMappedHeaders, "headerMapper");
(DefaultHttpHeaderMapper) TestUtils.getPropertyValue(withMappedHeaders, "headerMapper");

HttpHeaders headers = new HttpHeaders();
headers.set("foo", "foo");
Expand All @@ -207,7 +212,7 @@ public void requestWithHeaders() throws Exception {
@Test
public void requestWithHeadersWithConversionService() throws Exception {
DefaultHttpHeaderMapper headerMapper =
(DefaultHttpHeaderMapper) TestUtils.getPropertyValue(withMappedHeadersAndConverter, "headerMapper");
(DefaultHttpHeaderMapper) TestUtils.getPropertyValue(withMappedHeadersAndConverter, "headerMapper");

headerMapper.setUserDefinedHeaderPrefix("X-");

Expand Down Expand Up @@ -239,7 +244,7 @@ public void requestWithHeadersWithConversionService() throws Exception {
public void testInboundGatewayWithMessageConverterDefaults() {
@SuppressWarnings("unchecked")
List<HttpMessageConverter<?>> messageConverters =
TestUtils.getPropertyValue(gatewayWithOneCustomConverter, "messageConverters", List.class);
TestUtils.getPropertyValue(gatewayWithOneCustomConverter, "messageConverters", List.class);
assertThat("There should be only 1 message converter, by default register-default-converters is off",
messageConverters.size(), is(1));

Expand Down

0 comments on commit b60e1dc

Please sign in to comment.