Search in sources :

Example 1 with NinjaException

use of com.evolveum.midpoint.ninja.impl.NinjaException in project midpoint by Evolveum.

the class SearchProducerWorker method run.

@Override
public void run() {
    Log log = context.getLog();
    try {
        GetOperationOptionsBuilder optionsBuilder = context.getSchemaService().getOperationOptionsBuilder();
        if (options.isRaw()) {
            optionsBuilder = optionsBuilder.raw();
        }
        optionsBuilder = NinjaUtils.addIncludeOptionsForExport(optionsBuilder, type.getClassDefinition());
        ResultHandler<?> handler = (object, parentResult) -> {
            try {
                queue.put(object.asObjectable());
            } catch (InterruptedException ex) {
                log.error("Couldn't queue object {}, reason: {}", ex, object, ex.getMessage());
            }
            return true;
        };
        RepositoryService repository = context.getRepository();
        repository.searchObjectsIterative(type.getClassDefinition(), query, handler, optionsBuilder.build(), true, operation.getResult());
    } catch (SchemaException ex) {
        log.error("Unexpected exception, reason: {}", ex, ex.getMessage());
    } catch (NinjaException ex) {
        log.error(ex.getMessage(), ex);
    } finally {
        markDone();
        if (isWorkersDone()) {
            if (!operation.isFinished()) {
                operation.producerFinish();
            }
        }
    }
}
Also used : ResultHandler(com.evolveum.midpoint.schema.ResultHandler) ExportOptions(com.evolveum.midpoint.ninja.opts.ExportOptions) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) OperationStatus(com.evolveum.midpoint.ninja.util.OperationStatus) NinjaUtils(com.evolveum.midpoint.ninja.util.NinjaUtils) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) BlockingQueue(java.util.concurrent.BlockingQueue) NinjaContext(com.evolveum.midpoint.ninja.impl.NinjaContext) GetOperationOptionsBuilder(com.evolveum.midpoint.schema.GetOperationOptionsBuilder) List(java.util.List) Log(com.evolveum.midpoint.ninja.util.Log) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Log(com.evolveum.midpoint.ninja.util.Log) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) GetOperationOptionsBuilder(com.evolveum.midpoint.schema.GetOperationOptionsBuilder) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService)

Example 2 with NinjaException

use of com.evolveum.midpoint.ninja.impl.NinjaException in project midpoint by Evolveum.

the class CountRepositoryAction method execute.

@Override
public void execute() throws Exception {
    RepositoryService repository = context.getRepository();
    FileReference fileReference = options.getFilter();
    if (fileReference != null && options.getFilter() == null) {
        throw new NinjaException("Type must be defined");
    }
    List<ObjectTypes> types = NinjaUtils.getTypes(options.getType());
    int total = 0;
    OperationResult result = new OperationResult(OPERATION_COUNT);
    for (ObjectTypes type : types) {
        ObjectQuery query = NinjaUtils.createObjectQuery(options.getFilter(), context, type.getClassDefinition());
        int count = repository.countObjects(type.getClassDefinition(), query, new ArrayList<>(), result);
        if (count == 0 && options.getType() == null) {
            continue;
        }
        log.info("{}:\t{}", type.name(), count);
        total += count;
    }
    log.info("===\nTotal:\t{}", total);
}
Also used : NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) FileReference(com.evolveum.midpoint.ninja.util.FileReference) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService)

Example 3 with NinjaException

use of com.evolveum.midpoint.ninja.impl.NinjaException in project midpoint by Evolveum.

the class ImportProducerWorker method processStream.

private void processStream(InputStream input) throws IOException {
    ApplicationContext appContext = context.getApplicationContext();
    PrismContext prismContext = appContext.getBean(PrismContext.class);
    MatchingRuleRegistry matchingRuleRegistry = appContext.getBean(MatchingRuleRegistry.class);
    EventHandler<T> handler = new EventHandler<>() {

        @Override
        public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
            currentOid = objectElement.getAttribute("oid");
            return EventResult.cont();
        }

        @Override
        public EventResult postMarshall(T object, Element objectElement, OperationResult objectResult) {
            try {
                if (filter != null) {
                    boolean match = ObjectQuery.match(object, filter, matchingRuleRegistry);
                    if (!match) {
                        operation.incrementSkipped();
                        return EventResult.skipObject("Object doesn't match filter");
                    }
                }
                if (!matchSelectedType(object.getClass())) {
                    operation.incrementSkipped();
                    return EventResult.skipObject("Type doesn't match");
                }
                queue.put(object);
            } catch (Exception ex) {
                throw new NinjaException(getErrorMessage() + ", reason: " + ex.getMessage(), ex);
            }
            currentOid = null;
            return stopAfterFound ? EventResult.skipObject() : EventResult.cont();
        }

        @Override
        public void handleGlobalError(OperationResult currentResult, Exception cause) {
            // This should not
            // Should we log error?
            operation.incrementError();
            String message = getErrorMessage();
            if (continueOnInputError) {
                if (context.isVerbose()) {
                    context.getLog().error(message, cause);
                } else {
                    context.getLog().error(message + ", reason: {}", cause.getMessage());
                }
            } else {
                // We need to throw runtime exception in order to stop validator, otherwise validator will continue
                // fill queue and this may result in deadlock
                operation.finish();
                throw new NinjaException(message + ", reason: " + cause.getMessage(), cause);
            }
        }
    };
    // FIXME: MID-5151: If validateSchema is false we are not validating unknown attributes on import
    LegacyValidator<?> validator = new LegacyValidator<>(prismContext, handler);
    validator.setValidateSchema(false);
    OperationResult result = operation.getResult();
    Charset charset = context.getCharset();
    Reader reader = new InputStreamReader(input, charset);
    validator.validate(new ReaderInputStream(reader, charset), result, result.getOperation());
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) Charset(java.nio.charset.Charset) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) ApplicationContext(org.springframework.context.ApplicationContext) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) LegacyValidator(com.evolveum.midpoint.common.validator.LegacyValidator) MatchingRuleRegistry(com.evolveum.midpoint.prism.match.MatchingRuleRegistry)

