From d7347a2dc3f73e76d03b9b3ef95015dd539e16ac Mon Sep 17 00:00:00 2001 From: Mike Thomsen Date: Tue, 28 Nov 2017 10:32:11 -0500 Subject: [PATCH] NIFI-4644 Fixed LookupService API to allow for more than String/String lookup pairs. This closes #2304. --- .../nifi/lookup/script/ScriptedLookupService.java | 2 +- .../nifi/processors/standard/LookupRecord.java | 5 +++-- .../processors/standard/TestLookupAttribute.java | 2 +- .../nifi/processors/standard/TestLookupRecord.java | 12 ++++++------ .../hbase/HBase_1_1_2_RecordLookupService.java | 8 ++++++-- .../nifi/hbase/TestRecordLookupProcessor.java | 2 +- .../java/org/apache/nifi/lookup/LookupService.java | 2 +- .../apache/nifi/lookup/RecordLookupService.java | 14 -------------- .../apache/nifi/lookup/StringLookupService.java | 14 -------------- .../apache/nifi/lookup/CSVRecordLookupService.java | 4 ++-- .../nifi/lookup/SimpleCsvFileLookupService.java | 4 ++-- .../nifi/lookup/SimpleKeyValueLookupService.java | 4 ++-- .../CommonsConfigurationLookupService.java | 4 ++-- .../nifi/lookup/maxmind/IPLookupService.java | 9 +++++---- .../apache/nifi/mongodb/MongoDBLookupService.java | 2 +- .../nifi/mongodb/TestMongoDBLookupService.java | 6 +++--- 16 files changed, 36 insertions(+), 58 deletions(-) diff --git a/nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/main/java/org/apache/nifi/lookup/script/ScriptedLookupService.java b/nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/main/java/org/apache/nifi/lookup/script/ScriptedLookupService.java index ca79ba9b24..64fa4ec1eb 100644 --- a/nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/main/java/org/apache/nifi/lookup/script/ScriptedLookupService.java +++ b/nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/main/java/org/apache/nifi/lookup/script/ScriptedLookupService.java @@ -66,7 +66,7 @@ public class ScriptedLookupService extends AbstractScriptedControllerService imp private volatile File kerberosServiceKeytab = null; @Override - public Optional lookup(Map coordinates) throws LookupFailureException { + public Optional lookup(Map coordinates) throws LookupFailureException { // Delegate the lookup() call to the scripted LookupService return lookupService.get().lookup(coordinates); } diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java index 286f7eee79..6d7db8693a 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java @@ -259,7 +259,7 @@ public class LookupRecord extends AbstractRouteRecord, RecordPath> flowFileContext) { final Map recordPaths = flowFileContext.getKey(); - final Map lookupCoordinates = new HashMap<>(recordPaths.size()); + final Map lookupCoordinates = new HashMap<>(recordPaths.size()); for (final Map.Entry entry : recordPaths.entrySet()) { final String coordinateKey = entry.getKey(); @@ -284,7 +284,8 @@ public class LookupRecord extends AbstractRouteRecord lookup(Map coordinates) { + public Optional lookup(Map coordinates) { return Optional.empty(); } diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestLookupRecord.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestLookupRecord.java index 29966e7c51..8cdce71499 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestLookupRecord.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestLookupRecord.java @@ -383,12 +383,12 @@ public class TestLookupRecord { } @Override - public Optional lookup(final Map coordinates) { - if (coordinates == null) { + public Optional lookup(final Map coordinates) { + if (coordinates == null || coordinates.get("lookup") == null) { return Optional.empty(); } - final String key = coordinates.get("lookup"); + final String key = (String)coordinates.get("lookup"); if (key == null) { return Optional.empty(); } @@ -415,12 +415,12 @@ public class TestLookupRecord { } @Override - public Optional lookup(final Map coordinates) { - if (coordinates == null) { + public Optional lookup(final Map coordinates) { + if (coordinates == null || coordinates.get("lookup") == null) { return Optional.empty(); } - final String key = coordinates.get("lookup"); + final String key = (String)coordinates.get("lookup"); if (key == null) { return Optional.empty(); } diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-hbase_1_1_2-client-service-bundle/nifi-hbase_1_1_2-client-service/src/main/java/org/apache/nifi/hbase/HBase_1_1_2_RecordLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-hbase_1_1_2-client-service-bundle/nifi-hbase_1_1_2-client-service/src/main/java/org/apache/nifi/hbase/HBase_1_1_2_RecordLookupService.java index 20dc0d498c..ddaa76561d 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-hbase_1_1_2-client-service-bundle/nifi-hbase_1_1_2-client-service/src/main/java/org/apache/nifi/hbase/HBase_1_1_2_RecordLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-hbase_1_1_2-client-service-bundle/nifi-hbase_1_1_2-client-service/src/main/java/org/apache/nifi/hbase/HBase_1_1_2_RecordLookupService.java @@ -114,8 +114,12 @@ public class HBase_1_1_2_RecordLookupService extends AbstractControllerService i } @Override - public Optional lookup(Map coordinates) throws LookupFailureException { - final String rowKey = coordinates.get(ROW_KEY_KEY); + public Optional lookup(Map coordinates) throws LookupFailureException { + if (coordinates.get(ROW_KEY_KEY) == null) { + return Optional.empty(); + } + + final String rowKey = coordinates.get(ROW_KEY_KEY).toString(); if (StringUtils.isBlank(rowKey)) { return Optional.empty(); } diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-hbase_1_1_2-client-service-bundle/nifi-hbase_1_1_2-client-service/src/test/java/org/apache/nifi/hbase/TestRecordLookupProcessor.java b/nifi-nar-bundles/nifi-standard-services/nifi-hbase_1_1_2-client-service-bundle/nifi-hbase_1_1_2-client-service/src/test/java/org/apache/nifi/hbase/TestRecordLookupProcessor.java index d3df016fd0..76184c7bab 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-hbase_1_1_2-client-service-bundle/nifi-hbase_1_1_2-client-service/src/test/java/org/apache/nifi/hbase/TestRecordLookupProcessor.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-hbase_1_1_2-client-service-bundle/nifi-hbase_1_1_2-client-service/src/test/java/org/apache/nifi/hbase/TestRecordLookupProcessor.java @@ -88,7 +88,7 @@ public class TestRecordLookupProcessor extends AbstractProcessor { final String rowKey = context.getProperty(HBASE_ROW).getValue(); - final Map coordinates = new HashMap<>(); + final Map coordinates = new HashMap<>(); coordinates.put(HBase_1_1_2_RecordLookupService.ROW_KEY_KEY, rowKey); final LookupService lookupService = context.getProperty(HBASE_LOOKUP_SERVICE).asControllerService(LookupService.class); diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/LookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/LookupService.java index 48ec173a73..a1f904d80f 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/LookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/LookupService.java @@ -33,7 +33,7 @@ public interface LookupService extends ControllerService { * * @throws LookupFailureException if unable to lookup a value for the given coordinates */ - Optional lookup(Map coordinates) throws LookupFailureException; + Optional lookup(Map coordinates) throws LookupFailureException; /** * @return the Class that represents the type of value that will be returned by {@link #lookup(Map)} diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/RecordLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/RecordLookupService.java index aefb880b76..4d8d8c07fb 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/RecordLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/RecordLookupService.java @@ -17,24 +17,10 @@ package org.apache.nifi.lookup; -import java.util.Map; -import java.util.Optional; - import org.apache.nifi.serialization.record.Record; public interface RecordLookupService extends LookupService { - /** - * Returns an Optional Record that corresponds to the given coordinates - * - * @param coordinates the coordinates to lookup - * @return an Optional Record that corresponds to the given coordinates - * - * @throws LookupFailureException if unable to lookup a value for the given coordinates - */ - @Override - Optional lookup(Map coordinates) throws LookupFailureException; - @Override default Class getValueType() { return Record.class; diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/StringLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/StringLookupService.java index 216dd5a41d..0acbedc6c9 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/StringLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-service-api/src/main/java/org/apache/nifi/lookup/StringLookupService.java @@ -17,22 +17,8 @@ package org.apache.nifi.lookup; -import java.util.Map; -import java.util.Optional; - public interface StringLookupService extends LookupService { - /** - * Returns an Optional value that corresponds to the given coordinates - * - * @param coordinates the coordinates to lookup - * @return an Optional String that represents the value for the given coordinates - * - * @throws LookupFailureException if unable to lookup a value for the given key - */ - @Override - Optional lookup(Map coordinates) throws LookupFailureException; - @Override default Class getValueType() { return String.class; diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/CSVRecordLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/CSVRecordLookupService.java index b526e25e6b..793ed5469f 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/CSVRecordLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/CSVRecordLookupService.java @@ -206,12 +206,12 @@ public class CSVRecordLookupService extends AbstractControllerService implements } @Override - public Optional lookup(final Map coordinates) throws LookupFailureException { + public Optional lookup(final Map coordinates) throws LookupFailureException { if (coordinates == null) { return Optional.empty(); } - final String key = coordinates.get(KEY); + final String key = (String)coordinates.get(KEY); if (StringUtils.isBlank(key)) { return Optional.empty(); } diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/SimpleCsvFileLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/SimpleCsvFileLookupService.java index ae304a97ad..878cbe8e19 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/SimpleCsvFileLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/SimpleCsvFileLookupService.java @@ -194,12 +194,12 @@ public class SimpleCsvFileLookupService extends AbstractControllerService implem } @Override - public Optional lookup(final Map coordinates) throws LookupFailureException { + public Optional lookup(final Map coordinates) throws LookupFailureException { if (coordinates == null) { return Optional.empty(); } - final String key = coordinates.get(KEY); + final String key = coordinates.get(KEY).toString(); if (StringUtils.isBlank(key)) { return Optional.empty(); } diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/SimpleKeyValueLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/SimpleKeyValueLookupService.java index 445d83c3a2..ec9371d67c 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/SimpleKeyValueLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/SimpleKeyValueLookupService.java @@ -58,12 +58,12 @@ public class SimpleKeyValueLookupService extends AbstractControllerService imple } @Override - public Optional lookup(final Map coordinates) { + public Optional lookup(final Map coordinates) { if (coordinates == null) { return Optional.empty(); } - final String key = coordinates.get(KEY); + final String key = coordinates.get(KEY).toString(); if (key == null) { return Optional.empty(); } diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/configuration2/CommonsConfigurationLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/configuration2/CommonsConfigurationLookupService.java index ef1a73f98d..9248132e9d 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/configuration2/CommonsConfigurationLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/configuration2/CommonsConfigurationLookupService.java @@ -122,12 +122,12 @@ public abstract class CommonsConfigurationLookupService lookup(final Map coordinates) throws LookupFailureException { + public Optional lookup(final Map coordinates) throws LookupFailureException { if (coordinates == null) { return Optional.empty(); } - final String key = coordinates.get(KEY); + final String key = coordinates.get(KEY).toString(); if (StringUtils.isBlank(key)) { return Optional.empty(); } diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/maxmind/IPLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/maxmind/IPLookupService.java index 58c868feb3..5661b94a8b 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/maxmind/IPLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-lookup-services-bundle/nifi-lookup-services/src/main/java/org/apache/nifi/lookup/maxmind/IPLookupService.java @@ -189,7 +189,7 @@ public class IPLookupService extends AbstractControllerService implements Record } @Override - public Optional lookup(final Map coordinates) throws LookupFailureException { + public Optional lookup(final Map coordinates) throws LookupFailureException { if (coordinates == null) { return Optional.empty(); } @@ -237,12 +237,13 @@ public class IPLookupService extends AbstractControllerService implements Record } } - private Optional doLookup(final DatabaseReader databaseReader, final Map coordinates) throws LookupFailureException, InvalidDatabaseException { - final String ipAddress = coordinates.get(IP_KEY); - if (ipAddress == null) { + private Optional doLookup(final DatabaseReader databaseReader, final Map coordinates) throws LookupFailureException, InvalidDatabaseException { + if (coordinates.get(IP_KEY) == null) { return Optional.empty(); } + final String ipAddress = coordinates.get(IP_KEY).toString(); + final InetAddress inetAddress; try { inetAddress = InetAddress.getByName(ipAddress); diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java index f85274445f..a816ca0aed 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java @@ -74,7 +74,7 @@ public class MongoDBLookupService extends MongoDBControllerService implements Lo } @Override - public Optional lookup(Map coordinates) throws LookupFailureException { + public Optional lookup(Map coordinates) throws LookupFailureException { Map clean = new HashMap<>(); clean.putAll(coordinates); Document query = new Document(clean); diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestMongoDBLookupService.java b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestMongoDBLookupService.java index ff220ae4e7..16beefe903 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestMongoDBLookupService.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-mongodb-services-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestMongoDBLookupService.java @@ -71,7 +71,7 @@ public class TestMongoDBLookupService { Document document = service.convertJson("{ \"uuid\": \"x-y-z\", \"message\": \"Hello, world\" }"); service.insert(document); - Map criteria = new HashMap<>(); + Map criteria = new HashMap<>(); criteria.put("uuid", "x-y-z"); Optional result = service.lookup(criteria); @@ -99,7 +99,7 @@ public class TestMongoDBLookupService { Document document = service.convertJson("{ \"uuid\": \"x-y-z\", \"message\": \"Hello, world\" }"); service.insert(document); - Map criteria = new HashMap<>(); + Map criteria = new HashMap<>(); criteria.put("uuid", "x-y-z"); Optional result = service.lookup(criteria); @@ -129,7 +129,7 @@ public class TestMongoDBLookupService { Document document = service.convertJson("{ \"uuid\": \"x-y-z\", \"message\": \"Hello, world\" }"); service.insert(document); - Map criteria = new HashMap<>(); + Map criteria = new HashMap<>(); criteria.put("uuid", "x-y-z"); boolean error = false;