exceptionfactory commented on code in PR #9933: URL: https://github.com/apache/nifi/pull/9933#discussion_r2085155037
########## nifi-extension-bundles/nifi-basex-bundle/nifi-basex-processors/src/main/java/org/apache/nifi/processors/basex/EvaluateBaseXQuery.java: ########## @@ -0,0 +1,277 @@ +/* + * 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.processors.basex; + + +import org.apache.nifi.annotation.behavior.*; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.components.*; +import org.apache.nifi.expression.AttributeExpression; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.logging.ComponentLog; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +import org.basex.core.Context; +import org.basex.core.Prop; +import org.basex.io.IOContent; +import org.basex.query.QueryException; +import org.basex.query.QueryProcessor; +import org.basex.query.value.Value; +import org.basex.query.value.item.Item; +import org.basex.query.value.node.DBNode; + +import java.io.BufferedInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; + + +@SideEffectFree +@SupportsBatching +@Tags({"XML", "evaluate", "XPath", "XQuery", "basex"}) +@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) +@CapabilityDescription( + "Executes an XQuery expression using the BaseX engine, passing the FlowFile content as a string context to the script. " + + "An XQuery script must be provided for the processor to run. Users can choose how FlowFile attributes are mapped " + + "to external variables: map all attributes, map only a specific list, or ignore attributes altogether. " + + "The output of the query can be written to a specified FlowFile attribute (similarly to the InvokeHTTP processor) " + + "or used to replace the content of the original FlowFile. The processor defines the following relationships: " + + "'success' (on successful execution), 'failure' (if the script fails), and 'original' (preserves the unmodified FlowFile)." +) +@SystemResourceConsiderations({ + @SystemResourceConsideration(resource = SystemResource.MEMORY, description = "Processing requires reading the entire FlowFile into memory") +}) +public class EvaluateBaseXQuery extends AbstractProcessor { + + + public static final Relationship REL_SUCCESS = new Relationship.Builder().name("success").build(); + public static final Relationship REL_FAILURE = new Relationship.Builder().name("failure") + .description("Content extraction failed").build(); + public static final Relationship REL_ORIGINAL = new Relationship.Builder().name("original") + .description("Success for original input FlowFiles").build(); + + public static final PropertyDescriptor XQUERY_SCRIPT = new PropertyDescriptor.Builder() + .name("XQuery Script") + .description("XQuery to execute on the incoming FlowFile content") + .required(true) + .addValidator(new BaseXqueryValidator()) + .build(); + + + + public static final String MAP_ALL = "Map all"; + public static final String MAP_LIST = "Map list"; + public static final String DO_NOT_MAP = "Do not map"; + + public static final PropertyDescriptor ATTR_MAPPING_STRATEGY = new PropertyDescriptor.Builder() + .name("Attribute Mapping Strategy") + .description("How to map FlowFile attributes as external XQuery variables") + .allowableValues(MAP_ALL, MAP_LIST, DO_NOT_MAP) + .required(true) + .build(); + + public static final PropertyDescriptor MAPPING_LIST = new PropertyDescriptor.Builder() + .name("Mapping list") + .description("List of attributes to map to external variables, separated by comma") + .required(false) + .dependsOn(ATTR_MAPPING_STRATEGY, MAP_LIST) + .addValidator(Validator.VALID) + .build(); + + public static final PropertyDescriptor OUTPUT_BODY_ATTRIBUTE_NAME = new PropertyDescriptor.Builder() + .name("Response Body Attribute Name") + .description("FlowFile attribute name used to write an xquery output body for FlowFiles transferred to the Original relationship.") + .addValidator(StandardValidators.createAttributeExpressionLanguageValidator(AttributeExpression.ResultType.STRING)) + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .build(); + + private static final Set<Relationship> RELATIONSHIPS = Set.of( + REL_SUCCESS, + REL_FAILURE, + REL_ORIGINAL + ); + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + final List<PropertyDescriptor> descriptors = new ArrayList<>(); + descriptors.add(XQUERY_SCRIPT); + descriptors.add(ATTR_MAPPING_STRATEGY); + descriptors.add(MAPPING_LIST); + descriptors.add(OUTPUT_BODY_ATTRIBUTE_NAME); + return descriptors; + } + + @Override + public Set<Relationship> getRelationships() { + return RELATIONSHIPS; + } + + + @Override + public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { + Context basexContext = new Context(); + final List<FlowFile> flowFileBatch = session.get(50); Review Comment: Is there a particular reason for having this hard-coded batch of 50, as opposed to just handling one FlowFile and supporting batching through annotations? -- 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]
