Search in sources :

Example 11 with MediaType

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

the class FormHttpMessageConverter method read.

@Override
public MultiValueMap<String, String> read(Class<? extends MultiValueMap<String, ?>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    MediaType contentType = inputMessage.getHeaders().getContentType();
    Charset charset = (contentType.getCharset() != null ? contentType.getCharset() : this.charset);
    String body = StreamUtils.copyToString(inputMessage.getBody(), charset);
    String[] pairs = StringUtils.tokenizeToStringArray(body, "&");
    MultiValueMap<String, String> result = new LinkedMultiValueMap<>(pairs.length);
    for (String pair : pairs) {
        int idx = pair.indexOf('=');
        if (idx == -1) {
            result.add(URLDecoder.decode(pair, charset.name()), null);
        } else {
            String name = URLDecoder.decode(pair.substring(0, idx), charset.name());
            String value = URLDecoder.decode(pair.substring(idx + 1), charset.name());
            result.add(name, value);
        }
    }
    return result;
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset)

Example 12 with MediaType

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

the class FormHttpMessageConverter method writeMultipart.

private void writeMultipart(final MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage) throws IOException {
    final byte[] boundary = generateMultipartBoundary();
    Map<String, String> parameters = Collections.singletonMap("boundary", new String(boundary, "US-ASCII"));
    MediaType contentType = new MediaType(MediaType.MULTIPART_FORM_DATA, parameters);
    HttpHeaders headers = outputMessage.getHeaders();
    headers.setContentType(contentType);
    if (outputMessage instanceof StreamingHttpOutputMessage) {
        StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
        streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {

            @Override
            public void writeTo(OutputStream outputStream) throws IOException {
                writeParts(outputStream, parts, boundary);
                writeEnd(outputStream, boundary);
            }
        });
    } else {
        writeParts(outputMessage.getBody(), parts, boundary);
        writeEnd(outputMessage.getBody(), boundary);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) OutputStream(java.io.OutputStream) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException)

Example 13 with MediaType

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

the class AbstractWireFeedHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(T wireFeed, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    String wireFeedEncoding = wireFeed.getEncoding();
    if (!StringUtils.hasLength(wireFeedEncoding)) {
        wireFeedEncoding = DEFAULT_CHARSET.name();
    }
    MediaType contentType = outputMessage.getHeaders().getContentType();
    if (contentType != null) {
        Charset wireFeedCharset = Charset.forName(wireFeedEncoding);
        contentType = new MediaType(contentType.getType(), contentType.getSubtype(), wireFeedCharset);
        outputMessage.getHeaders().setContentType(contentType);
    }
    WireFeedOutput feedOutput = new WireFeedOutput();
    try {
        Writer writer = new OutputStreamWriter(outputMessage.getBody(), wireFeedEncoding);
        feedOutput.output(wireFeed, writer);
    } catch (FeedException ex) {
        throw new HttpMessageNotWritableException("Could not write WireFeed: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) WireFeedOutput(com.rometools.rome.io.WireFeedOutput) FeedException(com.rometools.rome.io.FeedException) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 14 with MediaType

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

the class AbstractWireFeedHttpMessageConverter method readInternal.

@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    WireFeedInput feedInput = new WireFeedInput();
    MediaType contentType = inputMessage.getHeaders().getContentType();
    Charset charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : DEFAULT_CHARSET);
    try {
        Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
        return (T) feedInput.build(reader);
    } catch (FeedException ex) {
        throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex);
    }
}
Also used : WireFeedInput(com.rometools.rome.io.WireFeedInput) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) InputStreamReader(java.io.InputStreamReader) FeedException(com.rometools.rome.io.FeedException) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Example 15 with MediaType

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

the class AbstractJackson2HttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
    try {
        writePrefix(generator, object);
        Class<?> serializationView = null;
        FilterProvider filters = null;
        Object value = object;
        JavaType javaType = null;
        if (object instanceof MappingJacksonValue) {
            MappingJacksonValue container = (MappingJacksonValue) object;
            value = container.getValue();
            serializationView = container.getSerializationView();
            filters = container.getFilters();
        }
        if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
            javaType = getJavaType(type, null);
        }
        ObjectWriter objectWriter;
        if (serializationView != null) {
            objectWriter = this.objectMapper.writerWithView(serializationView);
        } else if (filters != null) {
            objectWriter = this.objectMapper.writer(filters);
        } else {
            objectWriter = this.objectMapper.writer();
        }
        if (javaType != null && javaType.isContainerType()) {
            objectWriter = objectWriter.forType(javaType);
        }
        SerializationConfig config = objectWriter.getConfig();
        if (contentType != null && contentType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
            objectWriter = objectWriter.with(this.ssePrettyPrinter);
        }
        objectWriter.writeValue(generator, value);
        writeSuffix(generator, object);
        generator.flush();
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Aggregations

MediaType (org.springframework.http.MediaType)253 Test (org.junit.Test)157 HttpHeaders (org.springframework.http.HttpHeaders)49 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)31 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)24 Charset (java.nio.charset.Charset)21 ArrayList (java.util.ArrayList)21 URI (java.net.URI)20 ByteArrayInputStream (java.io.ByteArrayInputStream)15 IOException (java.io.IOException)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)15 List (java.util.List)14 HttpInputMessage (org.springframework.http.HttpInputMessage)14 HttpStatus (org.springframework.http.HttpStatus)11 Resource (org.springframework.core.io.Resource)10 HttpEntity (org.springframework.http.HttpEntity)10 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)9 MediaTypeRequestMatcher (org.springframework.security.web.util.matcher.MediaTypeRequestMatcher)9 HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)9 InputStream (java.io.InputStream)8