org.apache.nifi.web.api.dto.TemplateDTO Java Examples

The following examples show how to use org.apache.nifi.web.api.dto.TemplateDTO. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JerseyProcessGroupClient.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public TemplateEntity uploadTemplate(
        final String processGroupId, final TemplateDTO templateDTO) throws NiFiClientException, IOException {
    if (StringUtils.isBlank(processGroupId)) {
        throw new IllegalArgumentException("Process group id cannot be null or blank");
    }

    if (templateDTO == null) {
        throw new IllegalArgumentException("The template dto cannot be null");
    }

    return executeAction("Error uploading template file", () -> {
        final WebTarget target = processGroupsTarget
                .path("{id}/templates/upload")
                .resolveTemplate("id", processGroupId)
                .register(MultiPartFeature.class);

        FormDataMultiPart form = new FormDataMultiPart();
        form.field("template", templateDTO, MediaType.TEXT_XML_TYPE);

        return getRequestBuilder(target).post(
                Entity.entity(form, MediaType.MULTIPART_FORM_DATA),
                TemplateEntity.class
        );
    });
}
 
Example #2
Source File: StandardProcessGroup.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void verifyCanAddTemplate(final String name) {
    // ensure the name is specified
    if (StringUtils.isBlank(name)) {
        throw new IllegalArgumentException("Template name cannot be blank.");
    }

    for (final Template template : getRoot().findAllTemplates()) {
        final TemplateDTO existingDto = template.getDetails();

        // ensure a template with this name doesnt already exist
        if (name.equals(existingDto.getName())) {
            throw new IllegalStateException(String.format("A template named '%s' already exists.", name));
        }
    }
}
 
Example #3
Source File: TemplateSerializer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * This method when called assumes the Framework Nar ClassLoader is in the
 * classloader hierarchy of the current context class loader.
 * @param dto the template dto to serialize
 * @return serialized representation of the DTO
 */
public static byte[] serialize(final TemplateDTO dto) {
    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final BufferedOutputStream bos = new BufferedOutputStream(baos);

        JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
        Marshaller marshaller = context.createMarshaller();
        XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(bos));
        marshaller.marshal(dto, writer);

        bos.flush();
        return baos.toByteArray(); //Note: For really large templates this could use a lot of heap space
    } catch (final IOException | JAXBException | XMLStreamException e) {
        throw new FlowSerializationException(e);
    }
}
 
Example #4
Source File: StandardTemplateDAO.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public Template createTemplate(final TemplateDTO templateDTO, final String groupId) {
    final ProcessGroup processGroup = flowController.getFlowManager().getGroup(groupId);
    if (processGroup == null) {
        throw new ResourceNotFoundException("Could not find Process Group with ID " + groupId);
    }

    verifyCanAddTemplate(templateDTO.getName(), groupId);

    TemplateUtils.scrubTemplate(templateDTO);
    TemplateUtils.escapeParameterReferences(templateDTO);

    final Template template = new Template(templateDTO);
    processGroup.addTemplate(template);

    return template;
}
 
Example #5
Source File: StandardFlowSynchronizer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private void addLocalTemplates(final Element processGroupElement, final ProcessGroup processGroup, final FlowEncodingVersion encodingVersion) {
    // Replace the templates with those from the proposed flow
    final List<Element> templateNodeList = getChildrenByTagName(processGroupElement, "template");
    if (templateNodeList != null) {
        for (final Element templateElement : templateNodeList) {
            final TemplateDTO templateDto = TemplateUtils.parseDto(templateElement);
            final Template template = new Template(templateDto);

            // If the Process Group does not have the template, add it.
            if (processGroup.getTemplate(template.getIdentifier()) == null) {
                processGroup.addTemplate(template);
            }
        }
    }

    final List<Element> childGroupElements = getChildrenByTagName(processGroupElement, "processGroup");
    for (final Element childGroupElement : childGroupElements) {
        final String childGroupId = getString(childGroupElement, "id");
        final ProcessGroup childGroup = processGroup.getProcessGroup(childGroupId);
        addLocalTemplates(childGroupElement, childGroup, encodingVersion);
    }
}
 
