Search in sources :

Example 16 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class RegistryService method getFlowSnapshot.

public VersionedFlowSnapshot getFlowSnapshot(final String bucketIdentifier, final String flowIdentifier, final Integer version) {
    if (StringUtils.isBlank(bucketIdentifier)) {
        throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
    }
    if (StringUtils.isBlank(flowIdentifier)) {
        throw new IllegalArgumentException("Flow identifier cannot be null or blank");
    }
    if (version == null) {
        throw new IllegalArgumentException("Version cannot be null or blank");
    }
    readLock.lock();
    try {
        final BucketEntity existingBucket = metadataService.getBucketById(bucketIdentifier);
        if (existingBucket == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        // we need to populate the version count here so we have to do this retrieval instead of snapshotEntity.getFlow()
        final FlowEntity flowEntityWithCount = metadataService.getFlowByIdWithSnapshotCounts(flowIdentifier);
        if (flowEntityWithCount == null) {
            LOGGER.warn("The specified flow id [{}] does not exist.", flowIdentifier);
            throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
        }
        if (!existingBucket.getId().equals(flowEntityWithCount.getBucketId())) {
            throw new IllegalStateException("The requested flow is not located in the given bucket");
        }
        // ensure the snapshot exists
        final FlowSnapshotEntity snapshotEntity = metadataService.getFlowSnapshot(flowIdentifier, version);
        if (snapshotEntity == null) {
            LOGGER.warn("The specified flow snapshot id [{}] does not exist for version [{}].", flowIdentifier, version);
            throw new ResourceNotFoundException("The specified versioned flow snapshot does not exist for this flow.");
        }
        // get the serialized bytes of the snapshot
        final byte[] serializedSnapshot = flowPersistenceProvider.getFlowContent(bucketIdentifier, flowIdentifier, version);
        if (serializedSnapshot == null || serializedSnapshot.length == 0) {
            throw new IllegalStateException("No serialized content found for snapshot with flow identifier " + flowIdentifier + " and version " + version);
        }
        // deserialize the contents
        final InputStream input = new ByteArrayInputStream(serializedSnapshot);
        final VersionedProcessGroup flowContents = processGroupSerializer.deserialize(input);
        // map entities to data model
        final Bucket bucket = DataModelMapper.map(existingBucket);
        final VersionedFlow versionedFlow = DataModelMapper.map(existingBucket, flowEntityWithCount);
        final VersionedFlowSnapshotMetadata snapshotMetadata = DataModelMapper.map(existingBucket, snapshotEntity);
        // create the snapshot to return
        final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
        snapshot.setFlowContents(flowContents);
        snapshot.setSnapshotMetadata(snapshotMetadata);
        snapshot.setFlow(versionedFlow);
        snapshot.setBucket(bucket);
        return snapshot;
    } finally {
        readLock.unlock();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Bucket(org.apache.nifi.registry.bucket.Bucket) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException)

Example 17 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class RegistryService method getLatestFlowSnapshotMetadata.

public VersionedFlowSnapshotMetadata getLatestFlowSnapshotMetadata(final String bucketIdentifier, final String flowIdentifier) {
    if (StringUtils.isBlank(bucketIdentifier)) {
        throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
    }
    if (StringUtils.isBlank(flowIdentifier)) {
        throw new IllegalArgumentException("Flow identifier cannot be null or blank");
    }
    readLock.lock();
    try {
        // ensure the bucket exists
        final BucketEntity existingBucket = metadataService.getBucketById(bucketIdentifier);
        if (existingBucket == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        // ensure the flow exists
        final FlowEntity existingFlow = metadataService.getFlowById(flowIdentifier);
        if (existingFlow == null) {
            LOGGER.warn("The specified flow id [{}] does not exist.", flowIdentifier);
            throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
        }
        if (!existingBucket.getId().equals(existingFlow.getBucketId())) {
            throw new IllegalStateException("The requested flow is not located in the given bucket");
        }
        // get latest snapshot for the flow
        final FlowSnapshotEntity latestSnapshot = metadataService.getLatestSnapshot(existingFlow.getId());
        if (latestSnapshot == null) {
            throw new ResourceNotFoundException("The specified flow ID has no versions");
        }
        return DataModelMapper.map(existingBucket, latestSnapshot);
    } finally {
        readLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity)

Example 18 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class RegistryService method deleteFlowSnapshot.

public VersionedFlowSnapshotMetadata deleteFlowSnapshot(final String bucketIdentifier, final String flowIdentifier, final Integer version) {
    if (StringUtils.isBlank(bucketIdentifier)) {
        throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
    }
    if (StringUtils.isBlank(flowIdentifier)) {
        throw new IllegalArgumentException("Flow identifier cannot be null or blank");
    }
    if (version == null) {
        throw new IllegalArgumentException("Version cannot be null or blank");
    }
    writeLock.lock();
    try {
        // ensure the bucket exists
        final BucketEntity existingBucket = metadataService.getBucketById(bucketIdentifier);
        if (existingBucket == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        // ensure the flow exists
        final FlowEntity existingFlow = metadataService.getFlowById(flowIdentifier);
        if (existingFlow == null) {
            LOGGER.warn("The specified flow id [{}] does not exist.", flowIdentifier);
            throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
        }
        if (!existingBucket.getId().equals(existingFlow.getBucketId())) {
            throw new IllegalStateException("The requested flow is not located in the given bucket");
        }
        // ensure the snapshot exists
        final FlowSnapshotEntity snapshotEntity = metadataService.getFlowSnapshot(flowIdentifier, version);
        if (snapshotEntity == null) {
            throw new ResourceNotFoundException("Versioned flow snapshot does not exist for flow " + flowIdentifier + " and version " + version);
        }
        // delete the content of the snapshot
        flowPersistenceProvider.deleteFlowContent(bucketIdentifier, flowIdentifier, version);
        // delete the snapshot itself
        metadataService.deleteFlowSnapshot(snapshotEntity);
        return DataModelMapper.map(existingBucket, snapshotEntity);
    } finally {
        writeLock.unlock();
    }
}
Also used : BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity)

Example 19 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class AccessPolicyResource method getAccessPolicyForResource.

/**
 * Retrieve a specified access policy for a given (action, resource) pair.
 *
 * @param action the action, i.e. "read", "write"
 * @param rawResource the name of the resource as a raw string
 * @return An access policy.
 */
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("{action}/{resource: .+}")
@ApiOperation(value = "Gets an access policy for the specified action and resource", response = AccessPolicy.class, extensions = { @Extension(name = "access-policy", properties = { @ExtensionProperty(name = "action", value = "read"), @ExtensionProperty(name = "resource", value = "/policies") }) })
@ApiResponses({ @ApiResponse(code = 400, message = HttpStatusMessages.MESSAGE_400), @ApiResponse(code = 401, message = HttpStatusMessages.MESSAGE_401), @ApiResponse(code = 403, message = HttpStatusMessages.MESSAGE_403), @ApiResponse(code = 404, message = HttpStatusMessages.MESSAGE_404), @ApiResponse(code = 409, message = HttpStatusMessages.MESSAGE_409) })
public Response getAccessPolicyForResource(@ApiParam(value = "The request action.", allowableValues = "read, write, delete", required = true) @PathParam("action") final String action, @ApiParam(value = "The resource of the policy.", required = true) @PathParam("resource") final String rawResource) {
    verifyAuthorizerIsManaged();
    authorizeAccess(RequestAction.READ);
    // parse the action and resource type
    final RequestAction requestAction = RequestAction.valueOfValue(action);
    final String resource = "/" + rawResource;
    AccessPolicy accessPolicy = authorizationService.getAccessPolicy(resource, requestAction);
    if (accessPolicy == null) {
        throw new ResourceNotFoundException("No policy found for action='" + action + "', resource='" + resource + "'");
    }
    return generateOkResponse(accessPolicy).build();
}
Also used : RequestAction(org.apache.nifi.registry.security.authorization.RequestAction) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) AccessPolicy(org.apache.nifi.registry.authorization.AccessPolicy) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 20 with ResourceNotFoundException

use of org.apache.nifi.registry.exception.ResourceNotFoundException in project nifi-registry by apache.

the class TenantResource method updateUser.

/**
 * Updates a user.
 *
 * @param httpServletRequest request
 * @param identifier The id of the user to update
 * @param requestUser The user with updated fields.
 * @return The updated user
 */
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("users/{id}")
@ApiOperation(value = "Updates a user", notes = NON_GUARANTEED_ENDPOINT, response = User.class, extensions = { @Extension(name = "access-policy", properties = { @ExtensionProperty(name = "action", value = "write"), @ExtensionProperty(name = "resource", value = "/tenants") }) })
@ApiResponses({ @ApiResponse(code = 400, message = HttpStatusMessages.MESSAGE_400), @ApiResponse(code = 401, message = HttpStatusMessages.MESSAGE_401), @ApiResponse(code = 403, message = HttpStatusMessages.MESSAGE_403), @ApiResponse(code = 404, message = HttpStatusMessages.MESSAGE_404), @ApiResponse(code = 409, message = HttpStatusMessages.MESSAGE_409) })
public Response updateUser(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The user id.", required = true) @PathParam("id") final String identifier, @ApiParam(value = "The user configuration details.", required = true) final User requestUser) {
    verifyAuthorizerSupportsConfigurableUserGroups();
    authorizeAccess(RequestAction.WRITE);
    if (requestUser == null) {
        throw new IllegalArgumentException("User details must be specified when updating a user.");
    }
    if (!identifier.equals(requestUser.getIdentifier())) {
        throw new IllegalArgumentException(String.format("The user id in the request body (%s) does not equal the " + "user id of the requested resource (%s).", requestUser.getIdentifier(), identifier));
    }
    final User updatedUser = authorizationService.updateUser(requestUser);
    if (updatedUser == null) {
        logger.warn("The specified user id [{}] does not exist.", identifier);
        throw new ResourceNotFoundException("The specified user ID does not exist in this registry.");
    }
    return generateOkResponse(updatedUser).build();
}
Also used : User(org.apache.nifi.registry.authorization.User) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ResourceNotFoundException (org.apache.nifi.registry.exception.ResourceNotFoundException)23 BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)14 FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)12 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Consumes (javax.ws.rs.Consumes)9 Path (javax.ws.rs.Path)9 Produces (javax.ws.rs.Produces)9 FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)7 GET (javax.ws.rs.GET)4 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)4 TreeSet (java.util.TreeSet)3 DELETE (javax.ws.rs.DELETE)3 User (org.apache.nifi.registry.authorization.User)3 Bucket (org.apache.nifi.registry.bucket.Bucket)3 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2