Java Code Examples for org.apache.olingo.odata2.api.ep.EntityProviderReadProperties#isValidatingFacets()

The following examples show how to use org.apache.olingo.odata2.api.ep.EntityProviderReadProperties#isValidatingFacets() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlPropertyConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
protected Object readStartedElement(XMLStreamReader reader, final String name, final EntityPropertyInfo propertyInfo,
    final EntityTypeMapping typeMappings, final EntityProviderReadProperties readProperties)
    throws EntityProviderException, EdmException {
  Object result = null;

  try {
    reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_D_2007_08, name);
    final String nullAttribute = reader.getAttributeValue(Edm.NAMESPACE_M_2007_08, FormatXml.M_NULL);

    if (!(nullAttribute == null || TRUE.equals(nullAttribute) || FALSE.equals(nullAttribute))) {
      throw new EntityProviderException(EntityProviderException.COMMON);
    }

    if (TRUE.equals(nullAttribute)) {
      if ((readProperties == null || readProperties.isValidatingFacets()) && propertyInfo.isMandatory()) {
        throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_NULL_NOT_ALLOWED.addContent(name));
      }
      reader.nextTag();
    } else if (propertyInfo.isComplex()) {
      final String typeAttribute = reader.getAttributeValue(Edm.NAMESPACE_M_2007_08, FormatXml.M_TYPE);
      if (typeAttribute != null) {
        final String expectedTypeAttributeValue =
            propertyInfo.getType().getNamespace() + Edm.DELIMITER + propertyInfo.getType().getName();
        if (!expectedTypeAttributeValue.equals(typeAttribute)) {
          throw new EntityProviderException(EntityProviderException.INVALID_COMPLEX_TYPE.addContent(
              expectedTypeAttributeValue).addContent(typeAttribute));
        }
      }

      reader.nextTag();
      Map<String, Object> name2Value = new HashMap<String, Object>();
      while (reader.hasNext() && !reader.isEndElement()) {
        final String childName = reader.getLocalName();
        final EntityPropertyInfo childProperty =
            ((EntityComplexPropertyInfo) propertyInfo).getPropertyInfo(childName);
        if (childProperty == null) {
          throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(childName));
        }
        final Object value = readStartedElement(reader, childName, childProperty,
            typeMappings.getEntityTypeMapping(name), readProperties);
        name2Value.put(childName, value);
        reader.nextTag();
      }
      result = name2Value;
    } else {
      result = convert(propertyInfo, reader.getElementText(), typeMappings.getMappingClass(name), readProperties);
    }
    reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_D_2007_08, name);

    return result;
  } catch (XMLStreamException e) {
    throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass()
        .getSimpleName()), e);
  }
}
 
Example 2
Source File: JsonPropertyConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
    final Object typeMapping, final EntityProviderReadProperties readProperties)
    throws EdmException, EntityProviderException, IOException {
  final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
  Object value = null;
  final JsonToken tokenType = reader.peek();
  if (tokenType == JsonToken.NULL) {
    reader.nextNull();
  } else {
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case Boolean:
      if (tokenType == JsonToken.BOOLEAN) {
        value = reader.nextBoolean();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      if (tokenType == JsonToken.NUMBER) {
        value = reader.nextInt();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Single:
    case Double:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else if (tokenType == JsonToken.NUMBER) {
        value = reader.nextDouble();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    default:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    }
  }

  final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
  final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ?
      entityPropertyInfo.getFacets() : null;
  return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}
 
Example 3
Source File: JsonPropertyConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private Object readComplexProperty(final JsonReader reader, final EntityComplexPropertyInfo complexPropertyInfo,
    final Object typeMapping, final EntityProviderReadProperties readProperties)
    throws EdmException, EntityProviderException, IOException {
  if (reader.peek().equals(JsonToken.NULL)) {
    reader.nextNull();
    if ((readProperties == null || readProperties.isValidatingFacets()) && complexPropertyInfo.isMandatory()) {
      throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE.addContent(complexPropertyInfo
          .getName()));
    }
    return null;
  }

  reader.beginObject();
  Map<String, Object> data = new HashMap<String, Object>();

  Map<String, Object> mapping;
  if (typeMapping != null) {
    if (typeMapping instanceof Map) {
      mapping = (Map<String, Object>) typeMapping;
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_MAPPING.addContent(complexPropertyInfo
          .getName()));
    }
  } else {
    mapping = new HashMap<String, Object>();
  }

  while (reader.hasNext()) {
    final String childName = reader.nextName();
    if (FormatJson.METADATA.equals(childName)) {
      readAndCheckTypeInfo(reader,
          complexPropertyInfo.getType().getNamespace() + Edm.DELIMITER + complexPropertyInfo.getType().getName());
    } else {
      EntityPropertyInfo childPropertyInfo = complexPropertyInfo.getPropertyInfo(childName);
      if (childPropertyInfo == null) {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(childName));
      }
      Object childData = readPropertyValue(reader, childPropertyInfo, mapping.get(childName), readProperties);
      if (data.containsKey(childName)) {
        throw new EntityProviderException(EntityProviderException.DOUBLE_PROPERTY.addContent(childName));
      }
      data.put(childName, childData);
    }
  }
  reader.endObject();
  return data;
}