Search in sources :

Example 21 with Audit

use of io.atlasmap.v2.Audit in project alfresco-repository by Alfresco.

the class AuditModelRegistryImpl method unmarshallModel.

/**
 * Unmarshalls the Audit model from a stream.
 */
private static Audit unmarshallModel(InputStream is, final String source) {
    final Schema schema;
    final JAXBContext jaxbCtx;
    final Unmarshaller jaxbUnmarshaller;
    try {
        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schema = sf.newSchema(ResourceUtils.getURL(AUDIT_SCHEMA_LOCATION));
        jaxbCtx = JAXBContext.newInstance("org.alfresco.repo.audit.model._3");
        jaxbUnmarshaller = jaxbCtx.createUnmarshaller();
        jaxbUnmarshaller.setSchema(schema);
        jaxbUnmarshaller.setEventHandler(new ValidationEventHandler() {

            public boolean handleEvent(ValidationEvent ve) {
                if (ve.getSeverity() == ValidationEvent.FATAL_ERROR || ve.getSeverity() == ValidationEvent.ERROR) {
                    ValidationEventLocator locator = ve.getLocator();
                    logger.error("Invalid Audit XML: \n" + "   Source:   " + source + "\n" + "   Location: Line " + locator.getLineNumber() + " column " + locator.getColumnNumber() + "\n" + "   Error:    " + ve.getMessage());
                }
                return false;
            }
        });
    } catch (Throwable e) {
        throw new AlfrescoRuntimeException("Failed to load Alfresco Audit Schema from " + AUDIT_SCHEMA_LOCATION, e);
    }
    try {
        // Unmarshall with validation
        @SuppressWarnings("unchecked") JAXBElement<Audit> auditElement = (JAXBElement<Audit>) jaxbUnmarshaller.unmarshal(is);
        Audit audit = auditElement.getValue();
        // Done
        return audit;
    } catch (Throwable e) {
        // Dig out a SAXParseException, if there is one
        Throwable saxError = ExceptionStackUtil.getCause(e, SAXParseException.class);
        if (saxError != null) {
            e = saxError;
        }
        throw new AuditModelException("Failed to read Audit model XML: \n" + "   Source: " + source + "\n" + "   Error:  " + e.getMessage());
    } finally {
        try {
            is.close();
        } catch (IOException e) {
        }
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException) Audit(org.alfresco.repo.audit.model._3.Audit) SAXParseException(org.xml.sax.SAXParseException) ValidationEvent(javax.xml.bind.ValidationEvent) ValidationEventLocator(javax.xml.bind.ValidationEventLocator) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

Audit (io.atlasmap.v2.Audit)19 Test (org.junit.jupiter.api.Test)7 AtlasSession (io.atlasmap.api.AtlasSession)4 AtlasContext (io.atlasmap.api.AtlasContext)3 AtlasMappingBaseTest (io.atlasmap.itests.reference.AtlasMappingBaseTest)3 BaseContact (io.atlasmap.java.test.BaseContact)3 File (java.io.File)3 AtlasException (io.atlasmap.api.AtlasException)2 AtlasMapping (io.atlasmap.v2.AtlasMapping)2 BaseMapping (io.atlasmap.v2.BaseMapping)2 Mapping (io.atlasmap.v2.Mapping)2 TargetContact (io.atlasmap.java.test.TargetContact)1 AtlasJsonTestUnrootedMapper (io.atlasmap.json.test.AtlasJsonTestUnrootedMapper)1 AtlasModule (io.atlasmap.spi.AtlasModule)1 Action (io.atlasmap.v2.Action)1 Audits (io.atlasmap.v2.Audits)1 CustomMapping (io.atlasmap.v2.CustomMapping)1 Field (io.atlasmap.v2.Field)1 Mappings (io.atlasmap.v2.Mappings)1 ProcessMappingRequest (io.atlasmap.v2.ProcessMappingRequest)1