Search in sources :

Example 6 with TemplateDTO

use of org.apache.nifi.web.api.dto.TemplateDTO in project nifi by apache.

the class ProcessGroupResource method uploadTemplate.

/**
 * Imports the specified template.
 *
 * @param httpServletRequest request
 * @param in                 The template stream
 * @return A templateEntity or an errorResponse XML snippet.
 * @throws InterruptedException if interrupted
 */
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_XML)
@Path("{id}/templates/upload")
@ApiOperation(value = "Uploads a template", response = TemplateEntity.class, authorizations = { @Authorization(value = "Write - /process-groups/{uuid}") })
@ApiImplicitParams(value = { @ApiImplicitParam(name = "template", value = "The binary content of the template file being uploaded.", required = true, type = "file", paramType = "formData") })
@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 uploadTemplate(@Context final HttpServletRequest httpServletRequest, @Context final UriInfo uriInfo, @ApiParam(value = "The process group id.", required = true) @PathParam("id") final String groupId, @FormDataParam("template") final InputStream in) throws InterruptedException {
    // unmarshal the template
    final TemplateDTO template;
    try {
        JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        XMLStreamReader xsr = XmlUtils.createSafeReader(in);
        JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(xsr, TemplateDTO.class);
        template = templateElement.getValue();
    } catch (JAXBException jaxbe) {
        logger.warn("An error occurred while parsing a template.", jaxbe);
        String responseXml = String.format("<errorResponse status=\"%s\" statusText=\"The specified template is not in a valid format.\"/>", Response.Status.BAD_REQUEST.getStatusCode());
        return Response.status(Response.Status.OK).entity(responseXml).type("application/xml").build();
    } catch (IllegalArgumentException iae) {
        logger.warn("Unable to import template.", iae);
        String responseXml = String.format("<errorResponse status=\"%s\" statusText=\"%s\"/>", Response.Status.BAD_REQUEST.getStatusCode(), iae.getMessage());
        return Response.status(Response.Status.OK).entity(responseXml).type("application/xml").build();
    } catch (Exception e) {
        logger.warn("An error occurred while importing a template.", e);
        String responseXml = String.format("<errorResponse status=\"%s\" statusText=\"Unable to import the specified template: %s\"/>", Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
        return Response.status(Response.Status.OK).entity(responseXml).type("application/xml").build();
    }
    // build the response entity
    TemplateEntity entity = new TemplateEntity();
    entity.setTemplate(template);
    if (isReplicateRequest()) {
        // convert request accordingly
        final UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
        uriBuilder.segment("process-groups", groupId, "templates", "import");
        final URI importUri = uriBuilder.build();
        final Map<String, String> headersToOverride = new HashMap<>();
        headersToOverride.put("content-type", MediaType.APPLICATION_XML);
        // to the cluster nodes themselves.
        if (getReplicationTarget() == ReplicationTarget.CLUSTER_NODES) {
            return getRequestReplicator().replicate(HttpMethod.POST, importUri, entity, getHeaders(headersToOverride)).awaitMergedResponse().getResponse();
        } else {
            return getRequestReplicator().forwardToCoordinator(getClusterCoordinatorNode(), HttpMethod.POST, importUri, entity, getHeaders(headersToOverride)).awaitMergedResponse().getResponse();
        }
    }
    // otherwise import the template locally
    return importTemplate(httpServletRequest, groupId, entity);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) URI(java.net.URI) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) TemplateEntity(org.apache.nifi.web.api.entity.TemplateEntity) Unmarshaller(javax.xml.bind.Unmarshaller) UriBuilder(javax.ws.rs.core.UriBuilder) Path(javax.ws.rs.Path) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with TemplateDTO

use of org.apache.nifi.web.api.dto.TemplateDTO in project nifi by apache.

the class ProcessGroupResource method importTemplate.

