Skip to content

Commit 6e4ddb6

Browse files
committed
("Response.getXXX must not throw an IllegalStateException when the status code is 204") Avoid throwing an exception when たn empty body is expected.
1 parent 57f6e80 commit 6e4ddb6

File tree

5 files changed

+42
-32
lines changed

5 files changed

+42
-32
lines changed

src/main/java/com/ning/http/client/providers/apache/ApacheResponse.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
public class ApacheResponse implements Response {
3737
private final static String DEFAULT_CHARSET = "ISO-8859-1";
3838
private final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler.";
39-
private final static String BODY_NOT_COMPUTED = "Response's body hasn't been computed by your AsyncHandler.";
4039

4140
private final URI uri;
4241
private final Collection<HttpResponseBodyPart> bodyParts;
@@ -87,7 +86,7 @@ public String getResponseBody(String charset) throws IOException {
8786
}
8887

8988
String contentToString(String charset) throws UnsupportedEncodingException {
90-
checkBodyParts();
89+
AsyncHttpProviderUtils.checkBodyParts(status.getStatusCode(), bodyParts);
9190

9291
StringBuilder b = new StringBuilder();
9392
for (HttpResponseBodyPart bp : bodyParts) {
@@ -99,16 +98,10 @@ String contentToString(String charset) throws UnsupportedEncodingException {
9998
/* @Override */
10099

101100
public InputStream getResponseBodyAsStream() throws IOException {
102-
checkBodyParts();
101+
AsyncHttpProviderUtils.checkBodyParts(status.getStatusCode(), bodyParts);
103102
return new ByteArrayInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()])[0].getBodyPartBytes());
104103
}
105104

106-
private void checkBodyParts() {
107-
if (bodyParts == null || bodyParts.size() == 0) {
108-
throw new IllegalStateException(BODY_NOT_COMPUTED);
109-
}
110-
}
111-
112105
/* @Override */
113106

114107
public String getResponseBodyExcerpt(int maxLength) throws IOException {

src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
public class JDKResponse implements Response {
3838
private final static String DEFAULT_CHARSET = "ISO-8859-1";
3939
private final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler.";
40-
private final static String BODY_NOT_COMPUTED = "Response's body hasn't been computed by your AsyncHandler.";
4140

4241
private final URI uri;
4342
private final Collection<HttpResponseBodyPart> bodyParts;
@@ -93,7 +92,7 @@ public String getResponseBody(String charset) throws IOException {
9392
}
9493

9594
String contentToString(String charset) throws UnsupportedEncodingException {
96-
checkBodyParts();
95+
AsyncHttpProviderUtils.checkBodyParts(status.getStatusCode(), bodyParts);
9796

9897
StringBuilder b = new StringBuilder();
9998
for (HttpResponseBodyPart bp : bodyParts) {
@@ -106,7 +105,7 @@ String contentToString(String charset) throws UnsupportedEncodingException {
106105
/* @Override */
107106

108107
public InputStream getResponseBodyAsStream() throws IOException {
109-
checkBodyParts();
108+
AsyncHttpProviderUtils.checkBodyParts(status.getStatusCode(), bodyParts);
110109

111110
if (contentComputed.get()) {
112111
return new ByteArrayInputStream(content.getBytes(DEFAULT_CHARSET));
@@ -159,12 +158,6 @@ public int read() throws IOException {
159158
}
160159
}
161160

162-
private void checkBodyParts() {
163-
if (bodyParts == null || bodyParts.size() == 0) {
164-
throw new IllegalStateException(BODY_NOT_COMPUTED);
165-
}
166-
}
167-
168161
/* @Override */
169162

170163
public String getResponseBodyExcerpt(int maxLength) throws IOException {

src/main/java/com/ning/http/client/providers/netty/NettyResponse.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
public class NettyResponse implements Response {
4444
private final static String DEFAULT_CHARSET = "ISO-8859-1";
4545
private final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler.";
46-
private final static String BODY_NOT_COMPUTED = "Response's body hasn't been computed by your AsyncHandler.";
4746

4847
private final URI uri;
4948
private final Collection<HttpResponseBodyPart> bodyParts;
@@ -52,8 +51,8 @@ public class NettyResponse implements Response {
5251
private final List<Cookie> cookies = new ArrayList<Cookie>();
5352

5453
public NettyResponse(HttpResponseStatus status,
55-
HttpResponseHeaders headers,
56-
Collection<HttpResponseBodyPart> bodyParts) {
54+
HttpResponseHeaders headers,
55+
Collection<HttpResponseBodyPart> bodyParts) {
5756

5857
this.status = status;
5958
this.headers = headers;
@@ -94,7 +93,7 @@ public String getResponseBody(String charset) throws IOException {
9493

9594

9695
String contentToString(String charset) throws UnsupportedEncodingException {
97-
checkBodyParts();
96+
AsyncHttpProviderUtils.checkBodyParts(status.getStatusCode(), bodyParts);
9897

9998
StringBuilder b = new StringBuilder();
10099
for (HttpResponseBodyPart bp : bodyParts) {
@@ -106,7 +105,7 @@ String contentToString(String charset) throws UnsupportedEncodingException {
106105
/* @Override */
107106

108107
public InputStream getResponseBodyAsStream() throws IOException {
109-
checkBodyParts();
108+
AsyncHttpProviderUtils.checkBodyParts(status.getStatusCode(), bodyParts);
110109

111110
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
112111
for (HttpResponseBodyPart bp : bodyParts) {
@@ -121,20 +120,14 @@ public InputStream getResponseBodyAsStream() throws IOException {
121120
return new ChannelBufferInputStream(buf);
122121
}
123122

124-
private void checkBodyParts() {
125-
if (bodyParts == null || bodyParts.size() == 0) {
126-
throw new IllegalStateException(BODY_NOT_COMPUTED);
127-
}
128-
}
129-
130123
/* @Override */
131124

132125
public String getResponseBodyExcerpt(int maxLength) throws IOException {
133126
return getResponseBodyExcerpt(maxLength, DEFAULT_CHARSET);
134127
}
135128

136129
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
137-
checkBodyParts();
130+
AsyncHttpProviderUtils.checkBodyParts(status.getStatusCode(), bodyParts);
138131

139132
String contentType = getContentType();
140133
if (contentType != null) {

src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.ning.http.client.Cookie;
1818
import com.ning.http.client.FilePart;
1919
import com.ning.http.client.FluentStringsMap;
20+
import com.ning.http.client.HttpResponseBodyPart;
2021
import com.ning.http.client.Part;
2122
import com.ning.http.client.StringPart;
2223
import com.ning.http.multipart.ByteArrayPartSource;
@@ -39,6 +40,7 @@
3940
* The cookies's handling code is from the Netty framework.
4041
*/
4142
public class AsyncHttpProviderUtils {
43+
private final static String BODY_NOT_COMPUTED = "Response's body hasn't been computed by your AsyncHandler.";
4244

4345
private final static SimpleDateFormat[] RFC2822_LIKE_DATE_FORMATS =
4446
{
@@ -442,4 +444,14 @@ private static int convertExpireField(String timestring) throws ParseException {
442444

443445
throw exception;
444446
}
447+
448+
public static void checkBodyParts(int statusCode, Collection<HttpResponseBodyPart> bodyParts) {
449+
if (bodyParts == null || bodyParts.size() == 0) {
450+
451+
// We allow empty body on 204
452+
if (statusCode == 204) return;
453+
454+
throw new IllegalStateException(BODY_NOT_COMPUTED);
455+
}
456+
}
445457
}

src/test/java/com/ning/http/client/async/EmptyBodyTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.ning.http.client.HttpResponseBodyPart;
2121
import com.ning.http.client.HttpResponseHeaders;
2222
import com.ning.http.client.HttpResponseStatus;
23+
import com.ning.http.client.Response;
2324
import org.eclipse.jetty.server.Request;
2425
import org.eclipse.jetty.server.handler.AbstractHandler;
2526
import org.testng.annotations.Test;
@@ -37,6 +38,8 @@
3738
import static org.testng.Assert.assertEquals;
3839
import static org.testng.Assert.assertFalse;
3940
import static org.testng.Assert.assertTrue;
41+
import static org.testng.Assert.assertNotNull;
42+
4043
import static org.testng.Assert.fail;
4144

4245
/**
@@ -52,7 +55,12 @@ public void handle(
5255
HttpServletRequest req,
5356
HttpServletResponse resp)
5457
throws IOException, ServletException {
55-
resp.setStatus(HttpServletResponse.SC_OK);
58+
59+
if (!req.getMethod().equalsIgnoreCase("PUT")) {
60+
resp.setStatus(HttpServletResponse.SC_OK);
61+
} else {
62+
resp.setStatus(204);
63+
}
5664
request.setHandled(true);
5765
}
5866
}
@@ -90,7 +98,7 @@ public STATE onBodyPartReceived(HttpResponseBodyPart e) throws Exception {
9098

9199
public STATE onStatusReceived(HttpResponseStatus e) throws Exception {
92100
status.set(true);
93-
return STATE.CONTINUE;
101+
return AsyncHandler.STATE.CONTINUE;
94102
}
95103

96104
public STATE onHeadersReceived(HttpResponseHeaders e) throws Exception {
@@ -116,4 +124,15 @@ public Object onCompleted() throws Exception {
116124
assertEquals(headers.get(), 1);
117125
ahc.close();
118126
}
127+
128+
@Test(groups = {"standalone", "default_provider"})
129+
public void testPutEmptyBody() throws Throwable {
130+
AsyncHttpClient ahc = getAsyncHttpClient(null);
131+
Response response = ahc.preparePut(getTargetUrl()).setBody("String").execute().get();
132+
133+
assertNotNull(response);
134+
assertEquals(response.getStatusCode(), 204);
135+
assertEquals(response.getResponseBody(), "");
136+
ahc.close();
137+
}
119138
}

0 commit comments

Comments
 (0)