Search in sources :

Example 31 with Revision

use of org.apache.nifi.web.Revision in project nifi by apache.

the class ProcessorResource method deleteProcessor.

/**
 * Removes the specified processor.
 *
 * @param httpServletRequest request
 * @param version            The revision is used to verify the client is working with the latest version of the flow.
 * @param clientId           Optional client id. If the client id is not specified, a new one will be generated. This value (whether specified or generated) is included in the response.
 * @param id                 The id of the processor to remove.
 * @return A processorEntity.
 * @throws InterruptedException if interrupted
 */
@DELETE
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}")
@ApiOperation(value = "Deletes a processor", response = ProcessorEntity.class, authorizations = { @Authorization(value = "Write - /processors/{uuid}"), @Authorization(value = "Write - Parent Process Group - /process-groups/{uuid}"), @Authorization(value = "Read - any referenced Controller Services - /controller-services/{uuid}") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response deleteProcessor(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The revision is used to verify the client is working with the latest version of the flow.", required = false) @QueryParam(VERSION) final LongParameter version, @ApiParam(value = "If the client id is not specified, new one will be generated. This value (whether specified or generated) is included in the response.", required = false) @QueryParam(CLIENT_ID) @DefaultValue(StringUtils.EMPTY) final ClientIdParameter clientId, @ApiParam(value = "The processor id.", required = true) @PathParam("id") final String id) throws InterruptedException {
    if (isReplicateRequest()) {
        return replicate(HttpMethod.DELETE);
    }
    final ProcessorEntity requestProcessorEntity = new ProcessorEntity();
    requestProcessorEntity.setId(id);
    final Revision requestRevision = new Revision(version == null ? null : version.getLong(), clientId.getClientId(), id);
    return withWriteLock(serviceFacade, requestProcessorEntity, requestRevision, lookup -> {
        final ComponentAuthorizable processor = lookup.getProcessor(id);
        // ensure write permission to the processor
        processor.getAuthorizable().authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
        // ensure write permission to the parent process group
        processor.getAuthorizable().getParentAuthorizable().authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
        // verify any referenced services
        AuthorizeControllerServiceReference.authorizeControllerServiceReferences(processor, authorizer, lookup, false);
    }, () -> serviceFacade.verifyDeleteProcessor(id), (revision, processorEntity) -> {
        // delete the processor
        final ProcessorEntity entity = serviceFacade.deleteProcessor(revision, processorEntity.getId());
        // generate the response
        return generateOkResponse(entity).build();
    });
}
Also used : ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Revision(org.apache.nifi.web.Revision) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 32 with Revision

use of org.apache.nifi.web.Revision in project nifi by apache.

the class ProcessorResource method updateProcessor.

/**
 * Updates the specified processor with the specified values.
 *
 * @param httpServletRequest request
 * @param id                 The id of the processor to update.
 * @param requestProcessorEntity    A processorEntity.
 * @return A processorEntity.
 * @throws InterruptedException if interrupted
 */
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id}")
@ApiOperation(value = "Updates a processor", response = ProcessorEntity.class, authorizations = { @Authorization(value = "Write - /processors/{uuid}"), @Authorization(value = "Read - any referenced Controller Services if this request changes the reference - /controller-services/{uuid}") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response updateProcessor(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The processor id.", required = true) @PathParam("id") final String id, @ApiParam(value = "The processor configuration details.", required = true) final ProcessorEntity requestProcessorEntity) throws InterruptedException {
    if (requestProcessorEntity == null || requestProcessorEntity.getComponent() == null) {
        throw new IllegalArgumentException("Processor details must be specified.");
    }
    if (requestProcessorEntity.getRevision() == null) {
        throw new IllegalArgumentException("Revision must be specified.");
    }
    // ensure the same id is being used
    final ProcessorDTO requestProcessorDTO = requestProcessorEntity.getComponent();
    if (!id.equals(requestProcessorDTO.getId())) {
        throw new IllegalArgumentException(String.format("The processor id (%s) in the request body does " + "not equal the processor id of the requested resource (%s).", requestProcessorDTO.getId(), id));
    }
    final PositionDTO proposedPosition = requestProcessorDTO.getPosition();
    if (proposedPosition != null) {
        if (proposedPosition.getX() == null || proposedPosition.getY() == null) {
            throw new IllegalArgumentException("The x and y coordinate of the proposed position must be specified.");
        }
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.PUT, requestProcessorEntity);
    }
    // handle expects request (usually from the cluster manager)
    final Revision requestRevision = getRevision(requestProcessorEntity, id);
    return withWriteLock(serviceFacade, requestProcessorEntity, requestRevision, lookup -> {
        final NiFiUser user = NiFiUserUtils.getNiFiUser();
        final ComponentAuthorizable authorizable = lookup.getProcessor(id);
        authorizable.getAuthorizable().authorize(authorizer, RequestAction.WRITE, user);
        final ProcessorConfigDTO config = requestProcessorDTO.getConfig();
        if (config != null) {
            AuthorizeControllerServiceReference.authorizeControllerServiceReferences(config.getProperties(), authorizable, authorizer, lookup);
        }
    }, () -> serviceFacade.verifyUpdateProcessor(requestProcessorDTO), (revision, processorEntity) -> {
        final ProcessorDTO processorDTO = processorEntity.getComponent();
        // update the processor
        final ProcessorEntity entity = serviceFacade.updateProcessor(revision, processorDTO);
        populateRemainingProcessorEntityContent(entity);
        return generateOkResponse(entity).build();
    });
}
Also used : ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) Revision(org.apache.nifi.web.Revision) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO) 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)

Example 33 with Revision