/**
 * Imports the specified template.
 *
 * @param httpServletRequest request
 * @param requestTemplateEntity     A templateEntity.
 * @return A templateEntity.
 */
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("{id}/templates/import")
@ApiOperation(value = "Imports a template", response = TemplateEntity.class, authorizations = { @Authorization(value = "Write - /process-groups/{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 = 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 importTemplate(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The process group id.", required = true) @PathParam("id") final String groupId, final TemplateEntity requestTemplateEntity) {
    // verify the template was specified
    if (requestTemplateEntity == null || requestTemplateEntity.getTemplate() == null || requestTemplateEntity.getTemplate().getSnippet() == null) {
        throw new IllegalArgumentException("Template details must be specified.");
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.POST, requestTemplateEntity);
    }
    return withWriteLock(serviceFacade, requestTemplateEntity, lookup -> {
        final Authorizable processGroup = lookup.getProcessGroup(groupId).getAuthorizable();
        processGroup.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
    }, () -> serviceFacade.verifyCanAddTemplate(groupId, requestTemplateEntity.getTemplate().getName()), templateEntity -> {
        try {
            // import the template
            final TemplateDTO template = serviceFacade.importTemplate(templateEntity.getTemplate(), groupId, getIdGenerationSeed());
            templateResource.populateRemainingTemplateContent(template);
            // build the response entity
            TemplateEntity entity = new TemplateEntity();
            entity.setTemplate(template);
            // build the response
            return generateCreatedResponse(URI.create(template.getUri()), entity).build();
        } catch (IllegalArgumentException | IllegalStateException e) {
            logger.info("Unable to import template: " + e);
            String responseXml = String.format("<errorResponse status=\"%s\" statusText=\"%s\"/>", Response.Status.BAD_REQUEST.getStatusCode(), e.getMessage());
            return Response.status(Response.Status.OK).entity(responseXml).type("application/xml").build();
        } catch (Exception e) {
            logger.warn("An error occurred while importing a template.", e);
            String responseXml = String.format("<errorResponse status=\"%s\" statusText=\"Unable to import the specified template: %s\"/>", Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
            return Response.status(Response.Status.OK).entity(responseXml).type("application/xml").build();
        }
    });
}
Also used : TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) SnippetAuthorizable(org.apache.nifi.authorization.SnippetAuthorizable) TemplateContentsAuthorizable(org.apache.nifi.authorization.TemplateContentsAuthorizable) ProcessGroupAuthorizable(org.apache.nifi.authorization.ProcessGroupAuthorizable) TemplateEntity(org.apache.nifi.web.api.entity.TemplateEntity) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with TemplateDTO

use of org.apache.nifi.web.api.dto.TemplateDTO in project nifi by apache.

the class TemplateSerializerTest method validateDiffWithChangingComponentIdAndAdditionalElements.

@Test
public void validateDiffWithChangingComponentIdAndAdditionalElements() throws Exception {
    // Create initial template (TemplateDTO)
    FlowSnippetDTO snippet = new FlowSnippetDTO();
    Set<ProcessorDTO> procs = new HashSet<>();
    for (int i = 4; i > 0; i--) {
        ProcessorDTO procDTO = new ProcessorDTO();
        procDTO.setType("Processor" + i + ".class");
        procDTO.setId(ComponentIdGenerator.generateId().toString());
        procs.add(procDTO);
    }
    snippet.setProcessors(procs);
    TemplateDTO origTemplate = new TemplateDTO();
    origTemplate.setDescription("MyTemplate");
    origTemplate.setId("MyTemplate");
    origTemplate.setSnippet(snippet);
    byte[] serTemplate = TemplateSerializer.serialize(origTemplate);
    // Deserialize Template into TemplateDTO
    ByteArrayInputStream in = new ByteArrayInputStream(serTemplate);
    JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    XMLStreamReader xsr = XmlUtils.createSafeReader(in);
    JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(xsr, TemplateDTO.class);
    TemplateDTO deserTemplate = templateElement.getValue();
    // Modify deserialized template
    FlowSnippetDTO deserSnippet = deserTemplate.getSnippet();
    Set<ProcessorDTO> deserProcs = deserSnippet.getProcessors();
    int c = 0;
    for (ProcessorDTO processorDTO : deserProcs) {
        if (c % 2 == 0) {
            processorDTO.setName("Hello-" + c);
        }
        c++;
    }
    // add new Processor
    ProcessorDTO procDTO = new ProcessorDTO();
    procDTO.setType("ProcessorNew" + ".class");
    procDTO.setId(ComponentIdGenerator.generateId().toString());
    deserProcs.add(procDTO);
    // Serialize modified template
    byte[] serTemplate2 = TemplateSerializer.serialize(deserTemplate);
    RawText rt1 = new RawText(serTemplate);
    RawText rt2 = new RawText(serTemplate2);
    EditList diffList = new EditList();
    diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt1, rt2));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (DiffFormatter diff = new DiffFormatter(out)) {
        diff.format(diffList, rt1, rt2);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray()), StandardCharsets.UTF_8));
        List<String> changes = reader.lines().peek(System.out::println).filter(line -> line.startsWith("+") || line.startsWith("-")).collect(Collectors.toList());
        assertEquals("+            <name>Hello-0</name>", changes.get(0));
        assertEquals("+            <name>Hello-2</name>", changes.get(1));
        assertEquals("+        <processors>", changes.get(2));
        assertEquals("+            <type>ProcessorNew.class</type>", changes.get(4));
        assertEquals("+        </processors>", changes.get(5));
    }
}
Also used : HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) XmlUtils(org.apache.nifi.security.xml.XmlUtils) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) HashSet(java.util.HashSet) ByteArrayInputStream(java.io.ByteArrayInputStream) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) XMLStreamReader(javax.xml.stream.XMLStreamReader) RawText(org.eclipse.jgit.diff.RawText) JAXBContext(javax.xml.bind.JAXBContext) ComponentIdGenerator(org.apache.nifi.util.ComponentIdGenerator) Unmarshaller(javax.xml.bind.Unmarshaller) EditList(org.eclipse.jgit.diff.EditList) JAXBElement(javax.xml.bind.JAXBElement) Set(java.util.Set) Test(org.junit.Test) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) BufferedReader(java.io.BufferedReader) Assert.assertEquals(org.junit.Assert.assertEquals) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RawText(org.eclipse.jgit.diff.RawText) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) BufferedReader(java.io.BufferedReader) EditList(org.eclipse.jgit.diff.EditList) Unmarshaller(javax.xml.bind.Unmarshaller) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with TemplateDTO

