Example usage for com.google.gson JsonElement isJsonNull

List of usage examples for com.google.gson JsonElement isJsonNull

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonNull.

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

From source file:org.opendolphin.core.comm.JsonCodec.java

License:Apache License

private Object toValidValue(JsonElement jsonElement) {
    if (jsonElement.isJsonNull()) {
        return null;
    } else if (jsonElement.isJsonPrimitive()) {
        JsonPrimitive primitive = jsonElement.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isString()) {
            return primitive.getAsString();
        } else {//from   w w  w. j  a  va2  s .c  om
            return primitive.getAsNumber();
        }
    } else if (jsonElement.isJsonObject()) {
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        if (jsonObject.has(Date.class.toString())) {
            try {
                return new SimpleDateFormat(ISO8601_FORMAT)
                        .parse(jsonObject.getAsJsonPrimitive(Date.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        } else if (jsonObject.has(BigDecimal.class.toString())) {
            try {
                return new BigDecimal(jsonObject.getAsJsonPrimitive(BigDecimal.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        } else if (jsonObject.has(Float.class.toString())) {
            try {
                return Float.valueOf(jsonObject.getAsJsonPrimitive(Float.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        } else if (jsonObject.has(Double.class.toString())) {
            try {
                return Double.valueOf(jsonObject.getAsJsonPrimitive(Double.class.toString()).getAsString());
            } catch (Exception e) {
                throw new RuntimeException("Can not converte!", e);
            }
        }
    }
    throw new RuntimeException("Can not converte!");
}

From source file:org.openecomp.sdc.be.components.impl.ResourceBusinessLogic.java

License:Open Source License

private Either<Map<String, List<ArtifactTemplateInfo>>, ResponseFormat> parseResourceArtifactsInfoFromFile(
        Resource resource, String artifactsMetaFile, String artifactFileName, User user) {

    try {/*from w w w.jav  a  2 s.  c o m*/
        JsonObject jsonElement = new JsonObject();
        jsonElement = gson.fromJson(artifactsMetaFile, jsonElement.getClass());

        JsonElement importStructureElement = jsonElement.get(Constants.IMPORT_STRUCTURE);
        if (importStructureElement == null || importStructureElement.isJsonNull()) {
            log.debug("Artifact  file is not in expected formatr, fileName  {}", artifactFileName);
            BeEcompErrorManager.getInstance().logInternalDataError(
                    "Artifact  file is not in expected formatr, fileName " + artifactFileName,
                    "Artifact internals are invalid", ErrorSeverity.ERROR);
            return Either.right(
                    componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName));
        }

        Map<String, List<Map<String, Object>>> artifactTemplateMap = new HashMap<String, List<Map<String, Object>>>();
        artifactTemplateMap = componentsUtils.parseJsonToObject(importStructureElement.toString(),
                HashMap.class);
        if (artifactTemplateMap.isEmpty()) {
            log.debug("Artifact  file is not in expected formatr, fileName  {}", artifactFileName);
            BeEcompErrorManager.getInstance().logInternalDataError(
                    "Artifact  file is not in expected formatr, fileName " + artifactFileName,
                    "Artifact internals are invalid", ErrorSeverity.ERROR);
            return Either.right(
                    componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName));
        }

        Set<String> artifactsTypeKeys = artifactTemplateMap.keySet();
        Map<String, List<ArtifactTemplateInfo>> artifactsMap = new HashMap<String, List<ArtifactTemplateInfo>>();
        List<ArtifactTemplateInfo> allGroups = new ArrayList<>();
        for (String artifactsTypeKey : artifactsTypeKeys) {

            List<Map<String, Object>> o = artifactTemplateMap.get(artifactsTypeKey);
            Either<List<ArtifactTemplateInfo>, ResponseFormat> artifactTemplateInfoListPairStatus = createArtifactTemplateInfoModule(
                    artifactsTypeKey, o);
            if (artifactTemplateInfoListPairStatus.isRight()) {
                log.debug("Artifact  file is not in expected formatr, fileName  {}", artifactFileName);
                BeEcompErrorManager.getInstance().logInternalDataError(
                        "Artifact  file is not in expected format, fileName " + artifactFileName,
                        "Artifact internals are invalid", ErrorSeverity.ERROR);
                return Either.right(artifactTemplateInfoListPairStatus.right().value());
            }
            List<ArtifactTemplateInfo> artifactTemplateInfoList = artifactTemplateInfoListPairStatus.left()
                    .value();
            if (artifactTemplateInfoList == null) {
                log.debug("Artifact  file is not in expected formatr, fileName  {}", artifactFileName);
                BeEcompErrorManager.getInstance().logInternalDataError(
                        "Artifact  file is not in expected format, fileName " + artifactFileName,
                        "Artifact internals are invalid", ErrorSeverity.ERROR);
                return Either.right(
                        componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName));

            }
            allGroups.addAll(artifactTemplateInfoList);
            artifactsMap.put(artifactsTypeKey, artifactTemplateInfoList);
        }
        int counter = groupBusinessLogic.getNextVfModuleNameCounter(resource.getGroups());
        Either<Boolean, ResponseFormat> validateGroupNamesRes = groupBusinessLogic
                .validateGenerateVfModuleGroupNames(allGroups, resource.getSystemName(), counter);
        if (validateGroupNamesRes.isRight()) {
            return Either.right(validateGroupNamesRes.right().value());
        }
        return Either.left(artifactsMap);
    } catch (Exception e) {
        log.debug("Artifact  file is not in expected format, fileName  {}", artifactFileName);
        log.debug("failed with exception.", e);
        BeEcompErrorManager.getInstance().logInternalDataError(
                "Artifact  file is not in expected format, fileName " + artifactFileName,
                "Artifact internals are invalid", ErrorSeverity.ERROR);
        return Either
                .right(componentsUtils.getResponseFormat(ActionStatus.CSAR_INVALID_FORMAT, artifactFileName));
    }

}

From source file:org.openecomp.sdc.be.model.operations.impl.AbstractOperation.java

License:Open Source License

protected String getValueFromJsonElement(JsonElement jsonElement) {
    String value = null;/*  w w w  . j  av  a  2s .c o  m*/

    if (jsonElement == null || jsonElement.isJsonNull()) {
        value = EMPTY_VALUE;
    } else {
        if (jsonElement.toString().isEmpty()) {
            value = "";
        } else {
            value = jsonElement.toString();
        }
    }

    return value;
}

From source file:org.openecomp.sdc.be.model.tosca.converters.ToscaListValueConverter.java

License:Open Source License

@Override
public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
    if (value == null) {
        return null;
    }/*  w w w . ja va2s. c om*/
    try {
        ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
        ToscaValueConverter innerConverter = null;
        boolean isScalar = true;
        if (innerToscaType != null) {
            innerConverter = innerToscaType.getValueConverter();
        } else {
            DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);

            if (dataTypeDefinition != null) {
                ToscaPropertyType toscaPropertyType = null;
                if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
                    innerConverter = toscaPropertyType.getValueConverter();
                } else {
                    isScalar = false;
                    innerConverter = ToscaMapValueConverter.getInstance();
                }
            } else {
                log.debug("inner Tosca Type is null");
                return value;
            }
        }
        JsonElement jsonElement = null;
        try {
            StringReader reader = new StringReader(value);
            JsonReader jsonReader = new JsonReader(reader);
            jsonReader.setLenient(true);

            jsonElement = jsonParser.parse(jsonReader);
        } catch (JsonSyntaxException e) {
            log.debug("convertToToscaValue failed to parse json value :", e);
            return null;
        }
        if (jsonElement == null || true == jsonElement.isJsonNull()) {
            log.debug("convertToToscaValue json element is null");
            return null;
        }
        if (jsonElement.isJsonArray() == false) {
            // get_input all array like get_input: qrouter_names
            return handleComplexJsonValue(jsonElement);
        }
        JsonArray asJsonArray = jsonElement.getAsJsonArray();

        ArrayList<Object> toscaList = new ArrayList<Object>();
        final boolean isScalarF = isScalar;
        final ToscaValueConverter innerConverterFinal = innerConverter;
        asJsonArray.forEach(e -> {
            Object convertedValue = null;
            if (isScalarF) {
                log.debug("try to convert scalar value {}", e.getAsString());
                if (e.getAsString() == null) {
                    convertedValue = null;
                } else {
                    JsonElement singleElement = jsonParser.parse(e.getAsString());
                    if (singleElement.isJsonPrimitive()) {
                        convertedValue = innerConverterFinal.convertToToscaValue(e.getAsString(), innerType,
                                dataTypes);
                    } else {
                        convertedValue = handleComplexJsonValue(singleElement);
                    }
                }
            } else {
                JsonObject asJsonObject = e.getAsJsonObject();
                Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();

                DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
                Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
                Map<String, Object> toscaObjectPresentation = new HashMap<>();
                // log.debug("try to convert datatype value {}",
                // e.getAsString());

                for (Entry<String, JsonElement> entry : entrySet) {
                    String propName = entry.getKey();

                    JsonElement elementValue = entry.getValue();
                    PropertyDefinition propertyDefinition = allProperties.get(propName);
                    if (propertyDefinition == null) {
                        log.debug("The property {} was not found under data type {}", propName,
                                dataTypeDefinition.getName());
                        continue;
                        // return null;
                    }
                    String type = propertyDefinition.getType();
                    ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
                    Object convValue;
                    if (propertyType != null) {
                        if (elementValue.isJsonPrimitive()) {
                            ToscaValueConverter valueConverter = propertyType.getValueConverter();
                            convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type,
                                    dataTypes);
                        } else {
                            if (ToscaPropertyType.MAP.equals(type)
                                    || ToscaPropertyType.LIST.equals(propertyType)) {
                                ToscaValueConverter valueConverter = propertyType.getValueConverter();
                                String json = gson.toJson(elementValue);
                                String innerTypeRecursive = propertyDefinition.getSchema().getProperty()
                                        .getType();
                                convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive,
                                        dataTypes);
                            } else {
                                convValue = handleComplexJsonValue(elementValue);
                            }
                        }
                    } else {
                        String json = gson.toJson(elementValue);
                        convValue = convertToToscaValue(json, type, dataTypes);
                    }
                    toscaObjectPresentation.put(propName, convValue);
                }
                convertedValue = toscaObjectPresentation;
            }
            toscaList.add(convertedValue);
        });
        return toscaList;
    } catch (

    JsonParseException e) {
        log.debug("Failed to parse json : {}. {}", value, e);
        BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
        return null;
    }
}