use of org.apache.nifi.web.Revision in project nifi by apache.

the class StandardSnippetDAO method mapDtoToRevision.

private Map<String, Revision> mapDtoToRevision(final Map<String, RevisionDTO> revisionMap) {
    final Map<String, Revision> revisions = new HashMap<>(revisionMap.size());
    for (final Map.Entry<String, RevisionDTO> entry : revisionMap.entrySet()) {
        final RevisionDTO revisionDto = entry.getValue();
        final Revision revision = new Revision(revisionDto.getVersion(), revisionDto.getClientId(), entry.getKey());
        revisions.put(entry.getKey(), revision);
    }
    return revisions;
}
Also used : Revision(org.apache.nifi.web.Revision) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO)

Example 34 with Revision

use of org.apache.nifi.web.Revision in project nifi by apache.

the class TestGetJMSQueue method testSendObjectToQueue.

@org.junit.Ignore
public void testSendObjectToQueue() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
    runner.setProperty(JmsProperties.URL, "tcp://localhost:61616");
    runner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE);
    runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
    runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
    WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runner.getProcessContext(), true);
    final Session jmsSession = wrappedProducer.getSession();
    final MessageProducer producer = wrappedProducer.getProducer();
    // Revision class is used because test just needs any Serializable class in core NiFi
    final ObjectMessage message = jmsSession.createObjectMessage(new Revision(1L, "ID", "COMP_ID"));
    producer.send(message);
    jmsSession.commit();
    producer.close();
    jmsSession.close();
}
Also used : Revision(org.apache.nifi.web.Revision) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) TestRunner(org.apache.nifi.util.TestRunner) ObjectMessage(javax.jms.ObjectMessage) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 35 with Revision

use of org.apache.nifi.web.Revision in project nifi by apache.

the class NaiveRevisionManager method updateRevision.

@Override
public <T> RevisionUpdate<T> updateRevision(final RevisionClaim originalClaim, final NiFiUser user, final UpdateRevisionTask<T> task) throws ExpiredRevisionClaimException {
    Objects.requireNonNull(user);
    logger.debug("Attempting to update revision using {}", originalClaim);
    final List<Revision> revisionList = new ArrayList<>(originalClaim.getRevisions());
    revisionList.sort(new RevisionComparator());
    for (final Revision revision : revisionList) {
        final Revision currentRevision = getRevision(revision.getComponentId());
        final boolean verified = revision.equals(currentRevision);
        if (!verified) {
            // Throw an Exception indicating that we failed to obtain the locks
            throw new InvalidRevisionException("Invalid Revision was given for component with ID '" + revision.getComponentId() + "'");
        }
    }
    // We successfully verified all revisions.
    logger.debug("Successfully verified Revision Claim for all revisions");
    RevisionUpdate<T> updatedComponent = null;
    try {
        updatedComponent = task.update();
    } finally {
        // Release the lock that we are holding and update the revision.
        // To do this, we need to map the old revision to the new revision
        // so that we have an efficient way to lookup the pairing, so that
        // we can easily obtain the old revision and the new revision for
        // the same component in order to call #unlock on the RevisionLock
        final Map<Revision, Revision> updatedRevisions = new HashMap<>();
        final Map<String, Revision> revisionsByComponentId = new HashMap<>();
        for (final Revision revision : revisionList) {
            updatedRevisions.put(revision, revision);
            revisionsByComponentId.put(revision.getComponentId(), revision);
        }
        if (updatedComponent != null) {
            for (final Revision updatedRevision : updatedComponent.getUpdatedRevisions()) {
                final Revision oldRevision = revisionsByComponentId.get(updatedRevision.getComponentId());
                if (oldRevision != null) {
                    updatedRevisions.put(oldRevision, updatedRevision);
                }
            }
        }
        for (final Revision revision : revisionList) {
            final Revision updatedRevision = updatedRevisions.get(revision);
            revisionMap.put(updatedRevision.getComponentId(), updatedRevision);
            if (updatedRevision.getVersion() != revision.getVersion()) {
                logger.debug("Unlocked Revision {} and updated associated Version to {}", revision, updatedRevision.getVersion());
            } else {
                logger.debug("Unlocked Revision {} without updating Version", revision);
            }
        }
    }
    return updatedComponent;
}
Also used : Revision(org.apache.nifi.web.Revision) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvalidRevisionException(org.apache.nifi.web.InvalidRevisionException)

Aggregations

Revision (org.apache.nifi.web.Revision)73 ApiOperation (io.swagger.annotations.ApiOperation)61 ApiResponses (io.swagger.annotations.ApiResponses)61 Consumes (javax.ws.rs.Consumes)61 Produces (javax.ws.rs.Produces)61 Path (javax.ws.rs.Path)60 Authorizable (org.apache.nifi.authorization.resource.Authorizable)51 PUT (javax.ws.rs.PUT)30 ComponentAuthorizable (org.apache.nifi.authorization.ComponentAuthorizable)30 POST (javax.ws.rs.POST)25 DELETE (javax.ws.rs.DELETE)24 ProcessGroupAuthorizable (org.apache.nifi.authorization.ProcessGroupAuthorizable)21 RevisionDTO (org.apache.nifi.web.api.dto.RevisionDTO)21 PositionDTO (org.apache.nifi.web.api.dto.PositionDTO)19 SnippetAuthorizable (org.apache.nifi.authorization.SnippetAuthorizable)17 NiFiUser (org.apache.nifi.authorization.user.NiFiUser)17 HashMap (java.util.HashMap)15 Map (java.util.Map)15 Set (java.util.Set)15 Collectors (java.util.stream.Collectors)15