Search in sources :

Example 16 with NiFiWebConfigurationContext

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

the class TestTransformJSONResource method testValidateWithInvalidSpec.

@Test
public void testValidateWithInvalidSpec() {
    final NiFiWebConfigurationContext niFiWebConfigurationContext = mock(NiFiWebConfigurationContext.class);
    final Map<String, String> properties = new HashMap<>();
    properties.put("jolt-transform", "jolt-transform-chain");
    final ComponentDetails componentDetails = new ComponentDetails.Builder().properties(properties).build();
    Mockito.when(servletContext.getAttribute(Mockito.anyString())).thenReturn(niFiWebConfigurationContext);
    Mockito.when(niFiWebConfigurationContext.getComponentDetails(any(NiFiWebRequestContext.class))).thenReturn(componentDetails);
    JoltSpecificationDTO joltSpecificationDTO = new JoltSpecificationDTO("jolt-transform-chain", "[]");
    ValidationDTO validate = client().target(getBaseUri()).path("/standard/transformjson/validate").request().post(Entity.json(joltSpecificationDTO), ValidationDTO.class);
    assertNotNull(validate);
    assertTrue(!validate.isValid());
}
Also used : HashMap(java.util.HashMap) JoltSpecificationDTO(org.apache.nifi.web.standard.api.transformjson.dto.JoltSpecificationDTO) ValidationDTO(org.apache.nifi.web.standard.api.transformjson.dto.ValidationDTO) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) ComponentDetails(org.apache.nifi.web.ComponentDetails) NiFiWebRequestContext(org.apache.nifi.web.NiFiWebRequestContext) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 17 with NiFiWebConfigurationContext

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

the class TestTransformJSONResource method testValidateWithCustomInvalidSpec.

@Test
public void testValidateWithCustomInvalidSpec() {
    final NiFiWebConfigurationContext niFiWebConfigurationContext = mock(NiFiWebConfigurationContext.class);
    final Map<String, String> properties = new HashMap<>();
    properties.put("jolt-transform", "jolt-transform-custom");
    final ComponentDetails componentDetails = new ComponentDetails.Builder().properties(properties).build();
    Mockito.when(servletContext.getAttribute(Mockito.anyString())).thenReturn(niFiWebConfigurationContext);
    Mockito.when(niFiWebConfigurationContext.getComponentDetails(any(NiFiWebRequestContext.class))).thenReturn(componentDetails);
    JoltSpecificationDTO joltSpecificationDTO = new JoltSpecificationDTO("jolt-transform-custom", "{ \"operation\": \"default\", \"spec\":{ \"custom-id\" :4 }}");
    joltSpecificationDTO.setCustomClass("TestCustomJoltTransform");
    joltSpecificationDTO.setModules("src/test/resources/TestTransformJSONResource/TestCustomJoltTransform.jar");
    ValidationDTO validate = client().target(getBaseUri()).path("/standard/transformjson/validate").request().post(Entity.json(joltSpecificationDTO), ValidationDTO.class);
    assertNotNull(validate);
    assertTrue(!validate.isValid());
}
Also used : HashMap(java.util.HashMap) JoltSpecificationDTO(org.apache.nifi.web.standard.api.transformjson.dto.JoltSpecificationDTO) ValidationDTO(org.apache.nifi.web.standard.api.transformjson.dto.ValidationDTO) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) ComponentDetails(org.apache.nifi.web.ComponentDetails) NiFiWebRequestContext(org.apache.nifi.web.NiFiWebRequestContext) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 18 with NiFiWebConfigurationContext

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

the class RuleResource method saveCriteria.

