Search in sources :

Example 6 with DecodeException

use of feign.codec.DecodeException in project feign by OpenFeign.

the class SAXDecoder method decode.

@Override
public Object decode(Response response, Type type) throws IOException, DecodeException {
    if (response.body() == null)
        return null;
    ContentHandlerWithResult.Factory<?> handlerFactory = handlerFactories.get(type);
    checkState(handlerFactory != null, "type %s not in configured handlers %s", type, handlerFactories.keySet());
    ContentHandlerWithResult<?> handler = handlerFactory.create();
    try {
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setFeature("http://xml.org/sax/features/namespaces", false);
        xmlReader.setFeature("http://xml.org/sax/features/validation", false);
        /* Explicitly control sax configuration to prevent XXE attacks */
        xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
        xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        xmlReader.setContentHandler(handler);
        InputStream inputStream = response.body().asInputStream();
        try {
            xmlReader.parse(new InputSource(inputStream));
        } finally {
            ensureClosed(inputStream);
        }
        return handler.result();
    } catch (SAXException e) {
        throw new DecodeException(response.status(), e.getMessage(), response.request(), e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) DecodeException(feign.codec.DecodeException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

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