Search in sources :

Example 1 with FieldNotFoundException

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) CityResponse(com.maxmind.geoip2.model.CityResponse) InetAddress(java.net.InetAddress) GeoProperty(com.nextdoor.bender.operations.geo.GeoIpOperationConfig.GeoProperty) OperationException(com.nextdoor.bender.operation.OperationException)

Example 2 with FieldNotFoundException

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);
            }
        }
    }
}
Also used : FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map) OperationException(com.nextdoor.bender.operation.OperationException)

Example 3 with FieldNotFoundException

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);
            }
        }
    }
}
Also used : FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map) OperationException(com.nextdoor.bender.operation.OperationException)

Example 4 with FieldNotFoundException

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);
}
Also used : RegexSubField(com.nextdoor.bender.operation.substitution.regex.RegexSubstitutionConfig.RegexSubField) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException)

Example 5 with FieldNotFoundException

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());
    }
}
Also used : FieldNotFoundException(com.nextdoor.bender.deserializer.FieldNotFoundException) OperationException(com.nextdoor.bender.operation.OperationException)

Aggregations

FieldNotFoundException (com.nextdoor.bender.deserializer.FieldNotFoundException)17 OperationException (com.nextdoor.bender.operation.OperationException)9 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 JsonObject (com.google.gson.JsonObject)4 DeserializedEvent (com.nextdoor.bender.deserializer.DeserializedEvent)3 ArrayList (java.util.ArrayList)3 InternalEvent (com.nextdoor.bender.InternalEvent)2 DummyStringEvent (com.nextdoor.bender.testutils.DummyDeserializerHelper.DummyStringEvent)2 Map (java.util.Map)2 JsonNull (com.google.gson.JsonNull)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 InvalidPathException (com.jayway.jsonpath.InvalidPathException)1 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)1 CityResponse (com.maxmind.geoip2.model.CityResponse)1 Variable (com.nextdoor.bender.operation.substitution.Variable)1 RegexSubField (com.nextdoor.bender.operation.substitution.regex.RegexSubstitutionConfig.RegexSubField)1 GeoProperty (com.nextdoor.bender.operations.geo.GeoIpOperationConfig.GeoProperty)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1