Java Code Examples for org.springframework.http.StreamingHttpOutputMessage#setBody()

The following examples show how to use org.springframework.http.StreamingHttpOutputMessage#setBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * This implementation sets the default headers by calling {@link #addDefaultHeaders},
 * and then calls {@link #writeInternal}.
 */
@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(outputStream -> writeInternal(t, new HttpOutputMessage() {
			@Override
			public OutputStream getBody() {
				return outputStream;
			}
			@Override
			public HttpHeaders getHeaders() {
				return headers;
			}
		}));
	}
	else {
		writeInternal(t, outputMessage);
		outputMessage.getBody().flush();
	}
}
 
Example 2
Source File: FormHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example 3
Source File: AbstractAsyncHttpRequestFactoryTestCase.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void multipleWrites() throws Exception {
	AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
	final byte[] body = "Hello World".getBytes("UTF-8");

	if (request instanceof StreamingHttpOutputMessage) {
		StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage) request;
		streamingRequest.setBody(outputStream -> StreamUtils.copy(body, outputStream));
	}
	else {
		StreamUtils.copy(body, request.getBody());
	}

	Future<ClientHttpResponse> futureResponse = request.executeAsync();
	ClientHttpResponse response = futureResponse.get();
	try {
		FileCopyUtils.copy(body, request.getBody());
		fail("IllegalStateException expected");
	}
	catch (IllegalStateException ex) {
		// expected
	}
	finally {
		response.close();
	}
}
 
Example 4
Source File: InterceptingClientHttpRequest.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
	if (this.iterator.hasNext()) {
		ClientHttpRequestInterceptor nextInterceptor = this.iterator.next();
		return nextInterceptor.intercept(request, body, this);
	}
	else {
		HttpMethod method = request.getMethod();
		Assert.state(method != null, "No standard HTTP method");
		ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method);
		request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
		if (body.length > 0) {
			if (delegate instanceof StreamingHttpOutputMessage) {
				StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate;
				streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(body, outputStream));
			}
			else {
				StreamUtils.copy(body, delegate.getBody());
			}
		}
		return delegate.execute();
	}
}
 
Example 5
Source File: AbstractGenericHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * This implementation sets the default headers by calling {@link #addDefaultHeaders},
 * and then calls {@link #writeInternal}.
 */
public final void write(final T t, @Nullable final Type type, @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(outputStream -> writeInternal(t, type, new HttpOutputMessage() {
			@Override
			public OutputStream getBody() {
				return outputStream;
			}
			@Override
			public HttpHeaders getHeaders() {
				return headers;
			}
		}));
	}
	else {
		writeInternal(t, type, outputMessage);
		outputMessage.getBody().flush();
	}
}
 
Example 6
Source File: BufferedImageHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final BufferedImage image, final MediaType contentType,
		final HttpOutputMessage outputMessage)
		throws IOException, HttpMessageNotWritableException {

	if (outputMessage instanceof StreamingHttpOutputMessage) {
		StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
		streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
			@Override
			public void writeTo(OutputStream outputStream) throws IOException {
				writeInternal(image, contentType, outputMessage.getHeaders(), outputStream);
			}
		});
	}
	else {
		writeInternal(image, contentType, outputMessage.getHeaders(), outputMessage.getBody());
	}
}
 
Example 7
Source File: FormHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void writeMultipart(final MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage)
		throws IOException {

	final byte[] boundary = generateMultipartBoundary();
	Map<String, String> parameters = new LinkedHashMap<>(2);
	if (!isFilenameCharsetSet()) {
		parameters.put("charset", this.charset.name());
	}
	parameters.put("boundary", new String(boundary, StandardCharsets.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(outputStream -> {
			writeParts(outputStream, parts, boundary);
			writeEnd(outputStream, boundary);
		});
	}
	else {
		writeParts(outputMessage.getBody(), parts, boundary);
		writeEnd(outputMessage.getBody(), boundary);
	}
}
 
Example 8
Source File: AbstractHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * This implementation sets the default headers by calling {@link #addDefaultHeaders},
 * and then calls {@link #writeInternal}.
 */
@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(outputStream -> writeInternal(t, new HttpOutputMessage() {
			@Override
			public OutputStream getBody() {
				return outputStream;
			}
			@Override
			public HttpHeaders getHeaders() {
				return headers;
			}
		}));
	}
	else {
		writeInternal(t, outputMessage);
		outputMessage.getBody().flush();
	}
}
 
Example 9
Source File: FormHttpMessageConverter.java    From onetwo with Apache License 2.0 6 votes vote down vote up
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());
	}

	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(outputStream -> {
			writeParts(outputStream, parts, boundary);
			writeEnd(outputStream, boundary);
		});
	}
	else {
		writeParts(outputMessage.getBody(), parts, boundary);
		writeEnd(outputMessage.getBody(), boundary);
	}
}
 
Example 10
Source File: BufferedImageHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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());
	}
}
 
Example 11
Source File: InterceptingClientHttpRequest.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
	if (this.iterator.hasNext()) {
		ClientHttpRequestInterceptor nextInterceptor = this.iterator.next();
		return nextInterceptor.intercept(request, body, this);
	}
	else {
		HttpMethod method = request.getMethod();
		Assert.state(method != null, "No standard HTTP method");
		ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method);
		request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
		if (body.length > 0) {
			if (delegate instanceof StreamingHttpOutputMessage) {
				StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate;
				streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(body, outputStream));
			}
			else {
				StreamUtils.copy(body, delegate.getBody());
			}
		}
		return delegate.execute();
	}
}
 
