use of feign.codec.DecodeException in project feign by OpenFeign.
the class JAXBDecoder method decode.
@Override
public Object decode(Response response, Type type) throws IOException {
if (response.status() == 204)
return Util.emptyValueOf(type);
if (response.body() == null)
return null;
while (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
type = ptype.getRawType();
}
if (!(type instanceof Class)) {
throw new UnsupportedOperationException("JAXB only supports decoding raw types. Found " + type);
}
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
/* Explicitly control sax configuration to prevent XXE attacks */
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
saxParserFactory.setNamespaceAware(namespaceAware);
return jaxbContextFactory.createUnmarshaller((Class<?>) type).unmarshal(new SAXSource(saxParserFactory.newSAXParser().getXMLReader(), new InputSource(response.body().asInputStream())));
} catch (JAXBException | ParserConfigurationException | SAXException e) {
throw new DecodeException(response.status(), e.toString(), response.request(), e);
} finally {
if (response.body() != null) {
response.body().close();
}
}
}
use of feign.codec.DecodeException in project feign by OpenFeign.
the class MeteredDecoder method decode.
@Override
public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
final RequestTemplate template = response.request().requestTemplate();
final MeteredBody body = response.body() == null ? null : new MeteredBody(response.body());
response = response.toBuilder().body(body).build();
final Object decoded;
try (final Context classTimer = metricRegistry.timer(metricName.metricName(template.methodMetadata(), template.feignTarget()).tagged("uri", template.methodMetadata().template().path()), metricSuppliers.timers()).time()) {
decoded = decoder.decode(response, type);
} catch (IOException | RuntimeException e) {
metricRegistry.meter(metricName.metricName(template.methodMetadata(), template.feignTarget(), "error_count").tagged("exception_name", e.getClass().getSimpleName()).tagged("uri", template.methodMetadata().template().path()), metricSuppliers.meters()).mark();
throw e;
} catch (Exception e) {
metricRegistry.meter(metricName.metricName(template.methodMetadata(), template.feignTarget(), "error_count").tagged("exception_name", e.getClass().getSimpleName()).tagged("uri", template.methodMetadata().template().path()), metricSuppliers.meters()).mark();
throw new IOException(e);
}
if (body != null) {
metricRegistry.histogram(metricName.metricName(template.methodMetadata(), template.feignTarget(), "response_size").tagged("uri", template.methodMetadata().template().path()), metricSuppliers.histograms()).update(body.count());
}
return decoded;
}
use of feign.codec.DecodeException in project feign by OpenFeign.
the class JacksonJrDecoder method findTransformer.
private static Transformer findTransformer(Response response, Type type) {
if (type instanceof Class) {
return (mapper, reader) -> mapper.beanFrom((Class<?>) type, reader);
}
if (type instanceof ParameterizedType) {
Type rawType = ((ParameterizedType) type).getRawType();
Type[] parameterType = ((ParameterizedType) type).getActualTypeArguments();
if (rawType.equals(List.class)) {
return (mapper, reader) -> mapper.listOfFrom((Class<?>) parameterType[0], reader);
}
if (rawType.equals(Map.class)) {
return (mapper, reader) -> mapper.mapOfFrom((Class<?>) parameterType[1], reader);
}
}
throw new DecodeException(500, "Cannot decode type: " + type.getTypeName(), response.request());
}
use of feign.codec.DecodeException in project feign by OpenFeign.
the class SOAPDecoder method decode.
@Override
public Object decode(Response response, Type type) throws IOException {
if (response.status() == 404)
return Util.emptyValueOf(type);
if (response.body() == null)
return null;
while (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
type = ptype.getRawType();
}
if (!(type instanceof Class)) {
throw new UnsupportedOperationException("SOAP only supports decoding raw types. Found " + type);
}
try {
SOAPMessage message = MessageFactory.newInstance(soapProtocol).createMessage(null, response.body().asInputStream());
if (message.getSOAPBody() != null) {
if (message.getSOAPBody().hasFault()) {
throw new SOAPFaultException(message.getSOAPBody().getFault());
}
Unmarshaller unmarshaller = jaxbContextFactory.createUnmarshaller((Class<?>) type);
if (this.useFirstChild) {
return unmarshaller.unmarshal(message.getSOAPBody().getFirstChild());
} else {
return unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
}
}
} catch (SOAPException | JAXBException e) {
throw new DecodeException(response.status(), e.toString(), response.request(), e);
} finally {
if (response.body() != null) {
response.body().close();
}
}
return Util.emptyValueOf(type);
}
use of feign.codec.DecodeException in project feign by OpenFeign.
the class JsonDecoder method decode.
@Override
public Object decode(Response response, Type type) throws IOException, DecodeException {
if (response.body() == null)
return null;
try (Reader reader = response.body().asReader(response.charset())) {
Reader bodyReader = (reader.markSupported()) ? reader : new BufferedReader(reader);
bodyReader.mark(1);
if (bodyReader.read() == -1) {
// Empty body
return null;
}
bodyReader.reset();
return decodeJSON(response, type, bodyReader);
} catch (JSONException jsonException) {
if (jsonException.getCause() != null && jsonException.getCause() instanceof IOException) {
throw (IOException) jsonException.getCause();
}
throw new DecodeException(response.status(), jsonException.getMessage(), response.request(), jsonException);
}
}
Aggregations