} else {
throw new RuntimeException("Unknown client side exception");
}
}
int status = responseCode.intValue();
ResponseBuilder currentResponseBuilder = Response.status(status);
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
if (null == entry.getKey()) {
continue;
}
if (entry.getValue().size() > 0) {
if (HttpUtils.isDateRelatedHeader(entry.getKey())) {
currentResponseBuilder.header(entry.getKey(), entry.getValue().get(0));
continue;
}
boolean splitPossible = !(HttpHeaders.SET_COOKIE.equalsIgnoreCase(entry.getKey())
&& entry.getValue().get(0).contains(HttpHeaders.EXPIRES));
for (String val : entry.getValue()) {
String[] values = splitPossible ? val.split(",") : new String[]{val};
for (String s : values) {
String theValue = s.trim();
if (theValue.length() > 0) {
currentResponseBuilder.header(entry.getKey(), theValue);
}
}
}
}
}
InputStream mStream = null;
if (inMessage != null) {
mStream = inMessage.getContent(InputStream.class);
}
if (status >= 400) {
try {
InputStream errorStream = mStream == null ? conn.getErrorStream() : mStream;
currentResponseBuilder.entity(errorStream);
} catch (Exception ex) {
// nothing we can do really
}
} else {
try {
InputStream stream = mStream == null ? conn.getInputStream() : mStream;
currentResponseBuilder.entity(stream);
} catch (Exception ex) {
// it may that the successful response has no response body
}
}
ResponseBuilder rb = currentResponseBuilder.clone();
state.setResponseBuilder(currentResponseBuilder);
return rb;
}