use of com.evolveum.midpoint.common.validator.LegacyValidator in project midpoint by Evolveum.
the class TestResources method test767ModifyConfigurationDiffExpressionRawValidatorParse.
/**
* This is what GUI "Repository objects" page really does with XML.
*/
@Test
public void test767ModifyConfigurationDiffExpressionRawValidatorParse() throws Exception {
modifyConfigurationDiffExpressionRaw(xml -> {
final Holder<PrismObject<ResourceType>> objectHolder = new Holder<>();
EventHandler<ResourceType> handler = new EventHandler<>() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
return EventResult.cont();
}
@Override
public EventResult postMarshall(ResourceType object, Element objectElement, OperationResult objectResult) {
objectHolder.setValue(object.asPrismObject());
return EventResult.cont();
}
@Override
public void handleGlobalError(OperationResult currentResult) {
}
};
LegacyValidator<ResourceType> validator = new LegacyValidator<>(prismContext, handler);
validator.setVerbose(true);
validator.setValidateSchema(false);
OperationResult result = createOperationResult("validator");
validator.validateObject(xml, result);
return objectHolder.getValue();
});
}
use of com.evolveum.midpoint.common.validator.LegacyValidator 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());
}
use of com.evolveum.midpoint.common.validator.LegacyValidator in project midpoint by Evolveum.
the class BasicValidatorTest method testStopOnErrors.
/**
* Same data as schemaViolation test, but this will set s lower threshold to stop after just two errors.
*/
@Test
public void testStopOnErrors() throws Exception {
OperationResult result = createOperationResult();
LegacyValidator<?> validator = new LegacyValidator<>(PrismTestUtil.getPrismContext());
validator.setVerbose(false);
validator.setStopAfterErrors(2);
validateFile("three-users-schema-violation.xml", validator, result);
System.out.println(result.debugDump());
assertFalse(result.isSuccess());
assertEquals(2, result.getSubresults().size());
}
use of com.evolveum.midpoint.common.validator.LegacyValidator in project midpoint by Evolveum.
the class ObjectImporter method importObjectsInternal.
private void importObjectsInternal(InputStream input, String language, ImportOptionsType options, Task task, OperationResult parentResult) {
if (options != null) {
if (isTrue(options.isSummarizeErrors())) {
parentResult.setSummarizeErrors(true);
}
if (isTrue(options.isSummarizeSucceses())) {
parentResult.setSummarizeSuccesses(true);
}
}
int stopAfterErrors = options != null && options.getStopAfterErrors() != null ? options.getStopAfterErrors() : 0;
if (!PrismContext.LANG_XML.equals(language)) {
AtomicInteger index = new AtomicInteger(0);
AtomicInteger errors = new AtomicInteger(0);
AtomicInteger successes = new AtomicInteger(0);
PrismParser.ObjectHandler handler = new PrismParser.ObjectHandler() {
@Override
public boolean handleData(PrismObject<?> object) {
OperationResult objectResult = parentResult.createSubresult(OperationConstants.IMPORT_OBJECT);
objectResult.addContext("objectNumber", index.incrementAndGet());
importParsedObject(object, objectResult, options, task);
objectResult.computeStatusIfUnknown();
objectResult.cleanupResult();
parentResult.summarize();
if (objectResult.isAcceptable()) {
successes.incrementAndGet();
} else {
errors.incrementAndGet();
}
return stopAfterErrors == 0 || errors.get() < stopAfterErrors;
}
@Override
public boolean handleError(Throwable t) {
OperationResult objectResult = parentResult.createSubresult(OperationConstants.IMPORT_OBJECT);
objectResult.addContext("objectNumber", index.incrementAndGet());
objectResult.recordFatalError("Couldn't parse object", t);
parentResult.summarize();
errors.incrementAndGet();
return stopAfterErrors == 0 || errors.get() < stopAfterErrors;
}
};
PrismParser parser = prismContext.parserFor(input).language(language);
if (options != null && options.isCompatMode() != null && options.isCompatMode()) {
parser = parser.compat();
}
try {
parser.parseObjectsIteratively(handler);
} catch (SchemaException | IOException e) {
parentResult.recordFatalError("Couldn't parse objects to be imported: " + e.getMessage(), e);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't parse objects to be imported", e);
return;
}
parentResult.computeStatus(errors.get() + " errors, " + successes.get() + " passed");
} else {
EventHandler<Objectable> handler = new EventHandler<>() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
return EventResult.cont();
}
@Override
public EventResult postMarshall(Objectable object, Element objectElement, OperationResult objectResult) {
// noinspection unchecked
return importParsedObject(object.asPrismObject(), objectResult, options, task);
}
@Override
public void handleGlobalError(OperationResult currentResult) {
// No reaction
}
};
LegacyValidator<?> validator = new LegacyValidator<>(prismContext, handler);
validator.setVerbose(true);
if (options != null) {
validator.setValidateSchema(isTrue(options.isValidateStaticSchema()));
if (options.getModelExecutionOptions() != null && isFalse(options.getModelExecutionOptions().isRaw())) {
// model will take care of this
validator.setValidateName(false);
}
}
validator.setStopAfterErrors(stopAfterErrors);
if (options != null && options.isCompatMode() != null && options.isCompatMode()) {
validator.setCompatMode(true);
}
validator.validate(input, parentResult, OperationConstants.IMPORT_OBJECT);
}
}
Aggregations