Search in sources :

Example 6 with Validator

use of com.evolveum.midpoint.common.validator.Validator in project midpoint by Evolveum.

the class ObjectImporter method importObjectsInternal.

private void importObjectsInternal(InputStream input, final ImportOptionsType options, final boolean raw, final Task task, final OperationResult parentResult) {
    EventHandler handler = new EventHandler() {

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

        @Override
        public <T extends Objectable> EventResult postMarshall(PrismObject<T> prismObjectObjectable, Element objectElement, OperationResult objectResult) {
            LOGGER.debug("Importing object {}", prismObjectObjectable);
            T objectable = prismObjectObjectable.asObjectable();
            if (!(objectable instanceof ObjectType)) {
                String message = "Cannot process type " + objectable.getClass() + " as it is not a subtype of " + ObjectType.class;
                objectResult.recordFatalError(message);
                LOGGER.error("Import of object {} failed: {}", new Object[] { prismObjectObjectable, message });
                return EventResult.skipObject(message);
            }
            PrismObject<? extends ObjectType> object = (PrismObject<? extends ObjectType>) prismObjectObjectable;
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("IMPORTING object:\n{}", object.debugDump());
            }
            object = migrator.migrate(object);
            Utils.resolveReferences(object, repository, (options == null || options.isReferentialIntegrity() == null) ? false : options.isReferentialIntegrity(), false, EvaluationTimeType.IMPORT, false, prismContext, objectResult);
            objectResult.computeStatus();
            if (!objectResult.isAcceptable()) {
                return EventResult.skipObject(objectResult.getMessage());
            }
            generateIdentifiers(object, repository, objectResult);
            objectResult.computeStatus();
            if (!objectResult.isAcceptable()) {
                return EventResult.skipObject(objectResult.getMessage());
            }
            if (options != null && BooleanUtils.isTrue(options.isValidateDynamicSchema())) {
                validateWithDynamicSchemas(object, objectElement, repository, objectResult);
            }
            objectResult.computeStatus();
            if (!objectResult.isAcceptable()) {
                return EventResult.skipObject(objectResult.getMessage());
            }
            if (options != null && BooleanUtils.isTrue(options.isEncryptProtectedValues())) {
                OperationResult opResult = objectResult.createMinorSubresult(ObjectImporter.class.getName() + ".encryptValues");
                try {
                    CryptoUtil.encryptValues(protector, object);
                    opResult.recordSuccess();
                } catch (EncryptionException e) {
                    opResult.recordFatalError(e);
                }
            }
            if (options == null || (options != null && !BooleanUtils.isTrue(options.isKeepMetadata()))) {
                MetadataType metaData = new MetadataType();
                String channel = SchemaConstants.CHANNEL_OBJECT_IMPORT_URI;
                metaData.setCreateChannel(channel);
                metaData.setCreateTimestamp(clock.currentTimeXMLGregorianCalendar());
                if (task.getOwner() != null) {
                    metaData.setCreatorRef(ObjectTypeUtil.createObjectRef(task.getOwner()));
                }
                object.asObjectable().setMetadata(metaData);
            }
            objectResult.computeStatus();
            if (!objectResult.isAcceptable()) {
                return EventResult.skipObject(objectResult.getMessage());
            }
            try {
                importObjectToRepository(object, options, raw, task, objectResult);
                LOGGER.info("Imported object {}", object);
            } catch (SchemaException e) {
                objectResult.recordFatalError("Schema violation: " + e.getMessage(), e);
                LOGGER.error("Import of object {} failed: Schema violation: {}", object, e.getMessage(), e);
            } catch (ObjectAlreadyExistsException e) {
                objectResult.recordFatalError("Object already exists: " + e.getMessage(), e);
                LOGGER.error("Import of object {} failed: Object already exists: {}", object, e.getMessage(), e);
                LOGGER.error("Object already exists", e);
            } catch (RuntimeException e) {
                objectResult.recordFatalError("Unexpected problem: " + e.getMessage(), e);
                LOGGER.error("Import of object {} failed: Unexpected problem: {}", object, e.getMessage(), e);
            } catch (ObjectNotFoundException e) {
                LOGGER.error("Import of object {} failed: Object referred from this object was not found: {}", object, e.getMessage(), e);
            } catch (ExpressionEvaluationException e) {
                LOGGER.error("Import of object {} failed: Expression evaluation error: {}", object, e.getMessage(), e);
            } catch (CommunicationException e) {
                LOGGER.error("Import of object {} failed: Communication error: {}", object, e.getMessage(), e);
            } catch (ConfigurationException e) {
                LOGGER.error("Import of object {} failed: Configuration error: {}", object, e.getMessage(), e);
            } catch (PolicyViolationException e) {
                LOGGER.error("Import of object {} failed: Policy violation: {}", object, e.getMessage(), e);
            } catch (SecurityViolationException e) {
                LOGGER.error("Import of object {} failed: Security violation: {}", object, e.getMessage(), e);
            }
            objectResult.recordSuccessIfUnknown();
            if (objectResult.isAcceptable()) {
                // Continue import
                return EventResult.cont();
            } else {
                return EventResult.skipObject(objectResult.getMessage());
            }
        }

        @Override
        public void handleGlobalError(OperationResult currentResult) {
        // No reaction
        }
    };
    Validator validator = new Validator(prismContext, handler);
    validator.setVerbose(true);
    if (options != null) {
        validator.setValidateSchema(BooleanUtils.isTrue(options.isValidateStaticSchema()));
        if (options.getStopAfterErrors() != null) {
            validator.setStopAfterErrors(options.getStopAfterErrors().longValue());
        }
        if (BooleanUtils.isTrue(options.isSummarizeErrors())) {
            parentResult.setSummarizeErrors(true);
        }
        if (BooleanUtils.isTrue(options.isSummarizeSucceses())) {
            parentResult.setSummarizeSuccesses(true);
        }
    }
    validator.validate(input, parentResult, OperationConstants.IMPORT_OBJECT);
}
Also used : Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) Validator(com.evolveum.midpoint.common.validator.Validator)

Aggregations

Validator (com.evolveum.midpoint.common.validator.Validator)6 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)5 EventHandler (com.evolveum.midpoint.common.validator.EventHandler)4 Element (org.w3c.dom.Element)4 Node (org.w3c.dom.Node)4 Objectable (com.evolveum.midpoint.prism.Objectable)3 PrismObject (com.evolveum.midpoint.prism.PrismObject)3 FileInputStream (java.io.FileInputStream)2 ResourceValidator (com.evolveum.midpoint.model.api.validator.ResourceValidator)1 PrismContext (com.evolveum.midpoint.prism.PrismContext)1 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 RepositoryService (com.evolveum.midpoint.repo.api.RepositoryService)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1