Java Code Examples for org.springframework.http.StreamingHttpOutputMessage

The following examples show how to use org.springframework.http.StreamingHttpOutputMessage. These examples are extracted from open source projects. 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
/**
 * 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 Project: spring-analysis-note   Source File: FormHttpMessageConverter.java    License: MIT License 6 votes vote down vote up
private void writeForm(MultiValueMap<String, Object> 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 3
Source Project: spring-analysis-note   Source File: FormHttpMessageConverter.java    License: 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 4
@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 5
/**
 * 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
@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 7
@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 8
@Test(expected = IllegalStateException.class)
public void multipleWrites() throws Exception {
	ClientHttpRequest request = factory.createRequest(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);
			outputStream.flush();
			outputStream.close();
		});
	}
	else {
		StreamUtils.copy(body, request.getBody());
	}

	request.execute();
	FileCopyUtils.copy(body, request.getBody());
}
 
Example 9
/**
 * 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 10
Source Project: java-technology-stack   Source File: FormHttpMessageConverter.java    License: MIT License 6 votes vote down vote up
private void writeForm(MultiValueMap<String, Object> 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 11
Source Project: java-technology-stack   Source File: FormHttpMessageConverter.java    License: 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 12
@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 13
/**
 * 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 14
@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 15
@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 16
@Test(expected = IllegalStateException.class)
public void multipleWrites() throws Exception {
	ClientHttpRequest request = factory.createRequest(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);
			outputStream.flush();
			outputStream.close();
		});
	}
	else {
		StreamUtils.copy(body, request.getBody());
	}

	request.execute();
	FileCopyUtils.copy(body, request.getBody());
}
 
Example 17
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 18
@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 19
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 20
@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 21
@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 22
@Test(expected = IllegalStateException.class)
public void multipleWrites() throws Exception {
	ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
	final byte[] body = "Hello World".getBytes("UTF-8");
	if (request instanceof StreamingHttpOutputMessage) {
		StreamingHttpOutputMessage streamingRequest =
				(StreamingHttpOutputMessage) request;
		streamingRequest.setBody(new StreamingHttpOutputMessage.Body() {
			@Override
			public void writeTo(OutputStream outputStream) throws IOException {
				StreamUtils.copy(body, outputStream);
			}
		});
	}
	else {
		StreamUtils.copy(body, request.getBody());
	}

	ClientHttpResponse response = request.execute();
	try {
		FileCopyUtils.copy(body, request.getBody());
	}
	finally {
		response.close();
	}
}
 
Example 23
Source Project: riptide   Source File: LogbookPlugin.java    License: 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 24
Source Project: onetwo   Source File: FormHttpMessageConverter.java    License: 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 25
Source Project: onetwo   Source File: FormHttpMessageConverter.java    License: 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 26
@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 27
@Test
public void echo() throws Exception {
	ClientHttpRequest request = factory.createRequest(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());
	}

	ClientHttpResponse response = request.execute();
	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 28
@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 29
@Test
public void echo() throws Exception {
	ClientHttpRequest request = factory.createRequest(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());
	}

	ClientHttpResponse response = request.execute();
	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 30
/**
 * 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();
	}
}