Java Code Examples for org.apache.nifi.processor.exception.FlowFileAccessException

The following examples show how to use org.apache.nifi.processor.exception.FlowFileAccessException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: localization_nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void exportTo(final FlowFile flowFile, final OutputStream out) {
    validateState(flowFile);
    if (flowFile == null || out == null) {
        throw new IllegalArgumentException("arguments cannot be null");
    }

    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }

    final MockFlowFile mock = (MockFlowFile) flowFile;

    try {
        out.write(mock.getData());
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }
}
 
Example 2
Source Project: localization_nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void exportTo(final FlowFile flowFile, final Path path, final boolean append) {
    validateState(flowFile);
    if (flowFile == null || path == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }

    final MockFlowFile mock = (MockFlowFile) flowFile;

    final OpenOption mode = append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE;

    try (final OutputStream out = Files.newOutputStream(path, mode)) {
        out.write(mock.getData());
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }
}
 
Example 3
Source Project: localization_nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public MockFlowFile importFrom(final InputStream in, final FlowFile flowFile) {
    validateState(flowFile);
    if (in == null || flowFile == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }
    final MockFlowFile mock = (MockFlowFile) flowFile;

    final MockFlowFile newFlowFile = new MockFlowFile(mock.getId(), flowFile);
    currentVersions.put(newFlowFile.getId(), newFlowFile);
    try {
        final byte[] data = readFully(in);
        newFlowFile.setData(data);
        return newFlowFile;
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }
}
 
Example 4
Source Project: localization_nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public MockFlowFile importFrom(final Path path, final boolean keepSourceFile, final FlowFile flowFile) {
    validateState(flowFile);
    if (path == null || flowFile == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }
    final MockFlowFile mock = (MockFlowFile) flowFile;
    MockFlowFile newFlowFile = new MockFlowFile(mock.getId(), flowFile);
    currentVersions.put(newFlowFile.getId(), newFlowFile);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        Files.copy(path, baos);
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }

    newFlowFile.setData(baos.toByteArray());
    newFlowFile = putAttribute(newFlowFile, CoreAttributes.FILENAME.key(), path.getFileName().toString());
    return newFlowFile;
}
 
Example 5
Source Project: localization_nifi   Source File: StandardProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
private void resetWriteClaims(final boolean suppressExceptions) {
    for (final ByteCountingOutputStream out : appendableStreams.values()) {
        try {
            try {
                out.flush();
            } finally {
                out.close();
            }
        } catch (final IOException e) {
            if (!suppressExceptions) {
                throw new FlowFileAccessException("Unable to flush the output of FlowFile to the Content Repository");
            }
        }
    }
    appendableStreams.clear();
}
 
Example 6
Source Project: localization_nifi   Source File: StandardProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void exportTo(final FlowFile source, final Path destination, final boolean append) {
    validateRecordState(source);
    final StandardRepositoryRecord record = records.get(source);
    try {
        ensureNotAppending(record.getCurrentClaim());

        claimCache.flush(record.getCurrentClaim());
        final long copyCount = context.getContentRepository().exportTo(record.getCurrentClaim(), destination, append, record.getCurrentClaimOffset(), source.getSize());
        bytesRead += copyCount;
        bytesWritten += copyCount;
    } catch (final ContentNotFoundException nfe) {
        handleContentNotFound(nfe, record);
    } catch (final Throwable t) {
        throw new FlowFileAccessException("Failed to export " + source + " to " + destination + " due to " + t.toString(), t);
    }
}
 
Example 7
@Override
public int read() throws IOException {
    try {
        final int byteRead = super.read();
        if (byteRead < 0) {
            ensureAllContentRead();
        } else {
            bytesConsumed++;
        }

        return byteRead;
    } catch (final ContentNotFoundException cnfe) {
        throw cnfe;
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not read from " + flowFile, ioe);
    }
}
 
