Ejemplo n.º 1
0
 // Evaluates the specified condition on the specified flowfile.
 private boolean evaluateCondition(
     final ProcessContext context, final Condition condition, final FlowFile flowfile) {
   try {
     // evaluate the expression for the given flow file
     return getPropertyValue(condition.getExpression(), context)
         .evaluateAttributeExpressions(flowfile)
         .asBoolean();
   } catch (final ProcessException pe) {
     throw new ProcessException(
         String.format("Unable to evaluate condition '%s': %s.", condition.getExpression(), pe),
         pe);
   }
 }
Ejemplo n.º 2
0
  @Override
  public Collection<SearchResult> search(final SearchContext context) {
    final String term = context.getSearchTerm();

    final Collection<SearchResult> results = new ArrayList<>();
    if (StringUtils.isBlank(context.getAnnotationData())) {
      return results;
    }

    try {
      // parse the annotation data
      final Criteria criteria = CriteriaSerDe.deserialize(context.getAnnotationData());

      // ensure there are some rules
      if (criteria.getRules() != null) {
        final FlowFilePolicy flowFilePolicy = criteria.getFlowFilePolicy();
        if (flowFilePolicy != null && StringUtils.containsIgnoreCase(flowFilePolicy.name(), term)) {
          results.add(
              new SearchResult.Builder()
                  .label("FlowFile policy")
                  .match(flowFilePolicy.name())
                  .build());
        }

        for (final Rule rule : criteria.getRules()) {
          if (StringUtils.containsIgnoreCase(rule.getName(), term)) {
            results.add(
                new SearchResult.Builder().label("Rule name").match(rule.getName()).build());
          }

          // ensure there are some conditions
          if (rule.getConditions() != null) {
            for (final Condition condition : rule.getConditions()) {
              if (StringUtils.containsIgnoreCase(condition.getExpression(), term)) {
                results.add(
                    new SearchResult.Builder()
                        .label(String.format("Condition in rule '%s'", rule.getName()))
                        .match(condition.getExpression())
                        .build());
              }
            }
          }

          // ensure there are some actions
          if (rule.getActions() != null) {
            for (final Action action : rule.getActions()) {
              if (StringUtils.containsIgnoreCase(action.getAttribute(), term)) {
                results.add(
                    new SearchResult.Builder()
                        .label(String.format("Action in rule '%s'", rule.getName()))
                        .match(action.getAttribute())
                        .build());
              }
              if (StringUtils.containsIgnoreCase(action.getValue(), term)) {
                results.add(
                    new SearchResult.Builder()
                        .label(String.format("Action in rule '%s'", rule.getName()))
                        .match(action.getValue())
                        .build());
              }
            }
          }
        }
      }

      return results;
    } catch (Exception e) {
      return results;
    }
  }
Ejemplo n.º 3
0
  @Override
  protected Collection<ValidationResult> customValidate(final ValidationContext context) {
    final List<ValidationResult> reasons = new ArrayList<>(super.customValidate(context));

    Criteria criteria = null;
    try {
      criteria = CriteriaSerDe.deserialize(context.getAnnotationData());
    } catch (IllegalArgumentException iae) {
      reasons.add(
          new ValidationResult.Builder()
              .valid(false)
              .explanation("Unable to deserialize the update criteria." + iae.getMessage())
              .build());
    }

    // if there is criteria, validate it
    if (criteria != null) {
      final List<Rule> rules = criteria.getRules();

      if (rules == null) {
        reasons.add(
            new ValidationResult.Builder()
                .valid(false)
                .explanation("Update criteria has been specified by no rules were found.")
                .build());
      } else {
        // validate the each rule
        for (final Rule rule : rules) {
          if (rule.getName() == null || rule.getName().trim().isEmpty()) {
            reasons.add(
                new ValidationResult.Builder()
                    .valid(false)
                    .explanation("A rule name was not specified.")
                    .build());
          }

          // validate each condition
          final Set<Condition> conditions = rule.getConditions();
          if (conditions == null) {
            reasons.add(
                new ValidationResult.Builder()
                    .valid(false)
                    .explanation(
                        String.format("No conditions for rule '%s' found.", rule.getName()))
                    .build());
          } else {
            for (final Condition condition : conditions) {
              if (condition.getExpression() == null) {
                reasons.add(
                    new ValidationResult.Builder()
                        .valid(false)
                        .explanation(
                            String.format(
                                "No expression for a condition in rule '%s' was found.",
                                rule.getName()))
                        .build());
              } else {
                final String expression = condition.getExpression().trim();
                reasons.add(
                    StandardValidators.createAttributeExpressionLanguageValidator(
                            AttributeExpression.ResultType.BOOLEAN, false)
                        .validate(
                            String.format("Condition for rule '%s'.", rule.getName()),
                            expression,
                            context));
              }
            }
          }

          // validate each action
          final Set<Action> actions = rule.getActions();
          if (actions == null) {
            reasons.add(
                new ValidationResult.Builder()
                    .valid(false)
                    .explanation(String.format("No actions for rule '%s' found.", rule.getName()))
                    .build());
          } else {
            for (final Action action : actions) {
              if (action.getAttribute() == null) {
                reasons.add(
                    new ValidationResult.Builder()
                        .valid(false)
                        .explanation(
                            String.format(
                                "An action in rule '%s' is missing the attribute name.",
                                rule.getName()))
                        .build());
              } else if (action.getValue() == null) {
                reasons.add(
                    new ValidationResult.Builder()
                        .valid(false)
                        .explanation(
                            String.format(
                                "No value for attribute '%s' in rule '%s' was found.",
                                action.getAttribute(), rule.getName()))
                        .build());
              } else {
                reasons.add(
                    StandardValidators.createAttributeExpressionLanguageValidator(
                            AttributeExpression.ResultType.STRING, true)
                        .validate(
                            String.format("Action for rule '%s'.", rule.getName()),
                            action.getValue(),
                            context));
              }
            }
          }
        }
      }
    }

    return reasons;
  }