Search in sources :

Example 6 with StreamingHttpOutputMessage

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

the class AbstractHttpRequestFactoryTestCase method multipleWrites.

@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);
                outputStream.flush();
                outputStream.close();
            }
        });
    } else {
        StreamUtils.copy(body, request.getBody());
    }
    request.execute();
    FileCopyUtils.copy(body, request.getBody());
}
Also used : StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.Test)

Example 7 with StreamingHttpOutputMessage

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

the class AbstractHttpRequestFactoryTestCase method echo.

@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(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 {
        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();
    }
}
Also used : StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URI(java.net.URI) Test(org.junit.Test)

Example 8 with StreamingHttpOutputMessage

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

the class AbstractAsyncHttpRequestFactoryTestCase method echo.

@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();
    }
}
Also used : StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) URI(java.net.URI) Test(org.junit.Test)

Example 9 with StreamingHttpOutputMessage

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

the class AbstractAsyncHttpRequestFactoryTestCase method multipleWrites.

@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();
    }
}
Also used : StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) URI(java.net.URI) Test(org.junit.Test)

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