下面列出了org.apache.zookeeper.client.ConnectStringParser#org.apache.nifi.components.ValidationResult 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Collection<ValidationResult> validate(final ValidationContext validationContext,
final CredentialsStrategy primaryStrategy) {
boolean thisIsSelectedStrategy = this == primaryStrategy;
String requiredMessageFormat = "property %1$s must be set with %2$s";
String excludedMessageFormat = "property %1$s cannot be used with %2$s";
String failureFormat = thisIsSelectedStrategy ? requiredMessageFormat : excludedMessageFormat;
Collection<ValidationResult> validationFailureResults = null;
for (PropertyDescriptor requiredProperty : requiredProperties) {
boolean requiredPropertyIsSet = validationContext.getProperty(requiredProperty).isSet();
if (requiredPropertyIsSet != thisIsSelectedStrategy) {
String message = String.format(failureFormat, requiredProperty.getDisplayName(),
primaryStrategy.getName());
if (validationFailureResults == null) {
validationFailureResults = new ArrayList<>();
}
validationFailureResults.add(new ValidationResult.Builder()
.subject(requiredProperty.getDisplayName())
.valid(false)
.explanation(message).build());
}
}
return validationFailureResults;
}
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
try {
JSONUtils.INSTANCE.load(input, new TypeReference<Map<String, String>>() {
});
} catch (IOException e) {
return new ValidationResult.Builder()
.subject(subject)
.input(value)
.valid(false)
.explanation("Not a valid JSON map value: " + e.getMessage())
.build();
}
return new ValidationResult.Builder()
.valid(true)
.input(value)
.subject(subject)
.build();
}
/**
* Updates the value of the property with the given PropertyDescriptor to
* the specified value IF and ONLY IF the value is valid according to the
* descriptor's validator. Otherwise, the property value is not updated. In
* either case, the ValidationResult is returned, indicating whether or not
* the property is valid
*
* @param descriptor of property to modify
* @param value new value
* @return result
*/
public ValidationResult setProperty(final PropertyDescriptor descriptor, final String value) {
requireNonNull(descriptor);
requireNonNull(value, "Cannot set property to null value; if the intent is to remove the property, call removeProperty instead");
final PropertyDescriptor fullyPopulatedDescriptor = component.getPropertyDescriptor(descriptor.getName());
final ValidationResult result = fullyPopulatedDescriptor.validate(value, new MockValidationContext(this, stateManager, variableRegistry));
String oldValue = properties.put(fullyPopulatedDescriptor, value);
if (oldValue == null) {
oldValue = fullyPopulatedDescriptor.getDefaultValue();
}
if ((value == null && oldValue != null) || (value != null && !value.equals(oldValue))) {
component.onPropertyModified(fullyPopulatedDescriptor, oldValue, value);
}
return result;
}
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
final Collection<ValidationResult> result = new ArrayList<>();
final String sendingHost = validationContext.getProperty(SENDING_HOST).getValue();
final String sendingPort = validationContext.getProperty(SENDING_HOST_PORT).getValue();
if (StringUtils.isBlank(sendingHost) && StringUtils.isNotBlank(sendingPort)) {
result.add(
new ValidationResult.Builder()
.subject(SENDING_HOST.getName())
.valid(false)
.explanation("Must specify Sending Host when specifying Sending Host Port")
.build());
} else if (StringUtils.isBlank(sendingPort) && StringUtils.isNotBlank(sendingHost)) {
result.add(
new ValidationResult.Builder()
.subject(SENDING_HOST_PORT.getName())
.valid(false)
.explanation("Must specify Sending Host Port when specifying Sending Host")
.build());
}
return result;
}
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
final String value = context.newPropertyValue(input).evaluateAttributeExpressions().getValue();
try {
SiteToSiteRestApiClient.parseClusterUrls(value);
return new ValidationResult.Builder()
.input(input)
.subject(subject)
.valid(true)
.build();
} catch (IllegalArgumentException ex) {
return new ValidationResult.Builder()
.input(input)
.subject(subject)
.valid(false)
.explanation(ex.getLocalizedMessage())
.build();
}
}
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
// We need to check to see if the native libraries loaded properly
List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext));
if (wEvtApiError != null) {
validationResults.add(new ValidationResult.Builder().valid(false).subject("System Configuration")
.explanation("NiFi failed to load wevtapi on this system. This processor utilizes native Windows APIs and will only work on Windows. ("
+ wEvtApiError.getMessage() + ")").build());
}
if (kernel32Error != null) {
validationResults.add(new ValidationResult.Builder().valid(false).subject("System Configuration")
.explanation("NiFi failed to load kernel32 on this system. This processor utilizes native Windows APIs and will only work on Windows. ("
+ kernel32Error.getMessage() + ")").build());
}
return validationResults;
}
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
List<ValidationResult> results = new ArrayList<>();
String clientAuth = validationContext.getProperty(CLIENT_AUTH).getValue();
SSLContextService sslContextService = validationContext.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
if (sslContextService != null && !StringUtils.hasText(clientAuth)) {
results.add(new ValidationResult.Builder()
.subject(CLIENT_AUTH.getDisplayName())
.explanation(CLIENT_AUTH.getDisplayName() + " must be provided when using " + SSL_CONTEXT_SERVICE.getDisplayName())
.valid(false)
.build());
} else if (sslContextService == null && StringUtils.hasText(clientAuth)) {
results.add(new ValidationResult.Builder()
.subject(SSL_CONTEXT_SERVICE.getDisplayName())
.explanation(SSL_CONTEXT_SERVICE.getDisplayName() + " must be provided when selecting " + CLIENT_AUTH.getDisplayName())
.valid(false)
.build());
}
return results;
}
@Override
protected Collection<ValidationResult> customValidate(ValidationContext context) {
final List<ValidationResult> problems = new ArrayList<>(super.customValidate(context));
final Long minAgeProp = context.getProperty(MIN_AGE).asTimePeriod(TimeUnit.MILLISECONDS);
final Long maxAgeProp = context.getProperty(MAX_AGE).asTimePeriod(TimeUnit.MILLISECONDS);
final long minimumAge = (minAgeProp == null) ? 0L : minAgeProp;
final long maximumAge = (maxAgeProp == null) ? Long.MAX_VALUE : maxAgeProp;
if (minimumAge > maximumAge) {
problems.add(new ValidationResult.Builder().valid(false).subject("GetHDFS Configuration")
.explanation(MIN_AGE.getName() + " cannot be greater than " + MAX_AGE.getName()).build());
}
try {
new Path(context.getProperty(DIRECTORY).evaluateAttributeExpressions().getValue());
} catch (Exception e) {
problems.add(new ValidationResult.Builder()
.valid(false)
.subject("Directory")
.explanation(e.getMessage())
.build());
}
return problems;
}
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
}
if (input == null) {
return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Time Period cannot be null").build();
}
if (TIME_DURATION_PATTERN.matcher(input.toLowerCase()).matches() || input.equals("-1")) {
return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
} else {
return new ValidationResult.Builder()
.subject(subject)
.input(input)
.valid(false)
.explanation("Must be of format <duration> <TimeUnit> where <duration> is a "
+ "non-negative integer and TimeUnit is a supported Time Unit, such "
+ "as: nanos, millis, secs, mins, hrs, days")
.build();
}
}
@Override
public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(value)) {
return new ValidationResult.Builder().subject(subject).input(value).explanation("Expression Language Present").valid(true).build();
}
String reason = null;
try {
final int intVal = Integer.parseInt(value);
if (intVal < 0) {
reason = "value is negative";
}
} catch (final NumberFormatException e) {
reason = "value is not a valid integer";
}
return new ValidationResult.Builder().subject(subject).input(value).explanation(reason).valid(reason == null).build();
}
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
final List<ValidationResult> results = new ArrayList<>();
if (validationContext.getProperty(TRANSLATE_CONTENT).asBoolean().equals(Boolean.FALSE)) {
boolean foundDynamic = false;
for (final PropertyDescriptor descriptor : validationContext.getProperties().keySet()) {
if (descriptor.isDynamic()) {
foundDynamic = true;
break;
}
}
if (!foundDynamic) {
results.add(new ValidationResult.Builder().subject("Text to translate").input("<none>").valid(false)
.explanation("Must either set 'Translate Content' to true or add at least one user-defined property").build());
}
}
return results;
}
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
}
if (input == null) {
return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Time Period cannot be null").build();
}
if (Pattern.compile(FormatUtils.TIME_DURATION_REGEX).matcher(input.toLowerCase()).matches()) {
return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
} else {
return new ValidationResult.Builder()
.subject(subject)
.input(input)
.valid(false)
.explanation("Must be of format <duration> <TimeUnit> where <duration> is a "
+ "non-negative integer and TimeUnit is a supported Time Unit, such "
+ "as: nanos, millis, secs, mins, hrs, days")
.build();
}
}
@Override
public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
if (value.length() < minimum || value.length() > maximum) {
return new ValidationResult.Builder()
.subject(subject)
.valid(false)
.input(value)
.explanation(String.format("String length invalid [min: %d, max: %d]", minimum, maximum))
.build();
} else {
return new ValidationResult.Builder()
.valid(true)
.input(value)
.subject(subject)
.build();
}
}
private static Validator createURLValidator() {
return new Validator() {
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
}
try {
final String evaluatedInput = context.newPropertyValue(input).evaluateAttributeExpressions().getValue();
new URL(evaluatedInput);
return new ValidationResult.Builder().subject(subject).input(input).explanation("Valid URL").valid(true).build();
} catch (final Exception e) {
return new ValidationResult.Builder().subject(subject).input(input).explanation("Not a valid URL").valid(false).build();
}
}
};
}
public static Validator createListValidator(boolean trimEntries, boolean excludeEmptyEntries, Validator validator) {
return (subject, input, context) -> {
if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
}
try {
if (input == null) {
return new ValidationResult.Builder().subject(subject).input(null).explanation("List must have at least one non-empty element").valid(false).build();
}
final String[] list = input.split(",");
for (String item : list) {
String itemToValidate = trimEntries ? item.trim() : item;
if (!isEmpty(itemToValidate) || !excludeEmptyEntries) {
ValidationResult result = validator.validate(subject, itemToValidate, context);
if (!result.isValid()) {
return result;
}
}
}
return new ValidationResult.Builder().subject(subject).input(input).explanation("Valid List").valid(true).build();
} catch (final Exception e) {
return new ValidationResult.Builder().subject(subject).input(input).explanation("Not a valid list").valid(false).build();
}
};
}
@Override
public Collection<ValidationResult> validate(ValidationContext validationContext) {
final List<ValidationResult> validationResults = new ArrayList<>();
consumeConfigurations(validationContext.getAllProperties(),
(namespacePatterns, patterns) -> {},
(entry, e) -> {
final ValidationResult result = new ValidationResult.Builder()
.subject(entry.getKey())
.input(entry.getValue())
.explanation(e.getMessage())
.valid(false)
.build();
validationResults.add(result);
});
return validationResults;
}
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
List<ValidationResult> validationResults = new ArrayList<>();
boolean textSpecified = validationContext.getProperty(TEXT).isSet();
boolean attachmentSpecified = validationContext.getProperties().keySet()
.stream()
.anyMatch(PropertyDescriptor::isDynamic);
boolean uploadFileYes = validationContext.getProperty(UPLOAD_FLOWFILE).asBoolean();
if (!textSpecified && !attachmentSpecified && !uploadFileYes) {
validationResults.add(new ValidationResult.Builder()
.subject(TEXT.getDisplayName())
.valid(false)
.explanation("it is required if no attachment has been specified, nor 'Upload FlowFile' has been set to 'Yes'.")
.build());
}
return validationResults;
}
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
ArrayList<ValidationResult> problems = new ArrayList<>();
boolean fileName = validationContext.getProperty(FILE_NAME).isSet();
boolean query = validationContext.getProperty(QUERY).isSet();
if (fileName && query) {
problems.add(new ValidationResult.Builder()
.valid(false)
.explanation("File name and Query cannot be set at the same time.")
.build()
);
} else if (!fileName && !query) {
problems.add(new ValidationResult.Builder()
.valid(false)
.explanation("File name or Query must be set, but not both at the same time.")
.build()
);
}
return problems;
}
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
final List<ValidationResult> results = new ArrayList<>(RedisUtils.validate(validationContext));
final RedisType redisType = RedisType.fromDisplayName(validationContext.getProperty(RedisUtils.REDIS_MODE).getValue());
if (redisType != null && redisType == RedisType.CLUSTER) {
results.add(new ValidationResult.Builder()
.subject(RedisUtils.REDIS_MODE.getDisplayName())
.valid(false)
.explanation(RedisUtils.REDIS_MODE.getDisplayName()
+ " is configured in clustered mode, and this service requires a non-clustered Redis")
.build());
}
return results;
}
@Override
public Collection<ValidationResult> customValidate(ValidationContext context) {
final Collection<ValidationResult> results = super.customValidate(context);
int newSize = context.getProperty(PROP_MAX_QUEUE_SIZE).asInteger();
if (mqttQueue == null) {
mqttQueue = new LinkedBlockingQueue<>(context.getProperty(PROP_MAX_QUEUE_SIZE).asInteger());
}
int msgPending = mqttQueue.size();
if (msgPending > newSize) {
results.add(new ValidationResult.Builder()
.valid(false)
.subject("ConsumeMQTT Configuration")
.explanation(String.format("%s (%d) is smaller than the number of messages pending (%d).",
PROP_MAX_QUEUE_SIZE.getDisplayName(), newSize, msgPending))
.build());
}
return results;
}
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext context) {
final Collection<ValidationResult> results = new ArrayList<>();
if (context.getProperty(URL).evaluateAttributeExpressions().getValue().startsWith("https") && context.getProperty(SSL_CONTEXT_SERVICE).getValue() == null) {
results.add(new ValidationResult.Builder()
.explanation("URL is set to HTTPS protocol but no SSLContext has been specified")
.valid(false)
.subject("SSL Context")
.build());
}
if (context.getProperty(PROXY_HOST).isSet() && !context.getProperty(PROXY_PORT).isSet()) {
results.add(new ValidationResult.Builder()
.explanation("Proxy Host was set but no Proxy Port was specified")
.valid(false)
.subject("Proxy server configuration")
.build());
}
return results;
}
/**
* Validates the current properties, returning ValidationResults for any
* invalid properties. All processor defined properties will be validated.
* If they are not included in the in the purposed configuration, the
* default value will be used.
*
* @return Collection of validation result objects for any invalid findings
* only. If the collection is empty then the processor is valid. Guaranteed
* non-null
*/
public Collection<ValidationResult> validate() {
final List<ValidationResult> results = new ArrayList<>();
final ValidationContext validationContext = new MockValidationContext(this, stateManager, variableRegistry);
final Collection<ValidationResult> componentResults = component.validate(validationContext);
results.addAll(componentResults);
final Collection<ValidationResult> serviceResults = validateReferencedControllerServices(validationContext);
results.addAll(serviceResults);
// verify all controller services are enabled
for (Map.Entry<String, ControllerServiceConfiguration> service : getControllerServices().entrySet()) {
if (!service.getValue().isEnabled()) {
results.add(new ValidationResult.Builder()
.explanation("Controller service " + service.getKey() + " for " + this.getName() + " is not enabled")
.valid(false)
.build());
}
}
return results;
}
/**
* Reloads the script located at the given path
*
* @param scriptPath the path to the script file to be loaded
* @return true if the script was loaded successfully; false otherwise
*/
private boolean reloadScriptFile(final String scriptPath) {
final Collection<ValidationResult> results = new HashSet<>();
try (final FileInputStream scriptStream = new FileInputStream(scriptPath)) {
return reloadScript(IOUtils.toString(scriptStream, Charset.defaultCharset()));
} catch (final Exception e) {
final ComponentLog logger = getLogger();
final String message = "Unable to load script: " + e;
logger.error(message, e);
results.add(new ValidationResult.Builder()
.subject("ScriptValidation")
.valid(false)
.explanation("Unable to load script due to " + e)
.input(scriptPath)
.build());
}
// store the updated validation results
validationResults.set(results);
// return whether there was any issues loading the configured script
return results.isEmpty();
}
@Test
public void testOutputDirectoryValidator() {
MoveHDFS proc = new TestableMoveHDFS(kerberosProperties);
TestRunner runner = TestRunners.newTestRunner(proc);
Collection<ValidationResult> results;
ProcessContext pc;
results = new HashSet<>();
runner.setProperty(MoveHDFS.INPUT_DIRECTORY_OR_FILE, "/source");
runner.enqueue(new byte[0]);
pc = runner.getProcessContext();
if (pc instanceof MockProcessContext) {
results = ((MockProcessContext) pc).validate();
}
Assert.assertEquals(1, results.size());
for (ValidationResult vr : results) {
assertTrue(vr.toString().contains("Output Directory is required"));
}
}
static Validator createPositiveShortValidator() {
return new Validator() {
@Override
public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
String reason = null;
try {
final short shortVal = Short.parseShort(value);
if (shortVal <= 0) {
reason = "short integer must be greater than zero";
}
} catch (final NumberFormatException e) {
reason = "[" + value + "] is not a valid short integer";
}
return new ValidationResult.Builder().subject(subject).input(value).explanation(reason).valid(reason == null)
.build();
}
};
}
public Collection<ValidationResult> validate(String configFiles, String principal, String keyTab, String password,
AtomicReference<ValidationResources> validationResourceHolder, ComponentLog log) {
final List<ValidationResult> problems = new ArrayList<>();
ValidationResources resources = validationResourceHolder.get();
// if no resources in the holder, or if the holder has different resources loaded,
// then load the Configuration and set the new resources in the holder
if (resources == null || !configFiles.equals(resources.getConfigResources())) {
log.debug("Reloading validation resources");
resources = new ValidationResources(configFiles, getConfigurationFromFiles(configFiles));
validationResourceHolder.set(resources);
}
final Configuration hiveConfig = resources.getConfiguration();
problems.addAll(KerberosProperties.validatePrincipalWithKeytabOrPassword(this.getClass().getSimpleName(), hiveConfig, principal, keyTab, password, log));
return problems;
}
@Override
protected final Collection<ValidationResult> customValidate(final ValidationContext context) {
final List<ValidationResult> problems = new ArrayList<>(super.customValidate(context));
final long minBytes = context.getProperty(MIN_SIZE).asDataSize(DataUnit.B).longValue();
final Double maxBytes = context.getProperty(MAX_SIZE).asDataSize(DataUnit.B);
if (maxBytes != null && maxBytes.longValue() < minBytes) {
problems.add(
new ValidationResult.Builder()
.subject(MIN_SIZE.getName())
.input(context.getProperty(MIN_SIZE).getValue())
.valid(false)
.explanation("Min Size must be less than or equal to Max Size")
.build()
);
}
final Long min = context.getProperty(MIN_ENTRIES).asLong();
final Long max = context.getProperty(MAX_ENTRIES).asLong();
if (min != null && max != null) {
if (min > max) {
problems.add(
new ValidationResult.Builder().subject(MIN_ENTRIES.getName())
.input(context.getProperty(MIN_ENTRIES).getValue())
.valid(false)
.explanation("Min Entries must be less than or equal to Max Entries")
.build()
);
}
}
Collection<ValidationResult> otherProblems = this.additionalCustomValidation(context);
if (otherProblems != null) {
problems.addAll(otherProblems);
}
return problems;
}
@Override
public Collection<ValidationResult> validate(final ValidationContext context) {
return Collections.singleton(new ValidationResult.Builder()
.input("Any Property")
.subject("Missing Reporting Task")
.valid(false)
.explanation("Reporting Task is of type " + canonicalClassName + ", but this is not a valid Reporting Task type")
.build());
}
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
try {
InetAddress.getByName(input);
return new ValidationResult.Builder().subject(subject).valid(true).input(input).build();
} catch (final UnknownHostException e) {
return new ValidationResult.Builder().subject(subject).valid(false).input(input).explanation("Unknown host: " + e).build();
}
}
@Test
public void inputWithMultipleInvalidEventTypeShouldProperlyDisplayEventsInExplanation() throws Exception {
String subject = "subject";
String input = "append, CREATE, invalidValue1, rename, metadata, unlink, invalidValue2";
ValidationResult result = eventTypeValidator.validate(subject, input, context);
assertEquals("subject", result.getSubject());
assertEquals("append, CREATE, invalidValue1, rename, metadata, unlink, invalidValue2", result.getInput());
assertEquals("The following are not valid event types: [invalidValue1, invalidValue2]", result.getExplanation());
assertFalse(result.isValid());
}