Example 8
@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
    try {
        final int count = super.read(b, off, len);
        if (count < 0) {
            ensureAllContentRead();
        } else {
            bytesConsumed += count;
        }

        return count;
    } catch (final ContentNotFoundException cnfe) {
        throw cnfe;
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not read from " + flowFile, ioe);
    }
}
 
Example 9
@Override
public int read(final byte[] b) throws IOException {
    try {
        final int count = super.read(b);
        if (count < 0) {
            ensureAllContentRead();
        } else {
            bytesConsumed += count;
        }

        return count;
    } catch (final ContentNotFoundException cnfe) {
        throw cnfe;
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not read from " + flowFile, ioe);
    }
}
 
Example 10
Source Project: nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
private void closeStreams(final Map<FlowFile, ? extends Closeable> streamMap, final boolean enforceClosed) {
    final Map<FlowFile, ? extends Closeable> openStreamCopy = new HashMap<>(streamMap); // avoid ConcurrentModificationException by creating a copy of the List
    for (final Map.Entry<FlowFile, ? extends Closeable> entry : openStreamCopy.entrySet()) {
        final FlowFile flowFile = entry.getKey();
        final Closeable openStream = entry.getValue();

        try {
            openStream.close();
        } catch (IOException e) {
            throw new FlowFileAccessException("Failed to close stream for " + flowFile, e);
        }

        if (enforceClosed) {
            throw new FlowFileHandlingException("Cannot commit session because the following streams were created via "
                + "calls to ProcessSession.read(FlowFile) or ProcessSession.write(FlowFile) and never closed: " + streamMap);
        }
    }
}
 
Example 11
Source Project: nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void exportTo(FlowFile flowFile, final OutputStream out) {
    flowFile = validateState(flowFile);
    if (flowFile == null || out == null) {
        throw new IllegalArgumentException("arguments cannot be null");
    }

    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }

    final MockFlowFile mock = (MockFlowFile) flowFile;

    try {
        out.write(mock.getData());
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }
}
 
Example 12
Source Project: nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void exportTo(FlowFile flowFile, final Path path, final boolean append) {
    flowFile = validateState(flowFile);
    if (flowFile == null || path == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }

    final MockFlowFile mock = (MockFlowFile) flowFile;

    final OpenOption mode = append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE;

    try (final OutputStream out = Files.newOutputStream(path, mode)) {
        out.write(mock.getData());
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }
}
 
Example 13
Source Project: nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public MockFlowFile importFrom(final InputStream in, FlowFile flowFile) {
    flowFile = validateState(flowFile);
    if (in == null || flowFile == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }
    final MockFlowFile mock = (MockFlowFile) flowFile;

    final MockFlowFile newFlowFile = new MockFlowFile(mock.getId(), flowFile);
    currentVersions.put(newFlowFile.getId(), newFlowFile);
    try {
        final byte[] data = readFully(in);
        newFlowFile.setData(data);
        return newFlowFile;
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }
}
 
Example 14
Source Project: nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public MockFlowFile importFrom(final Path path, final boolean keepSourceFile, FlowFile flowFile) {
    flowFile = validateState(flowFile);
    if (path == null || flowFile == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }
    final MockFlowFile mock = (MockFlowFile) flowFile;
    MockFlowFile newFlowFile = new MockFlowFile(mock.getId(), flowFile);
    currentVersions.put(newFlowFile.getId(), newFlowFile);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        Files.copy(path, baos);
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }

    newFlowFile.setData(baos.toByteArray());
    newFlowFile = putAttribute(newFlowFile, CoreAttributes.FILENAME.key(), path.getFileName().toString());
    return newFlowFile;
}
 