From source file:org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter.java

License:Open Source License

@Override
public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
    if (value == null) {
        return value;
    }//from   w ww  .  j ava 2 s.c o  m
    try {
        ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
        ToscaValueConverter innerConverter = null;
        boolean isScalar = true;
        if (innerToscaType != null) {
            innerConverter = innerToscaType.getValueConverter();
        } else {

            DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
            if (dataTypeDefinition != null) {
                ToscaPropertyType toscaPropertyType = null;
                if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
                    innerConverter = toscaPropertyType.getValueConverter();
                } else {
                    isScalar = false;
                }
            } else {
                // TODO handle getinput
                log.debug("inner Tosca Type is null");
                return value;
            }

        }
        JsonElement jsonElement = null;
        try {
            StringReader reader = new StringReader(value);
            JsonReader jsonReader = new JsonReader(reader);
            jsonReader.setLenient(true);

            jsonElement = jsonParser.parse(jsonReader);

        } catch (JsonSyntaxException e) {
            log.debug("convertToToscaValue failed to parse json value :", e);
            return null;
        }
        if (jsonElement == null || true == jsonElement.isJsonNull()) {
            log.debug("convertToToscaValue json element is null");
            return null;
        }
        JsonObject asJsonObject = jsonElement.getAsJsonObject();
        Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();

        Map<String, Object> toscaMap = new HashMap<>();
        final boolean isScalarF = isScalar;
        final ToscaValueConverter innerConverterFinal = innerConverter;
        entrySet.forEach(e -> {
            log.debug("try convert element {}", e.getValue());
            Object convertedValue = convertDataTypeToToscaMap(innerType, dataTypes, innerConverterFinal,
                    isScalarF, e.getValue());
            toscaMap.put(e.getKey(), convertedValue);
        });
        return toscaMap;
    } catch (JsonParseException e) {
        log.debug("Failed to parse json : {}. {}", value, e);
        BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
        return null;
    }
}

