Search in sources :

Example 1 with ContentNotFoundException

use of org.apache.nifi.controller.repository.ContentNotFoundException in project nifi by apache.

the class StandardConnectionDAO method getContent.

@Override
public DownloadableContent getContent(String id, String flowFileUuid, String requestUri) {
    try {
        final NiFiUser user = NiFiUserUtils.getNiFiUser();
        final Connection connection = locateConnection(id);
        final FlowFileQueue queue = connection.getFlowFileQueue();
        final FlowFileRecord flowFile = queue.getFlowFile(flowFileUuid);
        if (flowFile == null) {
            throw new ResourceNotFoundException(String.format("The FlowFile with UUID %s is no longer in the active queue.", flowFileUuid));
        }
        // get the attributes and ensure appropriate access
        final Map<String, String> attributes = flowFile.getAttributes();
        final Authorizable dataAuthorizable = new DataAuthorizable(connection.getSourceAuthorizable());
        dataAuthorizable.authorize(authorizer, RequestAction.READ, user, attributes);
        // get the filename and fall back to the identifier (should never happen)
        String filename = attributes.get(CoreAttributes.FILENAME.key());
        if (filename == null) {
            filename = flowFileUuid;
        }
        // get the mime-type
        final String type = attributes.get(CoreAttributes.MIME_TYPE.key());
        // get the content
        final InputStream content = flowController.getContent(flowFile, user.getIdentity(), requestUri);
        return new DownloadableContent(filename, type, content);
    } catch (final ContentNotFoundException cnfe) {
        throw new ResourceNotFoundException("Unable to find the specified content.");
    } catch (final IOException ioe) {
        logger.error(String.format("Unable to get the content for flowfile (%s) at this time.", flowFileUuid), ioe);
        throw new IllegalStateException("Unable to get the content at this time.");
    }
}
Also used : DownloadableContent(org.apache.nifi.web.DownloadableContent) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ContentNotFoundException(org.apache.nifi.controller.repository.ContentNotFoundException) InputStream(java.io.InputStream) Connection(org.apache.nifi.connectable.Connection) IOException(java.io.IOException) FlowFileQueue(org.apache.nifi.controller.queue.FlowFileQueue) DataAuthorizable(org.apache.nifi.authorization.resource.DataAuthorizable) DataAuthorizable(org.apache.nifi.authorization.resource.DataAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) FlowFileRecord(org.apache.nifi.controller.repository.FlowFileRecord) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException)

Example 2 with ContentNotFoundException

use of org.apache.nifi.controller.repository.ContentNotFoundException in project nifi by apache.

the class ControllerFacade method getContent.

/**
 * Gets the content for the specified claim.
 *
 * @param eventId event id
 * @param uri uri
 * @param contentDirection direction
 * @return the content for the specified claim
 */
public DownloadableContent getContent(final Long eventId, final String uri, final ContentDirection contentDirection) {
    try {
        final NiFiUser user = NiFiUserUtils.getNiFiUser();
        // get the event in order to get the filename
        final ProvenanceEventRecord event = flowController.getProvenanceRepository().getEvent(eventId);
        if (event == null) {
            throw new ResourceNotFoundException("Unable to find the specified event.");
        }
        // get the flowfile attributes
        final Map<String, String> attributes;
        if (ContentDirection.INPUT.equals(contentDirection)) {
            attributes = event.getPreviousAttributes();
        } else {
            attributes = event.getAttributes();
        }
        // authorize the event
        final Authorizable dataAuthorizable;
        if (event.isRemotePortType()) {
            dataAuthorizable = flowController.createRemoteDataAuthorizable(event.getComponentId());
        } else {
            dataAuthorizable = flowController.createLocalDataAuthorizable(event.getComponentId());
        }
        dataAuthorizable.authorize(authorizer, RequestAction.READ, user, attributes);
        // get the filename and fall back to the identifier (should never happen)
        String filename = attributes.get(CoreAttributes.FILENAME.key());
        if (filename == null) {
            filename = event.getFlowFileUuid();
        }
        // get the mime-type
        final String type = attributes.get(CoreAttributes.MIME_TYPE.key());
        // get the content
        final InputStream content = flowController.getContent(event, contentDirection, user.getIdentity(), uri);
        return new DownloadableContent(filename, type, content);
    } catch (final ContentNotFoundException cnfe) {
        throw new ResourceNotFoundException("Unable to find the specified content.");
    } catch (final IOException ioe) {
        logger.error(String.format("Unable to get the content for event (%s) at this time.", eventId), ioe);
        throw new IllegalStateException("Unable to get the content at this time.");
    }
}
Also used : DownloadableContent(org.apache.nifi.web.DownloadableContent) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ContentNotFoundException(org.apache.nifi.controller.repository.ContentNotFoundException) InputStream(java.io.InputStream) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Authorizable(org.apache.nifi.authorization.resource.Authorizable) IOException(java.io.IOException) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException)

Aggregations

IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Authorizable (org.apache.nifi.authorization.resource.Authorizable)2 NiFiUser (org.apache.nifi.authorization.user.NiFiUser)2 ContentNotFoundException (org.apache.nifi.controller.repository.ContentNotFoundException)2 DownloadableContent (org.apache.nifi.web.DownloadableContent)2 ResourceNotFoundException (org.apache.nifi.web.ResourceNotFoundException)2 DataAuthorizable (org.apache.nifi.authorization.resource.DataAuthorizable)1 Connection (org.apache.nifi.connectable.Connection)1 FlowFileQueue (org.apache.nifi.controller.queue.FlowFileQueue)1 FlowFileRecord (org.apache.nifi.controller.repository.FlowFileRecord)1 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)1