Search in sources :

Example 1 with StreamingHttpOutputMessage

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

the class FormHttpMessageConverter method writeForm.

private void writeForm(MultiValueMap<String, String> form, MediaType contentType, HttpOutputMessage outputMessage) throws IOException {
    Charset charset;
    if (contentType != null) {
        outputMessage.getHeaders().setContentType(contentType);
        charset = (contentType.getCharset() != null ? contentType.getCharset() : this.charset);
    } else {
        outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        charset = this.charset;
    }
    StringBuilder builder = new StringBuilder();
    for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext(); ) {
        String name = nameIterator.next();
        for (Iterator<String> valueIterator = form.get(name).iterator(); valueIterator.hasNext(); ) {
            String value = valueIterator.next();
            builder.append(URLEncoder.encode(name, charset.name()));
            if (value != null) {
                builder.append('=');
                builder.append(URLEncoder.encode(value, charset.name()));
                if (valueIterator.hasNext()) {
                    builder.append('&');
                }
            }
        }
        if (nameIterator.hasNext()) {
            builder.append('&');
        }
    }
    final byte[] bytes = builder.toString().getBytes(charset);
    outputMessage.getHeaders().setContentLength(bytes.length);
    if (outputMessage instanceof StreamingHttpOutputMessage) {
        StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
        streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {

            @Override
            public void writeTo(OutputStream outputStream) throws IOException {
                StreamUtils.copy(bytes, outputStream);
            }
        });
    } else {
        StreamUtils.copy(bytes, outputMessage.getBody());
    }
}
Also used : StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Example 2 with StreamingHttpOutputMessage

use of org.springframework.http.StreamingHttpOutputMessage 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 3 with StreamingHttpOutputMessage

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

the class BufferedImageHttpMessageConverter method write.

@Override
public void write(final BufferedImage image, final MediaType contentType, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    final MediaType selectedContentType = getContentType(contentType);
    outputMessage.getHeaders().setContentType(selectedContentType);
    if (outputMessage instanceof StreamingHttpOutputMessage) {
        StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
        streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {

            @Override
            public void writeTo(OutputStream outputStream) throws IOException {
                writeInternal(image, selectedContentType, outputStream);
            }
        });
    } else {
        writeInternal(image, selectedContentType, outputMessage.getBody());
    }
}
Also used : StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) FileCacheImageOutputStream(javax.imageio.stream.FileCacheImageOutputStream) OutputStream(java.io.OutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException)

Example 4 with StreamingHttpOutputMessage

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

the class AbstractGenericHttpMessageConverter method write.

/**
	 * This implementation sets the default headers by calling {@link #addDefaultHeaders},
	 * and then calls {@link #writeInternal}.
	 */
public final void write(final T t, final Type type, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    final HttpHeaders headers = outputMessage.getHeaders();
    addDefaultHeaders(headers, t, contentType);
    if (outputMessage instanceof StreamingHttpOutputMessage) {
        StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
        streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {

            @Override
            public void writeTo(final OutputStream outputStream) throws IOException {
                writeInternal(t, type, new HttpOutputMessage() {

                    @Override
                    public OutputStream getBody() throws IOException {
                        return outputStream;
                    }

                    @Override
                    public HttpHeaders getHeaders() {
                        return headers;
                    }
                });
            }
        });
    } else {
        writeInternal(t, type, outputMessage);
        outputMessage.getBody().flush();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) HttpOutputMessage(org.springframework.http.HttpOutputMessage) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 5 with StreamingHttpOutputMessage

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

the class AbstractHttpMessageConverter method write.

/**
	 * This implementation sets the default headers by calling {@link #addDefaultHeaders},
	 * and then calls {@link #writeInternal}.
	 */
@Override
public final void write(final T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    final HttpHeaders headers = outputMessage.getHeaders();
    addDefaultHeaders(headers, t, contentType);
    if (outputMessage instanceof StreamingHttpOutputMessage) {
        StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
        streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {

            @Override
            public void writeTo(final OutputStream outputStream) throws IOException {
                writeInternal(t, new HttpOutputMessage() {

                    @Override
                    public OutputStream getBody() throws IOException {
                        return outputStream;
                    }

                    @Override
                    public HttpHeaders getHeaders() {
                        return headers;
                    }
                });
            }
        });
    } else {
        writeInternal(t, outputMessage);
        outputMessage.getBody().flush();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) HttpOutputMessage(org.springframework.http.HttpOutputMessage) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Aggregations

StreamingHttpOutputMessage (org.springframework.http.StreamingHttpOutputMessage)9 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 URI (java.net.URI)4 Test (org.junit.Test)4 HttpHeaders (org.springframework.http.HttpHeaders)3 HttpOutputMessage (org.springframework.http.HttpOutputMessage)2 MediaType (org.springframework.http.MediaType)2 Charset (java.nio.charset.Charset)1 FileCacheImageOutputStream (javax.imageio.stream.FileCacheImageOutputStream)1 ImageOutputStream (javax.imageio.stream.ImageOutputStream)1 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)1