use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.
the class HBase_1_1_2_RecordLookupService method lookup.
@Override
public Optional<Record> lookup(Map<String, Object> 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();
}
final byte[] rowKeyBytes = rowKey.getBytes(StandardCharsets.UTF_8);
try {
final Map<String, Object> values = new HashMap<>();
hBaseClientService.scan(tableName, rowKeyBytes, rowKeyBytes, columns, (byte[] row, ResultCell[] resultCells) -> {
for (final ResultCell cell : resultCells) {
final byte[] qualifier = Arrays.copyOfRange(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierOffset() + cell.getQualifierLength());
final byte[] value = Arrays.copyOfRange(cell.getValueArray(), cell.getValueOffset(), cell.getValueOffset() + cell.getValueLength());
values.put(new String(qualifier, charset), new String(value, charset));
}
});
if (values.size() > 0) {
final List<RecordField> fields = new ArrayList<>();
for (String key : values.keySet()) {
fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
}
final RecordSchema schema = new SimpleRecordSchema(fields);
return Optional.ofNullable(new MapRecord(schema, values));
} else {
return Optional.empty();
}
} catch (IOException e) {
getLogger().error("Error occurred loading {}", new Object[] { coordinates.get("rowKey") }, e);
throw new LookupFailureException(e);
}
}
use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.
the class IPLookupService method doLookup.
private Optional<Record> doLookup(final DatabaseReader databaseReader, final Map<String, Object> 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);
} catch (final IOException ioe) {
getLogger().warn("Could not resolve the IP for value '{}'. This is usually caused by issue resolving the appropriate DNS record or " + "providing the service with an invalid IP address", new Object[] { coordinates }, ioe);
return Optional.empty();
}
final Record geoRecord;
if (getProperty(LOOKUP_CITY).asBoolean()) {
final CityResponse cityResponse;
try {
cityResponse = databaseReader.city(inetAddress);
} catch (final InvalidDatabaseException idbe) {
throw idbe;
} catch (final Exception e) {
throw new LookupFailureException("Failed to lookup City information for IP Address " + inetAddress, e);
}
geoRecord = createRecord(cityResponse);
} else {
geoRecord = null;
}
final Record ispRecord;
if (getProperty(LOOKUP_ISP).asBoolean()) {
final IspResponse ispResponse;
try {
ispResponse = databaseReader.isp(inetAddress);
} catch (final InvalidDatabaseException idbe) {
throw idbe;
} catch (final Exception e) {
throw new LookupFailureException("Failed to lookup ISP information for IP Address " + inetAddress, e);
}
ispRecord = createRecord(ispResponse);
} else {
ispRecord = null;
}
final String domainName;
if (getProperty(LOOKUP_DOMAIN).asBoolean()) {
final DomainResponse domainResponse;
try {
domainResponse = databaseReader.domain(inetAddress);
} catch (final InvalidDatabaseException idbe) {
throw idbe;
} catch (final Exception e) {
throw new LookupFailureException("Failed to lookup Domain information for IP Address " + inetAddress, e);
}
domainName = domainResponse == null ? null : domainResponse.getDomain();
} else {
domainName = null;
}
final String connectionType;
if (getProperty(LOOKUP_CONNECTION_TYPE).asBoolean()) {
final ConnectionTypeResponse connectionTypeResponse;
try {
connectionTypeResponse = databaseReader.connectionType(inetAddress);
} catch (final InvalidDatabaseException idbe) {
throw idbe;
} catch (final Exception e) {
throw new LookupFailureException("Failed to lookup Domain information for IP Address " + inetAddress, e);
}
if (connectionTypeResponse == null) {
connectionType = null;
} else {
final ConnectionType type = connectionTypeResponse.getConnectionType();
connectionType = type == null ? null : type.name();
}
} else {
connectionType = null;
}
final Record anonymousIpRecord;
if (getProperty(LOOKUP_ANONYMOUS_IP_INFO).asBoolean()) {
final AnonymousIpResponse anonymousIpResponse;
try {
anonymousIpResponse = databaseReader.anonymousIp(inetAddress);
} catch (final InvalidDatabaseException idbe) {
throw idbe;
} catch (final Exception e) {
throw new LookupFailureException("Failed to lookup Anonymous IP Information for IP Address " + inetAddress, e);
}
anonymousIpRecord = createRecord(anonymousIpResponse);
} else {
anonymousIpRecord = null;
}
if (geoRecord == null && ispRecord == null && domainName == null && connectionType == null && anonymousIpRecord == null) {
return Optional.empty();
}
return Optional.ofNullable(createContainerRecord(geoRecord, ispRecord, domainName, connectionType, anonymousIpRecord));
}
use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.
the class MongoDBLookupService method lookup.
@Override
public Optional<Object> lookup(Map<String, Object> coordinates) throws LookupFailureException {
Map<String, Object> clean = new HashMap<>();
clean.putAll(coordinates);
Document query = new Document(clean);
if (coordinates.size() == 0) {
throw new LookupFailureException("No keys were configured. Mongo query would return random documents.");
}
try {
Document result = this.findOne(query);
if (result == null) {
return Optional.empty();
} else if (!StringUtils.isEmpty(lookupValueField)) {
return Optional.ofNullable(result.get(lookupValueField));
} else {
final List<RecordField> fields = new ArrayList<>();
for (String key : result.keySet()) {
if (key.equals("_id")) {
continue;
}
fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
}
final RecordSchema schema = new SimpleRecordSchema(fields);
return Optional.ofNullable(new MapRecord(schema, result));
}
} catch (Exception ex) {
getLogger().error("Error during lookup {}", new Object[] { query.toJson() }, ex);
throw new LookupFailureException(ex);
}
}
use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.
the class MongoDBLookupServiceIT method testLookupSingle.
@Test
public void testLookupSingle() throws Exception {
runner.setProperty(service, MongoDBLookupService.LOOKUP_VALUE_FIELD, "message");
runner.enableControllerService(service);
Document document = service.convertJson("{ \"uuid\": \"x-y-z\", \"message\": \"Hello, world\" }");
service.insert(document);
Map<String, Object> criteria = new HashMap<>();
criteria.put("uuid", "x-y-z");
Optional result = service.lookup(criteria);
Assert.assertNotNull("The value was null.", result.get());
Assert.assertEquals("The value was wrong.", "Hello, world", result.get());
Map<String, Object> clean = new HashMap<>();
clean.putAll(criteria);
service.delete(new Document(clean));
try {
result = service.lookup(criteria);
} catch (LookupFailureException ex) {
Assert.fail();
}
Assert.assertTrue(!result.isPresent());
}
use of org.apache.nifi.lookup.LookupFailureException in project nifi by apache.
the class MongoDBLookupServiceIT method testServiceParameters.
@Test
public void testServiceParameters() throws Exception {
runner.enableControllerService(service);
Document document = service.convertJson("{ \"uuid\": \"x-y-z\", \"message\": \"Hello, world\" }");
service.insert(document);
Map<String, Object> criteria = new HashMap<>();
criteria.put("uuid", "x-y-z");
boolean error = false;
try {
service.lookup(criteria);
} catch (Exception ex) {
error = true;
}
Assert.assertFalse("An error was thrown when no error should have been thrown.", error);
error = false;
try {
service.lookup(new HashMap());
} catch (Exception ex) {
error = true;
Assert.assertTrue("The exception was the wrong type", ex instanceof LookupFailureException);
}
Assert.assertTrue("An error was not thrown when the input was empty", error);
}
Aggregations