Example usage for org.springframework.http StreamingHttpOutputMessage setBody

List of usage examples for org.springframework.http StreamingHttpOutputMessage setBody

Introduction

In this page you can find the example usage for org.springframework.http StreamingHttpOutputMessage setBody.

Prototype

void setBody(Body body);

Source Link

Document

Set the streaming body callback for this message.

Usage

From source file:com.onedrive.api.internal.MultipartRelatedHttpMessageConverter.java

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(MULTIPART_RELATED_MEDIA_TYPE, parameters);
    HttpHeaders headers = outputMessage.getHeaders();
    headers.setContentType(contentType);

    if (outputMessage instanceof StreamingHttpOutputMessage) {
        StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
        streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
            public void writeTo(OutputStream outputStream) throws IOException {
                writeParts(outputStream, parts, boundary);
                writeEnd(outputStream, boundary);
            }//ww  w .ja v a2 s  . c  o  m
        });
    } else {
        writeParts(outputMessage.getBody(), parts, boundary);
        writeEnd(outputMessage.getBody(), boundary);
    }
}

From source file:org.springframework.http.converter.AbstractHttpMessageConverter.java

/**
 * This implementation sets the default headers by calling {@link #addDefaultHeaders},
 * and then calls {@link #writeInternal}.
 *///www .j av a 2s.  c om
@Override
public final void write(final T t, @Nullable 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();
    }
}

From source file:org.springframework.http.converter.FormHttpMessageConverter.java

private void writeForm(MultiValueMap<String, String> form, @Nullable MediaType contentType,
        HttpOutputMessage outputMessage) throws IOException {

    Charset charset;/*from ww  w. ja va2  s  .c  o m*/
    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());
    }
}

From source file:org.springframework.http.converter.FormHttpMessageConverter.java

private void writeMultipart(final MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage)
        throws IOException {

    final byte[] boundary = generateMultipartBoundary();
    Map<String, String> parameters = new HashMap<>(2);
    parameters.put("boundary", new String(boundary, "US-ASCII"));
    if (!isFilenameCharsetSet()) {
        parameters.put("charset", this.charset.name());
    }//from  w  w w .j a v a2  s  .c  o m

    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);
    }
}