Search in sources :

Example 1 with UnmarshallException

use of com.att.cdp.openstack.exception.UnmarshallException in project AJSC by att.

the class Unmarshaller method unmarshallList.

/**
 * Unmarshalls a list of data into the model object provided
 *
 * @param model
 *            The model object to contain the unmarshalled data
 * @param list
 *            The list of data to be unmarshalled
 * @throws UnmarshallException
 *             If the map does not match the model object
 */
private <T extends ModelObject> void unmarshallList(T model, List<Object> list) throws UnmarshallException {
    if (model instanceof Scalar) {
        Scalar scalar = (Scalar) model;
        StringBuffer buffer = new StringBuffer();
        for (Object value : list) {
            buffer.append(value.toString());
            buffer.append(',');
        }
        if (buffer.length() > 0) {
            buffer.delete(buffer.length() - 1, buffer.length());
        }
        scalar.setValue(buffer.toString());
    } else {
        throw new UnmarshallException(String.format("Expecting scalar, but found %s", model.getClass().getSimpleName()));
    }
}
Also used : ModelObject(com.att.cdp.openstack.heat.model.ModelObject) UnmarshallException(com.att.cdp.openstack.exception.UnmarshallException) Scalar(com.att.cdp.openstack.heat.model.Scalar)

Example 2 with UnmarshallException

use of com.att.cdp.openstack.exception.UnmarshallException in project AJSC by att.

the class Unmarshaller method unmarshallMapObject.

/**
 * Unmarshalls a map into a non-scalar model object
 *
 * @param model
 *            The non-scalar model object
 * @param map
 *            The map to be unmarshalled
 * @throws UnmarshallException
 *             If the map does not match the model object
 */
