Search in sources :

Example 1 with AttributeValueDecorator

use of org.apache.nifi.expression.AttributeValueDecorator in project nifi by apache.

the class RouteOnContent method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    final List<FlowFile> flowFiles = session.get(1);
    if (flowFiles.isEmpty()) {
        return;
    }
    final AttributeValueDecorator quoteDecorator = new AttributeValueDecorator() {

        @Override
        public String decorate(final String attributeValue) {
            return (attributeValue == null) ? null : Pattern.quote(attributeValue);
        }
    };
    final Map<FlowFile, Set<Relationship>> flowFileDestinationMap = new HashMap<>();
    final ComponentLog logger = getLogger();
    final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).getValue());
    final byte[] buffer = new byte[context.getProperty(BUFFER_SIZE).asDataSize(DataUnit.B).intValue()];
    for (final FlowFile flowFile : flowFiles) {
        final Set<Relationship> destinations = new HashSet<>();
        flowFileDestinationMap.put(flowFile, destinations);
        final AtomicInteger bufferedByteCount = new AtomicInteger(0);
        session.read(flowFile, new InputStreamCallback() {

            @Override
            public void process(final InputStream in) throws IOException {
                bufferedByteCount.set(StreamUtils.fillBuffer(in, buffer, false));
            }
        });
        final String contentString = new String(buffer, 0, bufferedByteCount.get(), charset);
        for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
            if (!descriptor.isDynamic()) {
                continue;
            }
            final String regex = context.getProperty(descriptor).evaluateAttributeExpressions(flowFile, quoteDecorator).getValue();
            final Pattern pattern = Pattern.compile(regex);
            final boolean matches;
            if (context.getProperty(MATCH_REQUIREMENT).getValue().equalsIgnoreCase(MATCH_ALL)) {
                matches = pattern.matcher(contentString).matches();
            } else {
                matches = pattern.matcher(contentString).find();
            }
            if (matches) {
                final Relationship relationship = new Relationship.Builder().name(descriptor.getName()).build();
                destinations.add(relationship);
            }
        }
    }
    for (final Map.Entry<FlowFile, Set<Relationship>> entry : flowFileDestinationMap.entrySet()) {
        FlowFile flowFile = entry.getKey();
        final Set<Relationship> destinations = entry.getValue();
        if (destinations.isEmpty()) {
            flowFile = session.putAttribute(flowFile, ROUTE_ATTRIBUTE_KEY, REL_NO_MATCH.getName());
            session.transfer(flowFile, REL_NO_MATCH);
            session.getProvenanceReporter().route(flowFile, REL_NO_MATCH);
            logger.info("Routing {} to 'unmatched'", new Object[] { flowFile });
        } else {
            final Relationship firstRelationship = destinations.iterator().next();
            destinations.remove(firstRelationship);
            for (final Relationship relationship : destinations) {
                FlowFile clone = session.clone(flowFile);
                clone = session.putAttribute(clone, ROUTE_ATTRIBUTE_KEY, relationship.getName());
                session.getProvenanceReporter().route(clone, relationship);
                session.transfer(clone, relationship);
                logger.info("Cloning {} to {} and routing clone to {}", new Object[] { flowFile, clone, relationship });
            }
            flowFile = session.putAttribute(flowFile, ROUTE_ATTRIBUTE_KEY, firstRelationship.getName());
            session.getProvenanceReporter().route(flowFile, firstRelationship);
            session.transfer(flowFile, firstRelationship);
            logger.info("Routing {} to {}", new Object[] { flowFile, firstRelationship });
        }
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) Pattern(java.util.regex.Pattern) HashSet(java.util.HashSet) Set(java.util.Set) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Relationship(org.apache.nifi.processor.Relationship) DynamicRelationship(org.apache.nifi.annotation.behavior.DynamicRelationship) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) AttributeValueDecorator(org.apache.nifi.expression.AttributeValueDecorator) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Pattern (java.util.regex.Pattern)1 DynamicRelationship (org.apache.nifi.annotation.behavior.DynamicRelationship)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 AttributeValueDecorator (org.apache.nifi.expression.AttributeValueDecorator)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 Relationship (org.apache.nifi.processor.Relationship)1 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)1