Search in sources :

Example 1 with SerializingHttpMessageConverter

use of org.springframework.integration.http.converter.SerializingHttpMessageConverter in project spring-integration by spring-projects.

the class HttpInboundGatewayParserTests method checkFlow.

@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/my-serialized");
    request.setParameter("foo", "bar");
    MockHttpServletResponse response = new MockHttpServletResponse();
    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/my-serialized");
}
Also used : SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 2 with SerializingHttpMessageConverter

use of org.springframework.integration.http.converter.SerializingHttpMessageConverter in project spring-integration by spring-projects.

the class HttpRequestHandlingMessagingGatewayTests method serializableRequestBody.

@Test
public void serializableRequestBody() throws Exception {
    QueueChannel channel = new QueueChannel();
    HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setRequestPayloadTypeClass(TestBean.class);
    gateway.setRequestChannel(channel);
    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
    converters.add(new SerializingHttpMessageConverter());
    gateway.setMessageConverters(converters);
    gateway.afterPropertiesSet();
    gateway.start();
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test");
    request.setContentType("application/x-java-serialized-object");
    TestBean testBean = new TestBean();
    testBean.setName("T. Bean");
    testBean.setAge(42);
    request.setContent(SerializationUtils.serialize(testBean));
    MockHttpServletResponse response = new MockHttpServletResponse();
    gateway.handleRequest(request, response);
    byte[] bytes = response.getContentAsByteArray();
    assertNotNull(bytes);
    Message<?> message = channel.receive(0);
    assertNotNull(message);
    assertNotNull(message.getPayload());
    assertEquals(TestBean.class, message.getPayload().getClass());
    TestBean result = (TestBean) message.getPayload();
    assertEquals("T. Bean", result.name);
    assertEquals(84, result.age);
}
Also used : SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) QueueChannel(org.springframework.integration.channel.QueueChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) AbstractHttpMessageConverter(org.springframework.http.converter.AbstractHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with SerializingHttpMessageConverter

use of org.springframework.integration.http.converter.SerializingHttpMessageConverter in project spring-integration by spring-projects.

the class HttpRequestExecutingMessageHandlerTests method acceptHeaderForSerializableResponseMessageExchange.

@Test
public void acceptHeaderForSerializableResponseMessageExchange() throws Exception {
    HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    handler.setHttpMethod(HttpMethod.GET);
    handler.setExtractPayload(false);
    handler.setExpectedResponseType(GenericMessage.class);
    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
    converters.add(new SerializingHttpMessageConverter());
    handler.setMessageConverters(converters);
    setBeanFactory(handler);
    handler.afterPropertiesSet();
    RestTemplate restTemplate = TestUtils.getPropertyValue(handler, "restTemplate", RestTemplate.class);
    HttpHeaders requestHeaders = setUpMocksToCaptureSentHeaders(restTemplate);
    Message<?> message = MessageBuilder.withPayload("foo").build();
    Exception exception = null;
    try {
        handler.handleMessage(message);
    } catch (Exception e) {
        exception = e;
    }
    assertTrue(requestHeaders.getAccept() != null);
    assertTrue(requestHeaders.getAccept().size() > 0);
    assertEquals("404 Not Found", exception.getCause().getMessage());
    List<MediaType> accept = requestHeaders.getAccept();
    assertTrue(accept != null && accept.size() > 0);
    assertEquals("application", accept.get(0).getType());
    assertEquals("x-java-serialized-object", accept.get(0).getSubtype());
}
Also used : SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) ArrayList(java.util.ArrayList) RestTemplate(org.springframework.web.client.RestTemplate) MediaType(org.springframework.http.MediaType) URISyntaxException(java.net.URISyntaxException) RestClientException(org.springframework.web.client.RestClientException) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with SerializingHttpMessageConverter

use of org.springframework.integration.http.converter.SerializingHttpMessageConverter in project spring-integration by spring-projects.

the class HttpRequestExecutingMessageHandlerTests method acceptHeaderForSerializableResponse.

@Test
public void acceptHeaderForSerializableResponse() throws Exception {
    HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
    handler.setHttpMethod(HttpMethod.GET);
    handler.setExpectedResponseType(Foo.class);
    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
    converters.add(new SerializingHttpMessageConverter());
    handler.setMessageConverters(converters);
    setBeanFactory(handler);
    handler.afterPropertiesSet();
    RestTemplate restTemplate = TestUtils.getPropertyValue(handler, "restTemplate", RestTemplate.class);
    HttpHeaders requestHeaders = setUpMocksToCaptureSentHeaders(restTemplate);
    Message<?> message = MessageBuilder.withPayload("foo").build();
    Exception exception = null;
    try {
        handler.handleMessage(message);
    } catch (Exception e) {
        exception = e;
    }
    assertTrue(requestHeaders.getAccept() != null);
    assertTrue(requestHeaders.getAccept().size() > 0);
    assertEquals("404 Not Found", exception.getCause().getMessage());
    List<MediaType> accept = requestHeaders.getAccept();
    assertTrue(accept != null && accept.size() > 0);
    assertEquals("application", accept.get(0).getType());
    assertEquals("x-java-serialized-object", accept.get(0).getSubtype());
}
Also used : SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) ArrayList(java.util.ArrayList) RestTemplate(org.springframework.web.client.RestTemplate) MediaType(org.springframework.http.MediaType) URISyntaxException(java.net.URISyntaxException) RestClientException(org.springframework.web.client.RestClientException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)4 SerializingHttpMessageConverter (org.springframework.integration.http.converter.SerializingHttpMessageConverter)4 MediaType (org.springframework.http.MediaType)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 HttpHeaders (org.springframework.http.HttpHeaders)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 RestClientException (org.springframework.web.client.RestClientException)2 RestTemplate (org.springframework.web.client.RestTemplate)2 BeanFactory (org.springframework.beans.factory.BeanFactory)1 AbstractHttpMessageConverter (org.springframework.http.converter.AbstractHttpMessageConverter)1 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 DirtiesContext (org.springframework.test.annotation.DirtiesContext)1