@Override public BodyBuilder contentLength(long contentLength) { this.headers.setContentLength(contentLength); return this; }
@Override public ServerResponse.BodyBuilder contentLength(long contentLength) { this.headers.setContentLength(contentLength); return this; }
@Override public EntityResponse.Builder<T> contentLength(long contentLength) { this.headers.setContentLength(contentLength); return this; }
@Override public BodyBuilder contentLength(long contentLength) { this.headers.setContentLength(contentLength); return this; }
@Override public BodyBuilder contentLength(long contentLength) { this.headers.setContentLength(contentLength); return this; }
@Override public BodyBuilder contentLength(long contentLength) { this.headers.setContentLength(contentLength); return this; }
@Override public DefaultRequestBodyUriSpec contentLength(long contentLength) { getHeaders().setContentLength(contentLength); return this; }
@Override protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers) throws IOException { byte[] bytes = this.bufferedOutput.toByteArray(); if (headers.getContentLength() < 0) { headers.setContentLength(bytes.length); } ListenableFuture<ClientHttpResponse> result = executeInternal(headers, bytes); this.bufferedOutput = new ByteArrayOutputStream(0); return result; }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException { byte[] bytes = this.bufferedOutput.toByteArray(); if (headers.getContentLength() < 0) { headers.setContentLength(bytes.length); } ClientHttpResponse result = executeInternal(headers, bytes); this.bufferedOutput = new ByteArrayOutputStream(0); return result; }
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()); } }
@Override public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { try { ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response; Resource logo = new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class); File logoFile = logo.getFile(); zeroCopyResponse.getHeaders().setContentType(MediaType.IMAGE_PNG); zeroCopyResponse.getHeaders().setContentLength(logoFile.length()); return zeroCopyResponse.writeWith(logoFile, 0, logoFile.length()); } catch (Throwable ex) { return Mono.error(ex); } } }
/** * Apply {@link Flux#reduce(Object, BiFunction) reduce} on the body, count * the number of bytes produced, release data buffers without writing, and * set the {@literal Content-Length} header. */ @Override public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { return Flux.from(body) .reduce(0, (current, buffer) -> { int next = current + buffer.readableByteCount(); DataBufferUtils.release(buffer); return next; }) .doOnNext(count -> getHeaders().setContentLength(count)) .then(); }
@Test public void handleNoMatchOverride() throws Exception { this.errorHandler.setSeriesMapping(Collections .singletonMap(HttpStatus.Series.CLIENT_ERROR, null)); given(this.response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value()); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); given(this.response.getHeaders()).willReturn(responseHeaders); byte[] body = "{\"foo\":\"bar\"}".getBytes(StandardCharsets.UTF_8); responseHeaders.setContentLength(body.length); given(this.response.getBody()).willReturn(new ByteArrayInputStream(body)); this.errorHandler.handleError(this.response); }
private void mockResponseBody(String expectedBody, MediaType mediaType) throws Exception { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(mediaType); responseHeaders.setContentLength(expectedBody.length()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream(expectedBody.getBytes())); given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expectedBody); }
@Test public void writeWithError() throws Exception { TestServerHttpResponse response = new TestServerHttpResponse(); response.getHeaders().setContentLength(12); IllegalStateException error = new IllegalStateException("boo"); response.writeWith(Flux.error(error)).onErrorResume(ex -> Mono.empty()).block(); assertFalse(response.statusCodeWritten); assertFalse(response.headersWritten); assertFalse(response.cookiesWritten); assertFalse(response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)); assertTrue(response.body.isEmpty()); }
@Test public void contentLength() { long length = 42L; headers.setContentLength(length); assertEquals("Invalid Content-Length header", length, headers.getContentLength()); assertEquals("Invalid Content-Length header", "42", headers.getFirst("Content-Length")); }
@Test public void handleErrorSeriesMatch() throws Exception { given(this.response.getRawStatusCode()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR.value()); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); given(this.response.getHeaders()).willReturn(responseHeaders); byte[] body = "{\"foo\":\"bar\"}".getBytes(StandardCharsets.UTF_8); responseHeaders.setContentLength(body.length); given(this.response.getBody()).willReturn(new ByteArrayInputStream(body)); try { this.errorHandler.handleError(this.response); fail("MyRestClientException expected"); } catch (MyRestClientException ex) { assertEquals("bar", ex.getFoo()); } }
@Test public void zeroContentLength() throws IOException { HttpMessageConverter<?> converter = mock(HttpMessageConverter.class); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentLength(0); extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); given(response.getHeaders()).willReturn(responseHeaders); Object result = extractor.extractData(response); assertNull(result); }
@Test public void patchForObjectNull() throws Exception { mockTextPlainHttpMessageConverter(); HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(PATCH, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.TEXT_PLAIN); responseHeaders.setContentLength(10); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(StreamUtils.emptyInput()); String result = template.patchForObject("http://example.com", null, String.class); assertNull("Invalid POST result", result); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); verify(response).close(); }
@Override public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { Mono<Integer> requestSizeMono = request.getBody(). reduce(0, (integer, dataBuffer) -> integer + dataBuffer.readableByteCount()). doOnSuccessOrError((size, throwable) -> { assertNull(throwable); assertEquals(REQUEST_SIZE, (long) size); }); response.getHeaders().setContentLength(RESPONSE_SIZE); return requestSizeMono.then(response.writeWith(multipleChunks())); }