org.apache.nifi.util.FlowFileUnpackagerV2 Java Examples

The following examples show how to use org.apache.nifi.util.FlowFileUnpackagerV2. 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 File: UnpackContent.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final ComponentLog logger = getLogger();
    PackageFormat packagingFormat = PackageFormat.getFormat(context.getProperty(PACKAGING_FORMAT).getValue().toLowerCase());
    if (packagingFormat == PackageFormat.AUTO_DETECT_FORMAT) {
        packagingFormat = null;
        final String mimeType = flowFile.getAttribute(CoreAttributes.MIME_TYPE.key());
        if (mimeType == null) {
            logger.error("No mime.type attribute set for {}; routing to failure", new Object[]{flowFile});
            session.transfer(flowFile, REL_FAILURE);
            return;
        }

        for (PackageFormat format: PackageFormat.values()) {
            if (mimeType.toLowerCase().equals(format.getMimeType())) {
                packagingFormat = format;
            }
        }
        if (packagingFormat == null) {
            logger.info("Cannot unpack {} because its mime.type attribute is set to '{}', which is not a format that can be unpacked; routing to 'success'", new Object[]{flowFile, mimeType});
            session.transfer(flowFile, REL_SUCCESS);
            return;
        }
    }

    // set the Unpacker to use for this FlowFile.  FlowFileUnpackager objects maintain state and are not reusable.
    final Unpacker unpacker;
    final boolean addFragmentAttrs;
    switch (packagingFormat) {
    case TAR_FORMAT:
    case X_TAR_FORMAT:
        unpacker = tarUnpacker;
        addFragmentAttrs = true;
        break;
    case ZIP_FORMAT:
        unpacker = zipUnpacker;
        addFragmentAttrs = true;
        break;
    case FLOWFILE_STREAM_FORMAT_V2:
        unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV2());
        addFragmentAttrs = false;
        break;
    case FLOWFILE_STREAM_FORMAT_V3:
        unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV3());
        addFragmentAttrs = false;
        break;
    case FLOWFILE_TAR_FORMAT:
        unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV1());
        addFragmentAttrs = false;
        break;
    case AUTO_DETECT_FORMAT:
    default:
        // The format of the unpacker should be known before initialization
        throw new ProcessException(packagingFormat + " is not a valid packaging format");
    }

    final List<FlowFile> unpacked = new ArrayList<>();
    try {
        unpacker.unpack(session, flowFile, unpacked);
        if (unpacked.isEmpty()) {
            logger.error("Unable to unpack {} because it does not appear to have any entries; routing to failure", new Object[]{flowFile});
            session.transfer(flowFile, REL_FAILURE);
            return;
        }

        if (addFragmentAttrs) {
            finishFragmentAttributes(session, flowFile, unpacked);
        }
        session.transfer(unpacked, REL_SUCCESS);
        final String fragmentId = unpacked.size() > 0 ? unpacked.get(0).getAttribute(FRAGMENT_ID) : null;
        flowFile = FragmentAttributes.copyAttributesToOriginal(session, flowFile, fragmentId, unpacked.size());
        session.transfer(flowFile, REL_ORIGINAL);
        session.getProvenanceReporter().fork(flowFile, unpacked);
        logger.info("Unpacked {} into {} and transferred to success", new Object[]{flowFile, unpacked});
    } catch (final ProcessException | InvalidPathException e) {
        logger.error("Unable to unpack {} due to {}; routing to failure", new Object[]{flowFile, e});
        session.transfer(flowFile, REL_FAILURE);
        session.remove(unpacked);
    }
}
 
Example #2
Source File: UnpackContent.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final ComponentLog logger = getLogger();
    PackageFormat packagingFormat = PackageFormat.getFormat(context.getProperty(PACKAGING_FORMAT).getValue().toLowerCase());
    if (packagingFormat == PackageFormat.AUTO_DETECT_FORMAT) {
        packagingFormat = null;
        final String mimeType = flowFile.getAttribute(CoreAttributes.MIME_TYPE.key());
        if (mimeType == null) {
            logger.error("No mime.type attribute set for {}; routing to failure", new Object[]{flowFile});
            session.transfer(flowFile, REL_FAILURE);
            return;
        }

        for (PackageFormat format: PackageFormat.values()) {
            if (mimeType.toLowerCase().equals(format.getMimeType())) {
                packagingFormat = format;
            }
        }
        if (packagingFormat == null) {
            logger.info("Cannot unpack {} because its mime.type attribute is set to '{}', which is not a format that can be unpacked; routing to 'success'", new Object[]{flowFile, mimeType});
            session.transfer(flowFile, REL_SUCCESS);
            return;
        }
    }

    // set the Unpacker to use for this FlowFile.  FlowFileUnpackager objects maintain state and are not reusable.
    final Unpacker unpacker;
    final boolean addFragmentAttrs;
    switch (packagingFormat) {
    case TAR_FORMAT:
    case X_TAR_FORMAT:
        unpacker = tarUnpacker;
        addFragmentAttrs = true;
        break;
    case ZIP_FORMAT:
        unpacker = zipUnpacker;
        addFragmentAttrs = true;
        break;
    case FLOWFILE_STREAM_FORMAT_V2:
        unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV2());
        addFragmentAttrs = false;
        break;
    case FLOWFILE_STREAM_FORMAT_V3:
        unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV3());
        addFragmentAttrs = false;
        break;
    case FLOWFILE_TAR_FORMAT:
        unpacker = new FlowFileStreamUnpacker(new FlowFileUnpackagerV1());
        addFragmentAttrs = false;
        break;
    case AUTO_DETECT_FORMAT:
    default:
        // The format of the unpacker should be known before initialization
        throw new ProcessException(packagingFormat + " is not a valid packaging format");
    }

    final List<FlowFile> unpacked = new ArrayList<>();
    try {
        unpacker.unpack(session, flowFile, unpacked);
        if (unpacked.isEmpty()) {
            logger.error("Unable to unpack {} because it does not appear to have any entries; routing to failure", new Object[]{flowFile});
            session.transfer(flowFile, REL_FAILURE);
            return;
        }

        if (addFragmentAttrs) {
            finishFragmentAttributes(session, flowFile, unpacked);
        }
        session.transfer(unpacked, REL_SUCCESS);
        final String fragmentId = unpacked.size() > 0 ? unpacked.get(0).getAttribute(FRAGMENT_ID) : null;
        flowFile = FragmentAttributes.copyAttributesToOriginal(session, flowFile, fragmentId, unpacked.size());
        session.transfer(flowFile, REL_ORIGINAL);
        session.getProvenanceReporter().fork(flowFile, unpacked);
        logger.info("Unpacked {} into {} and transferred to success", new Object[]{flowFile, unpacked});
    } catch (final Exception e) {
        logger.error("Unable to unpack {} due to {}; routing to failure", new Object[]{flowFile, e});
        session.transfer(flowFile, REL_FAILURE);
        session.remove(unpacked);
    }
}