Example 12
Source File: LogbookPlugin.java    From riptide with MIT License 6 votes vote down vote up
@Override
public void writeTo(final HttpOutputMessage message) throws IOException {
    final Process process = process(message);

    if (entity.isEmpty()) {
        process.close();
        return;
    }

    if (message instanceof StreamingHttpOutputMessage) {
        final StreamingHttpOutputMessage streaming =
                (StreamingHttpOutputMessage) message;
        streaming.setBody(process::writeTo);
    } else {
        process.writeTo(message.getBody());
    }
}
 
Example 13
Source File: BufferedImageHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void write(final BufferedImage image, @Nullable 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(outputStream -> writeInternal(image, selectedContentType, outputStream));
	}
	else {
		writeInternal(image, selectedContentType, outputMessage.getBody());
	}
}
 
Example 14
Source File: FormHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void writeMultipart(final MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage)
		throws IOException {

	final byte[] boundary = generateMultipartBoundary();
	Map<String, String> parameters = new LinkedHashMap<>(2);
	if (!isFilenameCharsetSet()) {
		parameters.put("charset", this.charset.name());
	}
	parameters.put("boundary", new String(boundary, StandardCharsets.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(outputStream -> {
			writeParts(outputStream, parts, boundary);
			writeEnd(outputStream, boundary);
		});
	}
	else {
		writeParts(outputMessage.getBody(), parts, boundary);
		writeEnd(outputMessage.getBody(), boundary);
	}
}
 
Example 15
Source File: FormHttpMessageConverter.java    From onetwo with Apache License 2.0 6 votes vote down vote up
private void writeForm(MultiValueMap<String, String> formData, @Nullable MediaType contentType,
		HttpOutputMessage outputMessage) throws IOException {

	contentType = getMediaType(contentType);
	outputMessage.getHeaders().setContentType(contentType);

	Charset charset = contentType.getCharset();
	Assert.notNull(charset, "No charset"); // should never occur

	final byte[] bytes = serializeForm(formData, charset).getBytes(charset);
	outputMessage.getHeaders().setContentLength(bytes.length);

	if (outputMessage instanceof StreamingHttpOutputMessage) {
		StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
		streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(bytes, outputStream));
	}
	else {
		StreamUtils.copy(bytes, outputMessage.getBody());
	}
}
 
Example 16
Source File: AbstractGenericHttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
	}
}
 
Example 17
Source File: AbstractHttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
	}
}
 
Example 18
Source File: AbstractGenericHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
	}
}
 
Example 19
Source File: AbstractAsyncHttpRequestFactoryTestCase.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void echo() throws Exception {
	AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.PUT);
	assertEquals("Invalid HTTP method", HttpMethod.PUT, request.getMethod());
	String headerName = "MyHeader";
	String headerValue1 = "value1";
	request.getHeaders().add(headerName, headerValue1);
	String headerValue2 = "value2";
	request.getHeaders().add(headerName, headerValue2);
	final byte[] body = "Hello World".getBytes("UTF-8");
	request.getHeaders().setContentLength(body.length);

	if (request instanceof StreamingHttpOutputMessage) {
		StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage) request;
		streamingRequest.setBody(outputStream -> StreamUtils.copy(body, outputStream));
	}
	else {
		StreamUtils.copy(body, request.getBody());
	}

	Future<ClientHttpResponse> futureResponse = request.executeAsync();
	ClientHttpResponse response = futureResponse.get();
	try {
		assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode());
		assertTrue("Header not found", response.getHeaders().containsKey(headerName));
		assertEquals("Header value not found", Arrays.asList(headerValue1, headerValue2),
				response.getHeaders().get(headerName));
		byte[] result = FileCopyUtils.copyToByteArray(response.getBody());
		assertTrue("Invalid body", Arrays.equals(body, result));
	}
	finally {
		response.close();
	}
}
 
Example 20
Source File: AbstractAsyncHttpRequestFactoryTestCase.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void echo() throws Exception {
	AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.PUT);
	assertEquals("Invalid HTTP method", HttpMethod.PUT, request.getMethod());
	String headerName = "MyHeader";
	String headerValue1 = "value1";
	request.getHeaders().add(headerName, headerValue1);
	String headerValue2 = "value2";
	request.getHeaders().add(headerName, headerValue2);
	final byte[] body = "Hello World".getBytes("UTF-8");
	request.getHeaders().setContentLength(body.length);

	if (request instanceof StreamingHttpOutputMessage) {
		StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage) request;
		streamingRequest.setBody(outputStream -> StreamUtils.copy(body, outputStream));
	}
	else {
		StreamUtils.copy(body, request.getBody());
	}

	Future<ClientHttpResponse> futureResponse = request.executeAsync();
	ClientHttpResponse response = futureResponse.get();
	try {
		assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode());
		assertTrue("Header not found", response.getHeaders().containsKey(headerName));
		assertEquals("Header value not found", Arrays.asList(headerValue1, headerValue2),
				response.getHeaders().get(headerName));
		byte[] result = FileCopyUtils.copyToByteArray(response.getBody());
		assertTrue("Invalid body", Arrays.equals(body, result));
	}
	finally {
		response.close();
	}
}