krisztina-zsihovszki commented on code in PR #6355: URL: https://github.com/apache/nifi/pull/6355#discussion_r968478534
########## nifi-nar-bundles/nifi-box-bundle/nifi-box-processors/src/main/resources/docs/org.apache.nifi.processors.box.FetchBoxFiles/additionalDetails.html: ########## @@ -0,0 +1,36 @@ +<!DOCTYPE html> +<html lang="en"> +<!-- + 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. +--> +<head> + <meta charset="utf-8" /> + <title>ListBoxFiles</title> + + <link rel="stylesheet" href="../../../../../css/component-usage.css" type="text/css" /> +</head> +<body> +<p>This processor requires a pre-configured App under the User owning the files that are being fetched.</p> +<p>The App should have the following configuration:</p> +<ul> + <li>If you create a new App, select 'Server Authentication (with JWT)' authentication method. + If you want to use an existing App, chose one with 'OAuth 2.0 with JSON Web Tokens (Server Authentication)' as Authentication method.</li> + <li>Should have a Client ID and Client Secret</li> + <li>App Access Level should be 'App + Enterprise Access'</li> + <li>Application Scopes should have 'Read all files' enabled</li> Review Comment: Read access is not enough, download requires write access, please change this line to: "Application Scopes should have 'Write all files and folders in Box' enabled." ########## nifi-nar-bundles/nifi-box-bundle/nifi-box-processors/src/main/resources/docs/org.apache.nifi.processors.box.FetchBoxFiles/additionalDetails.html: ########## @@ -0,0 +1,36 @@ +<!DOCTYPE html> +<html lang="en"> +<!-- + 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. +--> +<head> + <meta charset="utf-8" /> + <title>ListBoxFiles</title> + + <link rel="stylesheet" href="../../../../../css/component-usage.css" type="text/css" /> +</head> +<body> +<p>This processor requires a pre-configured App under the User owning the files that are being fetched.</p> +<p>The App should have the following configuration:</p> +<ul> + <li>If you create a new App, select 'Server Authentication (with JWT)' authentication method. + If you want to use an existing App, chose one with 'OAuth 2.0 with JSON Web Tokens (Server Authentication)' as Authentication method.</li> + <li>Should have a Client ID and Client Secret</li> + <li>App Access Level should be 'App + Enterprise Access'</li> + <li>Application Scopes should have 'Read all files' enabled</li> + <li>Advanced Features should have 'Generate user access tokens' enabled</li> Review Comment: Besides 'Generate user access tokens', the 'Make API calls using the as-user header" needs to be enabled, otherwise the API returns 403 error. ########## nifi-nar-bundles/nifi-box-bundle/nifi-box-processors/src/main/java/org/apache/nifi/processors/box/ListBoxFiles.java: ########## @@ -0,0 +1,273 @@ +/* + * 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.box; + +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxFile; +import com.box.sdk.BoxFolder; +import com.box.sdk.BoxItem; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; +import org.apache.nifi.annotation.behavior.PrimaryNodeOnly; +import org.apache.nifi.annotation.behavior.Stateful; +import org.apache.nifi.annotation.behavior.TriggerSerially; +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.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.state.Scope; +import org.apache.nifi.context.PropertyContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.processor.util.list.AbstractListProcessor; +import org.apache.nifi.processor.util.list.ListedEntityTracker; +import org.apache.nifi.proxy.ProxyConfiguration; +import org.apache.nifi.proxy.ProxySpec; +import org.apache.nifi.serialization.record.RecordSchema; + +import java.io.IOException; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +@PrimaryNodeOnly +@TriggerSerially +@Tags({"box", "storage"}) +@CapabilityDescription("Lists files in a Box folder. " + + "Each listed file may result in one flowfile, the metadata being written as flowfile attributes. " + + "Or - in case the 'Record Writer' property is set - the entire result is written as records to a single flowfile. " + + "This Processor is designed to run on Primary Node only in a cluster. If the primary node changes, the new Primary Node will pick up where the " + + "previous node left off without duplicating all of the data.") +@SeeAlso({FetchBoxFiles.class}) +@InputRequirement(Requirement.INPUT_FORBIDDEN) +@WritesAttributes({@WritesAttribute(attribute = BoxFileInfo.ID, description = "The id of the file"), + @WritesAttribute(attribute = BoxFileInfo.FILENAME, description = "The name of the file"), + @WritesAttribute(attribute = BoxFileInfo.PATH, description = "The path of the file on Box"), + @WritesAttribute(attribute = BoxFileInfo.SIZE, description = "The size of the file (in bytes)"), + @WritesAttribute(attribute = BoxFileInfo.TIMESTAMP, description = "The last modified time of the file.")}) +@Stateful(scopes = {Scope.CLUSTER}, description = "The processor stores necessary data to be able to keep track what files have been listed already." + + " What exactly needs to be stored depends on the 'Listing Strategy'.") +public class ListBoxFiles extends AbstractListProcessor<BoxFileInfo> implements BoxTrait { + public static final PropertyDescriptor FOLDER_ID = new PropertyDescriptor.Builder() + .name("box-folder-id") + .displayName("Folder ID") + .description("The ID of the folder from which to pull list of files.") + .addValidator(StandardValidators.NON_EMPTY_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .required(true) + .build(); + + public static final PropertyDescriptor RECURSIVE_SEARCH = new PropertyDescriptor.Builder() + .name("recursive-search") + .displayName("Search Recursively") + .description("When 'true', will include list of files from sub-folders." + + " Otherwise, will return only files that are within the folder defined by the 'Folder ID' property.") + .required(true) + .defaultValue("true") + .allowableValues("true", "false") + .build(); + + public static final PropertyDescriptor MIN_AGE = new PropertyDescriptor.Builder() + .name("min-age") + .displayName("Minimum File Age") + .description("The minimum age a file must be in order to be considered; any files younger than this will be ignored.") + .required(true) + .addValidator(StandardValidators.TIME_PERIOD_VALIDATOR) + .defaultValue("0 sec") + .build(); + + public static final PropertyDescriptor LISTING_STRATEGY = new PropertyDescriptor.Builder() + .fromPropertyDescriptor(AbstractListProcessor.LISTING_STRATEGY) + .allowableValues(BY_TIMESTAMPS, BY_ENTITIES, BY_TIME_WINDOW, NO_TRACKING) + .build(); + + public static final PropertyDescriptor TRACKING_STATE_CACHE = new PropertyDescriptor.Builder() + .fromPropertyDescriptor(ListedEntityTracker.TRACKING_STATE_CACHE) + .dependsOn(LISTING_STRATEGY, BY_ENTITIES) + .build(); + + public static final PropertyDescriptor TRACKING_TIME_WINDOW = new PropertyDescriptor.Builder() + .fromPropertyDescriptor(ListedEntityTracker.TRACKING_TIME_WINDOW) + .dependsOn(LISTING_STRATEGY, BY_ENTITIES) + .build(); + + public static final PropertyDescriptor INITIAL_LISTING_TARGET = new PropertyDescriptor.Builder() + .fromPropertyDescriptor(ListedEntityTracker.INITIAL_LISTING_TARGET) + .dependsOn(LISTING_STRATEGY, BY_ENTITIES) + .build(); + + private static final ProxySpec[] PROXY_SPECS = {ProxySpec.HTTP_AUTH}; + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + FOLDER_ID, + USER_ID, + BOX_CONFIG_FILE, + RECURSIVE_SEARCH, + MIN_AGE, + LISTING_STRATEGY, + TRACKING_STATE_CACHE, + TRACKING_TIME_WINDOW, + INITIAL_LISTING_TARGET, + RECORD_WRITER, + ProxyConfiguration.createProxyConfigPropertyDescriptor(false, PROXY_SPECS) + )); + + private volatile BoxAPIConnection boxAPIConnection; + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @Override + protected void customValidate(ValidationContext validationContext, Collection<ValidationResult> results) { Review Comment: This method can be removed since it does not override the parent implementation (which is also an empty method) ########## nifi-nar-bundles/nifi-box-bundle/nifi-box-processors/src/test/java/org/apache/nifi/processors/box/ListBoxFilesIT.java: ########## @@ -0,0 +1,164 @@ +/* + * 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.box; + +import com.box.sdk.BoxFolder; +import org.apache.nifi.util.MockFlowFile; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * See Javadoc {@link AbstractBoxFilesIT} for instructions how to run this test. + */ +public class ListBoxFilesIT extends AbstractBoxFilesIT<ListBoxFiles> { + @BeforeEach + public void init() throws Exception { + super.init(); + testRunner.setProperty(ListBoxFiles.FOLDER_ID, mainFolderId); + testRunner.setProperty(ListBoxFiles.USER_ID, USER_ID); + testRunner.setProperty(ListBoxFiles.BOX_CONFIG_FILE, BOX_CONFIG_FILE); + } + + @Override + public ListBoxFiles createTestSubject() { + ListBoxFiles testSubject = new ListBoxFiles(); + + return testSubject; + } + + @Test + void listFilesFrom3LayerDeepDirectoryTree() throws Exception { + // GIVEN + BoxFolder.Info main_sub1 = createFolder("main_sub1", mainFolderId); + BoxFolder.Info main_sub2 = createFolder("main_sub2", mainFolderId); + + BoxFolder.Info main_sub1_sub1 = createFolder("main_sub1_sub1", main_sub1.getID()); + + createFileWithDefaultContent("main_file1", mainFolderId); + createFileWithDefaultContent("main_file2", mainFolderId); + createFileWithDefaultContent("main_file3", mainFolderId); + + createFileWithDefaultContent("main_sub1_file1", main_sub1.getID()); + + createFileWithDefaultContent("main_sub2_file1", main_sub2.getID()); + createFileWithDefaultContent("main_sub2_file2", main_sub2.getID()); + + createFileWithDefaultContent("main_sub1_sub1_file1", main_sub1_sub1.getID()); + createFileWithDefaultContent("main_sub1_sub1_file2", main_sub1_sub1.getID()); + createFileWithDefaultContent("main_sub1_sub1_file3", main_sub1_sub1.getID()); + + Set<String> expectedFileNames = new HashSet<>(Arrays.asList( + "main_file1", "main_file2", "main_file3", + "main_sub1_file1", + "main_sub2_file1", "main_sub2_file2", + "main_sub1_sub1_file1", "main_sub1_sub1_file2", "main_sub1_sub1_file3" + )); + + // The creation of the files are not (completely) synchronized. + Thread.sleep(2000); + + // WHEN + testRunner.run(); + + // THEN + List<MockFlowFile> successFlowFiles = testRunner.getFlowFilesForRelationship(ListBoxFiles.REL_SUCCESS); + + Set<String> actualFileNames = successFlowFiles.stream() Review Comment: Please consider extracting this call to a method since the same logic is used to collect actual filenames in 4 places. ########## nifi-nar-bundles/nifi-box-bundle/nifi-box-processors/src/test/java/org/apache/nifi/processors/box/AbstractBoxFilesIT.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.box; + +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxConfig; +import com.box.sdk.BoxDeveloperEditionAPIConnection; +import com.box.sdk.BoxFile; +import com.box.sdk.BoxFolder; +import org.apache.nifi.processor.Processor; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import java.io.ByteArrayInputStream; +import java.io.FileReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Set the following constants before running:<br /> + * <br /> + * FOLDER_ID - The ID of a Folder the test can use to create files and sub-folders within.<br /> + * USER_ID - The ID of the user owning the Folder.<br /> + * BOX_CONFIG_FILE - An App Settings Configuration JSON file. The app needs to have 'OAuth 2.0 with JSON Web Tokens (Server Authentication)' authentication method, Review Comment: Please mention the "Make API calls using the as-user header" here as well. ########## nifi-nar-bundles/nifi-box-bundle/nifi-box-processors/src/main/resources/docs/org.apache.nifi.processors.box.FetchBoxFiles/additionalDetails.html: ########## @@ -0,0 +1,36 @@ +<!DOCTYPE html> +<html lang="en"> +<!-- + 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. +--> +<head> + <meta charset="utf-8" /> + <title>ListBoxFiles</title> Review Comment: Wrong title, it should be FetchBoxFiles. ########## nifi-nar-bundles/nifi-box-bundle/nifi-box-processors/src/main/java/org/apache/nifi/processors/box/ListBoxFiles.java: ########## @@ -0,0 +1,273 @@ +/* + * 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.box; + +import com.box.sdk.BoxAPIConnection; +import com.box.sdk.BoxFile; +import com.box.sdk.BoxFolder; +import com.box.sdk.BoxItem; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; +import org.apache.nifi.annotation.behavior.PrimaryNodeOnly; +import org.apache.nifi.annotation.behavior.Stateful; +import org.apache.nifi.annotation.behavior.TriggerSerially; +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.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.state.Scope; +import org.apache.nifi.context.PropertyContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.processor.util.list.AbstractListProcessor; +import org.apache.nifi.processor.util.list.ListedEntityTracker; +import org.apache.nifi.proxy.ProxyConfiguration; +import org.apache.nifi.proxy.ProxySpec; +import org.apache.nifi.serialization.record.RecordSchema; + +import java.io.IOException; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +@PrimaryNodeOnly +@TriggerSerially +@Tags({"box", "storage"}) +@CapabilityDescription("Lists files in a Box folder. " + + "Each listed file may result in one flowfile, the metadata being written as flowfile attributes. " + Review Comment: Please consider using "FlowFile" instead of "flowfile" in descriptions. ########## nifi-nar-bundles/nifi-box-bundle/nifi-box-processors/src/main/resources/docs/org.apache.nifi.processors.box.FetchBoxFiles/additionalDetails.html: ########## @@ -0,0 +1,36 @@ +<!DOCTYPE html> +<html lang="en"> +<!-- + 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. +--> +<head> + <meta charset="utf-8" /> + <title>ListBoxFiles</title> + + <link rel="stylesheet" href="../../../../../css/component-usage.css" type="text/css" /> +</head> +<body> +<p>This processor requires a pre-configured App under the User owning the files that are being fetched.</p> +<p>The App should have the following configuration:</p> +<ul> + <li>If you create a new App, select 'Server Authentication (with JWT)' authentication method. + If you want to use an existing App, chose one with 'OAuth 2.0 with JSON Web Tokens (Server Authentication)' as Authentication method.</li> + <li>Should have a Client ID and Client Secret</li> Review Comment: Please mention the private key generation and the application authorization as well. -- 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]
