turcsanyip commented on code in PR #6279: URL: https://github.com/apache/nifi/pull/6279#discussion_r949402361
########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-processors/src/main/java/org/apache/nifi/processors/smb/FetchSmb.java: ########## @@ -0,0 +1,230 @@ +/* + * 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.smb; + +import static java.util.Arrays.asList; +import static org.apache.nifi.processor.util.StandardValidators.NON_EMPTY_EL_VALIDATOR; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +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.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +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.schema.access.SchemaNotFoundException; +import org.apache.nifi.serialization.MalformedRecordException; +import org.apache.nifi.serialization.RecordReader; +import org.apache.nifi.serialization.RecordReaderFactory; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.services.smb.SmbClientProviderService; +import org.apache.nifi.services.smb.SmbClientService; +import org.apache.nifi.services.smb.SmbException; + +@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) +@Tags({"samba, smb, cifs, files", "fetch"}) +@CapabilityDescription("Fetches files from a SMB Share. Designed to be used in tandem with ListSmb.") +@SeeAlso({ListSmb.class, PutSmbFile.class, GetSmbFile.class}) +@WritesAttributes({ + @WritesAttribute(attribute = FetchSmb.ERROR_CODE_ATTRIBUTE, description = "The error code returned by SMB when the fetch of a file fails"), + @WritesAttribute(attribute = FetchSmb.ERROR_MESSAGE_ATTRIBUTE, description = "The error message returned by SMB when the fetch of a file fails") +}) +public class FetchSmb extends AbstractProcessor { + + public static final String ERROR_CODE_ATTRIBUTE = "error.code"; + public static final String ERROR_MESSAGE_ATTRIBUTE = "error.message"; + + public static final PropertyDescriptor REMOTE_FILE = new PropertyDescriptor + .Builder().name("remote-file") + .displayName("Remote File") + .description("The full path of the file to be retrieved from the remote server.") + .required(true) + .defaultValue("${absolute.path}/${filename}") Review Comment: It dos not work this way as `absolute.path` contains the filename already. It should be `${path}/${filename}` ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-processors/src/main/java/org/apache/nifi/processors/smb/FetchSmb.java: ########## @@ -0,0 +1,230 @@ +/* + * 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.smb; + +import static java.util.Arrays.asList; +import static org.apache.nifi.processor.util.StandardValidators.NON_EMPTY_EL_VALIDATOR; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +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.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +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.schema.access.SchemaNotFoundException; +import org.apache.nifi.serialization.MalformedRecordException; +import org.apache.nifi.serialization.RecordReader; +import org.apache.nifi.serialization.RecordReaderFactory; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.services.smb.SmbClientProviderService; +import org.apache.nifi.services.smb.SmbClientService; +import org.apache.nifi.services.smb.SmbException; + +@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) +@Tags({"samba, smb, cifs, files", "fetch"}) +@CapabilityDescription("Fetches files from a SMB Share. Designed to be used in tandem with ListSmb.") +@SeeAlso({ListSmb.class, PutSmbFile.class, GetSmbFile.class}) +@WritesAttributes({ + @WritesAttribute(attribute = FetchSmb.ERROR_CODE_ATTRIBUTE, description = "The error code returned by SMB when the fetch of a file fails"), + @WritesAttribute(attribute = FetchSmb.ERROR_MESSAGE_ATTRIBUTE, description = "The error message returned by SMB when the fetch of a file fails") +}) +public class FetchSmb extends AbstractProcessor { + + public static final String ERROR_CODE_ATTRIBUTE = "error.code"; + public static final String ERROR_MESSAGE_ATTRIBUTE = "error.message"; + + public static final PropertyDescriptor REMOTE_FILE = new PropertyDescriptor + .Builder().name("remote-file") + .displayName("Remote File") + .description("The full path of the file to be retrieved from the remote server.") + .required(true) + .defaultValue("${absolute.path}/${filename}") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor REMOTE_FILE_FIELD = new PropertyDescriptor + .Builder().name("remote-file-field") + .displayName("Remote File Field") + .description("The name of the filed in a record to use as the remote file name. This field is being used " + + "when record reader is set.") + .required(true) + .defaultValue("identifier") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor SMB_CLIENT_PROVIDER_SERVICE = new Builder() + .name("smb-client-provider-service") + .displayName("SMB Client Provider Service") + .description("Specifies the SMB client provider to use for creating SMB connections.") + .required(true) + .identifiesControllerService(SmbClientProviderService.class) + .build(); + + static final PropertyDescriptor RECORD_READER = new PropertyDescriptor.Builder() + .name("record-reader") + .displayName("Record Reader") + .description( + "Specifies the Controller Service to use for reading incoming NiFi Records. Each record should contain \"identifier\"" + + " attribute set to the path and name of the file to fetch." + + " If not set, the Processor expects as attributes of a separate flowfile for each File to fetch.") + .identifiesControllerService(RecordReaderFactory.class) + .required(false) + .build(); + + public static final Relationship REL_SUCCESS = + new Relationship.Builder() + .name("success") + .description("A flowfile will be routed here for each successfully fetched File.") + .build(); + + public static final Relationship REL_FAILURE = + new Relationship.Builder().name("failure") + .description( + "A flowfile will be routed here for each File for which fetch was attempted but failed.") + .build(); + + public static final Relationship REL_INPUT_FAILURE = + new Relationship.Builder().name("input_failure") + .description("The incoming flowfile will be routed here if its content could not be processed.") + .build(); + + public static final Set<Relationship> relationships = Collections.unmodifiableSet(new HashSet<>(asList( + REL_SUCCESS, + REL_FAILURE, + REL_INPUT_FAILURE + ))); + + private static final List<PropertyDescriptor> PROPERTIES = asList( + REMOTE_FILE, + REMOTE_FILE_FIELD, + SMB_CLIENT_PROVIDER_SERVICE, + RECORD_READER + ); + + @Override + public Set<Relationship> getRelationships() { + return relationships; + } + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @Override + public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { + FlowFile flowFile = session.get(); + if (flowFile == null) { + return; + } + + boolean shouldDeleteFlowFile = false; + + final SmbClientProviderService clientProviderService = + context.getProperty(SMB_CLIENT_PROVIDER_SERVICE).asControllerService(SmbClientProviderService.class); + + try (SmbClientService client = clientProviderService.getClient()) { + + if (context.getProperty(RECORD_READER).isSet()) { + final RecordReaderFactory recordReaderFactory = + context.getProperty(RECORD_READER).asControllerService(RecordReaderFactory.class); + + try (InputStream inFlowFile = session.read(flowFile); + RecordReader reader = recordReaderFactory.createRecordReader(flowFile, inFlowFile, getLogger()) + ) { + final String fieldName = context.getProperty(REMOTE_FILE_FIELD).getValue(); + Record record; + while ((record = reader.nextRecord()) != null) { + final String fileName = record.getAsString(fieldName); + final FlowFile outFlowFile = session.create(flowFile); + fetchAndTransfer(session, client, outFlowFile, fileName); + } + shouldDeleteFlowFile = true; + } catch (Exception e) { + handleInputError(e, session, flowFile); + } Review Comment: In case of "input error", the session needs to be rolled back I think, because some FFs may already be processed and sent to SUCCESS/FAILURE. After rollback, the original FF can be sent to REL_INPUT_FAILURE. In "input error" case, I think only this output needs to be produced. ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-client-api/src/main/java/org/apache/nifi/services/smb/SmbListableEntity.java: ########## @@ -154,7 +154,7 @@ public Record toRecord() { final Map<String, Object> record = new TreeMap<>(); record.put("filename", getName()); record.put("shortName", getShortName()); - record.put("path", path); + record.put("path", getPath()); record.put("identifier", getPathWithName()); record.put("timestamp", getTimestamp()); record.put("creationTime", getCreationTime()); Review Comment: Please use the exactly same names for record fields and FF attributes: - filename - shortName - path - serviceLocation - timestamp - creationTime - lastAccessTime - changeTime - size - allocationSize `identifier` should be omitted. `absolute.path` may also be not necessary because it can be calculated from `path` and `filename`. ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-processors/pom.xml: ########## @@ -79,6 +79,18 @@ <version>1.18.0-SNAPSHOT</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-record-serialization-services</artifactId> + <version>1.18.0-SNAPSHOT</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-schema-registry-service-api</artifactId> + <version>1.18.0-SNAPSHOT</version> + <scope>test</scope> + </dependency> Review Comment: `nifi-schema-registry-service-api` dependency does not seem to be needed. ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-processors/src/main/java/org/apache/nifi/processors/smb/FetchSmb.java: ########## @@ -0,0 +1,230 @@ +/* + * 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.smb; + +import static java.util.Arrays.asList; +import static org.apache.nifi.processor.util.StandardValidators.NON_EMPTY_EL_VALIDATOR; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +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.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +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.schema.access.SchemaNotFoundException; +import org.apache.nifi.serialization.MalformedRecordException; +import org.apache.nifi.serialization.RecordReader; +import org.apache.nifi.serialization.RecordReaderFactory; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.services.smb.SmbClientProviderService; +import org.apache.nifi.services.smb.SmbClientService; +import org.apache.nifi.services.smb.SmbException; + +@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) +@Tags({"samba, smb, cifs, files", "fetch"}) +@CapabilityDescription("Fetches files from a SMB Share. Designed to be used in tandem with ListSmb.") +@SeeAlso({ListSmb.class, PutSmbFile.class, GetSmbFile.class}) +@WritesAttributes({ + @WritesAttribute(attribute = FetchSmb.ERROR_CODE_ATTRIBUTE, description = "The error code returned by SMB when the fetch of a file fails"), + @WritesAttribute(attribute = FetchSmb.ERROR_MESSAGE_ATTRIBUTE, description = "The error message returned by SMB when the fetch of a file fails") +}) +public class FetchSmb extends AbstractProcessor { + + public static final String ERROR_CODE_ATTRIBUTE = "error.code"; + public static final String ERROR_MESSAGE_ATTRIBUTE = "error.message"; + + public static final PropertyDescriptor REMOTE_FILE = new PropertyDescriptor + .Builder().name("remote-file") + .displayName("Remote File") + .description("The full path of the file to be retrieved from the remote server.") + .required(true) + .defaultValue("${absolute.path}/${filename}") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor REMOTE_FILE_FIELD = new PropertyDescriptor + .Builder().name("remote-file-field") + .displayName("Remote File Field") + .description("The name of the filed in a record to use as the remote file name. This field is being used " + + "when record reader is set.") + .required(true) + .defaultValue("identifier") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor SMB_CLIENT_PROVIDER_SERVICE = new Builder() + .name("smb-client-provider-service") + .displayName("SMB Client Provider Service") + .description("Specifies the SMB client provider to use for creating SMB connections.") + .required(true) + .identifiesControllerService(SmbClientProviderService.class) + .build(); + + static final PropertyDescriptor RECORD_READER = new PropertyDescriptor.Builder() + .name("record-reader") + .displayName("Record Reader") + .description( + "Specifies the Controller Service to use for reading incoming NiFi Records. Each record should contain \"identifier\"" + + " attribute set to the path and name of the file to fetch." + + " If not set, the Processor expects as attributes of a separate flowfile for each File to fetch.") + .identifiesControllerService(RecordReaderFactory.class) + .required(false) + .build(); + + public static final Relationship REL_SUCCESS = + new Relationship.Builder() + .name("success") + .description("A flowfile will be routed here for each successfully fetched File.") + .build(); + + public static final Relationship REL_FAILURE = + new Relationship.Builder().name("failure") + .description( + "A flowfile will be routed here for each File for which fetch was attempted but failed.") + .build(); + + public static final Relationship REL_INPUT_FAILURE = + new Relationship.Builder().name("input_failure") + .description("The incoming flowfile will be routed here if its content could not be processed.") + .build(); + + public static final Set<Relationship> relationships = Collections.unmodifiableSet(new HashSet<>(asList( + REL_SUCCESS, + REL_FAILURE, + REL_INPUT_FAILURE + ))); + + private static final List<PropertyDescriptor> PROPERTIES = asList( + REMOTE_FILE, + REMOTE_FILE_FIELD, + SMB_CLIENT_PROVIDER_SERVICE, + RECORD_READER + ); + + @Override + public Set<Relationship> getRelationships() { + return relationships; + } + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @Override + public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { + FlowFile flowFile = session.get(); + if (flowFile == null) { + return; + } + + boolean shouldDeleteFlowFile = false; + + final SmbClientProviderService clientProviderService = + context.getProperty(SMB_CLIENT_PROVIDER_SERVICE).asControllerService(SmbClientProviderService.class); + + try (SmbClientService client = clientProviderService.getClient()) { + + if (context.getProperty(RECORD_READER).isSet()) { + final RecordReaderFactory recordReaderFactory = + context.getProperty(RECORD_READER).asControllerService(RecordReaderFactory.class); + + try (InputStream inFlowFile = session.read(flowFile); + RecordReader reader = recordReaderFactory.createRecordReader(flowFile, inFlowFile, getLogger()) + ) { + final String fieldName = context.getProperty(REMOTE_FILE_FIELD).getValue(); + Record record; + while ((record = reader.nextRecord()) != null) { + final String fileName = record.getAsString(fieldName); + final FlowFile outFlowFile = session.create(flowFile); + fetchAndTransfer(session, client, outFlowFile, fileName); + } + shouldDeleteFlowFile = true; + } catch (Exception e) { + handleInputError(e, session, flowFile); + } + } else { + PropertyValue property = context.getProperty(REMOTE_FILE); + final String fileName = property.evaluateAttributeExpressions(flowFile).getValue(); + fetchAndTransfer(session, client, flowFile, fileName); + } + } catch (Exception e) { + getLogger().error("Couldn't connect to smb due to "+ e.getMessage()); + flowFile = session.putAttribute(flowFile, ERROR_CODE_ATTRIBUTE, getErrorCode(e)); + flowFile = session.putAttribute(flowFile, ERROR_MESSAGE_ATTRIBUTE, e.getMessage()); + session.transfer(flowFile, REL_FAILURE); + } + if (shouldDeleteFlowFile) { + session.remove(flowFile); + } + + } + + private void fetchAndTransfer(ProcessSession session, SmbClientService client, FlowFile flowFile, String fileName) { + try { + if (fileName == null || fileName.isEmpty()) { + throw new IllegalArgumentException("Couldn't find filename in flowfile"); + } + flowFile = session.write(flowFile, outputStream -> client.read(fileName, outputStream)); + session.transfer(flowFile, REL_SUCCESS); + } catch (Exception e) { + getLogger().error("Couldn't fetch file {} due to {}", fileName, e.getMessage()); + flowFile = session.putAttribute(flowFile, ERROR_CODE_ATTRIBUTE, getErrorCode(e)); + flowFile = session.putAttribute(flowFile, ERROR_MESSAGE_ATTRIBUTE, e.getMessage()); + session.transfer(flowFile, REL_FAILURE); + } + } + + private void handleInputError(Exception exception, ProcessSession session, FlowFile flowFile) { + if (exception instanceof IOException || exception instanceof MalformedRecordException + || exception instanceof SchemaNotFoundException) { + getLogger().error("Failed to read input records {}", flowFile, exception); + } else { + getLogger().error("Failed to read input {}", flowFile, exception); + } + session.putAttribute(flowFile, ERROR_MESSAGE_ATTRIBUTE, exception.getMessage()); + session.transfer(flowFile, REL_INPUT_FAILURE); + } + + private String getErrorCode(Exception exception) { + return Optional.ofNullable(exception instanceof SmbException ? (SmbException) exception : null) + .map(SmbException::getErrorCode) + .map(String::valueOf) + .orElse("-2"); Review Comment: Could we use some constants for these negative error codes? ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-processors/src/main/java/org/apache/nifi/processors/smb/FetchSmb.java: ########## @@ -0,0 +1,230 @@ +/* + * 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.smb; + +import static java.util.Arrays.asList; +import static org.apache.nifi.processor.util.StandardValidators.NON_EMPTY_EL_VALIDATOR; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +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.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +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.schema.access.SchemaNotFoundException; +import org.apache.nifi.serialization.MalformedRecordException; +import org.apache.nifi.serialization.RecordReader; +import org.apache.nifi.serialization.RecordReaderFactory; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.services.smb.SmbClientProviderService; +import org.apache.nifi.services.smb.SmbClientService; +import org.apache.nifi.services.smb.SmbException; + +@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) +@Tags({"samba, smb, cifs, files", "fetch"}) +@CapabilityDescription("Fetches files from a SMB Share. Designed to be used in tandem with ListSmb.") +@SeeAlso({ListSmb.class, PutSmbFile.class, GetSmbFile.class}) +@WritesAttributes({ + @WritesAttribute(attribute = FetchSmb.ERROR_CODE_ATTRIBUTE, description = "The error code returned by SMB when the fetch of a file fails"), + @WritesAttribute(attribute = FetchSmb.ERROR_MESSAGE_ATTRIBUTE, description = "The error message returned by SMB when the fetch of a file fails") +}) +public class FetchSmb extends AbstractProcessor { + + public static final String ERROR_CODE_ATTRIBUTE = "error.code"; + public static final String ERROR_MESSAGE_ATTRIBUTE = "error.message"; + + public static final PropertyDescriptor REMOTE_FILE = new PropertyDescriptor + .Builder().name("remote-file") + .displayName("Remote File") + .description("The full path of the file to be retrieved from the remote server.") + .required(true) + .defaultValue("${absolute.path}/${filename}") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor REMOTE_FILE_FIELD = new PropertyDescriptor + .Builder().name("remote-file-field") + .displayName("Remote File Field") + .description("The name of the filed in a record to use as the remote file name. This field is being used " + + "when record reader is set.") + .required(true) + .defaultValue("identifier") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); Review Comment: I would still vote for using a single property for Remote File (file path) and Remote Path Field (record field). If not, this property should depend on the Record Reader property (as it was suggested in a previous comment). Also, I would allow to configure a generic record path, not only a single field. Default value: could we get rid of the "identifier" name here as well? ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-processors/src/main/java/org/apache/nifi/processors/smb/FetchSmb.java: ########## @@ -0,0 +1,230 @@ +/* + * 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.smb; + +import static java.util.Arrays.asList; +import static org.apache.nifi.processor.util.StandardValidators.NON_EMPTY_EL_VALIDATOR; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +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.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +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.schema.access.SchemaNotFoundException; +import org.apache.nifi.serialization.MalformedRecordException; +import org.apache.nifi.serialization.RecordReader; +import org.apache.nifi.serialization.RecordReaderFactory; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.services.smb.SmbClientProviderService; +import org.apache.nifi.services.smb.SmbClientService; +import org.apache.nifi.services.smb.SmbException; + +@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) +@Tags({"samba, smb, cifs, files", "fetch"}) +@CapabilityDescription("Fetches files from a SMB Share. Designed to be used in tandem with ListSmb.") +@SeeAlso({ListSmb.class, PutSmbFile.class, GetSmbFile.class}) +@WritesAttributes({ + @WritesAttribute(attribute = FetchSmb.ERROR_CODE_ATTRIBUTE, description = "The error code returned by SMB when the fetch of a file fails"), + @WritesAttribute(attribute = FetchSmb.ERROR_MESSAGE_ATTRIBUTE, description = "The error message returned by SMB when the fetch of a file fails") +}) +public class FetchSmb extends AbstractProcessor { + + public static final String ERROR_CODE_ATTRIBUTE = "error.code"; + public static final String ERROR_MESSAGE_ATTRIBUTE = "error.message"; + + public static final PropertyDescriptor REMOTE_FILE = new PropertyDescriptor + .Builder().name("remote-file") + .displayName("Remote File") + .description("The full path of the file to be retrieved from the remote server.") + .required(true) + .defaultValue("${absolute.path}/${filename}") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor REMOTE_FILE_FIELD = new PropertyDescriptor + .Builder().name("remote-file-field") + .displayName("Remote File Field") + .description("The name of the filed in a record to use as the remote file name. This field is being used " + + "when record reader is set.") + .required(true) + .defaultValue("identifier") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor SMB_CLIENT_PROVIDER_SERVICE = new Builder() + .name("smb-client-provider-service") + .displayName("SMB Client Provider Service") + .description("Specifies the SMB client provider to use for creating SMB connections.") + .required(true) + .identifiesControllerService(SmbClientProviderService.class) + .build(); + + static final PropertyDescriptor RECORD_READER = new PropertyDescriptor.Builder() + .name("record-reader") + .displayName("Record Reader") + .description( + "Specifies the Controller Service to use for reading incoming NiFi Records. Each record should contain \"identifier\"" + + " attribute set to the path and name of the file to fetch." + + " If not set, the Processor expects as attributes of a separate flowfile for each File to fetch.") + .identifiesControllerService(RecordReaderFactory.class) + .required(false) + .build(); + + public static final Relationship REL_SUCCESS = + new Relationship.Builder() + .name("success") + .description("A flowfile will be routed here for each successfully fetched File.") + .build(); + + public static final Relationship REL_FAILURE = + new Relationship.Builder().name("failure") + .description( + "A flowfile will be routed here for each File for which fetch was attempted but failed.") + .build(); + + public static final Relationship REL_INPUT_FAILURE = + new Relationship.Builder().name("input_failure") + .description("The incoming flowfile will be routed here if its content could not be processed.") + .build(); + + public static final Set<Relationship> relationships = Collections.unmodifiableSet(new HashSet<>(asList( + REL_SUCCESS, + REL_FAILURE, + REL_INPUT_FAILURE + ))); + + private static final List<PropertyDescriptor> PROPERTIES = asList( + REMOTE_FILE, + REMOTE_FILE_FIELD, + SMB_CLIENT_PROVIDER_SERVICE, Review Comment: I would move the controller service property to the top. ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-processors/src/main/java/org/apache/nifi/processors/smb/ListSmb.java: ########## @@ -92,9 +92,8 @@ + "given a remote location smb://HOSTNAME:PORT/SHARE/DIRECTORY, and a file is being listen from " + "SHARE/DIRECTORY/sub/folder/file then the absolute.path attribute will be set to " + "SHARE/DIRECTORY/sub/folder/file."), - @WritesAttribute(attribute = "identifier", description = - "The identifier of the file. This equals to the path attribute so two files with the same relative path " - + "coming from different file shares considered to be identical."), + @WritesAttribute(attribute = "serviceLocation", description = + "The serviceLocation is set to the host and port of the remote smb server."), Review Comment: The attribute contains the smb url of the share in fact. So the following description would be more adequate: > The SMB URL of the share. ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-client-api/src/main/java/org/apache/nifi/services/smb/SmbClientService.java: ########## @@ -27,4 +29,5 @@ public interface SmbClientService extends AutoCloseable { void createDirectory(String path); + void read(String fileName, OutputStream outputStream) throws IOException; Review Comment: I would call it `readFile`. ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-processors/src/main/java/org/apache/nifi/processors/smb/FetchSmb.java: ########## @@ -0,0 +1,230 @@ +/* + * 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.smb; + +import static java.util.Arrays.asList; +import static org.apache.nifi.processor.util.StandardValidators.NON_EMPTY_EL_VALIDATOR; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +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.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +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.schema.access.SchemaNotFoundException; +import org.apache.nifi.serialization.MalformedRecordException; +import org.apache.nifi.serialization.RecordReader; +import org.apache.nifi.serialization.RecordReaderFactory; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.services.smb.SmbClientProviderService; +import org.apache.nifi.services.smb.SmbClientService; +import org.apache.nifi.services.smb.SmbException; + +@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) +@Tags({"samba, smb, cifs, files", "fetch"}) +@CapabilityDescription("Fetches files from a SMB Share. Designed to be used in tandem with ListSmb.") +@SeeAlso({ListSmb.class, PutSmbFile.class, GetSmbFile.class}) +@WritesAttributes({ + @WritesAttribute(attribute = FetchSmb.ERROR_CODE_ATTRIBUTE, description = "The error code returned by SMB when the fetch of a file fails"), + @WritesAttribute(attribute = FetchSmb.ERROR_MESSAGE_ATTRIBUTE, description = "The error message returned by SMB when the fetch of a file fails") +}) +public class FetchSmb extends AbstractProcessor { + + public static final String ERROR_CODE_ATTRIBUTE = "error.code"; + public static final String ERROR_MESSAGE_ATTRIBUTE = "error.message"; + + public static final PropertyDescriptor REMOTE_FILE = new PropertyDescriptor + .Builder().name("remote-file") + .displayName("Remote File") + .description("The full path of the file to be retrieved from the remote server.") + .required(true) + .defaultValue("${absolute.path}/${filename}") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor REMOTE_FILE_FIELD = new PropertyDescriptor + .Builder().name("remote-file-field") + .displayName("Remote File Field") + .description("The name of the filed in a record to use as the remote file name. This field is being used " + + "when record reader is set.") + .required(true) + .defaultValue("identifier") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor SMB_CLIENT_PROVIDER_SERVICE = new Builder() + .name("smb-client-provider-service") + .displayName("SMB Client Provider Service") + .description("Specifies the SMB client provider to use for creating SMB connections.") + .required(true) + .identifiesControllerService(SmbClientProviderService.class) + .build(); + + static final PropertyDescriptor RECORD_READER = new PropertyDescriptor.Builder() + .name("record-reader") + .displayName("Record Reader") + .description( + "Specifies the Controller Service to use for reading incoming NiFi Records. Each record should contain \"identifier\"" + + " attribute set to the path and name of the file to fetch." + + " If not set, the Processor expects as attributes of a separate flowfile for each File to fetch.") + .identifiesControllerService(RecordReaderFactory.class) + .required(false) + .build(); + + public static final Relationship REL_SUCCESS = + new Relationship.Builder() + .name("success") + .description("A flowfile will be routed here for each successfully fetched File.") + .build(); + + public static final Relationship REL_FAILURE = + new Relationship.Builder().name("failure") + .description( + "A flowfile will be routed here for each File for which fetch was attempted but failed.") + .build(); + + public static final Relationship REL_INPUT_FAILURE = + new Relationship.Builder().name("input_failure") + .description("The incoming flowfile will be routed here if its content could not be processed.") + .build(); + + public static final Set<Relationship> relationships = Collections.unmodifiableSet(new HashSet<>(asList( + REL_SUCCESS, + REL_FAILURE, + REL_INPUT_FAILURE + ))); + + private static final List<PropertyDescriptor> PROPERTIES = asList( + REMOTE_FILE, + REMOTE_FILE_FIELD, + SMB_CLIENT_PROVIDER_SERVICE, + RECORD_READER + ); + + @Override + public Set<Relationship> getRelationships() { + return relationships; + } + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @Override + public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { + FlowFile flowFile = session.get(); + if (flowFile == null) { + return; + } + + boolean shouldDeleteFlowFile = false; + + final SmbClientProviderService clientProviderService = + context.getProperty(SMB_CLIENT_PROVIDER_SERVICE).asControllerService(SmbClientProviderService.class); + + try (SmbClientService client = clientProviderService.getClient()) { + + if (context.getProperty(RECORD_READER).isSet()) { + final RecordReaderFactory recordReaderFactory = + context.getProperty(RECORD_READER).asControllerService(RecordReaderFactory.class); + + try (InputStream inFlowFile = session.read(flowFile); + RecordReader reader = recordReaderFactory.createRecordReader(flowFile, inFlowFile, getLogger()) + ) { + final String fieldName = context.getProperty(REMOTE_FILE_FIELD).getValue(); + Record record; + while ((record = reader.nextRecord()) != null) { + final String fileName = record.getAsString(fieldName); + final FlowFile outFlowFile = session.create(flowFile); + fetchAndTransfer(session, client, outFlowFile, fileName); + } + shouldDeleteFlowFile = true; + } catch (Exception e) { + handleInputError(e, session, flowFile); + } + } else { + PropertyValue property = context.getProperty(REMOTE_FILE); + final String fileName = property.evaluateAttributeExpressions(flowFile).getValue(); + fetchAndTransfer(session, client, flowFile, fileName); + } + } catch (Exception e) { + getLogger().error("Couldn't connect to smb due to "+ e.getMessage()); + flowFile = session.putAttribute(flowFile, ERROR_CODE_ATTRIBUTE, getErrorCode(e)); + flowFile = session.putAttribute(flowFile, ERROR_MESSAGE_ATTRIBUTE, e.getMessage()); + session.transfer(flowFile, REL_FAILURE); + } Review Comment: In what circumstances can it happen? I think the REL_INPUT_FAILURE would be more adequate here too and rollback may also be needed. ########## nifi-nar-bundles/nifi-smb-bundle/nifi-smb-processors/src/main/java/org/apache/nifi/processors/smb/FetchSmb.java: ########## @@ -0,0 +1,230 @@ +/* + * 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.smb; + +import static java.util.Arrays.asList; +import static org.apache.nifi.processor.util.StandardValidators.NON_EMPTY_EL_VALIDATOR; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +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.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.flowfile.FlowFile; +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.schema.access.SchemaNotFoundException; +import org.apache.nifi.serialization.MalformedRecordException; +import org.apache.nifi.serialization.RecordReader; +import org.apache.nifi.serialization.RecordReaderFactory; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.services.smb.SmbClientProviderService; +import org.apache.nifi.services.smb.SmbClientService; +import org.apache.nifi.services.smb.SmbException; + +@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED) +@Tags({"samba, smb, cifs, files", "fetch"}) +@CapabilityDescription("Fetches files from a SMB Share. Designed to be used in tandem with ListSmb.") +@SeeAlso({ListSmb.class, PutSmbFile.class, GetSmbFile.class}) +@WritesAttributes({ + @WritesAttribute(attribute = FetchSmb.ERROR_CODE_ATTRIBUTE, description = "The error code returned by SMB when the fetch of a file fails"), + @WritesAttribute(attribute = FetchSmb.ERROR_MESSAGE_ATTRIBUTE, description = "The error message returned by SMB when the fetch of a file fails") +}) +public class FetchSmb extends AbstractProcessor { + + public static final String ERROR_CODE_ATTRIBUTE = "error.code"; + public static final String ERROR_MESSAGE_ATTRIBUTE = "error.message"; + + public static final PropertyDescriptor REMOTE_FILE = new PropertyDescriptor + .Builder().name("remote-file") + .displayName("Remote File") + .description("The full path of the file to be retrieved from the remote server.") + .required(true) + .defaultValue("${absolute.path}/${filename}") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor REMOTE_FILE_FIELD = new PropertyDescriptor + .Builder().name("remote-file-field") + .displayName("Remote File Field") + .description("The name of the filed in a record to use as the remote file name. This field is being used " + + "when record reader is set.") + .required(true) + .defaultValue("identifier") + .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) + .addValidator(NON_EMPTY_EL_VALIDATOR) + .build(); + + public static final PropertyDescriptor SMB_CLIENT_PROVIDER_SERVICE = new Builder() + .name("smb-client-provider-service") + .displayName("SMB Client Provider Service") + .description("Specifies the SMB client provider to use for creating SMB connections.") + .required(true) + .identifiesControllerService(SmbClientProviderService.class) + .build(); + + static final PropertyDescriptor RECORD_READER = new PropertyDescriptor.Builder() + .name("record-reader") + .displayName("Record Reader") + .description( + "Specifies the Controller Service to use for reading incoming NiFi Records. Each record should contain \"identifier\"" + + " attribute set to the path and name of the file to fetch." + + " If not set, the Processor expects as attributes of a separate flowfile for each File to fetch.") + .identifiesControllerService(RecordReaderFactory.class) + .required(false) + .build(); + + public static final Relationship REL_SUCCESS = + new Relationship.Builder() + .name("success") + .description("A flowfile will be routed here for each successfully fetched File.") + .build(); + + public static final Relationship REL_FAILURE = + new Relationship.Builder().name("failure") + .description( + "A flowfile will be routed here for each File for which fetch was attempted but failed.") + .build(); + + public static final Relationship REL_INPUT_FAILURE = + new Relationship.Builder().name("input_failure") + .description("The incoming flowfile will be routed here if its content could not be processed.") + .build(); + + public static final Set<Relationship> relationships = Collections.unmodifiableSet(new HashSet<>(asList( + REL_SUCCESS, + REL_FAILURE, + REL_INPUT_FAILURE + ))); + + private static final List<PropertyDescriptor> PROPERTIES = asList( + REMOTE_FILE, + REMOTE_FILE_FIELD, + SMB_CLIENT_PROVIDER_SERVICE, + RECORD_READER + ); + + @Override + public Set<Relationship> getRelationships() { + return relationships; + } + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @Override + public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { + FlowFile flowFile = session.get(); + if (flowFile == null) { + return; + } + + boolean shouldDeleteFlowFile = false; + + final SmbClientProviderService clientProviderService = + context.getProperty(SMB_CLIENT_PROVIDER_SERVICE).asControllerService(SmbClientProviderService.class); + + try (SmbClientService client = clientProviderService.getClient()) { + + if (context.getProperty(RECORD_READER).isSet()) { + final RecordReaderFactory recordReaderFactory = + context.getProperty(RECORD_READER).asControllerService(RecordReaderFactory.class); + + try (InputStream inFlowFile = session.read(flowFile); + RecordReader reader = recordReaderFactory.createRecordReader(flowFile, inFlowFile, getLogger()) + ) { + final String fieldName = context.getProperty(REMOTE_FILE_FIELD).getValue(); + Record record; + while ((record = reader.nextRecord()) != null) { + final String fileName = record.getAsString(fieldName); + final FlowFile outFlowFile = session.create(flowFile); + fetchAndTransfer(session, client, outFlowFile, fileName); + } + shouldDeleteFlowFile = true; + } catch (Exception e) { + handleInputError(e, session, flowFile); + } + } else { + PropertyValue property = context.getProperty(REMOTE_FILE); + final String fileName = property.evaluateAttributeExpressions(flowFile).getValue(); Review Comment: `PropertyValue property` could be `final`. However, it is more common to use `PropertyValue` in-line without declaring a variable: `context.getProperty(REMOTE_FILE).evaluateAttributeExpressions(flowFile).getValue()` -- 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]
