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"));
}
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);
}
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);
}
}
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());
}
}
}
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());
}
}
Aggregations