From source file:org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter.java

License:Open Source License

private ImmutablePair<JsonElement, Boolean> validateAndUpdate(JsonElement jsonElement,
        DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) {

    Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);

    ToscaPropertyType toscaPropertyType = null;
    if ((toscaPropertyType = isDataTypeDerviedFromScalarType(dataTypeDefinition)) != null) {

        PropertyTypeValidator validator = toscaPropertyType.getValidator();
        PropertyValueConverter converter = toscaPropertyType.getConverter();
        if (jsonElement == null || true == jsonElement.isJsonNull()) {
            boolean valid = validator.isValid(null, null, allDataTypes);
            if (false == valid) {
                log.trace("Failed in validation of property {} from type {}", dataTypeDefinition.getName(),
                        dataTypeDefinition.getName());
                return falseResult;
            }/*from w w w. j a v a  2 s.c  o m*/
            return new ImmutablePair<JsonElement, Boolean>(jsonElement, true);

        } else {
            if (true == jsonElement.isJsonPrimitive()) {
                String value = null;
                if (jsonElement != null) {
                    if (jsonElement.toString().isEmpty()) {
                        value = "";
                    } else {
                        value = jsonElement.toString();
                    }
                }
                boolean valid = validator.isValid(value, null, null);
                if (false == valid) {
                    log.trace("Failed in validation of property {} from type {}. Json primitive value is {}",
                            dataTypeDefinition.getName(), dataTypeDefinition.getName(), value);
                    return falseResult;
                }

                String convertedValue = converter.convert(value, null, allDataTypes);
                JsonElement element = null;
                try {
                    element = jsonParser.parse(convertedValue);
                } catch (JsonSyntaxException e) {
                    log.debug("Failed to parse value {} of property {}. {}", convertedValue,
                            dataTypeDefinition.getName(), e);
                    return falseResult;
                }

                return new ImmutablePair<JsonElement, Boolean>(element, true);

            } else {
                // MAP, LIST, OTHER types cannot be applied data type
                // definition scalar type. We currently cannot derived from
                // map/list. (cannot add the entry schema to it)
                log.debug(
                        "We cannot derive from list/map. Thus, the value cannot be not primitive since the data type {} is scalar one",
                        dataTypeDefinition.getName());

                return falseResult;
            }
        }
    } else {

        if (jsonElement == null || jsonElement.isJsonNull()) {

            return new ImmutablePair<JsonElement, Boolean>(jsonElement, true);

        } else {

            if (jsonElement.isJsonObject()) {

                JsonObject buildJsonObject = new JsonObject();

                JsonObject asJsonObject = jsonElement.getAsJsonObject();
                Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();

                for (Entry<String, JsonElement> entry : entrySet) {
                    String propName = entry.getKey();

                    JsonElement elementValue = entry.getValue();

                    PropertyDefinition propertyDefinition = allProperties.get(propName);
                    if (propertyDefinition == null) {
                        log.debug("The property {} was not found under data type {}", propName,
                                dataTypeDefinition.getName());
                        return falseResult;
                    }
                    String type = propertyDefinition.getType();
                    boolean isScalarType = ToscaPropertyType.isScalarType(type);

                    if (true == isScalarType) {
                        ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
                        if (propertyType == null) {
                            log.debug("cannot find the {} under default tosca property types", type);
                            return falseResult;
                        }
                        PropertyTypeValidator validator = propertyType.getValidator();
                        String innerType = null;
                        if (propertyType == ToscaPropertyType.LIST || propertyType == ToscaPropertyType.MAP) {
                            if (propertyDefinition.getSchema() != null
                                    && propertyDefinition.getSchema().getProperty() != null) {
                                innerType = propertyDefinition.getSchema().getProperty().getType();
                                if (innerType == null) {
                                    log.debug("Property type {} must have inner type in its declaration.",
                                            propertyType);
                                    return falseResult;
                                }
                            }
                        }

                        String value = null;
                        if (elementValue != null) {
                            if (elementValue.isJsonPrimitive() && elementValue.getAsString().isEmpty()) {
                                value = "";
                            } else {
                                value = elementValue.toString();
                            }
                        }

                        boolean isValid = validator.isValid(value, innerType, allDataTypes);
                        if (false == isValid) {
                            log.debug("Failed to validate the value {} from type {}", value, propertyType);
                            return falseResult;
                        }

                        PropertyValueConverter converter = propertyType.getConverter();
                        String convertedValue = converter.convert(value, innerType, allDataTypes);

                        JsonElement element = null;
                        if (convertedValue != null) {
                            if (convertedValue.isEmpty()) {
                                element = new JsonPrimitive("");
                            } else {
                                try {
                                    element = jsonParser.parse(convertedValue);
                                } catch (JsonSyntaxException e) {
                                    log.debug("Failed to parse value {} of type {}. {}", convertedValue,
                                            propertyType, e);
                                    return falseResult;
                                }
                            }
                        }
                        buildJsonObject.add(propName, element);

                    } else {

                        DataTypeDefinition typeDefinition = allDataTypes.get(type);
                        if (typeDefinition == null) {
                            log.debug("The data type {] cannot be found in the given data type list.", type);
                            return falseResult;
                        }

                        ImmutablePair<JsonElement, Boolean> isValid = validateAndUpdate(elementValue,
                                typeDefinition, allDataTypes);

                        if (false == isValid.getRight().booleanValue()) {
                            log.debug("Failed in validation of value {} from type {}",
                                    (elementValue != null ? elementValue.toString() : null),
                                    typeDefinition.getName());
                            return falseResult;
                        }

                        buildJsonObject.add(propName, isValid.getLeft());
                    }

                }

                return new ImmutablePair<JsonElement, Boolean>(buildJsonObject, true);
            } else {
                log.debug("The value {} of type {} should be json object",
                        (jsonElement != null ? jsonElement.toString() : null), dataTypeDefinition.getName());
                return falseResult;
            }

        }
    }

}

