示例#1
0
  // Evaluates the specified Criteria on the specified flowfile. Clones the
  // specified flow file for each rule that is applied.
  private boolean evaluateCriteria(
      final ProcessSession session,
      final ProcessContext context,
      final Criteria criteria,
      final FlowFile flowfile,
      final Map<FlowFile, List<Rule>> matchedRules) {
    final ComponentLog logger = getLogger();
    final List<Rule> rules = criteria.getRules();

    // consider each rule and hold a copy of the flowfile for each matched rule
    for (final Rule rule : rules) {
      // evaluate the rule
      if (evaluateRule(context, rule, flowfile)) {
        final FlowFile flowfileToUse;

        // determine if we should use the original flow file or clone
        if (FlowFilePolicy.USE_ORIGINAL.equals(criteria.getFlowFilePolicy())
            || matchedRules.isEmpty()) {
          flowfileToUse = flowfile;
        } else {
          // clone the original for this rule
          flowfileToUse = session.clone(flowfile);
        }

        // store the flow file to use when executing this rule
        List<Rule> rulesForFlowFile = matchedRules.get(flowfileToUse);
        if (rulesForFlowFile == null) {
          rulesForFlowFile = new ArrayList<>();
          matchedRules.put(flowfileToUse, rulesForFlowFile);
        }
        rulesForFlowFile.add(rule);

        // log if appropriate
        if (logger.isDebugEnabled()) {
          logger.debug(
              this
                  + " all conditions met for rule '"
                  + rule.getName()
                  + "'. Using flow file - "
                  + flowfileToUse);
        }
      }
    }

    return !matchedRules.isEmpty();
  }
示例#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;
    }
  }
示例#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;
  }