Search in sources :

Example 1 with Message

use of org.apache.apex.malhar.lib.appdata.schemas.Message in project apex-malhar by apache.

the class MessageDeserializerFactory method deserialize.

/**
 * This is the method that is called to deserialize a given json string into a Message object of
 * the appropriate type.
 * @param json The json to deserialize.
 * @return The deserialized Message.
 * @throws IOException
 */
public Message deserialize(String json) throws IOException {
    String type;
    try {
        JSONObject jsonObject = new JSONObject(json);
        type = jsonObject.getString(Message.FIELD_TYPE);
    } catch (JSONException e) {
        throw new IOException(e);
    }
    CustomMessageDeserializer cqb = typeToCustomQueryBuilder.get(type);
    if (cqb == null) {
        throw new IOException("The query type " + type + " does not have a corresponding deserializer.");
    }
    CustomMessageValidator cqv = typeToCustomQueryValidator.get(type);
    Object context = deserializationContext.get(typeToClass.get(type));
    Message data = cqb.deserialize(json, typeToClass.get(type), context);
    LOG.debug("{}", data);
    if (data == null || !(cqv != null && cqv.validate(data, context))) {
        return null;
    }
    data.setType(type);
    return data;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) Message(org.apache.apex.malhar.lib.appdata.schemas.Message) JSONException(org.codehaus.jettison.json.JSONException) JSONObject(org.codehaus.jettison.json.JSONObject) IOException(java.io.IOException)

Example 2 with Message

use of org.apache.apex.malhar.lib.appdata.schemas.Message in project apex-malhar by apache.

the class MessageDeserializerFactory method setClasses.

/**
 * This is a helper method which validates setting the desired class that data will be deserialized into.
 * @param schemas The classes that data will be deserialized into.
 */
private void setClasses(Class<? extends Message>[] schemas) {
    Preconditions.checkArgument(schemas.length != 0, "No schemas provided.");
    Set<Class<? extends Message>> clazzes = Sets.newHashSet();
    for (Class<? extends Message> schema : schemas) {
        Preconditions.checkNotNull(schema, "Provided schema cannot be null");
        Preconditions.checkArgument(!clazzes.contains(schema), "Schema %s was passed twice.", schema);
        clazzes.add(schema);
        Annotation[] ans = schema.getAnnotations();
        String schemaType = null;
        Class<? extends CustomMessageDeserializer> cqd = null;
        Class<? extends CustomMessageValidator> cqv = null;
        for (Annotation an : ans) {
            if (an instanceof MessageType) {
                if (schemaType != null) {
                    throw new IllegalArgumentException("Cannot specify the " + MessageType.class + " annotation twice on the class: " + schema);
                }
                schemaType = ((MessageType) an).type();
                LOG.debug("Detected schemaType for {} is {}", schema, schemaType);
            } else if (an instanceof MessageDeserializerInfo) {
                if (cqd != null) {
                    throw new IllegalArgumentException("Cannot specify the " + MessageDeserializerInfo.class + " annotation twice on the class: " + schema);
                }
                cqd = ((MessageDeserializerInfo) an).clazz();
            } else if (an instanceof MessageValidatorInfo) {
                if (cqv != null) {
                    throw new IllegalArgumentException("Cannot specify the " + MessageValidatorInfo.class + " annotation twice on the class: ");
                }
                cqv = ((MessageValidatorInfo) an).clazz();
            }
        }
        if (schemaType == null) {
            throw new IllegalArgumentException("No " + MessageType.class + " annotation found on class: " + schema);
        }
        if (cqd == null) {
            throw new IllegalArgumentException("No " + MessageDeserializerInfo.class + " annotation found on class: " + schema);
        }
        if (cqv == null) {
            throw new IllegalArgumentException("No " + MessageValidatorInfo.class + " annotation found on class: " + schema);
        }
        Class<? extends Message> prevSchema = typeToClass.put(schemaType, schema);
        LOG.debug("prevSchema {}:", prevSchema);
        if (prevSchema != null) {
            throw new IllegalArgumentException("Cannot have the " + schemaType + " schemaType defined on multiple classes: " + schema + ", " + prevSchema);
        }
        try {
            CustomMessageDeserializer cqdI = cqd.newInstance();
            CustomMessageValidator cqvI = cqv.newInstance();
            typeToCustomQueryBuilder.put(schemaType, cqdI);
            typeToCustomQueryValidator.put(schemaType, cqvI);
        } catch (InstantiationException | IllegalAccessException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : Message(org.apache.apex.malhar.lib.appdata.schemas.Message) Annotation(java.lang.annotation.Annotation)

Example 3 with Message

use of org.apache.apex.malhar.lib.appdata.schemas.Message in project apex-malhar by apache.

the class MessageDeserializerFactoryTest method testUnregisteredQueryType.

@Test
public void testUnregisteredQueryType() {
    @SuppressWarnings("unchecked") MessageDeserializerFactory qdf = new MessageDeserializerFactory(SchemaQuery.class);
    String unsupportedQuery = "{\"id\":\"1\",\"type\":\"Invalid type\"}";
    boolean exception = false;
    Message data = null;
    try {
        data = qdf.deserialize(unsupportedQuery);
    } catch (IOException e) {
        exception = true;
    }
    Assert.assertTrue("Resulting query should be null.", exception);
}
Also used : Message(org.apache.apex.malhar.lib.appdata.schemas.Message) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with Message

use of org.apache.apex.malhar.lib.appdata.schemas.Message in project apex-malhar by apache.

the class AbstractAppDataSnapshotServer method processQuery.

/**
 * process the query send.
 * provide this method to give sub class a chance to override.
 * @param queryJSON
 */
protected void processQuery(String queryJSON) {
    LOG.debug("query {}", queryJSON);
    Message query = null;
    try {
        query = queryDeserializerFactory.deserialize(queryJSON);
    } catch (IOException ex) {
        LOG.error("Error parsing query: {}", queryJSON);
        LOG.error("{}", ex);
        return;
    }
    if (query instanceof SchemaQuery) {
        SchemaResult schemaResult = schemaRegistry.getSchemaResult((SchemaQuery) query);
        if (schemaResult != null) {
            LOG.debug("queueing {}", schemaResult);
            schemaQueue.add(schemaResult);
        }
    } else if (query instanceof DataQuerySnapshot) {
        queryProcessor.enqueue((DataQuerySnapshot) query, null, null);
    }
}
Also used : SchemaResult(org.apache.apex.malhar.lib.appdata.schemas.SchemaResult) Message(org.apache.apex.malhar.lib.appdata.schemas.Message) DataQuerySnapshot(org.apache.apex.malhar.lib.appdata.schemas.DataQuerySnapshot) IOException(java.io.IOException) SchemaQuery(org.apache.apex.malhar.lib.appdata.schemas.SchemaQuery)

Aggregations

Message (org.apache.apex.malhar.lib.appdata.schemas.Message)4 IOException (java.io.IOException)3 Annotation (java.lang.annotation.Annotation)1 DataQuerySnapshot (org.apache.apex.malhar.lib.appdata.schemas.DataQuerySnapshot)1 SchemaQuery (org.apache.apex.malhar.lib.appdata.schemas.SchemaQuery)1 SchemaResult (org.apache.apex.malhar.lib.appdata.schemas.SchemaResult)1 JSONException (org.codehaus.jettison.json.JSONException)1 JSONObject (org.codehaus.jettison.json.JSONObject)1 Test (org.junit.Test)1