Search in sources :

Example 81 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class TestISPEnrichIP method successfulMaxMindResponseShouldFlowToFoundRelationshipWhenAsnIsNotSet.

@Test
public void successfulMaxMindResponseShouldFlowToFoundRelationshipWhenAsnIsNotSet() throws Exception {
    testRunner.setProperty(ISPEnrichIP.GEO_DATABASE_FILE, "./");
    testRunner.setProperty(ISPEnrichIP.IP_ADDRESS_ATTRIBUTE, "ip");
    final IspResponse ispResponse = getIspResponseWithoutASNDetail();
    when(databaseReader.isp(InetAddress.getByName("1.2.3.4"))).thenReturn(ispResponse);
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("ip", "1.2.3.4");
    testRunner.enqueue(new byte[0], attributes);
    testRunner.run();
    List<MockFlowFile> notFound = testRunner.getFlowFilesForRelationship(ISPEnrichIP.REL_NOT_FOUND);
    List<MockFlowFile> found = testRunner.getFlowFilesForRelationship(ISPEnrichIP.REL_FOUND);
    assertEquals(0, notFound.size());
    assertEquals(1, found.size());
    FlowFile finishedFound = found.get(0);
    assertNotNull(finishedFound.getAttribute("ip.isp.lookup.micros"));
    assertNotNull(finishedFound.getAttribute("ip.isp.lookup.micros"));
    assertEquals("Apache NiFi - Test ISP", finishedFound.getAttribute("ip.isp.name"));
    assertEquals("Apache NiFi - Test Organization", finishedFound.getAttribute("ip.isp.organization"));
    assertNull(finishedFound.getAttribute("ip.isp.asn"));
    assertNull(finishedFound.getAttribute("ip.isp.asn.organization"));
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) IspResponse(com.maxmind.geoip2.model.IspResponse) FlowFile(org.apache.nifi.flowfile.FlowFile) MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 82 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class MalformedChunkHandler method handle.

public void handle(FlowFile original, ProcessSession processSession, String chunkName, byte[] badChunk) {
    FlowFile flowFile = processSession.create(original);
    flowFile = processSession.putAttribute(flowFile, CoreAttributes.FILENAME.key(), chunkName);
    flowFile = processSession.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), MediaType.APPLICATION_BINARY.toString());
    flowFile = processSession.write(flowFile, out -> out.write(badChunk));
    processSession.transfer(flowFile, badChunkRelationship);
}
Also used : MediaType(com.google.common.net.MediaType) FlowFile(org.apache.nifi.flowfile.FlowFile) Relationship(org.apache.nifi.processor.Relationship) ProcessSession(org.apache.nifi.processor.ProcessSession) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) FlowFile(org.apache.nifi.flowfile.FlowFile)

Example 83 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class ParseEvtx method onTrigger.

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    ComponentLog logger = getLogger();
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    String basename = getBasename(flowFile, logger);
    String granularity = context.getProperty(GRANULARITY).getValue();
    if (FILE.equals(granularity)) {
        // File granularity will emit a FlowFile for each input
        FlowFile original = session.clone(flowFile);
        AtomicReference<Exception> exceptionReference = new AtomicReference<>(null);
        FlowFile updated = session.write(flowFile, (in, out) -> {
            processFileGranularity(session, logger, original, basename, exceptionReference, in, out);
        });
        session.transfer(original, REL_ORIGINAL);
        resultProcessor.process(session, logger, updated, exceptionReference.get(), getName(basename, null, null, XML_EXTENSION));
    } else {
        session.read(flowFile, in -> {
            if (RECORD.equals(granularity)) {
                // Record granularity will emit a FlowFile for every record (event)
                processRecordGranularity(session, logger, flowFile, basename, in);
            } else if (CHUNK.equals(granularity)) {
                // Chunk granularity will emit a FlowFile for each chunk of the file
                processChunkGranularity(session, logger, flowFile, basename, in);
            }
        });
        session.transfer(flowFile, REL_ORIGINAL);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) AtomicReference(java.util.concurrent.atomic.AtomicReference) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) MalformedChunkException(org.apache.nifi.processors.evtx.parser.MalformedChunkException) IOException(java.io.IOException)

Example 84 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class ParseEvtx method processRecordGranularity.

