Example #1
0
  @Override
  public FlowSnippetDTO instantiateTemplate(
      String groupId, Double originX, Double originY, String templateId) {
    ProcessGroup group = locateProcessGroup(flowController, groupId);

    // get the template id and find the template
    Template template = flowController.getTemplate(templateId);

    // ensure the template could be found
    if (template == null) {
      throw new ResourceNotFoundException(
          String.format("Unable to locate template with id '%s'.", templateId));
    }

    try {
      // copy the template which pre-processes all ids
      TemplateDTO templateDetails = template.getDetails();
      FlowSnippetDTO snippet = snippetUtils.copy(templateDetails.getSnippet(), group);

      // reposition the template contents
      org.apache.nifi.util.SnippetUtils.moveSnippet(snippet, originX, originY);

      // instantiate the template into this group
      flowController.instantiateSnippet(group, snippet);

      return snippet;
    } catch (ProcessorInstantiationException pie) {
      throw new NiFiCoreException(
          String.format(
              "Unable to instantiate template because processor type '%s' is unknown to this NiFi.",
              StringUtils.substringAfterLast(pie.getMessage(), ".")));
    }
  }
Example #2
0
  @Override
  public FlowSnippetDTO instantiateTemplate(
      String groupId, Double originX, Double originY, String templateId, String idGenerationSeed) {
    ProcessGroup group = locateProcessGroup(flowController, groupId);

    // get the template id and find the template
    Template template = getTemplate(templateId);

    // ensure the template could be found
    if (template == null) {
      throw new ResourceNotFoundException(
          String.format("Unable to locate template with id '%s'.", templateId));
    }

    try {
      // copy the template which pre-processes all ids
      TemplateDTO templateDetails = template.getDetails();
      FlowSnippetDTO snippet =
          snippetUtils.copy(templateDetails.getSnippet(), group, idGenerationSeed);

      // calculate scaling factors based on the template encoding version
      // attempt to parse the encoding version
      final FlowEncodingVersion templateEncodingVersion =
          FlowEncodingVersion.parse(templateDetails.getEncodingVersion());
      // get the major version, or 0 if no version could be parsed
      int templateEncodingMajorVersion =
          templateEncodingVersion != null ? templateEncodingVersion.getMajorVersion() : 0;
      // based on the major version < 1, use the default scaling factors.  Otherwise, don't scale
      // (use factor of 1.0)
      double factorX =
          templateEncodingMajorVersion < 1 ? FlowController.DEFAULT_POSITION_SCALE_FACTOR_X : 1.0;
      double factorY =
          templateEncodingMajorVersion < 1 ? FlowController.DEFAULT_POSITION_SCALE_FACTOR_Y : 1.0;

      // reposition and scale the template contents
      org.apache.nifi.util.SnippetUtils.moveAndScaleSnippet(
          snippet, originX, originY, factorX, factorY);

      // find all the child process groups in each process group in the top level of this snippet
      final List<ProcessGroupDTO> childProcessGroups =
          org.apache.nifi.util.SnippetUtils.findAllProcessGroups(snippet);
      // scale (but don't reposition) child process groups
      childProcessGroups
          .stream()
          .forEach(
              processGroup ->
                  org.apache.nifi.util.SnippetUtils.scaleSnippet(
                      processGroup.getContents(), factorX, factorY));

      // instantiate the template into this group
      flowController.instantiateSnippet(group, snippet);

      return snippet;
    } catch (ProcessorInstantiationException pie) {
      throw new NiFiCoreException(
          String.format(
              "Unable to instantiate template because processor type '%s' is unknown to this NiFi.",
              StringUtils.substringAfterLast(pie.getMessage(), ".")));
    }
  }
Example #3
0
  /**
   * Retrieves the specified template.
   *
   * @param id The id of the template to retrieve
   * @return A templateEntity.
   */
  @GET
  @Consumes(MediaType.WILDCARD)
  @Produces(MediaType.APPLICATION_XML)
  @Path("{id}/download")
  @ApiOperation(
      value = "Exports a template",
      response = TemplateDTO.class,
      authorizations = {@Authorization(value = "Read - /templates/{uuid}", type = "")})
  @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 exportTemplate(
      @ApiParam(value = "The template id.", required = true) @PathParam("id") final String id) {

    if (isReplicateRequest()) {
      return replicate(HttpMethod.GET);
    }

    // authorize access
    serviceFacade.authorizeAccess(
        lookup -> {
          final Authorizable template = lookup.getTemplate(id);
          template.authorize(authorizer, RequestAction.READ, NiFiUserUtils.getNiFiUser());
        });

    // get the template
    final TemplateDTO template = serviceFacade.exportTemplate(id);

    // prune the template id
    template.setId(null);

    // determine the name of the attachement - possible issues with spaces in file names
    String attachmentName = template.getName();
    if (StringUtils.isBlank(attachmentName)) {
      attachmentName = "template";
    } else {
      attachmentName = attachmentName.replaceAll("\\s", "_");
    }

    // generate the response
    /*
     * Here instead of relying on default JAXB marshalling we are simply
     * serializing template to String (formatted, indented etc) and sending
     * it as part of the response.
     */
    String serializedTemplate =
        new String(TemplateSerializer.serialize(template), StandardCharsets.UTF_8);
    return generateOkResponse(serializedTemplate)
        .header(
            "Content-Disposition", String.format("attachment; filename=\"%s.xml\"", attachmentName))
        .build();
  }
Example #4
0
 /** Populates the uri for the specified template. */
 public TemplateDTO populateRemainingTemplateContent(TemplateDTO template) {
   // populate the template uri
   template.setUri(generateResourceUri("templates", template.getId()));
   return template;
 }