Example 4 with NinjaException

use of com.evolveum.midpoint.ninja.impl.NinjaException in project midpoint by Evolveum.

the class ExportAuditProducerWorker method run.

@Override
public void run() {
    Log log = context.getLog();
    try {
        GetOperationOptionsBuilder optionsBuilder = context.getSchemaService().getOperationOptionsBuilder();
        optionsBuilder = NinjaUtils.addIncludeOptionsForExport(optionsBuilder, AuditEventRecordType.class);
        AuditResultHandler handler = (object, parentResult) -> {
            try {
                queue.put(object);
            } catch (InterruptedException ex) {
                log.error("Couldn't queue object {}, reason: {}", ex, object, ex.getMessage());
            }
            return true;
        };
        AuditService auditService = context.getAuditService();
        auditService.searchObjectsIterative(query, handler, optionsBuilder.build(), operation.getResult());
    } catch (SchemaException ex) {
        log.error("Unexpected exception, reason: {}", ex, ex.getMessage());
    } catch (NinjaException ex) {
        log.error(ex.getMessage(), ex);
    } finally {
        markDone();
        if (isWorkersDone()) {
            if (!operation.isFinished()) {
                operation.producerFinish();
            }
        }
    }
}
Also used : AuditService(com.evolveum.midpoint.audit.api.AuditService) OperationStatus(com.evolveum.midpoint.ninja.util.OperationStatus) NinjaUtils(com.evolveum.midpoint.ninja.util.NinjaUtils) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) BlockingQueue(java.util.concurrent.BlockingQueue) NinjaContext(com.evolveum.midpoint.ninja.impl.NinjaContext) GetOperationOptionsBuilder(com.evolveum.midpoint.schema.GetOperationOptionsBuilder) AuditEventRecordType(com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType) AuditResultHandler(com.evolveum.midpoint.audit.api.AuditResultHandler) List(java.util.List) Log(com.evolveum.midpoint.ninja.util.Log) BaseWorker(com.evolveum.midpoint.ninja.action.worker.BaseWorker) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Log(com.evolveum.midpoint.ninja.util.Log) AuditEventRecordType(com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) AuditResultHandler(com.evolveum.midpoint.audit.api.AuditResultHandler) GetOperationOptionsBuilder(com.evolveum.midpoint.schema.GetOperationOptionsBuilder) AuditService(com.evolveum.midpoint.audit.api.AuditService)

Example 5 with NinjaException

use of com.evolveum.midpoint.ninja.impl.NinjaException in project midpoint by Evolveum.

the class NinjaUtils method createWriter.

public static Writer createWriter(File output, Charset charset, boolean zip, boolean overwrite) throws IOException {
    OutputStream os;
    if (output != null) {
        if (!overwrite && output.exists()) {
            throw new NinjaException("Export file '" + output.getPath() + "' already exists");
        }
        output.createNewFile();
        os = new FileOutputStream(output);
    } else {
        os = System.out;
    }
    if (zip) {
        ZipOutputStream zos = new ZipOutputStream(os);
        String entryName = output.getName().replaceAll("\\.", "-") + ".xml";
        ZipEntry entry = new ZipEntry(entryName);
        zos.putNextEntry(entry);
        os = zos;
    }
    return new OutputStreamWriter(os, charset);
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException)

Aggregations

NinjaException (com.evolveum.midpoint.ninja.impl.NinjaException)8 Log (com.evolveum.midpoint.ninja.util.Log)4 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)3 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)3 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)3 NinjaContext (com.evolveum.midpoint.ninja.impl.NinjaContext)2 NinjaUtils (com.evolveum.midpoint.ninja.util.NinjaUtils)2 OperationStatus (com.evolveum.midpoint.ninja.util.OperationStatus)2 RepositoryService (com.evolveum.midpoint.repo.api.RepositoryService)2 GetOperationOptionsBuilder (com.evolveum.midpoint.schema.GetOperationOptionsBuilder)2 ObjectTypes (com.evolveum.midpoint.schema.constants.ObjectTypes)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 List (java.util.List)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 ZipEntry (java.util.zip.ZipEntry)2 ZipInputStream (java.util.zip.ZipInputStream)2 AuditResultHandler (com.evolveum.midpoint.audit.api.AuditResultHandler)1 AuditService (com.evolveum.midpoint.audit.api.AuditService)1 EventHandler (com.evolveum.midpoint.common.validator.EventHandler)1 LegacyValidator (com.evolveum.midpoint.common.validator.LegacyValidator)1