protected void processRecordGranularity(ProcessSession session, ComponentLog componentLog, FlowFile flowFile, String basename, InputStream in) throws IOException {
    FileHeader fileHeader = fileHeaderFactory.create(in, componentLog);
    while (fileHeader.hasNext()) {
        try {
            ChunkHeader chunkHeader = fileHeader.next();
            while (chunkHeader.hasNext()) {
                FlowFile updated = session.create(flowFile);
                AtomicReference<Exception> exceptionReference = new AtomicReference<>(null);
                try {
                    Record record = chunkHeader.next();
                    updated = session.write(updated, out -> {
                        try (RootNodeHandler rootNodeHandler = rootNodeHandlerFactory.create(out)) {
                            try {
                                rootNodeHandler.handle(record.getRootNode());
                            } catch (IOException e) {
                                exceptionReference.set(e);
                            }
                        } catch (IOException e) {
                            exceptionReference.set(e);
                        }
                    });
                    resultProcessor.process(session, componentLog, updated, exceptionReference.get(), getName(basename, chunkHeader.getChunkNumber(), record.getRecordNum(), XML_EXTENSION));
                } catch (Exception e) {
                    exceptionReference.set(e);
                    session.remove(updated);
                }
                if (exceptionReference.get() != null) {
                    malformedChunkHandler.handle(flowFile, session, getName(basename, chunkHeader.getChunkNumber(), null, EVTX_EXTENSION), chunkHeader.getBinaryReader().getBytes());
                }
            }
        } catch (MalformedChunkException e) {
            malformedChunkHandler.handle(flowFile, session, getName(basename, e.getChunkNum(), null, EVTX_EXTENSION), e.getBadChunk());
        }
    }
}
Also used : Arrays(java.util.Arrays) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) FileHeaderFactory(org.apache.nifi.processors.evtx.parser.FileHeaderFactory) FileHeader(org.apache.nifi.processors.evtx.parser.FileHeader) EventDriven(org.apache.nifi.annotation.behavior.EventDriven) ComponentLog(org.apache.nifi.logging.ComponentLog) SideEffectFree(org.apache.nifi.annotation.behavior.SideEffectFree) AtomicReference(java.util.concurrent.atomic.AtomicReference) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) HashSet(java.util.HashSet) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Relationship(org.apache.nifi.processor.Relationship) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) ReadsAttributes(org.apache.nifi.annotation.behavior.ReadsAttributes) Record(org.apache.nifi.processors.evtx.parser.Record) ChunkHeader(org.apache.nifi.processors.evtx.parser.ChunkHeader) OutputStream(java.io.OutputStream) MalformedChunkException(org.apache.nifi.processors.evtx.parser.MalformedChunkException) FlowFile(org.apache.nifi.flowfile.FlowFile) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) ProcessSession(org.apache.nifi.processor.ProcessSession) IOException(java.io.IOException) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) List(java.util.List) SupportsBatching(org.apache.nifi.annotation.behavior.SupportsBatching) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Tags(org.apache.nifi.annotation.documentation.Tags) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) Collections(java.util.Collections) ReadsAttribute(org.apache.nifi.annotation.behavior.ReadsAttribute) InputStream(java.io.InputStream) FlowFile(org.apache.nifi.flowfile.FlowFile) ChunkHeader(org.apache.nifi.processors.evtx.parser.ChunkHeader) AtomicReference(java.util.concurrent.atomic.AtomicReference) Record(org.apache.nifi.processors.evtx.parser.Record) IOException(java.io.IOException) MalformedChunkException(org.apache.nifi.processors.evtx.parser.MalformedChunkException) FileHeader(org.apache.nifi.processors.evtx.parser.FileHeader) ProcessException(org.apache.nifi.processor.exception.ProcessException) MalformedChunkException(org.apache.nifi.processors.evtx.parser.MalformedChunkException) IOException(java.io.IOException)

Example 85 with FlowFile

use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.

the class TestMockProcessSession method testReadWithoutCloseThrowsExceptionOnCommit.

@Test
public void testReadWithoutCloseThrowsExceptionOnCommit() throws IOException {
    final Processor processor = new PoorlyBehavedProcessor();
    final MockProcessSession session = new MockProcessSession(new SharedSessionState(processor, new AtomicLong(0L)), processor);
    FlowFile flowFile = session.createFlowFile("hello, world".getBytes());
    final InputStream in = session.read(flowFile);
    final byte[] buffer = new byte[12];
    StreamUtils.fillBuffer(in, buffer);
    assertEquals("hello, world", new String(buffer));
    session.remove(flowFile);
    try {
        session.commit();
        Assert.fail("Was able to commit session without closing InputStream");
    } catch (final FlowFileHandlingException ffhe) {
        System.out.println(ffhe.toString());
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) Processor(org.apache.nifi.processor.Processor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) InputStream(java.io.InputStream) FlowFileHandlingException(org.apache.nifi.processor.exception.FlowFileHandlingException) Test(org.junit.Test)

Aggregations

FlowFile (org.apache.nifi.flowfile.FlowFile)500 IOException (java.io.IOException)236 ProcessException (org.apache.nifi.processor.exception.ProcessException)193 HashMap (java.util.HashMap)160 InputStream (java.io.InputStream)145 OutputStream (java.io.OutputStream)131 ComponentLog (org.apache.nifi.logging.ComponentLog)119 Test (org.junit.Test)116 ArrayList (java.util.ArrayList)113 Map (java.util.Map)105 MockFlowFile (org.apache.nifi.util.MockFlowFile)103 ProcessSession (org.apache.nifi.processor.ProcessSession)99 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)83 Relationship (org.apache.nifi.processor.Relationship)78 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)78 HashSet (java.util.HashSet)75 List (java.util.List)67 StopWatch (org.apache.nifi.util.StopWatch)59 Set (java.util.Set)56 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)55