use of org.springframework.http.MediaType in project spring-integration by spring-projects.
the class WebFluxInboundEndpoint method selectMediaType.
private MediaType selectMediaType(ServerWebExchange exchange, Supplier<List<MediaType>> producibleTypesSupplier) {
List<MediaType> acceptableTypes = getAcceptableTypes(exchange);
List<MediaType> producibleTypes = getProducibleTypes(exchange, producibleTypesSupplier);
Set<MediaType> compatibleMediaTypes = new LinkedHashSet<>();
for (MediaType acceptable : acceptableTypes) {
for (MediaType producible : producibleTypes) {
if (acceptable.isCompatibleWith(producible)) {
compatibleMediaTypes.add(selectMoreSpecificMediaType(acceptable, producible));
}
}
}
List<MediaType> result = new ArrayList<>(compatibleMediaTypes);
MediaType.sortBySpecificityAndQuality(result);
for (MediaType mediaType : result) {
if (mediaType.isConcrete()) {
return mediaType;
} else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION_ALL)) {
return MediaType.APPLICATION_OCTET_STREAM;
}
}
return null;
}
use of org.springframework.http.MediaType in project spring-integration by spring-projects.
the class HttpRequestHandlingEndpointSupport method extractRequestBody.
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object extractRequestBody(ServletServerHttpRequest request) throws IOException {
MediaType contentType = request.getHeaders().getContentType();
if (contentType == null) {
contentType = MediaType.APPLICATION_OCTET_STREAM;
}
ResolvableType requestPayloadType = getRequestPayloadType();
Class<?> expectedType;
if (requestPayloadType == null) {
expectedType = "text".equals(contentType.getType()) ? String.class : byte[].class;
} else {
expectedType = requestPayloadType.resolve();
}
for (HttpMessageConverter<?> converter : this.messageConverters) {
if (converter.canRead(expectedType, contentType)) {
return converter.read((Class) expectedType, request);
}
}
throw new MessagingException("Could not convert request: no suitable HttpMessageConverter found for expected type [" + expectedType.getName() + "] and content type [" + contentType + "]");
}
use of org.springframework.http.MediaType in project spring-integration by spring-projects.
the class AbstractHttpRequestExecutingMessageHandler method createHttpEntityFromPayload.
private HttpEntity<?> createHttpEntityFromPayload(Message<?> message, HttpMethod httpMethod) {
Object payload = message.getPayload();
if (payload instanceof HttpEntity<?>) {
// payload is already an HttpEntity, just return it as-is
return (HttpEntity<?>) payload;
}
HttpHeaders httpHeaders = this.mapHeaders(message);
if (!shouldIncludeRequestBody(httpMethod)) {
return new HttpEntity<>(httpHeaders);
}
// otherwise, we are creating a request with a body and need to deal with the content-type header as well
if (httpHeaders.getContentType() == null) {
MediaType contentType = (payload instanceof String) ? resolveContentType((String) payload, this.charset) : resolveContentType(payload);
httpHeaders.setContentType(contentType);
}
if (MediaType.APPLICATION_FORM_URLENCODED.equals(httpHeaders.getContentType()) || MediaType.MULTIPART_FORM_DATA.equals(httpHeaders.getContentType())) {
if (!(payload instanceof MultiValueMap)) {
payload = this.convertToMultiValueMap((Map<?, ?>) payload);
}
}
return new HttpEntity<>(payload, httpHeaders);
}
use of org.springframework.http.MediaType in project spring-integration by spring-projects.
the class SimpleMultipartFileReader method readMultipartFile.
@Override
public Object readMultipartFile(MultipartFile multipartFile) throws IOException {
if (multipartFile.getContentType() != null && multipartFile.getContentType().startsWith("text")) {
MediaType contentType = MediaType.parseMediaType(multipartFile.getContentType());
Charset charset = contentType.getCharset();
if (charset == null) {
charset = this.defaultCharset;
}
return new String(multipartFile.getBytes(), charset.name());
} else {
return multipartFile.getBytes();
}
}
use of org.springframework.http.MediaType in project spring-integration by spring-projects.
the class MultipartAwareFormHttpMessageConverter method read.
@Override
public MultiValueMap<String, ?> read(Class<? extends MultiValueMap<String, ?>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
MediaType contentType = inputMessage.getHeaders().getContentType();
if (!MediaType.MULTIPART_FORM_DATA.includes(contentType)) {
return this.wrappedConverter.read(clazz, inputMessage);
}
Assert.state(inputMessage instanceof MultipartHttpInputMessage, "A request with 'multipart/form-data' Content-Type must be a MultipartHttpInputMessage. " + "Be sure to provide a 'multipartResolver' bean in the ApplicationContext.");
MultipartHttpInputMessage multipartInputMessage = (MultipartHttpInputMessage) inputMessage;
return this.readMultipart(multipartInputMessage);
}
Aggregations