Skip to content

Commit a40b88f

Browse files
committed
("add a Response.getResponseBodyAsBytes()")
1 parent 8c431f9 commit a40b88f

File tree

8 files changed

+65
-1
lines changed

8 files changed

+65
-1
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@
386386
<exclude>**/SpnegoEngine</exclude>
387387
<exclude>**/Request</exclude>
388388
<exclude>**/Request$EntityWriter</exclude>
389-
<exclude>**/RequestBuilderBase</exclude>
389+
<exclude>**/RequestBuilderBase</exclude>
390+
<exclude>**/Response</exclude>
390391
</excludes>
391392
</configuration>
392393
<executions>

src/main/java/com/ning/http/client/Response.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public interface Response {
4444
*/
4545
public String getStatusText();
4646

47+
/**
48+
* Return the entire response body as a byte[].
49+
* @return the entire response body as a byte[].
50+
* @throws IOException
51+
*/
52+
public byte[] getResponseBodyAsBytes() throws IOException;
53+
4754
/**
4855
* Returns an input stream for the response body. Note that you should not try to get this more than once,
4956
* and that you should not close the stream.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public String getStatusText() {
6666
}
6767

6868
/* @Override */
69+
public byte[] getResponseBodyAsBytes() throws IOException {
70+
return AsyncHttpProviderUtils.contentToByte(bodyParts);
71+
}
6972

73+
/* @Override */
7074
public String getResponseBody() throws IOException {
7175
return getResponseBody(DEFAULT_CHARSET);
7276
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public String getResponseBody() throws IOException {
7474
return getResponseBody(DEFAULT_CHARSET);
7575
}
7676

77+
/* @Override */
78+
public byte[] getResponseBodyAsBytes() throws IOException {
79+
return AsyncHttpProviderUtils.contentToByte(bodyParts);
80+
}
81+
7782
public String getResponseBody(String charset) throws IOException {
7883
String contentType = getContentType();
7984
if (contentType != null && charset == null) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ public String getStatusText() {
7272
}
7373

7474
/* @Override */
75+
public byte[] getResponseBodyAsBytes() throws IOException {
76+
return AsyncHttpProviderUtils.contentToByte(bodyParts);
77+
}
7578

79+
/* @Override */
7680
public String getResponseBody() throws IOException {
7781
return getResponseBody(DEFAULT_CHARSET);
7882
}

src/main/java/com/ning/http/client/webdav/WebDavResponse.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public String getStatusText() {
4444
return response.getStatusText();
4545
}
4646

47+
/* @Override */
48+
public byte[] getResponseBodyAsBytes() throws IOException {
49+
return response.getResponseBodyAsBytes();
50+
}
51+
4752
public InputStream getResponseBodyAsStream() throws IOException {
4853
return response.getResponseBodyAsStream();
4954
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,22 @@ public final static String contentToString(Collection<HttpResponseBodyPart> body
184184
return b.toString();
185185
}
186186

187+
public final static byte[] contentToByte(Collection<HttpResponseBodyPart> bodyParts) throws UnsupportedEncodingException {
188+
int size = 0;
189+
for (HttpResponseBodyPart body : bodyParts) {
190+
size += body.getBodyPartBytes().length;
191+
}
192+
byte[] bytes = new byte[size];
193+
int offset = 0;
194+
for (HttpResponseBodyPart body : bodyParts) {
195+
byte[] bodyBytes = body.getBodyPartBytes();
196+
System.arraycopy(bodyBytes, 0, bytes, offset, bodyBytes.length);
197+
offset += bodyBytes.length;
198+
}
199+
200+
return bytes;
201+
}
202+
187203
public final static URI getRedirectUri(URI uri, String location) {
188204
URI newUri = uri.resolve(location);
189205

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,4 +1602,26 @@ public void asyncHttpClientConfigBeanTest() throws Exception {
16021602
Response r = c.executeRequest(builder.build()).get();
16031603
assertEquals(200, r.getStatusCode());
16041604
}
1605+
1606+
@Test(groups = {"default_provider", "async"})
1607+
public void bodyAsByteTest() throws Throwable {
1608+
final AsyncHttpClient client = getAsyncHttpClient(new Builder().build());
1609+
Response r = client.prepareGet(getTargetUrl()).execute().get();
1610+
1611+
assertEquals(r.getStatusCode(), 200);
1612+
assertEquals(r.getResponseBodyAsBytes(), new byte[]{});
1613+
1614+
client.close();
1615+
}
1616+
1617+
@Test(groups = {"default_provider", "async"})
1618+
public void mirrorByteTest() throws Throwable {
1619+
final AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().build());
1620+
Response r = client.preparePost(getTargetUrl()).setBody("MIRROR").execute().get();
1621+
1622+
assertEquals(r.getStatusCode(), 200);
1623+
assertEquals(new String(r.getResponseBodyAsBytes(), "UTF-8"), "MIRROR");
1624+
1625+
client.close();
1626+
}
16051627
}

0 commit comments

Comments
 (0)