Example #6
Source File: TemplateSerializer.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * This method when called assumes the Framework Nar ClassLoader is in the
 * classloader hierarchy of the current context class loader.
 * @param dto the template dto to serialize
 * @return serialized representation of the DTO
 */
public static byte[] serialize(final TemplateDTO dto) {
    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final BufferedOutputStream bos = new BufferedOutputStream(baos);

        final Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(dto, bos);

        bos.flush();
        return baos.toByteArray(); //Note: For really large templates this could use a lot of heap space
    } catch (final IOException | JAXBException e) {
        throw new FlowSerializationException(e);
    }
}
 
Example #7
Source File: StandardFlowSynchronizer.java    From nifi with Apache License 2.0 6 votes vote down vote up
private void addLocalTemplates(final Element processGroupElement, final ProcessGroup processGroup) {
    // Replace the templates with those from the proposed flow
    final List<Element> templateNodeList = getChildrenByTagName(processGroupElement, "template");
    if (templateNodeList != null) {
        for (final Element templateElement : templateNodeList) {
            final TemplateDTO templateDto = TemplateUtils.parseDto(templateElement);
            final Template template = new Template(templateDto);

            // If the Process Group does not have the template, add it.
            if (processGroup.getTemplate(template.getIdentifier()) == null) {
                processGroup.addTemplate(template);
            }
        }
    }

    final List<Element> childGroupElements = getChildrenByTagName(processGroupElement, "processGroup");
    for (final Element childGroupElement : childGroupElements) {
        final String childGroupId = getString(childGroupElement, "id");
        final ProcessGroup childGroup = processGroup.getProcessGroup(childGroupId);
        addLocalTemplates(childGroupElement, childGroup);
    }
}
 
Example #8
Source File: TemplateUtils.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
public static List<Template> parseTemplateStream(final byte[] bytes) {
    final List<Template> templates = new ArrayList<>();

    try (final InputStream rawIn = new ByteArrayInputStream(bytes);
        final DataInputStream in = new DataInputStream(rawIn)) {

        while (isMoreData(in)) {
            final int length = in.readInt();
            final byte[] buffer = new byte[length];
            StreamUtils.fillBuffer(in, buffer, true);
            final TemplateDTO dto = TemplateDeserializer.deserialize(new ByteArrayInputStream(buffer));
            templates.add(new Template(dto));
        }
    } catch (final IOException e) {
        throw new RuntimeException("Could not parse bytes", e);  // won't happen because of the types of streams being used
    }

    return templates;
}
 
Example #9
Source File: TemplateUtils.java    From nifi with Apache License 2.0 6 votes vote down vote up
public static TemplateDTO parseDto(final Element templateElement) {
    try {
        final DOMSource domSource = new DOMSource(templateElement);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final StreamResult streamResult = new StreamResult(baos);

        // need to stream the template element as the TemplateDeserializer.deserialize operation needs to re-parse
        // in order to apply explicit properties on the XMLInputFactory
        final TransformerFactory transformerFactory = TransformerFactory.newInstance();
        final Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(domSource, streamResult);

        return parseDto(baos.toByteArray());
    } catch (final Exception e) {
        throw new RuntimeException("Could not parse XML as a valid template", e);
    }
}
 
Example #10
Source File: StandardNiFiServiceFacade.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public TemplateDTO importTemplate(final TemplateDTO templateDTO, final String groupId, final Optional<String> idGenerationSeed) {
    // ensure id is set
    final String uuid = idGenerationSeed.isPresent() ? (UUID.nameUUIDFromBytes(idGenerationSeed.get().getBytes(StandardCharsets.UTF_8))).toString() : UUID.randomUUID().toString();
    templateDTO.setId(uuid);

    // mark the timestamp
    templateDTO.setTimestamp(new Date());

    // ensure default values are populated
    ensureDefaultPropertyValuesArePopulated(templateDTO.getSnippet());

    // import the template
    final Template template = templateDAO.importTemplate(templateDTO, groupId);

    // save the flow
    controllerFacade.save();

    // return the template dto
    return dtoFactory.createTemplateDTO(template);
}
 