From source file:org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter.java

License:Open Source License

private String getValueFromJsonElement(JsonElement jsonElement) {
    String value = null;/*from   w w  w .  j a v  a2s . co m*/

    if (jsonElement == null || jsonElement.isJsonNull()) {
        value = PropertyOperation.EMPTY_VALUE;
    } else {
        if (jsonElement.toString().isEmpty()) {
            value = "";
        } else {
            value = jsonElement.toString();
        }
    }

    return value;
}

From source file:org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter.java

License:Open Source License

private boolean isValid(JsonElement jsonElement, DataTypeDefinition dataTypeDefinition,
        Map<String, DataTypeDefinition> allDataTypes) {

    Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);

    ToscaPropertyType toscaPropertyType = null;
    if ((toscaPropertyType = isDataTypeDerviedFromScalarType(dataTypeDefinition)) != null) {

        PropertyTypeValidator validator = toscaPropertyType.getValidator();
        if (jsonElement == null || true == jsonElement.isJsonNull()) {
            boolean valid = validator.isValid(null, null, allDataTypes);
            if (false == valid) {
                log.trace("Failed in validation of property " + dataTypeDefinition.getName() + " from type "
                        + dataTypeDefinition.getName());
                return false;
            }/*w w  w .jav a2 s.com*/

            return true;

        } else {
            if (true == jsonElement.isJsonPrimitive()) {
                String value = null;
                if (jsonElement != null) {
                    if (jsonElement.toString().isEmpty()) {
                        value = "";
                    } else {
                        value = jsonElement.toString();
                    }
                }
                boolean valid = validator.isValid(value, null, allDataTypes);
                if (false == valid) {
                    log.trace("Failed in validation of property {} from type {}. Json primitive value is {}",
                            dataTypeDefinition.getName(), dataTypeDefinition.getName(), value);
                    return false;
                }

                return true;

            } else {
                // MAP, LIST, OTHER types cannot be applied data type
                // definition scalar type. We currently cannot derived from
                // map/list. (cannot add the entry schema to it)
                log.debug(
                        "We cannot derive from list/map. Thus, the value cannot be not primitive since the data type {} is scalar one",
                        dataTypeDefinition.getName());

                return false;
            }
        }
    } else {

        if (jsonElement == null || jsonElement.isJsonNull()) {

            return true;

        } else {

            if (jsonElement.isJsonObject()) {

                JsonObject asJsonObject = jsonElement.getAsJsonObject();
                Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();

                for (Entry<String, JsonElement> entry : entrySet) {
                    String propName = entry.getKey();

                    JsonElement elementValue = entry.getValue();

                    PropertyDefinition propertyDefinition = allProperties.get(propName);
                    if (propertyDefinition == null) {
                        log.debug("The property {} was not found under data tpye {}", propName,
                                dataTypeDefinition.getName());
                        return false;
                    }
                    String type = propertyDefinition.getType();
                    boolean isScalarType = ToscaPropertyType.isScalarType(type);

                    if (true == isScalarType) {
                        ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
                        if (propertyType == null) {
                            log.debug("cannot find the {} under default tosca property types", type);
                            return false;
                        }
                        PropertyTypeValidator validator = propertyType.getValidator();
                        String innerType = null;
                        if (propertyType == ToscaPropertyType.LIST || propertyType == ToscaPropertyType.MAP) {
                            if (propertyDefinition.getSchema() != null
                                    && propertyDefinition.getSchema().getProperty() != null) {
                                innerType = propertyDefinition.getSchema().getProperty().getType();
                                if (innerType == null) {
                                    log.debug("Property type {} must have inner type in its decleration.",
                                            propertyType);
                                    return false;
                                }
                            }
                        }

                        String value = null;
                        if (elementValue != null) {
                            if (elementValue.isJsonPrimitive() && elementValue.getAsString().isEmpty()) {
                                value = "";
                            } else {
                                value = elementValue.toString();
                            }
                        }

                        boolean isValid = validator.isValid(value, innerType, allDataTypes);
                        if (false == isValid) {
                            log.debug("Failed to validate the value {} from type {}", value, propertyType);
                            return false;
                        }

                    } else {

                        DataTypeDefinition typeDefinition = allDataTypes.get(type);
                        if (typeDefinition == null) {
                            log.debug("The data type {} canot be found in the given data type list.", type);
                            return false;
                        }

                        boolean isValid = isValid(elementValue, typeDefinition, allDataTypes);

                        if (false == isValid) {
                            log.debug("Failed in validation of value {} from type {}",
                                    (elementValue != null ? elementValue.toString() : null),
                                    typeDefinition.getName());
                            return false;
                        }

                    }

                }

                return true;
            } else {
                log.debug("The value {} of type {} should be json object",
                        (jsonElement != null ? jsonElement.toString() : null), dataTypeDefinition.getName());
                return false;
            }

        }
    }

}

