From 8a8225925ed105142b7475ca5a918405218efb24 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 14 Oct 2015 14:00:37 +0200 Subject: [PATCH] Clarified that getBody() never returns null As the only place that historically differed, HttpComponents(Async)ClientHttpResponse returns an empty stream instead of null now. Issue: SPR-13563 (cherry picked from commit ca60d79) --- .../java/org/springframework/http/HttpInputMessage.java | 7 ++++--- .../main/java/org/springframework/http/HttpMessage.java | 8 ++++---- .../java/org/springframework/http/HttpOutputMessage.java | 7 ++++--- .../http/client/HttpComponentsClientHttpResponse.java | 5 +++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpInputMessage.java b/spring-web/src/main/java/org/springframework/http/HttpInputMessage.java index ddc1ebd55d6..9aeab46b9a1 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpInputMessage.java +++ b/spring-web/src/main/java/org/springframework/http/HttpInputMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,8 @@ import java.io.InputStream; * Represents an HTTP input message, consisting of {@linkplain #getHeaders() headers} * and a readable {@linkplain #getBody() body}. * - *

Typically implemented by an HTTP request on the server-side, or a response on the client-side. + *

Typically implemented by an HTTP request handle on the server side, + * or an HTTP response handle on the client side. * * @author Arjen Poutsma * @since 3.0 @@ -32,7 +33,7 @@ public interface HttpInputMessage extends HttpMessage { /** * Return the body of the message as an input stream. - * @return the input stream body + * @return the input stream body (never {@code null}) * @throws IOException in case of I/O Errors */ InputStream getBody() throws IOException; diff --git a/spring-web/src/main/java/org/springframework/http/HttpMessage.java b/spring-web/src/main/java/org/springframework/http/HttpMessage.java index 80f7ca292d5..1855c44f52b 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpMessage.java +++ b/spring-web/src/main/java/org/springframework/http/HttpMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,8 @@ package org.springframework.http; /** - * Represents the base interface for HTTP request and response messages. Consists of {@link HttpHeaders}, retrievable - * via {@link #getHeaders()}. + * Represents the base interface for HTTP request and response messages. + * Consists of {@link HttpHeaders}, retrievable via {@link #getHeaders()}. * * @author Arjen Poutsma * @since 3.0 @@ -27,7 +27,7 @@ public interface HttpMessage { /** * Return the headers of this message. - * @return a corresponding HttpHeaders object + * @return a corresponding HttpHeaders object (never {@code null}) */ HttpHeaders getHeaders(); diff --git a/spring-web/src/main/java/org/springframework/http/HttpOutputMessage.java b/spring-web/src/main/java/org/springframework/http/HttpOutputMessage.java index 3dae0074656..fdee15b0c96 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpOutputMessage.java +++ b/spring-web/src/main/java/org/springframework/http/HttpOutputMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,8 @@ import java.io.OutputStream; * Represents an HTTP output message, consisting of {@linkplain #getHeaders() headers} * and a writable {@linkplain #getBody() body}. * - *

Typically implemented by an HTTP request on the client-side, or a response on the server-side. + *

Typically implemented by an HTTP request handle on the client side, + * or an HTTP response handle on the server side. * * @author Arjen Poutsma * @since 3.0 @@ -32,7 +33,7 @@ public interface HttpOutputMessage extends HttpMessage { /** * Return the body of the message as an output stream. - * @return the output stream body + * @return the output stream body (never {@code null}) * @throws IOException in case of I/O Errors */ OutputStream getBody() throws IOException; diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java index d4033e4216a..2a2753df09b 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.http.client; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -69,7 +70,7 @@ final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse public InputStream getBody() throws IOException { HttpEntity entity = this.httpResponse.getEntity(); - return entity != null ? entity.getContent() : null; + return (entity != null ? entity.getContent() : new ByteArrayInputStream(new byte[0])); } public void close() {