int retcode = response.getHttpStatusCode();
if (retcode == HttpResponse.SC_INTERNAL_SERVER_ERROR) {
// Convert external "internal error" to gateway error:
retcode = HttpResponse.SC_BAD_GATEWAY;
}
throw new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT,
"Unable to retrieve template library xml. HTTP error " +
response.getHttpStatusCode(), retcode);
}
String content = response.getResponseAsString();
try {
String key = null;
Element element = null;
if (!context.getIgnoreCache()) {
try {
key = HashUtil.rawChecksum(content.getBytes("UTF-8"));
element = parsedXmlCache.getElement(key);
} catch (UnsupportedEncodingException e) {
// this won't happen, but if it does, cache won't be used.
}
}
if (element == null) {
element = XmlUtil.parse(content);
if (key != null) {
parsedXmlCache.addElement(key, element);
}
}
return new XmlTemplateLibrary(uri, element, content);
} catch (XmlException e) {
throw new GadgetException(GadgetException.Code.MALFORMED_XML_DOCUMENT, e,
HttpResponse.SC_BAD_REQUEST);
}
}