Search in sources :

Example 1 with DeserializationException

use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.

the class GenericJsonDeserializer method deserialize.

@Override
public DeserializedEvent deserialize(String raw) {
    GenericJsonEvent devent = new GenericJsonEvent(null);
    JsonElement elm;
    try {
        elm = parser.parse(raw);
    } catch (JsonSyntaxException e) {
        throw new DeserializationException(e);
    }
    if (!elm.isJsonObject()) {
        throw new DeserializationException("event is not a json object");
    }
    JsonObject obj = elm.getAsJsonObject();
    /*
     * Convert fields which are nested json strings into json objects
     */
    for (FieldConfig fconfig : this.nestedFieldConfigs) {
        if (obj.has(fconfig.getField())) {
            JsonElement msg = obj.get(fconfig.getField());
            /*
         * Find the JSON in the string and replace the field
         */
            NestedData data = deserialize(msg);
            obj.remove(fconfig.getField());
            obj.add(fconfig.getField(), data.nested);
            /*
         * If the string contained data before the JSON store it in a new field
         */
            if (fconfig.getPrefixField() != null && data.prefix != null) {
                obj.add(fconfig.getPrefixField(), data.prefix);
            }
        }
    }
    if (rootNodeOverridePath != null) {
        obj = JsonPathProvider.read(obj, rootNodeOverridePath);
        if (obj == null) {
            throw new DeserializationException(rootNodeOverridePath + " path not found in object");
        }
    }
    devent.setPayload(obj);
    return devent;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) FieldConfig(com.nextdoor.bender.deserializer.json.GenericJsonDeserializerConfig.FieldConfig) JsonObject(com.google.gson.JsonObject) DeserializationException(com.nextdoor.bender.deserializer.DeserializationException)

Example 2 with DeserializationException

use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.

the class Re2jRegexDeserializer method deserialize.

@Override
public DeserializedEvent deserialize(String raw) {
    Matcher m = this.pattern.matcher(raw);
    if (!m.matches()) {
        throw new DeserializationException("raw event does not match string");
    }
    int groups = m.groupCount();
    Map<String, Object> mapping = new HashMap<>(groups);
    for (int i = 0; i < groups && i < fields.size(); i++) {
        String str = m.group(i + 1);
        ReFieldConfig field = this.fields.get(i);
        mapping.put(field.getName(), RegexDeserializer.parse(str, field.getType()));
    }
    return new RegexEvent(mapping);
}
Also used : Matcher(com.google.re2j.Matcher) HashMap(java.util.HashMap) DeserializationException(com.nextdoor.bender.deserializer.DeserializationException)

Example 3 with DeserializationException

use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.

the class RegexDeserializer method deserialize.

@Override
public DeserializedEvent deserialize(String raw) {
    Matcher m = this.pattern.matcher(raw);
    if (!m.matches()) {
        throw new DeserializationException("raw event does not match string");
    }
    int groups = m.groupCount();
    Map<String, Object> mapping = new HashMap<>(groups);
    for (int i = 0; i < groups && i < fields.size(); i++) {
        String str = m.group(i + 1);
        ReFieldConfig field = this.fields.get(i);
        mapping.put(field.getName(), parse(str, field.getType()));
    }
    return new RegexEvent(mapping);
}
Also used : Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) DeserializationException(com.nextdoor.bender.deserializer.DeserializationException)

Example 4 with DeserializationException

use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.

the class BaseHandlerTest method testFilterFailedDeserialization.

@Test
public void testFilterFailedDeserialization() 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);
    DeserializerProcessor proc = handler.sources.get(0).getDeserProcessor();
    Deserializer deserSpy = spy(proc.getDeserializer());
    doThrow(new DeserializationException("expected")).when(deserSpy).deserialize(anyString());
    proc.setDeserializer(deserSpy);
    handler.handler(events, context);
    assertEquals(0, BufferedTransporter.output.size());
}
Also used : Deserializer(com.nextdoor.bender.deserializer.Deserializer) TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) DeserializerProcessor(com.nextdoor.bender.deserializer.DeserializerProcessor) DeserializationException(com.nextdoor.bender.deserializer.DeserializationException) Test(org.junit.Test)

Example 5 with DeserializationException

use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.

the class BaseHandlerTest method testSerializationException.

@Test
public void testSerializationException() throws HandlerException, SerializationException {
    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);
    Serializer serSpy = spy(handler.ser.getSerializer());
    doThrow(new DeserializationException("")).when(serSpy).serialize(any());
    handler.ser.setSerializer(serSpy);
    handler.handler(events, context);
    assertEquals(1, handler.ser.getErrorCountStat().getValue());
}
Also used : TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) DeserializationException(com.nextdoor.bender.deserializer.DeserializationException) Serializer(com.nextdoor.bender.serializer.Serializer) Test(org.junit.Test)

Aggregations

DeserializationException (com.nextdoor.bender.deserializer.DeserializationException)6 TestContext (com.nextdoor.bender.aws.TestContext)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Deserializer (com.nextdoor.bender.deserializer.Deserializer)2 DeserializerProcessor (com.nextdoor.bender.deserializer.DeserializerProcessor)2 HashMap (java.util.HashMap)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 Matcher (com.google.re2j.Matcher)1 FieldConfig (com.nextdoor.bender.deserializer.json.GenericJsonDeserializerConfig.FieldConfig)1 Serializer (com.nextdoor.bender.serializer.Serializer)1 Matcher (java.util.regex.Matcher)1