X Tutup
Skip to content

Commit 04c528e

Browse files
author
yuxuanchi
committed
Fixed ResponseStatusExceptionFilter.getBodyAsMessage() incorrect reading entity
1 parent 833dc2f commit 04c528e

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/main/java/com/github/dockerjava/jaxrs/util/ResponseStatusExceptionFilter.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.github.dockerjava.jaxrs.util;
22

3+
import java.io.EOFException;
34
import java.io.IOException;
5+
import java.nio.charset.Charset;
46

57
import javax.ws.rs.client.ClientRequestContext;
68
import javax.ws.rs.client.ClientResponseContext;
79
import javax.ws.rs.client.ClientResponseFilter;
10+
import javax.ws.rs.core.MediaType;
811

912
import org.apache.commons.io.IOUtils;
1013

@@ -55,9 +58,36 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
5558

5659
public String getBodyAsMessage(ClientResponseContext responseContext)
5760
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;
6292
}
6393
}

0 commit comments

Comments
 (0)
X Tutup