Search in sources :

Example 1 with OperationException

use of com.nextdoor.bender.operation.OperationException in project bender by Nextdoor.

the class GelfOperation method prefix.

protected InternalEvent prefix(InternalEvent ievent) {
    DeserializedEvent devent;
    if ((devent = ievent.getEventObj()) == null) {
        return null;
    }
    Object payload = devent.getPayload();
    if (payload == null) {
        return null;
    }
    if (!(payload instanceof JsonObject)) {
        throw new OperationException("Payload data is not a JsonObject");
    }
    JsonObject obj = (JsonObject) payload;
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);
    /*
     * Prefix additional fields with "_". Everything that is not a GELF field is additional.
     */
    for (Entry<String, JsonElement> entry : orgEntries) {
        String key = entry.getKey();
        if (GELF_FIELDS.contains(key)) {
            continue;
        }
        JsonElement val = entry.getValue();
        obj.remove(key);
        obj.add("_" + key, val);
    }
    return ievent;
}
Also used : DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) Entry(java.util.Map.Entry) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) OperationException(com.nextdoor.bender.operation.OperationException) HashSet(java.util.HashSet)

Example 2 with OperationException

use of com.nextdoor.bender.operation.OperationException in project bender by Nextdoor.

the class PayloadOperation method perform.

/**
 * The {@link DeserializedEvent} payload must be a {@link JsonObject}.
 *
 * @param ievent Event that contains a JSON object deserialized payload.
 * @return
 */
public InternalEvent perform(InternalEvent ievent) {
    DeserializedEvent devent;
    if ((devent = ievent.getEventObj()) == null) {
        return null;
    }
    Object payload = devent.getPayload();
    if (payload == null) {
        return null;
    }
    if (!(payload instanceof JsonObject)) {
        throw new OperationException("Payload data is not a JsonObject");
    }
    perform((JsonObject) payload);
    return ievent;
}
Also used : DeserializedEvent(com.nextdoor.bender.deserializer.DeserializedEvent) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) OperationException(com.nextdoor.bender.operation.OperationException)

Example 3 with OperationException

use of com.nextdoor.bender.operation.OperationException in project bender by Nextdoor.

the class BaseHandlerTest method testOperationException.

@Test
public void testOperationException() throws HandlerException {
    BaseHandler.CONFIG_FILE = "/config/handler_config.json";
    handler.skipWriteStats = true;
    List<DummyEvent> events = new ArrayList<DummyEvent>(1);
    events.add(new DummyEvent("foo", 0));
    TestContext context = new TestContext();
    context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:tag");
    handler.init(context);
    List<OperationProcessor> operationProcessors = handler.sources.get(0).getOperationProcessors();
    for (OperationProcessor operationProcessor : operationProcessors) {
        BaseOperation operation = spy(operationProcessor.getOperation());
        doThrow(new OperationException("expected")).when(operation).perform(any());
        operationProcessor.setOperation(operation);
    }
    handler.handler(events, context);
    assertEquals(1, operationProcessors.get(0).getErrorCountStat().getValue());
}
Also used : TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) BaseOperation(com.nextdoor.bender.operation.BaseOperation) OperationProcessor(com.nextdoor.bender.operation.OperationProcessor) OperationException(com.nextdoor.bender.operation.OperationException) Test(org.junit.Test)

Example 4 with OperationException

use of com.nextdoor.bender.operation.OperationException 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().getField(this.pathToIpAddress);
    } catch (NoSuchElementException 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;
        }
    }
    ievent.getEventObj().setField(this.destFieldName, geo);
    return ievent;
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) 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) NoSuchElementException(java.util.NoSuchElementException) OperationException(com.nextdoor.bender.operation.OperationException)

Example 5 with OperationException

use of com.nextdoor.bender.operation.OperationException in project bender by Nextdoor.

the class GeoIpOperationTest method testInvalidIpRequired.

@Test(expected = UnknownHostException.class)
public void testInvalidIpRequired() throws Throwable {
    GeoIpOperation op = setup(Arrays.asList(GeoProperty.LOCATION), true);
    DummpyEvent devent = new DummpyEvent();
    devent.setField("ip_address", "noanip");
    InternalEvent ievent = new InternalEvent("", null, 0);
    ievent.setEventObj(devent);
    try {
        op.perform(ievent);
    } catch (OperationException e) {
        throw e.getCause();
    }
}
Also used : OperationException(com.nextdoor.bender.operation.OperationException) InternalEvent(com.nextdoor.bender.InternalEvent) Test(org.junit.Test)

Aggregations

OperationException (com.nextdoor.bender.operation.OperationException)7 JsonObject (com.google.gson.JsonObject)3 Test (org.junit.Test)3 InternalEvent (com.nextdoor.bender.InternalEvent)2 DeserializedEvent (com.nextdoor.bender.deserializer.DeserializedEvent)2 JsonElement (com.google.gson.JsonElement)1 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)1 CityResponse (com.maxmind.geoip2.model.CityResponse)1 TestContext (com.nextdoor.bender.aws.TestContext)1 BaseOperation (com.nextdoor.bender.operation.BaseOperation)1 OperationProcessor (com.nextdoor.bender.operation.OperationProcessor)1 GeoProperty (com.nextdoor.bender.operations.geo.GeoIpOperationConfig.GeoProperty)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 NoSuchElementException (java.util.NoSuchElementException)1