use of org.apache.nifi.web.api.dto.TemplateDTO in project nifi by apache.

the class TemplateDeserializer method deserialize.

public static TemplateDTO deserialize(final StreamSource source) {
    try {
        JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
        XMLStreamReader xsr = XmlUtils.createSafeReader(source);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(xsr, TemplateDTO.class);
        return templateElement.getValue();
    } catch (final JAXBException | XMLStreamException e) {
        throw new FlowSerializationException(e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) JAXBException(javax.xml.bind.JAXBException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 10 with TemplateDTO

use of org.apache.nifi.web.api.dto.TemplateDTO in project nifi by apache.

the class StandardFlowService method loadTemplates.

/**
 * 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;
}
Also used : Path(java.nio.file.Path) BufferedInputStream(java.io.BufferedInputStream) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) ConnectionException(org.apache.nifi.cluster.ConnectionException) FlowSynchronizationException(org.apache.nifi.controller.serialization.FlowSynchronizationException) LifeCycleStartException(org.apache.nifi.lifecycle.LifeCycleStartException) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) IOException(java.io.IOException) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException)

Aggregations

TemplateDTO (org.apache.nifi.web.api.dto.TemplateDTO)40 NifiProperty (com.thinkbiganalytics.nifi.rest.model.NifiProperty)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 ProcessGroupDTO (org.apache.nifi.web.api.dto.ProcessGroupDTO)8 ApiOperation (io.swagger.annotations.ApiOperation)7 ApiResponses (io.swagger.annotations.ApiResponses)7 HashSet (java.util.HashSet)7 Produces (javax.ws.rs.Produces)7 ProcessorDTO (org.apache.nifi.web.api.dto.ProcessorDTO)7 RegisteredTemplate (com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate)6 HashMap (java.util.HashMap)6 ConnectionDTO (org.apache.nifi.web.api.dto.ConnectionDTO)6 Path (javax.ws.rs.Path)5 FlowSnippetDTO (org.apache.nifi.web.api.dto.FlowSnippetDTO)5 UploadProgressMessage (com.thinkbiganalytics.feedmgr.rest.model.UploadProgressMessage)4 Consumes (javax.ws.rs.Consumes)4 JAXBContext (javax.xml.bind.JAXBContext)4 JAXBException (javax.xml.bind.JAXBException)4 Unmarshaller (javax.xml.bind.Unmarshaller)4