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());
}
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");
}
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));
}
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());
}
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));
}
Aggregations