} else if (response!=null && requestErrorSetter!=null) {
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectMapper errorMapper=new ObjectMapper();
RequestError errorResponse=errorMapper.readValue(bais, RequestError.class);
if (errorResponse!=null) {
try {
requestErrorSetter.invoke(response, errorResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
tryAndIdentifyError=false;
}
}
} else {
InputStream is = con.getErrorStream();
if (is == null) {
is = con.getInputStream();
}
baos = new ByteArrayOutputStream();
int i;
while ((i = (byte) is.read()) != -1) baos.write(i);
}
if (response!=null && responseCodeSetter!=null) {
try {
responseCodeSetter.invoke(response, responseCode);
} catch (Exception e) {
e.printStackTrace();
}
}
if (response!=null && contentTypeSetter!=null) {
try {
contentTypeSetter.invoke(response, contentType);
} catch (Exception e) {
e.printStackTrace();
}
}
if (tryAndIdentifyError && response!=null && baos!=null && requestErrorSetter!=null) {
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectMapper errorMapper=new ObjectMapper();
try {
RequestErrorWrapper errorResponse=errorMapper.readValue(bais, RequestErrorWrapper.class);
if (errorResponse!=null && errorResponse.getRequestError()!=null) {
requestErrorSetter.invoke(response, errorResponse.getRequestError());
}
} catch (Exception e) {
logger.warn("Exception "+e.getMessage()+" trying to identify error response");
}