Example #11
Source File: DownloadTemplate.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public TemplateResult doExecute(final NiFiClient client, final Properties properties)
        throws NiFiClientException, IOException, CommandException, MissingOptionException {
    final String templateId = getRequiredArg(properties, CommandOption.TEMPLATE_ID);
    final TemplatesClient templatesClient = client.getTemplatesClient();

    final TemplateDTO templateEntityResult = templatesClient.getTemplate(templateId);

    // currently export doesn't use the ResultWriter concept, it always writes JSON
    // destination will be a file if outputFile is specified, otherwise it will be the output stream of the CLI
    final String outputFile;
    if (properties.containsKey(CommandOption.OUTPUT_FILE.getLongName())) {
        outputFile = properties.getProperty(CommandOption.OUTPUT_FILE.getLongName());
    } else {
        outputFile = null;
    }

    return new TemplateResult(templateEntityResult, outputFile);
}
 
Example #12
Source File: FlowControllerSchemaFunction.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
@Override
public FlowControllerSchema apply(TemplateDTO templateDTO) {
    Map<String, Object> map = new HashMap<>();
    map.put(CommonPropertyKeys.NAME_KEY, templateDTO.getName());
    map.put(CommonPropertyKeys.COMMENT_KEY, templateDTO.getDescription());
    return new FlowControllerSchema(map);
}
 
Example #13
Source File: StandardNiFiServiceFacade.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public TemplateDTO createTemplate(final String name, final String description, final String snippetId, final String groupId, final Optional<String> idGenerationSeed) {
    // get the specified snippet
    final Snippet snippet = snippetDAO.getSnippet(snippetId);

    // create the template
    final TemplateDTO templateDTO = new TemplateDTO();
    templateDTO.setName(name);
    templateDTO.setDescription(description);
    templateDTO.setTimestamp(new Date());
    templateDTO.setSnippet(snippetUtils.populateFlowSnippet(snippet, true, true, true));
    templateDTO.setEncodingVersion(TemplateDTO.MAX_ENCODING_VERSION);

    // set the id based on the specified seed
    final String uuid = idGenerationSeed.isPresent() ? (UUID.nameUUIDFromBytes(idGenerationSeed.get().getBytes(StandardCharsets.UTF_8))).toString() : UUID.randomUUID().toString();
    templateDTO.setId(uuid);

    // create the template
    final Template template = templateDAO.createTemplate(templateDTO, groupId);

    // drop the snippet
    snippetDAO.dropSnippet(snippetId);

    // save the flow
    controllerFacade.save();

    return dtoFactory.createTemplateDTO(template);
}
 
Example #14
Source File: TemplateUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * If template was serialized in a version before Parameters were supported, ensures that any reference to a
 * Parameter is escaped so that the value is treated as a literal value.
 * @param templateDto the template
 */
public static void escapeParameterReferences(final TemplateDTO templateDto) {
    final String encodingVersion = templateDto.getEncodingVersion();
    if (encodingVersion == null) {
        escapeParameterReferences(templateDto.getSnippet());
    } else {
        switch (encodingVersion) {
            case "1.0":
            case "1.1":
            case "1.2":
                escapeParameterReferences(templateDto.getSnippet());
                break;
        }
    }
}
 
Example #15
Source File: StandardNiFiServiceFacade.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public TemplateDTO exportTemplate(final String id) {
    final Template template = templateDAO.getTemplate(id);
    final TemplateDTO templateDetails = template.getDetails();

    final TemplateDTO templateDTO = dtoFactory.createTemplateDTO(template);
    templateDTO.setSnippet(dtoFactory.copySnippetContents(templateDetails.getSnippet()));
    return templateDTO;
}
 
Example #16
Source File: StandardTemplateDAO.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Template createTemplate(TemplateDTO templateDTO, String groupId) {
    final ProcessGroup processGroup = flowController.getGroup(groupId);
    if (processGroup == null) {
        throw new ResourceNotFoundException("Could not find Process Group with ID " + groupId);
    }

    verifyAdd(templateDTO.getName(), processGroup);

    TemplateUtils.scrubTemplate(templateDTO);
    final Template template = new Template(templateDTO);
    processGroup.addTemplate(template);

    return template;
}
 