private <T extends ModelObject> void unmarshallMapObject(T model, Map<String, Object> map) throws UnmarshallException {
    Class<? extends ModelObject> implClass = null;
    /*
         * The data we are processing is a map. In that case we iterate the map keys and use that to locate properties
         * that correspond to the key
         */
    for (String key : map.keySet()) {
        try {
            Object obj = map.get(key);
            BeanProperty beanProperty = BeanProperty.getBeanProperty(model, key);
            /*
                 * If we can't find a property, then check if it's ok to ignore unknown properties. If not, its an
                 * exception.
                 */
            if (beanProperty == null) {
                if (!ObjectHelper.isJsonIgnoreUnknownProperty(model.getClass(), key)) {
                    throw new UnmarshallException(String.format("Expected property %s was not found on %s", key, model.getClass().getSimpleName()));
                }
                continue;
            }
            /*
                 * Determine the implementation class of the object we need to map into
                 */
            implClass = determineImplementationClass(beanProperty, obj);
            if (beanProperty.isMap()) {
                /*
                     * If the property is a map, then we are creating a map of the implementation objects.
                     */
                Map<String, Object> values = (Map<String, Object>) obj;
                Map<String, Object> collection = (Map<String, Object>) beanProperty.get();
                if (collection == null) {
                    collection = new HashMap<String, Object>();
                    beanProperty.set(collection);
                }
                for (String valueKey : values.keySet()) {
                    collection.put(valueKey, unmarshall(implClass, values.get(valueKey)));
                }
            } else if (beanProperty.isList()) {
                /*
                     * If the property is a list, then we are creating a list of the implementation objects
                     */
                List<Object> values = (List<Object>) obj;
                List<Object> collection = (List<Object>) beanProperty.get();
                if (collection == null) {
                    collection = new ArrayList<Object>();
                    beanProperty.set(collection);
                }
                for (Object value : values) {
                    collection.add(unmarshall(implClass, value));
                }
            } else {
                /*
                     * Otherwise we are creating a scalar object
                     */
                beanProperty.set(unmarshall(Scalar.class, obj));
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new UnmarshallException(String.format("Exception instantiating model object of class %s", implClass.getSimpleName()), e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) BeanProperty(com.att.cdp.pal.util.BeanProperty) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) ArrayList(java.util.ArrayList) List(java.util.List) UnmarshallException(com.att.cdp.openstack.exception.UnmarshallException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with UnmarshallException

use of com.att.cdp.openstack.exception.UnmarshallException in project AJSC by att.

the class Unmarshaller method unmarshallObject.

/**
 * Unmarshalls a non-map and non-list (i.e., vector or scalar object) into the model object
 *
 * @param model
 *            The model object to contain the unmarshalled data
 * @param data
 *            The scalar object to be unmarshalled
 * @throws UnmarshallException
 *             If the map does not match the model object
 */
private <T extends ModelObject> void unmarshallObject(T model, Object data) throws UnmarshallException {
    if (model instanceof Scalar) {
        Scalar scalar = (Scalar) model;
        if (data instanceof Map) {
            Map<String, Object> values = (Map<String, Object>) data;
            String function = (String) values.keySet().toArray()[0];
            scalar.setFunction(function);
            Object args = values.get(function);
            List<String> arguments = scalar.getArguments();
            if (arguments == null) {
                arguments = new ArrayList<String>();
                scalar.setArguments(arguments);
            }
            if (args instanceof String) {
                arguments.add((String) args);
            } else if (args instanceof List) {
                for (Object element : (List) args) {
                    arguments.add(element.toString());
                }
            }
        } else {
            scalar.setValue(data.toString());
        }
    } else {
        throw new UnmarshallException(String.format("Expecting scalar, but found %s", model.getClass().getSimpleName()));
    }
}
Also used : ModelObject(com.att.cdp.openstack.heat.model.ModelObject) ArrayList(java.util.ArrayList) List(java.util.List) UnmarshallException(com.att.cdp.openstack.exception.UnmarshallException) HashMap(java.util.HashMap) Map(java.util.Map) Scalar(com.att.cdp.openstack.heat.model.Scalar)

Example 4 with UnmarshallException

use of com.att.cdp.openstack.exception.UnmarshallException in project AJSC by att.

the class Unmarshaller method determineImplementationClass.

/**
 * This method checks the specified bean property to locate the implementation class to be used. If the
 * implementation class supports polymorphic mappings, then the implementation class is further inspected for
 * JsonTypeInfo annotations. If the annotation is present, then the annotation directs the processing. Because the
 * processing of the annotation may be data-dependent, the mapped data to be unmarshalled must be provided as well.
 *
 * @param beanProperty
 *            The bean property we are mapping the contents into
 * @param obj
 *            The object that contains the mappings
 * @return The class to be constructed to contain the mappings
 * @throws UnmarshallException
 *             If the json type info uses unsupported metadata determination
 */
@SuppressWarnings("unchecked")
private Class<? extends ModelObject> determineImplementationClass(BeanProperty beanProperty, Object obj) throws UnmarshallException {
    Class<? extends ModelObject> implClass = null;
    JsonTypeInfo typeInfo = null;
    JsonSubTypes subTypes = null;
    Class<?> defaultImplClass = null;
    /*
         * First, try to determine the implementation class to be created based on the bean property type. If the bean
         * property is a scalar type, then use it as is. If it is a list, then use the first generic type. If it is a
         * map, use the second generic type (maps are always assumed to be keyed by strings).
         */
    implClass = (Class<? extends ModelObject>) beanProperty.getPropertyType();
    if (beanProperty.isList()) {
        implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[0];
    } else if (beanProperty.isMap()) {
        implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[1];
    }
    /*
         * HACK: If the implClass is Constraint, then we have a special case. We need to examine the first key in the
         * map to determine the type of constraint and return that name.
         */
    if (implClass.equals(Constraint.class)) {
        if (obj instanceof List) {
            Map<String, Object> map = (Map<String, Object>) ((List<Object>) obj).get(0);
            String constraintType = (String) map.keySet().toArray()[0];
            for (int index = 0; index < CONSTRAINT_TYPES.length; index++) {
                if (CONSTRAINT_TYPES[index].equals(constraintType)) {
                    implClass = CONSTRAINT_CLASSES[index];
                    return implClass;
                }
            }
        }
    }
    /*
         * If typeInfo annotation is present on the property type class, then check it to get the actual implementation
         * class. Otherwise, we will instantiate the property defined.
         */
    typeInfo = (JsonTypeInfo) ObjectHelper.getClassAnnotation(implClass, JsonTypeInfo.class);
    if (typeInfo != null) {
        subTypes = (JsonSubTypes) ObjectHelper.getClassAnnotation(implClass, JsonSubTypes.class);
        defaultImplClass = typeInfo.defaultImpl();
        JsonTypeInfo.Id use = typeInfo.use();
        JsonTypeInfo.As include = typeInfo.include();
        if (use.equals(JsonTypeInfo.Id.NAME) && include.equals(JsonTypeInfo.As.PROPERTY)) {
            if (obj instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) obj;
                String property = typeInfo.property();
                String propertyValue = (String) map.get(property);
                implClass = (Class<? extends ModelObject>) defaultImplClass;
                if (propertyValue != null) {
                    JsonSubTypes.Type[] types = subTypes.value();
                    for (JsonSubTypes.Type type : types) {
                        if (type.name().equals(propertyValue)) {
                            implClass = (Class<? extends ModelObject>) type.value();
                            break;
                        }
                    }
                }
            }
        } else if (use.equals(JsonTypeInfo.Id.CUSTOM)) {
            JsonDeserialize deserializeAnnotation = (JsonDeserialize) ObjectHelper.getClassAnnotation(implClass, JsonDeserialize.class);
            Class<? extends JsonDeserializer> deserializer = deserializeAnnotation.using();
        } else {
            throw new UnmarshallException(String.format("Only JsonTypeInfo use=\"NAME\" and include=\"PROPERTY\" " + " or use=\"CUSTOM\" with custom deserializer are supported.  The mapping specified " + "use=\"%s\" and include=\"%s\"", use.name(), include.name()));
        }
    }
    return implClass;
}
Also used : JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) PatternConstraint(com.att.cdp.openstack.heat.model.PatternConstraint) RangeConstraint(com.att.cdp.openstack.heat.model.RangeConstraint) Constraint(com.att.cdp.openstack.heat.model.Constraint) ValuesConstraint(com.att.cdp.openstack.heat.model.ValuesConstraint) CustomConstraint(com.att.cdp.openstack.heat.model.CustomConstraint) LengthConstraint(com.att.cdp.openstack.heat.model.LengthConstraint) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) ArrayList(java.util.ArrayList) List(java.util.List) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) UnmarshallException(com.att.cdp.openstack.exception.UnmarshallException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

UnmarshallException (com.att.cdp.openstack.exception.UnmarshallException)4 ModelObject (com.att.cdp.openstack.heat.model.ModelObject)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Scalar (com.att.cdp.openstack.heat.model.Scalar)2 Constraint (com.att.cdp.openstack.heat.model.Constraint)1 CustomConstraint (com.att.cdp.openstack.heat.model.CustomConstraint)1 LengthConstraint (com.att.cdp.openstack.heat.model.LengthConstraint)1 PatternConstraint (com.att.cdp.openstack.heat.model.PatternConstraint)1 RangeConstraint (com.att.cdp.openstack.heat.model.RangeConstraint)1 ValuesConstraint (com.att.cdp.openstack.heat.model.ValuesConstraint)1 BeanProperty (com.att.cdp.pal.util.BeanProperty)1 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1