|
1 | 1 | package com.github.dockerjava.jaxrs.util; |
2 | 2 |
|
| 3 | +import java.io.EOFException; |
3 | 4 | import java.io.IOException; |
| 5 | +import java.nio.charset.Charset; |
4 | 6 |
|
5 | 7 | import javax.ws.rs.client.ClientRequestContext; |
6 | 8 | import javax.ws.rs.client.ClientResponseContext; |
7 | 9 | import javax.ws.rs.client.ClientResponseFilter; |
| 10 | +import javax.ws.rs.core.MediaType; |
8 | 11 |
|
9 | 12 | import org.apache.commons.io.IOUtils; |
10 | 13 |
|
@@ -55,9 +58,36 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re |
55 | 58 |
|
56 | 59 | public String getBodyAsMessage(ClientResponseContext responseContext) |
57 | 60 | throws IOException { |
58 | | - byte[] buffer = new byte[1000]; |
59 | | - IOUtils.read(responseContext.getEntityStream(), buffer); |
60 | | - String message = new String(buffer); |
61 | | - return message; |
| 61 | + if (responseContext.hasEntity()) { |
| 62 | + int contentLength = responseContext.getLength(); |
| 63 | + if (contentLength != -1) { |
| 64 | + byte[] buffer = new byte[contentLength]; |
| 65 | + try { |
| 66 | + IOUtils.readFully(responseContext.getEntityStream(), buffer); |
| 67 | + } |
| 68 | + catch (EOFException e) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + Charset charset = null; |
| 72 | + MediaType mediaType = responseContext.getMediaType(); |
| 73 | + if (mediaType != null) { |
| 74 | + String charsetName = mediaType.getParameters().get("charset"); |
| 75 | + if (charsetName != null) { |
| 76 | + try { |
| 77 | + charset = Charset.forName(charsetName); |
| 78 | + } |
| 79 | + catch (Exception e) { |
| 80 | + //Do noting... |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + if (charset == null) { |
| 85 | + charset = Charset.defaultCharset(); |
| 86 | + } |
| 87 | + String message = new String(buffer, charset); |
| 88 | + return message; |
| 89 | + } |
| 90 | + } |
| 91 | + return null; |
62 | 92 | } |
63 | 93 | } |
0 commit comments