<commit_before>package org.ovirt.engine.api.restapi.resource.validation;

import org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider;
import org.jboss.resteasy.plugins.providers.jaxb.JAXBMarshalException;
import org.ovirt.engine.api.utils.InvalidValueException;

import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.helpers.DefaultValidationEventHandler;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

@Provider
@Consumes({ MediaType.APPLICATION_XML })
public class XmlMessageBodyReader extends AbstractJAXBProvider<Object> {

    /** Default event handler recognizes XML parsing as error and not as warning */
    private ValidationEventHandler errorhandler = new DefaultValidationEventHandler();

    @Override
    protected boolean isReadWritable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    /**
     * {@inheritDoc}
     * <p>
     * Standard JAXB unmarshaller with custom error handler- {@link #errorhandler}
     */
    @Override
    public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
                           MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {

        if (entityStream == null) {
            return null;
        }
        try {
            JAXBContext ctx = findJAXBContext(type, annotations, mediaType, true);
            Unmarshaller unmarshaller = ctx.createUnmarshaller();

            AbstractJAXBProvider.decorateUnmarshaller(type, annotations, mediaType, unmarshaller);

            unmarshaller.setEventHandler(errorhandler);
            Object parsedObj = unmarshaller.unmarshal(entityStream);
            if (parsedObj instanceof JAXBElement) {
                return ((JAXBElement) parsedObj).getValue();
            }
            return parsedObj;
        } catch (JAXBException e) {
            if (e.getLinkedException().getCause() instanceof InvalidValueException) {
                throw (InvalidValueException) e.getLinkedException().getCause();
            }
            throw new JAXBMarshalException(e);
        }
    }

}<commit_msg>restapi: No NPE when no linked exception<commit_after>package org.ovirt.engine.api.restapi.resource.validation;

import org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider;
import org.jboss.resteasy.plugins.providers.jaxb.JAXBMarshalException;
import org.ovirt.engine.api.utils.InvalidValueException;

import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.helpers.DefaultValidationEventHandler;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

@Provider
@Consumes({ MediaType.APPLICATION_XML })
public class XmlMessageBodyReader extends AbstractJAXBProvider<Object> {

    /** Default event handler recognizes XML parsing as error and not as warning */
    private ValidationEventHandler errorhandler = new DefaultValidationEventHandler();

    @Override
    protected boolean isReadWritable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    /**
     * {@inheritDoc}
     * <p>
     * Standard JAXB unmarshaller with custom error handler- {@link #errorhandler}
     */
    @Override
    public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
                           MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {

        if (entityStream == null) {
            return null;
        }
        try {
            JAXBContext ctx = findJAXBContext(type, annotations, mediaType, true);
            Unmarshaller unmarshaller = ctx.createUnmarshaller();

            AbstractJAXBProvider.decorateUnmarshaller(type, annotations, mediaType, unmarshaller);

            unmarshaller.setEventHandler(errorhandler);
            Object parsedObj = unmarshaller.unmarshal(entityStream);
            if (parsedObj instanceof JAXBElement) {
                return ((JAXBElement) parsedObj).getValue();
            }
            return parsedObj;
        }
        catch (JAXBException exception) {
            Throwable linked = exception.getLinkedException();
            if (linked != null) {
                Throwable cause = linked.getCause();
                if (cause instanceof InvalidValueException) {
                    throw (InvalidValueException) cause;
                }
            }
            throw new JAXBMarshalException(exception);
        }
    }

}
{'fraction_non_alphanumeric': 0.08945508647944951, 'fraction_numerical': 0.0, 'mean_word_length': 3.292098962490024, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7065792', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1436, 'n_words': 317}