private void saveCriteria(final NiFiWebConfigurationRequestContext requestContext, final Criteria criteria) {
    // serialize the criteria
    final String annotationData = CriteriaSerDe.serialize(criteria);
    // get the web context
    final NiFiWebConfigurationContext configurationContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
    try {
        // save the annotation data
        configurationContext.updateComponent(requestContext, annotationData, null);
    } catch (final InvalidRevisionException ire) {
        throw new WebApplicationException(ire, invalidRevision(ire.getMessage()));
    } catch (final Exception e) {
        final String message = String.format("Unable to save UpdateAttribute[id=%s] criteria: %s", requestContext.getId(), e);
        logger.error(message, e);
        throw new WebApplicationException(e, error(message));
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) NotFoundException(javax.ws.rs.NotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) InvalidRevisionException(org.apache.nifi.web.InvalidRevisionException) InvalidRevisionException(org.apache.nifi.web.InvalidRevisionException)

Example 19 with NiFiWebConfigurationContext

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

the class RuleResource method createRule.

@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules")
public Response createRule(@Context final UriInfo uriInfo, final RuleEntity requestEntity) {
    // get the web context
    final NiFiWebConfigurationContext configurationContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
    // ensure the rule has been specified
    if (requestEntity == null || requestEntity.getRule() == null) {
        throw new WebApplicationException(badRequest("The rule must be specified."));
    }
    final RuleDTO ruleDto = requestEntity.getRule();
    // ensure the id hasn't been specified
    if (ruleDto.getId() != null) {
        throw new WebApplicationException(badRequest("The rule id cannot be specified."));
    }
    // ensure there are some conditions
    if (ruleDto.getConditions() == null || ruleDto.getConditions().isEmpty()) {
        throw new WebApplicationException(badRequest("The rule conditions must be set."));
    }
    // ensure there are some actions
    if (ruleDto.getActions() == null || ruleDto.getActions().isEmpty()) {
        throw new WebApplicationException(badRequest("The rule actions must be set."));
    }
    // generate a new id
    final String uuid = UUID.randomUUID().toString();
    // build the request context
    final NiFiWebConfigurationRequestContext requestContext = getConfigurationRequestContext(requestEntity.getProcessorId(), requestEntity.getRevision(), requestEntity.getClientId());
    // load the criteria
    final Criteria criteria = getCriteria(configurationContext, requestContext);
    final UpdateAttributeModelFactory factory = new UpdateAttributeModelFactory();
    // create the new rule
    final Rule rule;
    try {
        rule = factory.createRule(ruleDto);
        rule.setId(uuid);
    } catch (final IllegalArgumentException iae) {
        throw new WebApplicationException(iae, badRequest(iae.getMessage()));
    }
    // add the rule
    criteria.addRule(rule);
    // save the criteria
    saveCriteria(requestContext, criteria);
    // create the response entity
    final RuleEntity responseEntity = new RuleEntity();
    responseEntity.setClientId(requestEntity.getClientId());
    responseEntity.setRevision(requestEntity.getRevision());
    responseEntity.setProcessorId(requestEntity.getProcessorId());
    responseEntity.setRule(DtoFactory.createRuleDTO(rule));
    // generate the response
    final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
    final ResponseBuilder response = Response.created(uriBuilder.path(uuid).build()).entity(responseEntity);
    return noCache(response).build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RuleDTO(org.apache.nifi.update.attributes.dto.RuleDTO) NiFiWebConfigurationRequestContext(org.apache.nifi.web.NiFiWebConfigurationRequestContext) UpdateAttributeModelFactory(org.apache.nifi.update.attributes.UpdateAttributeModelFactory) Criteria(org.apache.nifi.update.attributes.Criteria) Rule(org.apache.nifi.update.attributes.Rule) RuleEntity(org.apache.nifi.update.attributes.entity.RuleEntity) UriBuilder(javax.ws.rs.core.UriBuilder) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 20 with NiFiWebConfigurationContext

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

the class RuleResource method deleteRule.

@DELETE
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules/{id}")
public Response deleteRule(@PathParam("id") final String ruleId, @QueryParam("processorId") final String processorId, @QueryParam("clientId") final String clientId, @QueryParam("revision") final Long revision) {
    // get the web context
    final NiFiWebConfigurationContext configurationContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
    // build the web context config
    final NiFiWebConfigurationRequestContext requestContext = getConfigurationRequestContext(processorId, revision, clientId);
    // load the criteria and get the rule
    final Criteria criteria = getCriteria(configurationContext, requestContext);
    final Rule rule = criteria.getRule(ruleId);
    if (rule == null) {
        throw new NotFoundException();
    }
    // delete the rule
    criteria.deleteRule(rule);
    // save the criteria
    saveCriteria(requestContext, criteria);
    // create the response entity
    final RulesEntity responseEntity = new RulesEntity();
    responseEntity.setClientId(clientId);
    responseEntity.setRevision(revision);
    responseEntity.setProcessorId(processorId);
    // generate the response
    final ResponseBuilder response = Response.ok(responseEntity);
    return noCache(response).build();
}
Also used : NiFiWebConfigurationRequestContext(org.apache.nifi.web.NiFiWebConfigurationRequestContext) NotFoundException(javax.ws.rs.NotFoundException) Criteria(org.apache.nifi.update.attributes.Criteria) Rule(org.apache.nifi.update.attributes.Rule) RulesEntity(org.apache.nifi.update.attributes.entity.RulesEntity) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Aggregations

NiFiWebConfigurationContext (org.apache.nifi.web.NiFiWebConfigurationContext)20 Path (javax.ws.rs.Path)10 Produces (javax.ws.rs.Produces)10 ComponentDetails (org.apache.nifi.web.ComponentDetails)10 NiFiWebRequestContext (org.apache.nifi.web.NiFiWebRequestContext)9 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)8 Criteria (org.apache.nifi.update.attributes.Criteria)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)6 Rule (org.apache.nifi.update.attributes.Rule)6 NiFiWebConfigurationRequestContext (org.apache.nifi.web.NiFiWebConfigurationRequestContext)6 JerseyTest (org.glassfish.jersey.test.JerseyTest)6 GET (javax.ws.rs.GET)5 RuleDTO (org.apache.nifi.update.attributes.dto.RuleDTO)5 Consumes (javax.ws.rs.Consumes)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 JoltSpecificationDTO (org.apache.nifi.web.standard.api.transformjson.dto.JoltSpecificationDTO)4 ValidationDTO (org.apache.nifi.web.standard.api.transformjson.dto.ValidationDTO)4 NotFoundException (javax.ws.rs.NotFoundException)3 PUT (javax.ws.rs.PUT)3