Example #17
Source File: StandardFlowSynchronizer.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void addTemplates(final Element processGroupElement, final ProcessGroup processGroup) {
    final List<Element> templateNodeList = getChildrenByTagName(processGroupElement, "template");
    for (final Element templateNode : templateNodeList) {
        final TemplateDTO templateDTO = TemplateUtils.parseDto(templateNode);
        final Template template = new Template(templateDTO);
        processGroup.addTemplate(template);
    }
}
 
Example #18
Source File: TemplateUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static TemplateDTO parseDto(final byte[] bytes) {
    try (final InputStream in = new ByteArrayInputStream(bytes)) {
        return TemplateDeserializer.deserialize(in);
    } catch (final IOException ioe) {
        throw new RuntimeException("Could not parse bytes as template", ioe);
    }
}
 
Example #19
Source File: StandardNiFiServiceFacade.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Set<TemplateEntity> getTemplates() {
    return templateDAO.getTemplates().stream()
            .map(template -> {
                final TemplateDTO dto = dtoFactory.createTemplateDTO(template);
                final PermissionsDTO permissions = dtoFactory.createPermissionsDto(template);

                final TemplateEntity entity = new TemplateEntity();
                entity.setId(dto.getId());
                entity.setPermissions(permissions);
                entity.setTemplate(dto);
                return entity;
            }).collect(Collectors.toSet());
}
 
Example #20
Source File: TemplateDeserializer.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static TemplateDTO deserialize(final StreamSource source) {
    try {
        final XMLStreamReader xsr = XmlUtils.createSafeReader(source);
        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        final JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(xsr, TemplateDTO.class);
        final TemplateDTO templateDto = templateElement.getValue();

        return templateDto;
    } catch (final JAXBException | XMLStreamException e) {
        throw new FlowSerializationException(e);
    }
}
 
Example #21
Source File: StandardAuthorizableLookup.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public TemplateAuthorizable getTemplate(final String id) {
    final Template template = templateDAO.getTemplate(id);
    final TemplateDTO contents = template.getDetails();

    // templates are immutable so we can pre-compute all encapsulated processors and controller services
    final Set<ConfigurableComponentAuthorizable> processors = new HashSet<>();
    final Set<ConfigurableComponentAuthorizable> controllerServices = new HashSet<>();

    // find all processors and controller services
    createTemporaryProcessorsAndControllerServices(contents.getSnippet(), processors, controllerServices);

    return new TemplateAuthorizable() {
        @Override
        public Authorizable getAuthorizable() {
            return template;
        }

        @Override
        public Set<ConfigurableComponentAuthorizable> getEncapsulatedProcessors() {
            return processors;
        }

        @Override
        public Set<ConfigurableComponentAuthorizable> getEncapsulatedControllerServices() {
            return controllerServices;
        }
    };
}
 
Example #22
Source File: TemplateDeserializer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public static TemplateDTO deserialize(final InputStream inStream) {
    try {
        JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(new StreamSource(inStream), TemplateDTO.class);
        return templateElement.getValue();
    } catch (final JAXBException e) {
        throw new FlowSerializationException(e);
    }
}
 
Example #23
Source File: JerseyTemplatesClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public TemplateDTO getTemplate(final String templateId) throws NiFiClientException, IOException {
    if (StringUtils.isBlank(templateId)) {
        throw new IllegalArgumentException("Template id cannot be null");
    }

    return executeAction("Error retrieving template", () -> {
        final WebTarget target = templatesTarget
                .path("{id}/download")
                .resolveTemplate("id", templateId);
        return getRequestBuilder(target).get(TemplateDTO.class);
    });
}
 
Example #24
Source File: TemplateUtils.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public static TemplateDTO parseDto(final Element templateElement) {
    try {
        JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return unmarshaller.unmarshal(new DOMSource(templateElement), TemplateDTO.class).getValue();
    } catch (final Exception e) {
        throw new RuntimeException("Could not parse XML as a valid template", e);
    }
}
 