Example 15
Source Project: nifi   Source File: StatelessProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void exportTo(FlowFile flowFile, final OutputStream out) {
    flowFile = validateState(flowFile);
    if (flowFile == null || out == null) {
        throw new IllegalArgumentException("arguments cannot be null");
    }

    if (!(flowFile instanceof StatelessFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }

    try {
        copyTo(((StatelessFlowFile) flowFile).getDataStream(), out);
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }
}
 
Example 16
Source Project: nifi   Source File: StatelessProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void exportTo(FlowFile flowFile, final Path path, final boolean append) {
    flowFile = validateState(flowFile);
    if (flowFile == null || path == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof StatelessFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }
    StatelessFlowFile statelessFlowFile = (StatelessFlowFile) flowFile;

    final OpenOption mode = append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE;

    try (final OutputStream out = Files.newOutputStream(path, mode)) {
        if (statelessFlowFile.materializeContent)
            statelessFlowFile.materializeData();
        copyTo(statelessFlowFile.getDataStream(), out);
    } catch (final IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }
}
 
Example 17
Source Project: nifi   Source File: StatelessProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public StatelessFlowFile importFrom(final Path path, final boolean keepSourceFile, FlowFile flowFile) {
    flowFile = validateState(flowFile);
    if (path == null || flowFile == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof StatelessFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }
    if (!keepSourceFile) {
        throw new IllegalArgumentException("Not going to delete the file...");
    }
    StatelessFlowFile newFlowFile = new StatelessFlowFile((StatelessFlowFile) flowFile, this.materializeContent);
    try {
        newFlowFile.setData(Files.newInputStream(path));
    } catch (IOException e) {
        throw new FlowFileAccessException(e.toString(), e);
    }
    currentVersions.put(newFlowFile.getId(), newFlowFile);

    newFlowFile = putAttribute(newFlowFile, CoreAttributes.FILENAME.key(), path.getFileName().toString());
    return newFlowFile;
}
 
Example 18
Source Project: nifi   Source File: StandardProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
private void resetWriteClaims(final boolean suppressExceptions) {
    for (final ByteCountingOutputStream out : appendableStreams.values()) {
        try {
            try {
                out.flush();
            } finally {
                out.close();
            }
        } catch (final IOException e) {
            if (!suppressExceptions) {
                throw new FlowFileAccessException("Unable to flush the output of FlowFile to the Content Repository");
            }
        }
    }
    appendableStreams.clear();
}
 
Example 19
Source Project: nifi   Source File: StandardProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void exportTo(FlowFile source, final Path destination, final boolean append) {
    verifyTaskActive();
    source = validateRecordState(source);
    final StandardRepositoryRecord record = getRecord(source);
    try {
        ensureNotAppending(record.getCurrentClaim());

        claimCache.flush(record.getCurrentClaim());
        final long copyCount = context.getContentRepository().exportTo(record.getCurrentClaim(), destination, append, record.getCurrentClaimOffset(), source.getSize());
        bytesRead += copyCount;
        bytesWritten += copyCount;
    } catch (final ContentNotFoundException nfe) {
        handleContentNotFound(nfe, record);
    } catch (final Throwable t) {
        throw new FlowFileAccessException("Failed to export " + source + " to " + destination + " due to " + t.toString(), t);
    }
}
 
Example 20
Source Project: nifi   Source File: FlowFileAccessInputStream.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public int read() throws IOException {
    try {
        final int byteRead = super.read();
        if (byteRead < 0) {
            ensureAllContentRead();
        } else {
            bytesConsumed++;
        }

        return byteRead;
    } catch (final ContentNotFoundException cnfe) {
        throw cnfe;
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not read from " + flowFile, ioe);
    }
}
 
Example 21
Source Project: nifi   Source File: FlowFileAccessInputStream.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
    try {
        final int count = super.read(b, off, len);
        if (count < 0) {
            ensureAllContentRead();
        } else {
            bytesConsumed += count;
        }

        return count;
    } catch (final ContentNotFoundException cnfe) {
        throw cnfe;
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not read from " + flowFile, ioe);
    }
}
 
