Search in sources :

Example 6 with LimitingInputStream

use of org.apache.nifi.stream.io.LimitingInputStream in project nifi by apache.

the class SmtpConsumer method data.

@Override
public void data(final InputStream data) throws RejectException, TooMuchDataException, IOException {
    final ProcessSession processSession = sessionFactory.createSession();
    final StopWatch watch = new StopWatch();
    watch.start();
    try {
        FlowFile flowFile = processSession.create();
        final AtomicBoolean limitExceeded = new AtomicBoolean(false);
        flowFile = processSession.write(flowFile, (OutputStream out) -> {
            final LimitingInputStream lis = new LimitingInputStream(data, maxMessageSize);
            IOUtils.copy(lis, out);
            if (lis.hasReachedLimit()) {
                limitExceeded.set(true);
            }
        });
        if (limitExceeded.get()) {
            throw new TooMuchDataException("Maximum message size limit reached - client must send smaller messages");
        }
        flowFile = processSession.putAllAttributes(flowFile, extractMessageAttributes());
        watch.stop();
        processSession.getProvenanceReporter().receive(flowFile, "smtp://" + host + ":" + port + "/", watch.getDuration(TimeUnit.MILLISECONDS));
        processSession.transfer(flowFile, ListenSMTP.REL_SUCCESS);
        processSession.commit();
    } catch (FlowFileAccessException | IllegalStateException | RejectException | IOException ex) {
        log.error("Unable to fully process input due to " + ex.getMessage(), ex);
        throw ex;
    } finally {
        // make sure this happens no matter what - is safe
        processSession.rollback();
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFile(org.apache.nifi.flowfile.FlowFile) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TooMuchDataException(org.subethamail.smtp.TooMuchDataException) FlowFileAccessException(org.apache.nifi.processor.exception.FlowFileAccessException) OutputStream(java.io.OutputStream) RejectException(org.subethamail.smtp.RejectException) LimitingInputStream(org.apache.nifi.stream.io.LimitingInputStream) IOException(java.io.IOException) StopWatch(org.apache.nifi.util.StopWatch)

Example 7 with LimitingInputStream

use of org.apache.nifi.stream.io.LimitingInputStream in project nifi by apache.

the class ByteArraySchemaRecordReader method nextRecord.

@Override
protected StandardProvenanceEventRecord nextRecord(final DataInputStream in, final int serializationVersion) throws IOException {
    verifySerializationVersion(serializationVersion);
    final long byteOffset = getBytesConsumed();
    final int recordLength = in.readInt();
    final InputStream limitedIn = new LimitingInputStream(in, recordLength);
    final Record eventRecord = recordReader.readRecord(limitedIn);
    if (eventRecord == null) {
        return null;
    }
    return EventRecord.getEvent(eventRecord, getFilename(), byteOffset, getMaxAttributeLength());
}
Also used : DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) LimitingInputStream(org.apache.nifi.stream.io.LimitingInputStream) InputStream(java.io.InputStream) LimitingInputStream(org.apache.nifi.stream.io.LimitingInputStream) Record(org.apache.nifi.repository.schema.Record) EventRecord(org.apache.nifi.provenance.schema.EventRecord)

Example 8 with LimitingInputStream

use of org.apache.nifi.stream.io.LimitingInputStream in project nifi by apache.

the class EventIdFirstSchemaRecordReader method readRecord.

private StandardProvenanceEventRecord readRecord(final DataInputStream in, final long eventId, final long startOffset, final int recordLength) throws IOException {
    final InputStream limitedIn = new LimitingInputStream(in, recordLength);
    final Record eventRecord = recordReader.readRecord(limitedIn);
    if (eventRecord == null) {
        return null;
    }
    final StandardProvenanceEventRecord deserializedEvent = LookupTableEventRecord.getEvent(eventRecord, getFilename(), startOffset, getMaxAttributeLength(), firstEventId, systemTimeOffset, componentIds, componentTypes, queueIds, eventTypes);
    deserializedEvent.setEventId(eventId);
    return deserializedEvent;
}
Also used : DataInputStream(java.io.DataInputStream) LimitingInputStream(org.apache.nifi.stream.io.LimitingInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) LimitingInputStream(org.apache.nifi.stream.io.LimitingInputStream) Record(org.apache.nifi.repository.schema.Record) LookupTableEventRecord(org.apache.nifi.provenance.schema.LookupTableEventRecord)

Aggregations

LimitingInputStream (org.apache.nifi.stream.io.LimitingInputStream)8 InputStream (java.io.InputStream)7 DataInputStream (java.io.DataInputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 BufferedInputStream (java.io.BufferedInputStream)3 Record (org.apache.nifi.repository.schema.Record)3 ByteCountingInputStream (org.apache.nifi.stream.io.ByteCountingInputStream)3 FileInputStream (java.io.FileInputStream)2 LookupTableEventRecord (org.apache.nifi.provenance.schema.LookupTableEventRecord)2 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 OutputStream (java.io.OutputStream)1 DecimalFormat (java.text.DecimalFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ContentClaim (org.apache.nifi.controller.repository.claim.ContentClaim)1 ResourceClaim (org.apache.nifi.controller.repository.claim.ResourceClaim)1