use of org.apache.nifi.update.attributes.Criteria in project nifi by apache.
the class UpdateAttribute method search.
@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;
}
}
use of org.apache.nifi.update.attributes.Criteria in project nifi by apache.
the class UpdateAttribute method onScheduled.
@OnScheduled
public void onScheduled(final ProcessContext context) throws IOException {
criteriaCache.set(CriteriaSerDe.deserialize(context.getAnnotationData()));
propertyValues.clear();
if (stateful) {
StateManager stateManager = context.getStateManager();
StateMap state = stateManager.getState(Scope.LOCAL);
HashMap<String, String> tempMap = new HashMap<>();
tempMap.putAll(state.toMap());
String initValue = context.getProperty(STATEFUL_VARIABLES_INIT_VALUE).getValue();
// Initialize the stateful default actions
for (PropertyDescriptor entry : context.getProperties().keySet()) {
if (entry.isDynamic()) {
if (!tempMap.containsKey(entry.getName())) {
tempMap.put(entry.getName(), initValue);
}
}
}
// Initialize the stateful actions if the criteria exists
final Criteria criteria = criteriaCache.get();
if (criteria != null) {
for (Rule rule : criteria.getRules()) {
for (Action action : rule.getActions()) {
if (!tempMap.containsKey(action.getAttribute())) {
tempMap.put(action.getAttribute(), initValue);
}
}
}
}
context.getStateManager().setState(tempMap, Scope.LOCAL);
}
defaultActions = getDefaultActions(context.getProperties());
debugEnabled = getLogger().isDebugEnabled();
}
use of org.apache.nifi.update.attributes.Criteria in project nifi by apache.
the class RuleResource method createRule.
@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules")
public Response createRule(@Context final UriInfo uriInfo, final RuleEntity requestEntity) {
// get the web context
final NiFiWebConfigurationContext configurationContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
// ensure the rule has been specified
if (requestEntity == null || requestEntity.getRule() == null) {
throw new WebApplicationException(badRequest("The rule must be specified."));
}
final RuleDTO ruleDto = requestEntity.getRule();
// ensure the id hasn't been specified
if (ruleDto.getId() != null) {
throw new WebApplicationException(badRequest("The rule id cannot be specified."));
}
// ensure there are some conditions
if (ruleDto.getConditions() == null || ruleDto.getConditions().isEmpty()) {
throw new WebApplicationException(badRequest("The rule conditions must be set."));
}
// ensure there are some actions
if (ruleDto.getActions() == null || ruleDto.getActions().isEmpty()) {
throw new WebApplicationException(badRequest("The rule actions must be set."));
}
// generate a new id
final String uuid = UUID.randomUUID().toString();
// build the request context
final NiFiWebConfigurationRequestContext requestContext = getConfigurationRequestContext(requestEntity.getProcessorId(), requestEntity.getRevision(), requestEntity.getClientId());
// load the criteria
final Criteria criteria = getCriteria(configurationContext, requestContext);
final UpdateAttributeModelFactory factory = new UpdateAttributeModelFactory();
// create the new rule
final Rule rule;
try {
rule = factory.createRule(ruleDto);
rule.setId(uuid);
} catch (final IllegalArgumentException iae) {
throw new WebApplicationException(iae, badRequest(iae.getMessage()));
}
// add the rule
criteria.addRule(rule);
// save the criteria
saveCriteria(requestContext, criteria);
// create the response entity
final RuleEntity responseEntity = new RuleEntity();
responseEntity.setClientId(requestEntity.getClientId());
responseEntity.setRevision(requestEntity.getRevision());
responseEntity.setProcessorId(requestEntity.getProcessorId());
responseEntity.setRule(DtoFactory.createRuleDTO(rule));
// generate the response
final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
final ResponseBuilder response = Response.created(uriBuilder.path(uuid).build()).entity(responseEntity);
return noCache(response).build();
}
use of org.apache.nifi.update.attributes.Criteria in project nifi by apache.
the class RuleResource method deleteRule.
@DELETE
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules/{id}")
public Response deleteRule(@PathParam("id") final String ruleId, @QueryParam("processorId") final String processorId, @QueryParam("clientId") final String clientId, @QueryParam("revision") final Long revision) {
// get the web context
final NiFiWebConfigurationContext configurationContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
// build the web context config
final NiFiWebConfigurationRequestContext requestContext = getConfigurationRequestContext(processorId, revision, clientId);
// load the criteria and get the rule
final Criteria criteria = getCriteria(configurationContext, requestContext);
final Rule rule = criteria.getRule(ruleId);
if (rule == null) {
throw new NotFoundException();
}
// delete the rule
criteria.deleteRule(rule);
// save the criteria
saveCriteria(requestContext, criteria);
// create the response entity
final RulesEntity responseEntity = new RulesEntity();
responseEntity.setClientId(clientId);
responseEntity.setRevision(revision);
responseEntity.setProcessorId(processorId);
// generate the response
final ResponseBuilder response = Response.ok(responseEntity);
return noCache(response).build();
}
Aggregations