Example 22
Source Project: localization_nifi   Source File: TestListenSyslog.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorQueue() throws IOException {
    final List<ListenSyslog.RawSyslogEvent> msgs = new ArrayList<>();
    msgs.add(new ListenSyslog.RawSyslogEvent(VALID_MESSAGE.getBytes(), "sender-01"));
    msgs.add(new ListenSyslog.RawSyslogEvent(VALID_MESSAGE.getBytes(), "sender-01"));

    // Add message that will throw a FlowFileAccessException the first time that we attempt to read
    // the contents but will succeed the second time.
    final AtomicInteger getMessageAttempts = new AtomicInteger(0);
    msgs.add(new ListenSyslog.RawSyslogEvent(VALID_MESSAGE.getBytes(), "sender-01") {
        @Override
        public byte[] getData() {
            final int attempts = getMessageAttempts.incrementAndGet();
            if (attempts == 1) {
                throw new FlowFileAccessException("Unit test failure");
            } else {
                return VALID_MESSAGE.getBytes();
            }
        }
    });

    final CannedMessageProcessor proc = new CannedMessageProcessor(msgs);
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.setProperty(ListenSyslog.MAX_BATCH_SIZE, "5");
    runner.setProperty(ListenSyslog.PROTOCOL, ListenSyslog.UDP_VALUE.getValue());
    runner.setProperty(ListenSyslog.PORT, "0");
    runner.setProperty(ListenSyslog.PARSE_MESSAGES, "false");

    runner.run();
    assertEquals(1, proc.getErrorQueueSize());
    runner.assertAllFlowFilesTransferred(ListenSyslog.REL_SUCCESS, 1);
    runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).get(0).assertContentEquals(VALID_MESSAGE + "\n" + VALID_MESSAGE);

    // running again should pull from the error queue
    runner.clearTransferState();
    runner.run();
    runner.assertAllFlowFilesTransferred(ListenSyslog.REL_SUCCESS, 1);
    runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS).get(0).assertContentEquals(VALID_MESSAGE);
}
 
Example 23
Source Project: localization_nifi   Source File: StandardProcessSession.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FlowFile importFrom(final InputStream source, final FlowFile destination) {
    validateRecordState(destination);
    final StandardRepositoryRecord record = records.get(destination);
    ContentClaim newClaim = null;
    final long claimOffset = 0L;

    final long newSize;
    try {
        try {
            newClaim = context.getContentRepository().create(context.getConnectable().isLossTolerant());
            claimLog.debug("Creating ContentClaim {} for 'importFrom' for {}", newClaim, destination);

            newSize = context.getContentRepository().importFrom(source, newClaim);
            bytesWritten += newSize;
        } catch (final IOException e) {
            throw new FlowFileAccessException("Unable to create ContentClaim due to " + e.toString(), e);
        }
    } catch (final Throwable t) {
        if (newClaim != null) {
            destroyContent(newClaim);
        }

        throw new FlowFileAccessException("Failed to import data from " + source + " for " + destination + " due to " + t.toString(), t);
    }

    removeTemporaryClaim(record);
    final FlowFileRecord newFile = new StandardFlowFileRecord.Builder()
        .fromFlowFile(record.getCurrent())
        .contentClaim(newClaim)
        .contentClaimOffset(claimOffset)
        .size(newSize)
        .build();
    record.setWorking(newFile);
    return newFile;
}
 
Example 24
@Override
public int available() throws IOException {
    try {
        return super.available();
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not determine amount of data available from " + flowFile, ioe);
    }
}
 
Example 25
@Override
public long skip(final long n) throws IOException {
    try {
        final long count = super.skip(n);
        bytesConsumed += count;
        return count;
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not skip data in " + flowFile, ioe);
    }
}
 
Example 26
@Override
public void reset() throws IOException {
    try {
        super.reset();
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not reset stream from " + flowFile, ioe);
    }
}
 
Example 27
@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
    try {
        out.write(b, off, len);
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not write to " + flowFile, ioe);
    }
}
 
Example 28
@Override
public void write(final int b) throws IOException {
    try {
        out.write(b);
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not write to " + flowFile, ioe);
    }
}
 
Example 29
@Override
public void write(final byte[] b) throws IOException {
    try {
        out.write(b);
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not write to " + flowFile, ioe);
    }
}
 
Example 30
@Override
public void flush() throws IOException {
    try {
        out.flush();
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Could not flush OutputStream for " + flowFile, ioe);
    }
}