Search in sources :

Example 1 with DecodeException

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();
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) JAXBException(javax.xml.bind.JAXBException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DecodeException(feign.codec.DecodeException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 2 with DecodeException

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;
}
Also used : Context(io.dropwizard.metrics5.Timer.Context) RequestTemplate(feign.RequestTemplate) IOException(java.io.IOException) DecodeException(feign.codec.DecodeException) IOException(java.io.IOException) FeignException(feign.FeignException)

Example 3 with DecodeException

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());
}
Also used : Response(feign.Response) Decoder(feign.codec.Decoder) IOException(java.io.IOException) Reader(java.io.Reader) List(java.util.List) JSON(com.fasterxml.jackson.jr.ob.JSON) ParameterizedType(java.lang.reflect.ParameterizedType) JSONObjectException(com.fasterxml.jackson.jr.ob.JSONObjectException) JacksonJrExtension(com.fasterxml.jackson.jr.ob.JacksonJrExtension) Type(java.lang.reflect.Type) Map(java.util.Map) DecodeException(feign.codec.DecodeException) BufferedReader(java.io.BufferedReader) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) DecodeException(feign.codec.DecodeException)

Example 4 with DecodeException

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);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) SOAPException(javax.xml.soap.SOAPException) JAXBException(javax.xml.bind.JAXBException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Unmarshaller(javax.xml.bind.Unmarshaller) SOAPMessage(javax.xml.soap.SOAPMessage) DecodeException(feign.codec.DecodeException)

Example 5 with DecodeException

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);
    }
}
Also used : BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) JSONException(org.json.JSONException) IOException(java.io.IOException) DecodeException(feign.codec.DecodeException)

Aggregations

DecodeException (feign.codec.DecodeException)6 IOException (java.io.IOException)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 BufferedReader (java.io.BufferedReader)2 Reader (java.io.Reader)2 JAXBException (javax.xml.bind.JAXBException)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 JSON (com.fasterxml.jackson.jr.ob.JSON)1 JSONObjectException (com.fasterxml.jackson.jr.ob.JSONObjectException)1 JacksonJrExtension (com.fasterxml.jackson.jr.ob.JacksonJrExtension)1 FeignException (feign.FeignException)1 RequestTemplate (feign.RequestTemplate)1 Response (feign.Response)1 Decoder (feign.codec.Decoder)1 Context (io.dropwizard.metrics5.Timer.Context)1 InputStream (java.io.InputStream)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 Map (java.util.Map)1