Search in sources :

Example 71 with MediaType

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType)

Example 72 with MediaType

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 + "]");
}
Also used : MessagingException(org.springframework.messaging.MessagingException) MediaType(org.springframework.http.MediaType) ResolvableType(org.springframework.core.ResolvableType)

Example 73 with MediaType

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) MediaType(org.springframework.http.MediaType) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) ExpressionEvalMap(org.springframework.integration.expression.ExpressionEvalMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 74 with MediaType

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();
    }
}
Also used : MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset)

Example 75 with MediaType

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);
}
Also used : MultipartHttpInputMessage(org.springframework.integration.http.multipart.MultipartHttpInputMessage) MediaType(org.springframework.http.MediaType)

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