Search in sources :

Example 66 with MediaType

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

the class DefaultAmqpHeaderMapperTests method fromHeadersWithContentTypeAsMediaType.

@Test
public void fromHeadersWithContentTypeAsMediaType() {
    DefaultAmqpHeaderMapper headerMapper = DefaultAmqpHeaderMapper.inboundMapper();
    Map<String, Object> headerMap = new HashMap<String, Object>();
    MediaType contentType = MediaType.parseMediaType("text/html");
    headerMap.put(AmqpHeaders.CONTENT_TYPE, contentType);
    MessageHeaders integrationHeaders = new MessageHeaders(headerMap);
    MessageProperties amqpProperties = new MessageProperties();
    headerMapper.fromHeadersToRequest(integrationHeaders, amqpProperties);
    assertEquals("text/html", amqpProperties.getContentType());
    headerMap.put(AmqpHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
    integrationHeaders = new MessageHeaders(headerMap);
    amqpProperties = new MessageProperties();
    headerMapper.fromHeadersToRequest(integrationHeaders, amqpProperties);
    assertEquals(MimeTypeUtils.APPLICATION_JSON_VALUE, amqpProperties.getContentType());
}
Also used : HashMap(java.util.HashMap) MessageProperties(org.springframework.amqp.core.MessageProperties) MediaType(org.springframework.http.MediaType) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 67 with MediaType

use of org.springframework.http.MediaType 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 68 with MediaType

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

the class HttpRequestHandlingMessagingGatewayTests method INT2680DuplicateContentTypeHeader.

@Test
public void INT2680DuplicateContentTypeHeader() throws Exception {
    final DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return MessageBuilder.withPayload("Cartman".getBytes()).setHeader("Content-type", "text/plain").build();
        }
    });
    final List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
    supportedMediaTypes.add(MediaType.TEXT_HTML);
    final ByteArrayHttpMessageConverter messageConverter = new ByteArrayHttpMessageConverter();
    messageConverter.setSupportedMediaTypes(supportedMediaTypes);
    final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(messageConverter);
    final HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setMessageConverters(messageConverters);
    gateway.setRequestChannel(requestChannel);
    gateway.start();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    final ContentTypeCheckingMockHttpServletResponse response = new ContentTypeCheckingMockHttpServletResponse();
    gateway.handleRequest(request, response);
    assertEquals("Cartman", response.getContentAsString());
    /* Before fixing INT2680, 2 content type headers were being written. */
    final List<String> contentTypes = response.getContentTypeList();
    assertEquals("Exptecting only 1 content type being set.", Integer.valueOf(1), Integer.valueOf(contentTypes.size()));
    assertEquals("text/plain", contentTypes.get(0));
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) 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) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 69 with MediaType

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

the class DefaultHttpHeaderMapperFromMessageOutboundTests method validateAcceptHeaderSingleMediaType.

@Test
public void validateAcceptHeaderSingleMediaType() {
    HeaderMapper<HttpHeaders> mapper = DefaultHttpHeaderMapper.outboundMapper();
    Map<String, Object> messageHeaders = new HashMap<String, Object>();
    messageHeaders.put("Accept", new MediaType("bar", "foo"));
    HttpHeaders headers = new HttpHeaders();
    mapper.fromHeaders(new MessageHeaders(messageHeaders), headers);
    assertEquals("bar", headers.getAccept().get(0).getType());
    assertEquals("foo", headers.getAccept().get(0).getSubtype());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HashMap(java.util.HashMap) MediaType(org.springframework.http.MediaType) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 70 with MediaType

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

the class WebFluxInboundEndpoint method writeResponseBody.

@SuppressWarnings("unchecked")
private Mono<Void> writeResponseBody(ServerWebExchange exchange, Object body) {
    ResolvableType bodyType = ResolvableType.forInstance(body);
    ReactiveAdapter adapter = this.adapterRegistry.getAdapter(bodyType.resolve(), body);
    Publisher<?> publisher;
    ResolvableType elementType;
    if (adapter != null) {
        publisher = adapter.toPublisher(body);
        ResolvableType genericType = bodyType.getGeneric(0);
        elementType = getElementType(adapter, genericType);
    } else {
        publisher = Mono.justOrEmpty(body);
        elementType = bodyType;
    }
    if (void.class == elementType.getRawClass() || Void.class == elementType.getRawClass()) {
        return Mono.from((Publisher<Void>) publisher);
    }
    List<MediaType> producibleMediaTypes = getProducibleMediaTypes(bodyType);
    MediaType bestMediaType = selectMediaType(exchange, () -> producibleMediaTypes);
    if (bestMediaType != null) {
        for (HttpMessageWriter<?> writer : this.codecConfigurer.getWriters()) {
            if (writer.canWrite(bodyType, bestMediaType)) {
                return ((HttpMessageWriter<Object>) writer).write(publisher, elementType, bestMediaType, exchange.getResponse(), Collections.emptyMap());
            }
        }
    } else {
        if (producibleMediaTypes.isEmpty()) {
            return Mono.error(new IllegalStateException("No HttpMessageWriters for response type: " + bodyType));
        }
    }
    return Mono.error(new NotAcceptableStatusException(producibleMediaTypes));
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Aggregations

MediaType (org.springframework.http.MediaType)477 Test (org.junit.jupiter.api.Test)177 HttpHeaders (org.springframework.http.HttpHeaders)97 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)58 Charset (java.nio.charset.Charset)42 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)34 List (java.util.List)33 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)33 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)33 HashMap (java.util.HashMap)30 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)30 IOException (java.io.IOException)26 Resource (org.springframework.core.io.Resource)23 ResolvableType (org.springframework.core.ResolvableType)22 HttpEntity (org.springframework.http.HttpEntity)21 ServerWebExchange (org.springframework.web.server.ServerWebExchange)20 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)20 Nullable (org.springframework.lang.Nullable)18 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18