Example #25
Source File: FlowControllerSchemaTest.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    dto = new TemplateDTO();

    dto.setName(testName);
    dto.setDescription(testComment);

    map = new HashMap<>();

    map.put(CommonPropertyKeys.NAME_KEY, testName);
    map.put(CommonPropertyKeys.COMMENT_KEY, testComment);
}
 
Example #26
Source File: TemplateUtils.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public static TemplateDTO parseDto(final byte[] bytes) {
    try (final InputStream in = new ByteArrayInputStream(bytes)) {
        return TemplateDeserializer.deserialize(in);
    } catch (final IOException ioe) {
        throw new RuntimeException("Could not parse bytes as template", ioe); // won't happen because of the types of streams being used
    }
}
 
Example #27
Source File: ConfigMain.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
public static ConfigSchema transformTemplateToSchema(InputStream source) throws JAXBException, IOException {
    try {
        TemplateDTO templateDTO = (TemplateDTO) JAXBContext.newInstance(TemplateDTO.class).createUnmarshaller().unmarshal(source);

        FlowSnippetDTOEnricher enricher = new FlowSnippetDTOEnricher();
        enricher.enrich(templateDTO.getSnippet(), templateDTO.getEncodingVersion());

        ConfigSchema configSchema = new ConfigSchemaFunction().apply(templateDTO);
        return configSchema;
    } finally {
        source.close();
    }
}
 
Example #28
Source File: TemplatesResult.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeSimpleResult(final PrintStream output) throws IOException {
    final Collection<TemplateEntity> templateEntities = templatesEntity.getTemplates();
    if (templateEntities == null) {
        return;
    }

    final List<TemplateDTO> templateDTOS = templateEntities.stream()
        .map(TemplateEntity::getTemplate)
        .sorted(Comparator.comparing(TemplateDTO::getGroupId))
        .collect(Collectors.toList());

    final Table table = new Table.Builder()
        .column("#", 1, 4, false)
        .column("Name", 5, 40, false)
        .column("ID", 36, 36, false)
        .column("Group ID", 36, 36, false)
        .build();

    for (int i = 0; i < templateDTOS.size(); i++) {
        final TemplateDTO templateDTO = templateDTOS.get(i);
        table.addRow(
            String.valueOf(i + 1),
            templateDTO.getName(),
            templateDTO.getId(),
            templateDTO.getGroupId()
        );
    }

    final TableWriter tableWriter = new DynamicTableWriter();
    tableWriter.write(table, output);
}
 
Example #29
Source File: StandardFlowService.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * In NiFi 0.x, templates were stored in a templates directory as separate
 * files. They are now stored in the flow itself. If there already are
 * templates in that directory, though, we want to restore them.
 *
 * @return the templates found in the templates directory
 * @throws IOException if unable to read from the file system
 */
public List<Template> loadTemplates() throws IOException {
    final Path templatePath = nifiProperties.getTemplateDirectory();

    final File[] files = templatePath.toFile().listFiles(pathname -> {
        final String lowerName = pathname.getName().toLowerCase();
        return lowerName.endsWith(".template") || lowerName.endsWith(".xml");
    });

    if (files == null) {
        return Collections.emptyList();
    }

    final List<Template> templates = new ArrayList<>();
    for (final File file : files) {
        try (final FileInputStream fis = new FileInputStream(file);
                final BufferedInputStream bis = new BufferedInputStream(fis)) {

            final TemplateDTO templateDto;
            try {
                templateDto = TemplateDeserializer.deserialize(bis);
            } catch (final Exception e) {
                logger.error("Unable to interpret " + file + " as a Template. Skipping file.");
                continue;
            }

            if (templateDto.getId() == null) {
                // If there is no ID assigned, we need to assign one. We do this by generating
                // an ID from the name. This is because we know that Template Names are unique
                // and are consistent across all nodes in the cluster.
                final String uuid = UUID.nameUUIDFromBytes(templateDto.getName().getBytes(StandardCharsets.UTF_8)).toString();
                templateDto.setId(uuid);
            }

            final Template template = new Template(templateDto);
            templates.add(template);
        }
    }

    return templates;
}
 
Example #30
Source File: TemplateResult.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public TemplateDTO getResult() {
    return templateDTO;
}