Search in sources :

Example 1 with InvalidJSONException

use of org.orcid.core.exception.InvalidJSONException in project ORCID-Source by ORCID.

the class JSONInputValidator method validate2_1APIJSONInput.

public void validate2_1APIJSONInput(Object obj) {
    Class<?> clazz = obj.getClass();
    JAXBSource source = null;
    if (!SCHEMA_LOCATIONS_2_1_API.containsKey(clazz)) {
        LOGGER.error("Cannot validate " + clazz.getName());
        return;
    }
    try {
        source = new JAXBSource(CONTEXTS_2_1_API.get(clazz), obj);
        VALIDATORS_2_1_API.get(clazz).validate(source);
    } catch (SAXException e) {
        Map<String, String> params = new HashMap<>();
        params.put("error", e.getCause().getCause().getMessage());
        throw new InvalidJSONException(params);
    } catch (Exception e) {
        throw new ApplicationException(e);
    }
}
Also used : InvalidJSONException(org.orcid.core.exception.InvalidJSONException) ApplicationException(org.orcid.core.exception.ApplicationException) HashMap(java.util.HashMap) Map(java.util.Map) JAXBSource(javax.xml.bind.util.JAXBSource) ApplicationException(org.orcid.core.exception.ApplicationException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) InvalidJSONException(org.orcid.core.exception.InvalidJSONException) SAXException(org.xml.sax.SAXException)

Example 2 with InvalidJSONException

use of org.orcid.core.exception.InvalidJSONException in project ORCID-Source by ORCID.

the class JSONInputValidator method validateJSONInput.

public void validateJSONInput(Object obj) {
    Class<?> clazz = obj.getClass();
    JAXBSource source = null;
    if (!canValidate(clazz)) {
        LOGGER.error("Cannot validate " + clazz.getName());
        return;
    }
    try {
        source = new JAXBSource(CONTEXTS.get(clazz), obj);
        VALIDATORS.get(clazz).validate(source);
    } catch (SAXException e) {
        Map<String, String> params = new HashMap<>();
        params.put("error", e.getCause().getCause().getMessage());
        throw new InvalidJSONException(params);
    } catch (Exception e) {
        throw new ApplicationException(e);
    }
}
Also used : InvalidJSONException(org.orcid.core.exception.InvalidJSONException) ApplicationException(org.orcid.core.exception.ApplicationException) HashMap(java.util.HashMap) Map(java.util.Map) JAXBSource(javax.xml.bind.util.JAXBSource) ApplicationException(org.orcid.core.exception.ApplicationException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) InvalidJSONException(org.orcid.core.exception.InvalidJSONException) SAXException(org.xml.sax.SAXException)

Example 3 with InvalidJSONException

use of org.orcid.core.exception.InvalidJSONException in project ORCID-Source by ORCID.

the class OrcidJacksonJaxbJsonProviderPretty method readFrom.

/**
 * This adds a validation step when converting JSON into ORCID models.
 */
@Override
public Object readFrom(Class<Object> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5) throws IOException {
    Object o = null;
    try {
        o = super.readFrom(arg0, arg1, arg2, arg3, arg4, arg5);
    } catch (JsonMappingException e) {
        Map<String, String> params = new HashMap<>();
        params.put("error", e.getMessage());
        throw new InvalidJSONException(params);
    }
    if (jsonInputValidator.canValidate(o.getClass())) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        String apiVersion = (String) requestAttributes.getAttribute(ApiVersionFilter.API_VERSION_REQUEST_ATTRIBUTE_NAME, RequestAttributes.SCOPE_REQUEST);
        if (apiVersion != null && apiVersion.equals("2.1")) {
            jsonInputValidator.validate2_1APIJSONInput(o);
        } else {
            jsonInputValidator.validateJSONInput(o);
        }
    }
    return o;
}
Also used : InvalidJSONException(org.orcid.core.exception.InvalidJSONException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RequestAttributes(org.springframework.web.context.request.RequestAttributes) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)3 Map (java.util.Map)3 InvalidJSONException (org.orcid.core.exception.InvalidJSONException)3 JAXBException (javax.xml.bind.JAXBException)2 JAXBSource (javax.xml.bind.util.JAXBSource)2 ApplicationException (org.orcid.core.exception.ApplicationException)2 SAXException (org.xml.sax.SAXException)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 RequestAttributes (org.springframework.web.context.request.RequestAttributes)1