Java tutorial
/* * 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 com.sysunite.weaver.nifi; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import org.apache.commons.io.IOUtils; import org.apache.nifi.components.PropertyDescriptor; import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.processor.*; import org.apache.nifi.annotation.behavior.ReadsAttribute; import org.apache.nifi.annotation.behavior.ReadsAttributes; import org.apache.nifi.annotation.behavior.WritesAttribute; import org.apache.nifi.annotation.behavior.WritesAttributes; import org.apache.nifi.annotation.lifecycle.OnScheduled; import org.apache.nifi.annotation.documentation.CapabilityDescription; import org.apache.nifi.annotation.documentation.SeeAlso; import org.apache.nifi.annotation.documentation.Tags; import org.apache.nifi.processor.exception.ProcessException; import org.apache.nifi.processor.io.InputStreamCallback; import org.apache.nifi.processor.util.StandardValidators; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.*; import java.util.concurrent.atomic.AtomicReference; @Tags({ "weaver, filterxmlnodes" }) @CapabilityDescription("Filter xmlnodes by pregmatch argument.") @SeeAlso({}) @ReadsAttributes({ @ReadsAttribute(attribute = "", description = "") }) @WritesAttributes({ @WritesAttribute(attribute = "", description = "") }) public class FilterXMLNodes extends AbstractProcessor { public static final PropertyDescriptor PROP_PREGMATCH = new PropertyDescriptor.Builder().name("prop_pregmatch") .description("Use pregmatch argument as in java.matches({pregmatch-argument}).").required(true) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build(); public static final PropertyDescriptor PROP_NODE = new PropertyDescriptor.Builder().name("prop_node") .description("Enter the tag-name of the root node, where the NODE_ATTRIBUTE lives.").required(true) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build(); public static final PropertyDescriptor PROP_NODE_ATTRIBUTE = new PropertyDescriptor.Builder() .name("prop_node_attribute") .description("Setup node attribute to apply the pregmatch argument, i.e. name.").required(true) .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build(); public static final Relationship MY_RELATIONSHIP = new Relationship.Builder().name("my_relationship") .description("Example relationship").build(); private List<PropertyDescriptor> descriptors; private Set<Relationship> relationships; @Override protected void init(final ProcessorInitializationContext context) { final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>(); descriptors.add(PROP_PREGMATCH); descriptors.add(PROP_NODE); descriptors.add(PROP_NODE_ATTRIBUTE); this.descriptors = Collections.unmodifiableList(descriptors); final Set<Relationship> relationships = new HashSet<Relationship>(); relationships.add(MY_RELATIONSHIP); this.relationships = Collections.unmodifiableSet(relationships); } @Override public Set<Relationship> getRelationships() { return this.relationships; } @Override public final List<PropertyDescriptor> getSupportedPropertyDescriptors() { return descriptors; } @OnScheduled public void onScheduled(final ProcessContext context) { } @Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final FlowFile flowFileIn = session.get(); if (flowFileIn == null) { return; } final AtomicReference<InputStream> savedNode = new AtomicReference<>(); //read the flow file session.read(flowFileIn, new InputStreamCallback() { @Override public void process(InputStream isIn) throws IOException { //System.out.println("contact!"); try { String contents = IOUtils.toString(isIn); XML xmlNode = new XMLDocument(contents); System.out.println(xmlNode.toString()); // works: .xpath("/FunctionalPhysicalObject/@name") String attributeToSearchFor = context.getProperty(PROP_NODE_ATTRIBUTE).getValue(); // name attributeToSearchFor = "@" + attributeToSearchFor; //@name String nodeToSearchFor = context.getProperty(PROP_NODE).getValue(); //FunctionalPhysicalObject nodeToSearchFor = "/" + nodeToSearchFor + "/"; // /FunctionalPhysicalObject/ String fullNodePathWithAttribute = nodeToSearchFor + attributeToSearchFor; // /FunctionalPhysicalObject/@name //System.out.println(fullNodePathWithAttribute); String valueOfAttribute = xmlNode.xpath(fullNodePathWithAttribute).get(0); //System.out.println("naam: " + valueOfAttribute); //"(.*)AB(.*)CT(.*)" String filter = context.getProperty(PROP_PREGMATCH).getValue(); if (valueOfAttribute.matches(filter)) { //System.out.println("match!!"); //dit is de node die we echt willen! InputStream is = new ByteArrayInputStream(xmlNode.toString().getBytes()); savedNode.set(is); } } catch (IOException e) { System.out.println("w00t");// + e.getMessage()); } catch (IllegalArgumentException e) { System.out.println("is xml niet geldig?"); } catch (IndexOutOfBoundsException e) { System.out.println("bah! de node waar gezocht naar moet worden is niet gevonden!"); } } }); session.remove(flowFileIn); //process what we have read try { //check if there is something to process String contents = IOUtils.toString(savedNode.get()); if (contents != null && contents.length() > 0) { FlowFile flowfileOut = session.create(); XML xmlNode = new XMLDocument(contents); InputStream data = new ByteArrayInputStream(xmlNode.toString().getBytes()); flowfileOut = session.importFrom(data, flowfileOut); session.transfer(flowfileOut, MY_RELATIONSHIP); session.commit(); } } catch (IOException e) { System.out.println("w00t");// + e.getMessage()); } catch (IllegalArgumentException e) { System.out.println("xml niet geldig?"); } catch (NullPointerException e) { System.out.println("zonder gevonden node geen waarde om te initialiseren."); } } }