use of com.nextdoor.bender.deserializer.FieldNotFoundException in project bender by Nextdoor.
the class GeoIpOperation method perform.
@Override
public InternalEvent perform(InternalEvent ievent) {
String ipStr = null;
/*
* Get field containing an IP address
*/
try {
ipStr = ievent.getEventObj().getFieldAsString(this.pathToIpAddress);
} catch (FieldNotFoundException e) {
if (!this.required) {
return ievent;
}
throw new OperationException("ip address field " + this.pathToIpAddress + " does not exist");
}
if (ipStr == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("ip address field " + this.pathToIpAddress + " was null");
}
/*
* Sometimes the field contains comma separated ip addresses (ie forwarded web requests). In
* this case pick the first value in the list which is typically the user.
*/
if (!ipStr.isEmpty() && ipStr.contains(",")) {
ipStr = ipStr.split(",")[0];
}
InetAddress ipAddress = null;
try {
ipAddress = InetAddress.getByName(ipStr);
} catch (UnknownHostException e) {
if (!this.required) {
return ievent;
}
throw new OperationException(e);
}
if (ipAddress == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("ip address " + ipStr + " did not resolve");
}
CityResponse response = null;
try {
response = this.databaseReader.city(ipAddress);
} catch (IOException | GeoIp2Exception e) {
if (!this.required) {
return ievent;
}
throw new OperationException(e);
}
HashMap<String, Object> geo = new HashMap<String, Object>(1);
for (GeoProperty property : this.geoProperties) {
switch(property) {
case COUNTRY_NAME:
if (response.getCountry() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("country returned null");
}
geo.put("country_name", response.getCountry().getName());
break;
case COUNTRY_ISO_CODE:
if (response.getCountry() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("country returned null");
}
geo.put("country_iso_code", response.getCountry().getIsoCode());
break;
case SUBDIVISION_NAME:
if (response.getMostSpecificSubdivision() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("MostSpecificSubdivision returned null");
}
geo.put("subdivision_name", response.getMostSpecificSubdivision().getName());
break;
case SUBDIVISION_ISO_CODE:
if (response.getMostSpecificSubdivision() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("MostSpecificSubdivision returned null");
}
geo.put("subdivision_iso_code", response.getMostSpecificSubdivision().getIsoCode());
break;
case CITY_NAME:
if (response.getCity() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("city returned null");
}
geo.put("city_name", response.getCity().getName());
break;
case POSTAL_CODE:
if (response.getPostal() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("postal returned null");
}
geo.put("postal_code", response.getPostal().getCode());
break;
case LOCATION:
if (response.getLocation() == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("location returned null");
}
Double lat = response.getLocation().getLatitude();
Double lon = response.getLocation().getLongitude();
if (lat == null || lon == null) {
if (!this.required) {
return ievent;
}
throw new OperationException("error getting lat/lon");
}
HashMap<String, Object> location = new HashMap<String, Object>(2);
location.put("lat", lat);
location.put("lon", lon);
geo.put("location", location);
break;
}
}
try {
ievent.getEventObj().setField(this.destFieldName, geo);
} catch (FieldNotFoundException e) {
throw new OperationException(e);
}
return ievent;
}
use of com.nextdoor.bender.deserializer.FieldNotFoundException in project bender by Nextdoor.
the class RegexSubstitution method doSubstitution.
@Override
protected void doSubstitution(InternalEvent ievent, DeserializedEvent devent, Map<String, Object> nested) {
Pair<String, Map<String, Object>> kv;
try {
kv = getRegexMatches(devent);
} catch (FieldNotFoundException e) {
if (this.failSrcNotFound) {
throw new OperationException(e);
}
return;
}
nested.putAll(kv.getValue());
/*
* Remove source field
*/
if (this.removeSrcField) {
try {
devent.removeField(kv.getKey());
} catch (FieldNotFoundException e) {
if (this.failSrcNotFound) {
throw new OperationException(e);
}
}
}
}
use of com.nextdoor.bender.deserializer.FieldNotFoundException in project bender by Nextdoor.
the class RegexSubstitution method doSubstitution.
@Override
protected void doSubstitution(InternalEvent ievent, DeserializedEvent devent) {
Pair<String, Map<String, Object>> kv;
try {
kv = getRegexMatches(devent);
} catch (FieldNotFoundException e) {
if (this.failSrcNotFound) {
throw new OperationException(e);
}
return;
}
((Map<String, Object>) kv.getValue()).forEach((k, v) -> {
try {
devent.setField(k, v);
} catch (FieldNotFoundException e) {
if (this.failDstNotFound) {
throw new OperationException(e);
}
}
});
/*
* Do not remove source field if it has been replaced by a regex group.
*/
if (this.removeSrcField && !kv.getValue().containsKey(kv.getKey())) {
try {
devent.removeField(kv.getKey());
} catch (FieldNotFoundException e) {
if (this.failSrcNotFound) {
throw new OperationException(e);
}
}
}
}
use of com.nextdoor.bender.deserializer.FieldNotFoundException in project bender by Nextdoor.
the class RegexSubstitution method getRegexMatches.
/**
* Matches a regex against a field and extracts matching groups.
*
* @param devent
* @param config
* @return
* @throws FieldNotFoundException
*/
private Pair<String, Map<String, Object>> getRegexMatches(DeserializedEvent devent) throws FieldNotFoundException {
String foundSourceField = null;
Matcher matcher = null;
for (String sourceField : this.srcFields) {
String sourceValue;
try {
sourceValue = devent.getFieldAsString(sourceField);
} catch (FieldNotFoundException e) {
continue;
}
matcher = pattern.matcher(sourceValue);
if (matcher.find()) {
/*
* Keep track of the field name that we use so it can be removed later.
*/
foundSourceField = sourceField;
break;
}
}
if (foundSourceField == null) {
throw new FieldNotFoundException("unable to find field in: " + this.srcFields);
}
/*
* Go through each match group in the field config and attempt to add that match group from the
* regex match. If field type coercion does not succeed then field is skipped.
*/
Map<String, Object> matchedGroups = new HashMap<String, Object>(matcher.groupCount());
try {
for (RegexSubField field : this.fields) {
String matchStrVal = matcher.group(field.getRegexGroupName());
if (matchStrVal == null) {
continue;
}
switch(field.getType()) {
case BOOLEAN:
matchedGroups.put(field.getKey(), Boolean.parseBoolean(matchStrVal));
break;
case NUMBER:
matchedGroups.put(field.getKey(), NumberUtils.createNumber(matchStrVal));
break;
case STRING:
matchedGroups.put(field.getKey(), matchStrVal);
break;
default:
matchedGroups.put(field.getKey(), matchStrVal);
break;
}
}
} catch (NumberFormatException e) {
throw new FieldNotFoundException("matched field is not a number");
}
return new ImmutablePair<String, Map<String, Object>>(foundSourceField, matchedGroups);
}
use of com.nextdoor.bender.deserializer.FieldNotFoundException in project bender by Nextdoor.
the class FieldSubstitution method doSubstitution.
@Override
protected void doSubstitution(InternalEvent ievent, DeserializedEvent devent, Map<String, Object> nested) {
Pair<String, Object> kv;
try {
kv = getFieldAndSource(devent, srcFields, false);
} catch (FieldNotFoundException e) {
if (this.failSrcNotFound) {
throw new OperationException(e);
}
return;
}
nested.put(this.key, kv.getValue());
/*
* Remove source field
*/
if (this.removeSrcField) {
devent.deleteField(kv.getKey());
}
}
Aggregations