private Object primitive(final XMLEventReader reader, final StartElement start, final EdmType type, final boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale, final boolean isUnicode) throws XMLStreamException, EdmPrimitiveTypeException, DeserializerException { Object value = null; boolean foundEndProperty = false; while (reader.hasNext() && !foundEndProperty) { final XMLEvent event = reader.nextEvent(); if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) { if (type instanceof AbstractGeospatialType<?>) { throw new DeserializerException("geo types support not implemented", DeserializerException.MessageKeys.NOT_IMPLEMENTED); } final EdmPrimitiveType primitiveType = (EdmPrimitiveType) type; final String stringValue = event.asCharacters().getData(); value = primitiveType.valueOfString(stringValue, isNullable, maxLength, precision, scale, isUnicode, primitiveType.getDefaultType()); } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndProperty = true; } } return value; }
protected void writePrimitiveValue(final EdmPrimitiveType type, final Object primitiveValue, final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale, final Boolean isUnicode, final String xml10InvalidCharReplacement, final XMLStreamWriter writer) throws EdmPrimitiveTypeException, XMLStreamException { final String value = type.valueToString(primitiveValue, isNullable, maxLength, precision, scale, isUnicode); if (value == null) { writer.writeAttribute(METADATA, NS_METADATA, Constants.ATTR_NULL, "true"); } else { // XML 1.0 does not handle certain unicode characters, they need to be replaced writer.writeCharacters(replaceInvalidCharacters(type, value, isUnicode, xml10InvalidCharReplacement)); } }
@Override public Object get() throws ODataApplicationException { final EdmPrimitiveType edmType = ((EdmPrimitiveType) literal.getType()); try { final Class<?> defaultType = edmType.getDefaultType(); final Constructor<?> c = defaultType.getConstructor(String.class); return c.newInstance(edmType.fromUriLiteral(literalText)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | EdmPrimitiveTypeException | InstantiationException | NoSuchMethodException | SecurityException e) { throw new ODataJPAFilterException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } }
@Override public <T> T toCastValue(final Class<T> reference) throws EdmPrimitiveTypeException { if (value == null) { return null; } else if (typeKind.isGeospatial()) { return reference.cast(value); } else { // TODO: set facets return type.valueOfString(type.valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null), null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null, reference); } }
/** * Returns the primitive type's default class or the manually mapped class if present. * @param mapping * @param edmPrimitiveType * @return the java class to be used during deserialization */ private Class<?> getJavaClassForPrimitiveType(final EdmMapping mapping, final EdmPrimitiveType type) { final EdmPrimitiveType edmPrimitiveType = type.getKind() == EdmTypeKind.ENUM ? ((EdmEnumType) type).getUnderlyingType() : type .getKind() == EdmTypeKind.DEFINITION ? ((EdmTypeDefinition) type).getUnderlyingType() : type; return mapping == null || mapping.getMappedJavaClass() == null ? edmPrimitiveType.getDefaultType() : mapping .getMappedJavaClass(); }
edmParameter.getType(), edmParameter.isNullable(), edmParameter.isCollection(), edm, type, aliases).getText() : null; primitiveType.valueOfString(primitiveType.fromUriLiteral(text), edmParameter.isNullable(), edmParameter.getMaxLength(), edmParameter.getPrecision(), edmParameter.getScale(), true, edmParameter.getMapping() == null ? primitiveType.getDefaultType() : edmParameter.getMapping().getMappedJavaClass()); } catch (final EdmPrimitiveTypeException e) {
private Object readPrimitiveValue(final String name, final EdmPrimitiveType type, final boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale, final boolean isUnicode, final EdmMapping mapping, final JsonNode jsonNode) throws DeserializerException { checkForValueNode(name, jsonNode); if (isValidNull(name, isNullable, jsonNode)) { return null; } checkJsonTypeBasedOnPrimitiveType(name, type, jsonNode); Class<?> javaClass = getJavaClassForPrimitiveType(mapping, type); try { return type.valueOfString(jsonNode.asText(), isNullable, maxLength, precision, scale, isUnicode, javaClass); } catch (final EdmPrimitiveTypeException e) { throw new DeserializerException("Invalid value: " + jsonNode.asText() + " for property: " + name, e, DeserializerException.MessageKeys.INVALID_VALUE_FOR_PROPERTY, name); } }
protected Class<?> getDefaultType(final EdmPrimitiveType type) { return defaultTypeMapping.get(type) != null ? defaultTypeMapping.get(type) : type.getDefaultType(); }
private String getLiteral(final Object value) throws EdmPrimitiveTypeException { final EdmProperty edmProperty = getEdmProperty(); String uriLiteral; if (edmProperty != null) { uriLiteral = ((EdmPrimitiveType) type) .valueToString(value, edmProperty.isNullable(), edmProperty.getMaxLength(), edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode()); } else { uriLiteral = ((EdmPrimitiveType) type).valueToString(value, null, null, null, null, null); } return ((EdmPrimitiveType) type).toUriLiteral(uriLiteral); } }
public static Object convertValueOnFacet(final OData odata, JPAParamaterFacet returnType, final String value) throws ODataJPAFilterException { try { final EdmPrimitiveTypeKind edmTypeKind = EdmPrimitiveTypeKind.valueOfFQN(returnType.getTypeFQN()); final EdmPrimitiveType edmType = odata.createPrimitiveTypeInstance(edmTypeKind); String targetValue; targetValue = edmType.fromUriLiteral(value); return edmType.valueOfString(targetValue, true, returnType.getMaxLength(), returnType.getPrecision(), returnType .getScale(), true, returnType.getType()); } catch (EdmPrimitiveTypeException | ODataJPAModelException e) { throw new ODataJPAFilterException(e, HttpStatusCode.INTERNAL_SERVER_ERROR); } }
@Override public InputStream primitiveValue(final EdmPrimitiveType type, final Object value, final PrimitiveValueSerializerOptions options) throws SerializerException { try { final String result = type.valueToString(value, options.isNullable(), options.getMaxLength(), options.getPrecision(), options.getScale(), options.isUnicode()); return new ByteArrayInputStream(result.getBytes("UTF-8")); } catch (final EdmPrimitiveTypeException e) { throw new SerializerException("Error in primitive-value formatting.", e, SerializerException.MessageKeys.WRONG_PRIMITIVE_VALUE, type.getFullQualifiedName().getFullQualifiedNameAsString(), value.toString()); } catch (final UnsupportedEncodingException e) { throw new SerializerException("Encoding exception.", e, SerializerException.MessageKeys.IO_EXCEPTION); } }
if (typeNode != null && typeNode.isTextual()) { final Class<? extends Geospatial> geoDataType = jsonNameToGeoDataType.get(typeNode.asText()); if (geoDataType != null && (type == null || geoDataType.equals(type.getDefaultType()))) { final JsonNode topNode = jsonNode.remove( geoDataType.equals(GeospatialCollection.class) ? Constants.JSON_GEOMETRIES : Constants.JSON_COORDINATES); final Geospatial.Dimension dimension = type == null || type.getName().startsWith("Geometry") ? Geospatial.Dimension.GEOMETRY : Geospatial.Dimension.GEOGRAPHY;
private Object readPrimitiveValue(final String name, final EdmPrimitiveType type, final boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale, final boolean isUnicode, final EdmMapping mapping, final JsonNode jsonNode) throws DeserializerException { if (isValidNull(name, isNullable, jsonNode)) { return null; } final boolean isGeoType = type.getName().startsWith("Geo"); if (!isGeoType) { checkForValueNode(name, jsonNode); } checkJsonTypeBasedOnPrimitiveType(name, type, jsonNode); try { if (isGeoType) { return readPrimitiveGeoValue(name, type, (ObjectNode) jsonNode); } return type.valueOfString(jsonNode.asText(), isNullable, maxLength, precision, scale, isUnicode, getJavaClassForPrimitiveType(mapping, type)); } catch (final EdmPrimitiveTypeException e) { throw new DeserializerException( "Invalid value: " + jsonNode.asText() + " for property: " + name, e, DeserializerException.MessageKeys.INVALID_VALUE_FOR_PROPERTY, name); } }
private void writePrimitive(final EdmPrimitiveType type, final Property property, final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale, final Boolean isUnicode, final String xml10InvalidCharReplacement, final XMLStreamWriter writer) throws EdmPrimitiveTypeException, XMLStreamException, SerializerException { if (property.isPrimitive()) { if (type != EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.String)) { writer.writeAttribute(METADATA, NS_METADATA, Constants.ATTR_TYPE, type.getKind() == EdmTypeKind.DEFINITION ? "#" + type.getFullQualifiedName().getFullQualifiedNameAsString() : type.getName()); } writePrimitiveValue(type, property.asPrimitive(), isNullable, maxLength, precision, scale, isUnicode, xml10InvalidCharReplacement, writer); } else if (property.isGeospatial()) { throw new SerializerException("Property type not yet supported!", SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName()); } else if (property.isEnum()) { writer.writeAttribute(METADATA, NS_METADATA, Constants.ATTR_TYPE, "#" + type.getFullQualifiedName().getFullQualifiedNameAsString()); writePrimitiveValue(type, property.asEnum(), isNullable, maxLength, precision, scale, isUnicode, xml10InvalidCharReplacement, writer); } else { throw new SerializerException("Inconsistent property type!", SerializerException.MessageKeys.INCONSISTENT_PROPERTY_TYPE, property.getName()); } }
private static UriParameter createUriParameter(final EdmProperty edmProperty, final String parameterName, final String literalValue, final Edm edm, final EdmType referringType, final Map<String, AliasQueryOption> aliases) throws UriParserException, UriValidationException { final AliasQueryOption alias = literalValue.startsWith("@") ? getKeyAlias(literalValue, edmProperty, edm, referringType, aliases) : null; final String value = alias == null ? literalValue : alias.getText(); final EdmPrimitiveType primitiveType = (EdmPrimitiveType) edmProperty.getType(); try { if (!(primitiveType.validate(primitiveType.fromUriLiteral(value), edmProperty.isNullable(), edmProperty.getMaxLength(), edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode()))) { throw new UriValidationException("Invalid key property", UriValidationException.MessageKeys.INVALID_KEY_PROPERTY, parameterName); } } catch (final EdmPrimitiveTypeException e) { throw new UriValidationException("Invalid key property", e, UriValidationException.MessageKeys.INVALID_KEY_PROPERTY, parameterName); } return new UriParameterImpl() .setName(parameterName) .setText("null".equals(literalValue) ? null : literalValue) .setAlias(alias == null ? null : literalValue) .setExpression(alias == null ? null : alias.getValue() == null ? new LiteralImpl(value, primitiveType) : alias.getValue()); }
private static Property createPrimitive(final String name, EdmPrimitiveType type, final Object value) { return new Property(type.getFullQualifiedName().getFullQualifiedNameAsString(), name, (type instanceof AbstractGeospatialType?ValueType.GEOSPATIAL:ValueType.PRIMITIVE), value); }
@Override public String fromUriLiteral(final String literal) throws EdmPrimitiveTypeException { return getUnderlyingType().fromUriLiteral(literal); } }
@Override public String toUriLiteral(final String literal) { return getUnderlyingType().toUriLiteral(literal); }
private void checkJsonTypeBasedOnPrimitiveType(final String propertyName, final EdmPrimitiveType edmPrimitiveType, final JsonNode jsonNode) throws DeserializerException { boolean valid = true; if (edmPrimitiveType.getKind() == EdmTypeKind.DEFINITION) { checkJsonTypeBasedOnPrimitiveType(propertyName, ((EdmTypeDefinition) edmPrimitiveType).getUnderlyingType(), jsonNode); } else if (edmPrimitiveType.getKind() == EdmTypeKind.ENUM) { // Enum values must be strings. valid = jsonNode.isTextual(); } else { final String name = edmPrimitiveType.getName(); EdmPrimitiveTypeKind primKind; try { primKind = EdmPrimitiveTypeKind.valueOf(name); } catch (final IllegalArgumentException e) { throw new DeserializerException("Unknown Primitive Type: " + name, e, DeserializerException.MessageKeys.UNKNOWN_PRIMITIVE_TYPE, name, propertyName); } valid = matchTextualCase(jsonNode, primKind) || matchNumberCase(jsonNode, primKind) || matchBooleanCase(jsonNode, primKind) || matchIEEENumberCase(jsonNode, primKind) || jsonNode.isObject() && name.startsWith("Geo"); } if (!valid) { throw new DeserializerException( "Invalid json type: " + jsonNode.getNodeType() + " for " + edmPrimitiveType + " property: " + propertyName, DeserializerException.MessageKeys.INVALID_VALUE_FOR_PROPERTY, propertyName); } }
@Override public boolean validate(final String value, final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale, final Boolean isUnicode) { return getUnderlyingType().validate(value, isNullable, maxLength == null ? getMaxLength() : maxLength, precision == null ? getPrecision() : precision, scale == null ? getScale() : scale, isUnicode == null ? isUnicode() : isUnicode); }