/** * Ensure that non nullable navigation properties are present. * * @param entityType entityType * @throws ODataException If unable to ensure navigation properties are present */ public void ensureNavigationProperties(StructuredType entityType) throws ODataException { List<String> missingNavigationPropertyNames = new ArrayList<>(); entityType.getStructuralProperties().stream().filter(property -> (property instanceof NavigationProperty) && !property.isNullable()).forEach(property -> { LOG.debug("Validating non-nullable NavigationProperty property : {}", property.getName()); if (!links.containsKey(property.getName())) { missingNavigationPropertyNames.add(property.getName()); } }); if (missingNavigationPropertyNames.size() != 0) { StringJoiner joiner = new StringJoiner(","); missingNavigationPropertyNames.forEach(joiner::add); throw new ODataUnmarshallingException("The request does not specify the navigation links for '" + joiner.toString() + "."); } }
/** * Gets the properties of a structural type, including the properties of its base types (recursively). * * @param structuredType structuredType * @param entityDataModel The entity data model * @return listOfProperties * @throws ODataException If unable to get all properties from edm */ public static List<StructuralProperty> getAllProperties(StructuredType structuredType, EntityDataModel entityDataModel) throws ODataException { List<StructuralProperty> properties = new ArrayList<>(); String baseTypeName = structuredType.getBaseTypeName(); if (!isNullOrEmpty(baseTypeName)) { StructuredType baseType = (StructuredType) entityDataModel.getType(baseTypeName); if (baseType == null) { throw new ODataUnmarshallingException("OData type not found: " + baseTypeName); } // Get the properties of the base type properties.addAll(getAllProperties(baseType, entityDataModel)); } properties.addAll(structuredType.getStructuralProperties()); LOG.info("Total number of properties returning are {} for given structured type {}", properties.size(), structuredType.getName()); return properties; }
/** * Get the 'Structural Property' from the given 'Entity Data Model' and 'Structured Type' looking up all the base * types recursively. * * @param entityDataModel The given 'Entity Data Model'. * @param structuredType The given 'Structured Type'. * @param propertyName The name of the property to look up. * @return The 'Structural Property' or {@code null} if not found. */ public static StructuralProperty getStructuralProperty(EntityDataModel entityDataModel, StructuredType structuredType, String propertyName) { StructuralProperty structuralProperty = structuredType.getStructuralProperty(propertyName); if (structuralProperty != null) { return structuralProperty; } else { // Look up recursively in the 'base type' String baseTypeName = structuredType.getBaseTypeName(); if (!isNullOrEmpty(baseTypeName)) { Type baseType = entityDataModel.getType(baseTypeName); if (baseType != null && baseType instanceof StructuredType) { return getStructuralProperty(entityDataModel, (StructuredType) baseType, propertyName); } } } return null; }
/** * This methods write @odata.type of complex type. * If complex type has root-level, @odata.type won't be written. * * @param structuredType structuredType */ private void writeODataType(StructuredType structuredType) throws IOException { if (entitySet != null) { String typeName = entitySet.getTypeName(); String type = typeName.substring(typeName.lastIndexOf(".") + 1, typeName.length()); if (!type.equals(structuredType.getName())) { jsonGenerator.writeStringField(TYPE, String.format("#%s.%s", structuredType.getNamespace(), structuredType.getName())); } else { LOG.trace("{} has root level. {} won't be written here", entitySet.getName(), TYPE); } } }
/** * Calls a callback for each property of a structured type (entity or complex type), including the properties in * the base types that the type might derive from. * * @param entityDataModel The entity data model. * @param structType The structured type of which to visit the properties. * @param visitor The callback called for each property. * @param <E> The type of exception that the callback can throw. * @throws E If the callback throws an exception. */ public static <E extends ODataException> void visitProperties(EntityDataModel entityDataModel, StructuredType structType, PropertyVisitor<E> visitor) throws E { String baseTypeName = structType.getBaseTypeName(); if (!isNullOrEmpty(baseTypeName)) { visitProperties(entityDataModel, (StructuredType) entityDataModel.getType(baseTypeName), visitor); } for (StructuralProperty property : structType.getStructuralProperties()) { visitor.visit(property); } }
StructuredType structuredType = lookup.getStructuredType(typeName); if (structuredType != null) { for (StructuralProperty property : structuredType.getStructuralProperties()) { String propertyTypeName = property.isCollection() ? property.getElementTypeName() : property.getTypeName(); if (propertyType != null && propertyType.getMetaType() == MetaType.ENTITY) { throw new IllegalArgumentException("Found a non-navigation property that refers to an " + "entity type. This is not allowed. Properties that refer to an entity type must be " +
LOG.debug("Found link for navigation property: {}", propertyName); StructuralProperty property = entityType.getStructuralProperty(propertyName); if (!(property instanceof NavigationProperty)) { throw new ODataUnmarshallingException("The request contains a navigation link '" + propertyName +
private String generateXMLForComplexProperty(Object entity, StructuredType type) throws ODataException { LOG.trace("Complex property rendering started"); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { String typeFullyQualifiedName = type.getFullyQualifiedName(); String context = getContextURL(getODataUri(), getEntityDataModel()); LOG.trace("Context for complex property is {}", context); XMLStreamWriter writer = startElement(outputStream, VALUE, HASH + typeFullyQualifiedName, context, true); handleCollectionAndComplexProperties(entity, type, writer); endElement(writer); return outputStream.toString(UTF_8.name()); } catch (XMLStreamException | IOException e) { throw new ODataRenderException("Error while rendering complex property value.", e); } }
/** * Creates the an entity based on its name. * * @param entityName The name of the entity * @return the entity object * @throws ODataUnmarshallingException If unable to load entity */ public Object loadEntity(String entityName) throws ODataUnmarshallingException { Object entity = null; if (entityName != null) { try { StructuredType entityType = JsonParserUtils.getStructuredType(entityName, entityDataModel); if (entityType != null) { entity = entityType.getJavaType().newInstance(); } else { LOG.warn("Given entity '{}' is not found in entity data model", entityName); throw new ODataUnmarshallingException("Couldn't initiate entity because given entity [" + entityName + "] is not found in entity data model."); } } catch (InstantiationException | IllegalAccessException e) { throw new ODataUnmarshallingException("Cannot instantiate entity", e); } } return entity; }
if (!structType.isOpen()) { LOG.debug("{} property is not found in the following {} type. Ignoring", propertyName, structType.toString());
/** * Calls a callback for each property of a structured type (entity or complex type), including the properties in * the base types that the type might derive from. * * @param entityDataModel The entity data model. * @param structType The structured type of which to visit the properties. * @param visitor The callback called for each property. * @param <E> The type of exception that the callback can throw. * @throws E If the callback throws an exception. */ public static <E extends ODataException> void visitProperties(EntityDataModel entityDataModel, StructuredType structType, PropertyVisitor<E> visitor) throws E { String baseTypeName = structType.getBaseTypeName(); if (!isNullOrEmpty(baseTypeName)) { visitProperties(entityDataModel, (StructuredType) entityDataModel.getType(baseTypeName), visitor); } for (StructuralProperty property : structType.getStructuralProperties()) { visitor.visit(property); } }
StructuredType structuredType = lookup.getStructuredType(typeName); if (structuredType != null) { for (StructuralProperty property : structuredType.getStructuralProperties()) { String propertyTypeName = property.isCollection() ? property.getElementTypeName() : property.getTypeName(); if (propertyType != null && propertyType.getMetaType() == MetaType.ENTITY) { throw new IllegalArgumentException("Found a non-navigation property that refers to an " + "entity type. This is not allowed. Properties that refer to an entity type must be " +
/** * This methods write @odata.type of complex type. * If complex type has root-level, @odata.type won't be written. * * @param structuredType structuredType */ private void writeODataType(StructuredType structuredType) throws IOException { if (entitySet != null) { String typeName = entitySet.getTypeName(); String type = typeName.substring(typeName.lastIndexOf(".") + 1, typeName.length()); if (!type.equals(structuredType.getName())) { jsonGenerator.writeStringField(TYPE, String.format("#%s.%s", structuredType.getNamespace(), structuredType.getName())); } else { LOG.trace("{} has root level. {} won't be written here", entitySet.getName(), TYPE); } } }
LOG.debug("Found link for navigation property: {}", propertyName); StructuralProperty property = entityType.getStructuralProperty(propertyName); if (!(property instanceof NavigationProperty)) { throw new ODataUnmarshallingException("The request contains a navigation link '" + propertyName +
private String generateXMLForComplexProperty(Object entity, StructuredType type) throws ODataException { LOG.trace("Complex property rendering started"); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { String typeFullyQualifiedName = type.getFullyQualifiedName(); String context = getContextURL(getODataUri(), getEntityDataModel()); LOG.trace("Context for complex property is {}", context); XMLStreamWriter writer = startElement(outputStream, VALUE, HASH + typeFullyQualifiedName, context, true); handleCollectionAndComplexProperties(entity, type, writer); endElement(writer); return outputStream.toString(UTF_8.name()); } catch (XMLStreamException | IOException e) { throw new ODataRenderException("Error while rendering complex property value.", e); } }
/** * Creates the an entity based on its name. * * @param entityName The name of the entity * @return the entity object * @throws ODataUnmarshallingException If unable to load entity */ public Object loadEntity(String entityName) throws ODataUnmarshallingException { Object entity = null; if (entityName != null) { try { StructuredType entityType = JsonParserUtils.getStructuredType(entityName, entityDataModel); if (entityType != null) { entity = entityType.getJavaType().newInstance(); } else { LOG.warn("Given entity '{}' is not found in entity data model", entityName); throw new ODataUnmarshallingException("Couldn't initiate entity because given entity [" + entityName + "] is not found in entity data model."); } } catch (InstantiationException | IllegalAccessException e) { throw new ODataUnmarshallingException("Cannot instantiate entity", e); } } return entity; }
if (!structType.isOpen()) { LOG.debug("{} property is not found in the following {} type. Ignoring", propertyName, structType.toString());
/** * Ensure that non nullable navigation properties are present. * * @param entityType entityType * @throws ODataException If unable to ensure navigation properties are present */ public void ensureNavigationProperties(StructuredType entityType) throws ODataException { List<String> missingNavigationPropertyNames = new ArrayList<>(); entityType.getStructuralProperties().stream().filter(property -> (property instanceof NavigationProperty) && !property.isNullable()).forEach(property -> { LOG.debug("Validating non-nullable NavigationProperty property : {}", property.getName()); if (!links.containsKey(property.getName())) { missingNavigationPropertyNames.add(property.getName()); } }); if (missingNavigationPropertyNames.size() != 0) { StringJoiner joiner = new StringJoiner(","); missingNavigationPropertyNames.forEach(joiner::add); throw new ODataUnmarshallingException("The request does not specify the navigation links for '" + joiner.toString() + "."); } }
/** * Gets the properties of a structural type, including the properties of its base types (recursively). * * @param structuredType structuredType * @param entityDataModel The entity data model * @return listOfProperties * @throws ODataException If unable to get all properties from edm */ public static List<StructuralProperty> getAllProperties(StructuredType structuredType, EntityDataModel entityDataModel) throws ODataException { List<StructuralProperty> properties = new ArrayList<>(); String baseTypeName = structuredType.getBaseTypeName(); if (!isNullOrEmpty(baseTypeName)) { StructuredType baseType = (StructuredType) entityDataModel.getType(baseTypeName); if (baseType == null) { throw new ODataUnmarshallingException("OData type not found: " + baseTypeName); } // Get the properties of the base type properties.addAll(getAllProperties(baseType, entityDataModel)); } properties.addAll(structuredType.getStructuralProperties()); LOG.info("Total number of properties returning are {} for given structured type {}", properties.size(), structuredType.getName()); return properties; }
/** * Get the 'Structural Property' from the given 'Entity Data Model' and 'Structured Type' looking up all the base * types recursively. * * @param entityDataModel The given 'Entity Data Model'. * @param structuredType The given 'Structured Type'. * @param propertyName The name of the property to look up. * @return The 'Structural Property' or {@code null} if not found. */ public static StructuralProperty getStructuralProperty(EntityDataModel entityDataModel, StructuredType structuredType, String propertyName) { StructuralProperty structuralProperty = structuredType.getStructuralProperty(propertyName); if (structuralProperty != null) { return structuralProperty; } else { // Look up recursively in the 'base type' String baseTypeName = structuredType.getBaseTypeName(); if (!isNullOrEmpty(baseTypeName)) { Type baseType = entityDataModel.getType(baseTypeName); if (baseType != null && baseType instanceof StructuredType) { return getStructuralProperty(entityDataModel, (StructuredType) baseType, propertyName); } } } return null; }