Search in sources :

Example 1 with LoggingXmlParserErrorHandler

use of org.apache.nifi.util.LoggingXmlParserErrorHandler in project nifi by apache.

the class FlowParser method parse.

/**
 * Extracts the root group id from the flow configuration file provided in nifi.properties, and extracts
 * the root group input ports and output ports, and their access controls.
 */
public FlowInfo parse(final File flowConfigurationFile) {
    if (flowConfigurationFile == null) {
        logger.debug("Flow Configuration file was null");
        return null;
    }
    // if the flow doesn't exist or is 0 bytes, then return null
    final Path flowPath = flowConfigurationFile.toPath();
    try {
        if (!Files.exists(flowPath) || Files.size(flowPath) == 0) {
            logger.warn("Flow Configuration does not exist or was empty");
            return null;
        }
    } catch (IOException e) {
        logger.error("An error occurred determining the size of the Flow Configuration file");
        return null;
    }
    // otherwise create the appropriate input streams to read the file
    try (final InputStream in = Files.newInputStream(flowPath, StandardOpenOption.READ);
        final InputStream gzipIn = new GZIPInputStream(in)) {
        final byte[] flowBytes = IOUtils.toByteArray(gzipIn);
        if (flowBytes == null || flowBytes.length == 0) {
            logger.warn("Could not extract root group id because Flow Configuration File was empty");
            return null;
        }
        // create validating document builder
        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
        docFactory.setSchema(flowSchema);
        // parse the flow
        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        docBuilder.setErrorHandler(new LoggingXmlParserErrorHandler("Flow Configuration", logger));
        final Document document = docBuilder.parse(new ByteArrayInputStream(flowBytes));
        // extract the root group id
        final Element rootElement = document.getDocumentElement();
        final Element rootGroupElement = (Element) rootElement.getElementsByTagName("rootGroup").item(0);
        if (rootGroupElement == null) {
            logger.warn("rootGroup element not found in Flow Configuration file");
            return null;
        }
        final Element rootGroupIdElement = (Element) rootGroupElement.getElementsByTagName("id").item(0);
        if (rootGroupIdElement == null) {
            logger.warn("id element not found under rootGroup in Flow Configuration file");
            return null;
        }
        final String rootGroupId = rootGroupIdElement.getTextContent();
        final List<PortDTO> ports = new ArrayList<>();
        ports.addAll(getPorts(rootGroupElement, "inputPort"));
        ports.addAll(getPorts(rootGroupElement, "outputPort"));
        return new FlowInfo(rootGroupId, ports);
    } catch (final SAXException | ParserConfigurationException | IOException ex) {
        logger.error("Unable to parse flow {} due to {}", new Object[] { flowPath.toAbsolutePath(), ex });
        return null;
    }
}
Also used : Path(java.nio.file.Path) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PortDTO(org.apache.nifi.web.api.dto.PortDTO) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) GZIPInputStream(java.util.zip.GZIPInputStream) LoggingXmlParserErrorHandler(org.apache.nifi.util.LoggingXmlParserErrorHandler) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with LoggingXmlParserErrorHandler

use of org.apache.nifi.util.LoggingXmlParserErrorHandler in project nifi-minifi by apache.

the class FlowParser method parse.

/**
 * Generates a {@link Document} from the flow configuration file provided
 */
public Document parse(final File flowConfigurationFile) {
    if (flowConfigurationFile == null) {
        logger.debug("Flow Configuration file was null");
        return null;
    }
    // if the flow doesn't exist or is 0 bytes, then return null
    final Path flowPath = flowConfigurationFile.toPath();
    try {
        if (!Files.exists(flowPath) || Files.size(flowPath) == 0) {
            logger.warn("Flow Configuration does not exist or was empty");
            return null;
        }
    } catch (IOException e) {
        logger.error("An error occurred determining the size of the Flow Configuration file");
        return null;
    }
    // otherwise create the appropriate input streams to read the file
    try (final InputStream in = Files.newInputStream(flowPath, StandardOpenOption.READ);
        final InputStream gzipIn = new GZIPInputStream(in)) {
        final byte[] flowBytes = IOUtils.toByteArray(gzipIn);
        if (flowBytes == null || flowBytes.length == 0) {
            logger.warn("Could not extract root group id because Flow Configuration File was empty");
            return null;
        }
        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
        docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        // parse the flow
        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        docBuilder.setErrorHandler(new LoggingXmlParserErrorHandler("Flow Configuration", logger));
        final Document document = docBuilder.parse(new ByteArrayInputStream(flowBytes));
        return document;
    } catch (final SAXException | ParserConfigurationException | IOException ex) {
        logger.error("Unable to parse flow {} due to {}", new Object[] { flowPath.toAbsolutePath(), ex });
        return null;
    }
}
Also used : Path(java.nio.file.Path) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) GZIPInputStream(java.util.zip.GZIPInputStream) LoggingXmlParserErrorHandler(org.apache.nifi.util.LoggingXmlParserErrorHandler) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 3 with LoggingXmlParserErrorHandler

use of org.apache.nifi.util.LoggingXmlParserErrorHandler in project nifi by apache.

the class StandardFlowSynchronizer method parseFlowBytes.

private static Document parseFlowBytes(final byte[] flow) throws FlowSerializationException {
    // create document by parsing proposed flow bytes
    try {
        // create validating document builder
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final Schema schema = schemaFactory.newSchema(FLOW_XSD_RESOURCE);
        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
        docFactory.setSchema(schema);
        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        docBuilder.setErrorHandler(new LoggingXmlParserErrorHandler("Flow Configuration", logger));
        // parse flow
        return (flow == null || flow.length == 0) ? null : docBuilder.parse(new ByteArrayInputStream(flow));
    } catch (final SAXException | ParserConfigurationException | IOException ex) {
        throw new FlowSerializationException(ex);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) LoggingXmlParserErrorHandler(org.apache.nifi.util.LoggingXmlParserErrorHandler) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(javax.xml.validation.Schema) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 LoggingXmlParserErrorHandler (org.apache.nifi.util.LoggingXmlParserErrorHandler)3 SAXException (org.xml.sax.SAXException)3 InputStream (java.io.InputStream)2 Path (java.nio.file.Path)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 Document (org.w3c.dom.Document)2 ArrayList (java.util.ArrayList)1 Schema (javax.xml.validation.Schema)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 FlowSerializationException (org.apache.nifi.controller.serialization.FlowSerializationException)1 PortDTO (org.apache.nifi.web.api.dto.PortDTO)1 Element (org.w3c.dom.Element)1