exceptionfactory commented on code in PR #6054: URL: https://github.com/apache/nifi/pull/6054#discussion_r877392930
########## nifi-commons/nifi-flow-encryptor/src/main/java/org/apache/nifi/flow/encryptor/XmlFlowEncryptor.java: ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.flow.encryptor; + +import org.apache.nifi.encrypt.PropertyEncryptor; +import org.apache.nifi.xml.processing.stream.StandardXMLEventReaderProvider; +import org.apache.nifi.xml.processing.stream.XMLEventReaderProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.xml.stream.XMLEventFactory; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.StartDocument; +import javax.xml.stream.events.XMLEvent; +import javax.xml.transform.stream.StreamSource; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UncheckedIOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class XmlFlowEncryptor implements FlowEncryptor { + private static final Pattern ENCRYPTED_PATTERN = Pattern.compile("enc\\{([^\\}]+?)\\}"); + + private static final int FIRST_GROUP = 1; + + private static final String ENCRYPTED_FORMAT = "enc{%s}"; + + private static final XMLEventReaderProvider eventReaderProvider = new StandardXMLEventReaderProvider(); + + private final Logger logger = LoggerFactory.getLogger(XmlFlowEncryptor.class); + + @Override + public void processFlow(InputStream inputStream, OutputStream outputStream, PropertyEncryptor inputEncryptor, PropertyEncryptor outputEncryptor) { + XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); + XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); + final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); + try { + final XMLEventReader reader = eventReaderProvider.getEventReader(new StreamSource(inputStream)); + final XMLEventWriter writer = xmlOutputFactory.createXMLEventWriter(outputStream, "UTF-8"); + while (reader.hasNext()) { + final XMLEvent event = reader.nextEvent(); + if (event.getEventType() == XMLEvent.START_DOCUMENT) { + final StartDocument startDocument = (StartDocument) event; + handleXmlStartDocument(startDocument, outputStream); + } else if (event.getEventType() == XMLEvent.CHARACTERS) { + final Characters characters = event.asCharacters(); + final String value = characters.getData(); + final Matcher matcher = ENCRYPTED_PATTERN.matcher(value); + if (matcher.matches()) { + final String processedValue = getOutputEncrypted(matcher.group(FIRST_GROUP), inputEncryptor, outputEncryptor); + writer.add(eventFactory.createCharacters(processedValue)); + } else { + writer.add(characters); + } + } else if (event.getEventType() == XMLEvent.COMMENT) { + writer.add(event); + final Characters newLine = eventFactory.createCharacters("\n"); + writer.add(newLine); + } else if (event.getEventType() == XMLEvent.END_DOCUMENT) { + final Characters newLine = eventFactory.createCharacters("\n"); + writer.add(newLine); + } else { + writer.add(event); + } + } + writer.flush(); + writer.close(); + reader.close(); + outputStream.close(); + inputStream.close(); Review Comment: Good point, that makes sense. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
