Search in sources :

Example 1 with TemplatesEntity

use of org.apache.nifi.web.api.entity.TemplatesEntity in project nifi by apache.

the class TemplatesEndpointMerger method merge.

@Override
public final NodeResponse merge(final URI uri, final String method, final Set<NodeResponse> successfulResponses, final Set<NodeResponse> problematicResponses, final NodeResponse clientResponse) {
    if (!canHandle(uri, method)) {
        throw new IllegalArgumentException("Cannot use Endpoint Mapper of type " + getClass().getSimpleName() + " to map responses for URI " + uri + ", HTTP Method " + method);
    }
    final TemplatesEntity responseEntity = clientResponse.getClientResponse().readEntity(getEntityClass());
    // Find the templates that all nodes know about. We do this by mapping Template ID to Template and
    // then for each node, removing any template whose ID is not known to that node. After iterating over
    // all of the nodes, we are left with a Map whose contents are those Templates known by all nodes.
    Map<String, TemplateEntity> templatesById = null;
    for (final NodeResponse nodeResponse : successfulResponses) {
        final TemplatesEntity entity = nodeResponse == clientResponse ? responseEntity : nodeResponse.getClientResponse().readEntity(TemplatesEntity.class);
        final Set<TemplateEntity> templateEntities = entity.getTemplates();
        final Map<String, TemplateEntity> nodeTemplatesById = templateEntities.stream().collect(Collectors.toMap(ent -> ent.getId(), ent -> ent));
        if (templatesById == null) {
            // Create new HashMap so that the map that we have is modifiable.
            templatesById = new HashMap<>(nodeTemplatesById);
        } else {
            // Only keep templates that are known by this node.
            templatesById.keySet().retainAll(nodeTemplatesById.keySet());
        }
    }
    // Set the templates to the set of templates that all nodes know about
    responseEntity.setTemplates(new HashSet<>(templatesById.values()));
    // create a new client response
    return new NodeResponse(clientResponse, responseEntity);
}
Also used : HashSet(java.util.HashSet) TemplatesEntity(org.apache.nifi.web.api.entity.TemplatesEntity) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) Map(java.util.Map) Set(java.util.Set) TemplateEntity(org.apache.nifi.web.api.entity.TemplateEntity) HashMap(java.util.HashMap) EndpointResponseMerger(org.apache.nifi.cluster.coordination.http.EndpointResponseMerger) URI(java.net.URI) Pattern(java.util.regex.Pattern) Collectors(java.util.stream.Collectors) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) TemplatesEntity(org.apache.nifi.web.api.entity.TemplatesEntity) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) TemplateEntity(org.apache.nifi.web.api.entity.TemplateEntity)

Example 2 with TemplatesEntity

use of org.apache.nifi.web.api.entity.TemplatesEntity in project nifi by apache.

the class FlowResource method getTemplates.

// ---------
// templates
// ---------
/**
 * Retrieves all the of templates in this NiFi.
 *
 * @return A templatesEntity.
 */
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("templates")
@ApiOperation(value = "Gets all templates", response = TemplatesEntity.class, authorizations = { @Authorization(value = "Read - /flow") })
@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 = 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 getTemplates() {
    if (isReplicateRequest()) {
        return replicate(HttpMethod.GET);
    }
    // authorize access
    authorizeFlow();
    // get all the templates
    final Set<TemplateEntity> templates = serviceFacade.getTemplates();
    templateResource.populateRemainingTemplateEntitiesContent(templates);
    // create the response entity
    final TemplatesEntity entity = new TemplatesEntity();
    entity.setTemplates(templates);
    entity.setGenerated(new Date());
    // generate the response
    return generateOkResponse(entity).build();
}
Also used : TemplatesEntity(org.apache.nifi.web.api.entity.TemplatesEntity) TemplateEntity(org.apache.nifi.web.api.entity.TemplateEntity) Date(java.util.Date) 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)

Aggregations

TemplateEntity (org.apache.nifi.web.api.entity.TemplateEntity)2 TemplatesEntity (org.apache.nifi.web.api.entity.TemplatesEntity)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 URI (java.net.URI)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 EndpointResponseMerger (org.apache.nifi.cluster.coordination.http.EndpointResponseMerger)1 NodeResponse (org.apache.nifi.cluster.manager.NodeResponse)1 TemplateDTO (org.apache.nifi.web.api.dto.TemplateDTO)1