From source file:org.openecomp.sdc.be.servlets.RepresentationUtils.java

License:Open Source License

public static ArtifactDefinition convertJsonToArtifactDefinition(String content,
        Class<ArtifactDefinition> clazz) {

    JsonObject jsonElement = new JsonObject();
    ArtifactDefinition resourceInfo = null;
    try {//w  w w .  j  a va2 s  . c  o  m
        Gson gson = new Gson();
        jsonElement = gson.fromJson(content, jsonElement.getClass());
        JsonElement artifactGroupValue = jsonElement.get(Constants.ARTIFACT_GROUP_TYPE_FIELD);
        if (artifactGroupValue != null && !artifactGroupValue.isJsonNull()) {
            String groupValueUpper = artifactGroupValue.getAsString().toUpperCase();
            if (!ArtifactGroupTypeEnum.getAllTypes().contains(groupValueUpper)) {
                StringBuilder sb = new StringBuilder();
                for (String value : ArtifactGroupTypeEnum.getAllTypes()) {
                    sb.append(value).append(", ");
                }
                log.debug("artifactGroupType is {}. valid values are: {}", groupValueUpper, sb.toString());
                return null;
            } else {
                jsonElement.remove(Constants.ARTIFACT_GROUP_TYPE_FIELD);
                jsonElement.addProperty(Constants.ARTIFACT_GROUP_TYPE_FIELD, groupValueUpper);
            }
        }
        String payload = null;
        JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
        if (artifactPayload != null && !artifactPayload.isJsonNull()) {
            payload = artifactPayload.getAsString();
        }
        jsonElement.remove(Constants.ARTIFACT_PAYLOAD_DATA);
        resourceInfo = gson.fromJson(jsonElement, clazz);
        resourceInfo.setPayloadData(payload);

    } catch (Exception e) {
        BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeArtifactInformationInvalidError,
                "Artifact Upload / Update");
        BeEcompErrorManager.getInstance().logBeArtifactInformationInvalidError("Artifact Upload / Update");
        log.debug("Failed to convert the content {} to object. {}",
                content.substring(0, Math.min(50, content.length())), e);
    }

    return resourceInfo;
}

