Search in sources :

Example 1 with DataExtractor

use of org.alfresco.repo.audit.extractor.DataExtractor in project alfresco-repository by Alfresco.

the class AuditComponentImpl method extractData.

/**
 * Extracts data from a given map using data extractors from the given application.
 *
 * @param application           the application providing the data extractors
 * @param values                the data values from which to generate data
 * @return                      Returns a map of derived data keyed by full path
 *
 * @since 3.2
 */
private Map<String, Serializable> extractData(AuditApplication application, Map<String, Serializable> values) {
    Map<String, Serializable> newData = new HashMap<String, Serializable>(values.size());
    List<DataExtractorDefinition> extractors = application.getDataExtractors();
    for (DataExtractorDefinition extractorDef : extractors) {
        DataExtractor extractor = extractorDef.getDataExtractor();
        String triggerPath = extractorDef.getDataTrigger();
        String sourcePath = extractorDef.getDataSource();
        String targetPath = extractorDef.getDataTarget();
        // Check if it is triggered
        if (!values.containsKey(triggerPath)) {
            // It is not triggered
            continue;
        }
        // We observe the key, not the actual value
        if (!values.containsKey(sourcePath)) {
            // There is no data to extract
            continue;
        }
        Serializable value = values.get(sourcePath);
        // Check if the extraction is supported
        if (!extractor.isSupported(value)) {
            continue;
        }
        // Use the extractor to pull the value out
        final Serializable data;
        try {
            data = extractor.extractData(value);
        } catch (Throwable e) {
            throw new AlfrescoRuntimeException("Failed to extract audit data: \n" + "   Path:      " + sourcePath + "\n" + "   Raw value: " + value + "\n" + "   Extractor: " + extractor, e);
        }
        // Add it to the map
        newData.put(targetPath, data);
    }
    // Done
    if (logger.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        sb.append("\nExtracted audit data: \n" + "\tApplication:    " + application + "\n" + "\tValues:         " + "\n");
        for (Map.Entry<String, Serializable> entry : values.entrySet()) {
            sb.append("\t\t").append(entry).append("\n");
        }
        sb.append("\n\tNew Data: \n");
        for (Map.Entry<String, Serializable> entry : newData.entrySet()) {
            sb.append("\t\t").append(entry).append("\n");
        }
        logger.debug(sb.toString());
    }
    return newData;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) DataExtractor(org.alfresco.repo.audit.extractor.DataExtractor) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DataExtractorDefinition(org.alfresco.repo.audit.model.AuditApplication.DataExtractorDefinition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with DataExtractor

use of org.alfresco.repo.audit.extractor.DataExtractor in project alfresco-repository by Alfresco.

the class AuditApplication method buildAuditPaths.

/**
 * Recursive method to build generator and extractor mappings
 */
private void buildAuditPaths(AuditPath auditPath, String currentPath, Set<String> existingPaths, Map<String, DataGenerator> upperGeneratorsByPath) {
    // Clone the upper maps to prevent pollution
    upperGeneratorsByPath = new HashMap<String, DataGenerator>(upperGeneratorsByPath);
    // Append the current audit path to the current path
    if (currentPath == null) {
        currentPath = AuditApplication.buildPath(auditPath.getKey());
    } else {
        currentPath = AuditApplication.buildPath(currentPath, auditPath.getKey());
    }
    // Make sure we have not processed it before
    if (!existingPaths.add(currentPath)) {
        generateException(currentPath, "The audit path already exists.");
    }
    // Get the data extractors declared for this key
    for (RecordValue element : auditPath.getRecordValue()) {
        String key = element.getKey();
        String extractorPath = AuditApplication.buildPath(currentPath, key);
        if (!existingPaths.add(extractorPath)) {
            generateException(extractorPath, "The audit path already exists.");
        }
        String extractorName = element.getDataExtractor();
        DataExtractor extractor = dataExtractorsByName.get(extractorName);
        if (extractor == null) {
            generateException(extractorPath, "No data extractor exists for name: " + extractorName);
        }
        // The extractor may pull data from somewhere else
        String sourcePath = element.getDataSource();
        if (sourcePath == null) {
            sourcePath = currentPath;
        }
        // The extractor may be triggered by data from elsewhere
        String dataTrigger = element.getDataTrigger();
        if (dataTrigger == null) {
            dataTrigger = currentPath;
        }
        // Store the extractor definition
        DataExtractorDefinition extractorDef = new DataExtractorDefinition(dataTrigger, sourcePath, extractorPath, extractor);
        dataExtractors.add(extractorDef);
    }
    // Get the data generators declared for this key
    for (GenerateValue element : auditPath.getGenerateValue()) {
        String key = element.getKey();
        String generatorPath = AuditApplication.buildPath(currentPath, key);
        if (!existingPaths.add(generatorPath)) {
            generateException(generatorPath, "The audit path already exists.");
        }
        String generatorName = element.getDataGenerator();
        DataGenerator generator = dataGeneratorsByName.get(generatorName);
        if (generator == null) {
            generateException(generatorPath, "No data generator exists for name: " + generatorName);
        }
        // All generators that occur earlier in the path will also be applicable here
        upperGeneratorsByPath.put(generatorPath, generator);
    }
    // All the generators apply to the current path
    dataGenerators.put(currentPath, upperGeneratorsByPath);
    // Find all sub audit paths and recurse
    for (AuditPath element : auditPath.getAuditPath()) {
        buildAuditPaths(element, currentPath, existingPaths, upperGeneratorsByPath);
    }
}
Also used : GenerateValue(org.alfresco.repo.audit.model._3.GenerateValue) DataGenerator(org.alfresco.repo.audit.generator.DataGenerator) RecordValue(org.alfresco.repo.audit.model._3.RecordValue) DataExtractor(org.alfresco.repo.audit.extractor.DataExtractor) AuditPath(org.alfresco.repo.audit.model._3.AuditPath)

Aggregations

DataExtractor (org.alfresco.repo.audit.extractor.DataExtractor)2 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 DataGenerator (org.alfresco.repo.audit.generator.DataGenerator)1 DataExtractorDefinition (org.alfresco.repo.audit.model.AuditApplication.DataExtractorDefinition)1 AuditPath (org.alfresco.repo.audit.model._3.AuditPath)1 GenerateValue (org.alfresco.repo.audit.model._3.GenerateValue)1 RecordValue (org.alfresco.repo.audit.model._3.RecordValue)1