From source file:org.openecomp.sdc.ci.tests.execute.artifacts.DownloadComponentArt.java

License:Open Source License

protected ArtifactDefinition getArtifactDataFromJson(String json) {
    Gson gson = new Gson();
    JsonObject jsonElement = new JsonObject();
    jsonElement = gson.fromJson(json, jsonElement.getClass());
    ArtifactDefinition artifact = new ArtifactDefinition();
    String payload = null;//from ww w  .j  ava  2  s  .  com
    JsonElement artifactPayload = jsonElement.get(Constants.ARTIFACT_PAYLOAD_DATA);
    if (artifactPayload != null && !artifactPayload.isJsonNull()) {
        payload = artifactPayload.getAsString();
    }
    jsonElement.remove(Constants.ARTIFACT_PAYLOAD_DATA);
    artifact = gson.fromJson(jsonElement, ArtifactDefinition.class);
    artifact.setPayloadData(payload);

    /*
     * atifact.setArtifactName(UPLOAD_ARTIFACT_NAME);
     * artifact.setArtifactDisplayName("configure");
     * artifact.setArtifactType("SHELL"); artifact.setMandatory(false);
     * artifact.setDescription("ff");
     * artifact.setPayloadData(UPLOAD_ARTIFACT_PAYLOAD);
     * artifact.setArtifactLabel("configure");
     */
    return artifact;
}