org.apache.olingo.odata2.api.edm.EdmLiteralKind Java Examples

The following examples show how to use org.apache.olingo.odata2.api.edm.EdmLiteralKind. 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: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void valueToStringInt32() throws Exception {
  final EdmSimpleType instance = EdmSimpleTypeKind.Int32.getEdmSimpleTypeInstance();

  assertEquals("0", instance.valueToString(0, EdmLiteralKind.DEFAULT, null));
  assertEquals("0", instance.valueToString(0, EdmLiteralKind.JSON, null));
  assertEquals("0", instance.valueToString(0, EdmLiteralKind.URI, null));
  assertEquals("8", instance.valueToString((byte) 8, EdmLiteralKind.DEFAULT, null));
  assertEquals("16", instance.valueToString((short) 16, EdmLiteralKind.DEFAULT, null));
  assertEquals("32", instance.valueToString(Integer.valueOf(32), EdmLiteralKind.DEFAULT, null));
  assertEquals("255", instance.valueToString(255L, EdmLiteralKind.DEFAULT, null));

  expectErrorInValueToString(instance, 12345678901L, EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT);
  expectErrorInValueToString(instance, -2147483649L, EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT);

  expectErrorInValueToString(instance, 1.0, EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED);
  expectErrorInValueToString(instance, 1, null, null, EdmSimpleTypeException.LITERAL_KIND_MISSING);
}
 
Example #2
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse executeFunctionImportValue(final GetFunctionImportUriInfo uriInfo, final String contentType)
    throws ODataException {
  final EdmFunctionImport functionImport = uriInfo.getFunctionImport();
  final EdmSimpleType type = (EdmSimpleType) functionImport.getReturnType().getType();

  final Object data = dataSource.readData(
      functionImport,
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      null);

  if (data == null) {
    throw new ODataNotFoundException(ODataHttpException.COMMON);
  }

  ODataResponse response;
  if (type == EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance()) {
    response = EntityProvider.writeBinary(((BinaryData) data).getMimeType(), ((BinaryData) data).getData());
  } else {
    final String value = type.valueToString(data, EdmLiteralKind.DEFAULT, null);
    response = EntityProvider.writeText(value == null ? "" : value);
  }
  return ODataResponse.fromResponse(response).build();
}
 
Example #3
Source File: AtomEntryEntityProducer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private String getTargetPathValue(final EntityInfoAggregator eia, final String targetPath,
    final Map<String, Object> data) throws EntityProviderException {
  EntityPropertyInfo info = null;
  try {
    info = eia.getTargetPathInfo(targetPath);
    if (info != null) {
      EdmSimpleType type = (EdmSimpleType) info.getType();
      Object value = data.get(info.getName());
      return type.valueToString(value, EdmLiteralKind.DEFAULT, info.getFacets());
    }
    return null;
  } catch (final EdmSimpleTypeException e) {
    throw new EntityProviderProducerException(
        EdmSimpleTypeException.getMessageReference(e.getMessageReference()).
        updateContent(e.getMessageReference().getContent(), info.getName()), e);
  }
}
 
Example #4
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void valueOfStringSByte() throws Exception {
  final EdmSimpleType instance = EdmSimpleTypeKind.SByte.getEdmSimpleTypeInstance();

  assertEquals(Byte.valueOf((byte) 1), instance.valueOfString("1", EdmLiteralKind.DEFAULT, null, Byte.class));
  assertEquals(Short.valueOf((short) -2), instance.valueOfString("-2", EdmLiteralKind.JSON, null, Short.class));
  assertEquals(Byte.valueOf((byte) 127), instance.valueOfString("127", EdmLiteralKind.URI, null, Byte.class));
  assertEquals(Byte.valueOf((byte) -128), instance.valueOfString("-128", EdmLiteralKind.URI, null, Byte.class));
  assertEquals(Integer.valueOf(0), instance.valueOfString("0", EdmLiteralKind.DEFAULT, null, Integer.class));
  assertEquals(Long.valueOf(0), instance.valueOfString("0", EdmLiteralKind.DEFAULT, null, Long.class));

  expectErrorInValueOfString(instance, "128", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "-129", EdmLiteralKind.JSON, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "1.0", EdmLiteralKind.URI, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);

  expectTypeErrorInValueOfString(instance, "1", EdmLiteralKind.DEFAULT);
}
 
Example #5
Source File: XmlFeedConsumer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private DeletedEntryMetadataImpl readDeletedEntryMetadata(final XMLStreamReader reader)
    throws EntityProviderException, XMLStreamException {
  try {
    DeletedEntryMetadataImpl deletedEntryMetadata = new DeletedEntryMetadataImpl();

    String uri = reader.getAttributeValue(null, FormatXml.ATOM_TOMBSTONE_REF);
    String whenStr = reader.getAttributeValue(null, FormatXml.ATOM_TOMBSTONE_WHEN);
    Date when;
    when = EdmDateTimeOffset.getInstance().valueOfString(whenStr, EdmLiteralKind.DEFAULT, null,
        Date.class);

    deletedEntryMetadata.setUri(uri);
    deletedEntryMetadata.setWhen(when);
    return deletedEntryMetadata;
  } catch (EdmSimpleTypeException e) {
    throw new EntityProviderException(EntityProviderException.INVALID_DELETED_ENTRY_METADATA);
  }
}
 
Example #6
Source File: EdmString.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  String result;
  if (literalKind == EdmLiteralKind.URI) {
    if (value.length() >= 2 && value.startsWith("'") && value.endsWith("'")) {
      result = (value.substring(1, value.length() - 1)).replace("''", "'");
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value));
    }
  } else {
    result = value;
  }

  if (facets != null
      && (facets.isUnicode() != null && !facets.isUnicode() && !PATTERN_ASCII.matcher(result).matches()
      || facets.getMaxLength() != null && facets.getMaxLength()!=0 && facets.getMaxLength() < result.length())) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED.addContent(value, facets));
  }

  if (returnType.isAssignableFrom(String.class)) {
    return returnType.cast(result);
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
  }
}
 
Example #7
Source File: AtomEntryEntitySerializer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private String getTargetPathValue(final EntityInfoAggregator eia, final String targetPath,
    final Map<String, Object> data) throws EntityProviderException {
  EntityPropertyInfo info = null;
  try {
    info = eia.getTargetPathInfo(targetPath);
    if (info != null) {
      EdmSimpleType type = (EdmSimpleType) info.getType();
      Object value = data.get(info.getName());
      return type.valueToString(value, EdmLiteralKind.DEFAULT, info.getFacets());
    }
    return null;
  } catch (final EdmSimpleTypeException e) {
    throw new EntityProviderProducerException(
        EdmSimpleTypeException.getMessageReference(e.getMessageReference()).
        updateContent(e.getMessageReference().getContent(), info.getName()), e);
  }
}
 
Example #8
Source File: EdmBinary.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> String internalValueToString(final T value, final EdmLiteralKind literalKind, final EdmFacets facets)
    throws EdmSimpleTypeException {
  byte[] byteArrayValue;
  if (value instanceof byte[]) {
    byteArrayValue = (byte[]) value;
  } else if (value instanceof Byte[]) {
    final int length = ((Byte[]) value).length;
    byteArrayValue = new byte[length];
    for (int i = 0; i < length; i++) {
      byteArrayValue[i] = ((Byte[]) value)[i].byteValue();
    }
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(value.getClass()));
  }

  if (facets != null && facets.getMaxLength() != null && byteArrayValue.length > facets.getMaxLength()) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_FACETS_NOT_MATCHED.addContent(value, facets));
  }

  return Base64.encodeBase64String(byteArrayValue);
}
 
Example #9
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void valueOfStringGuid() throws Exception {
  final EdmSimpleType instance = EdmSimpleTypeKind.Guid.getEdmSimpleTypeInstance();
  final UUID uuid = UUID.fromString("aabbccdd-aabb-ccdd-eeff-aabbccddeeff");

  assertEquals(uuid, instance.valueOfString("aabbccdd-aabb-ccdd-eeff-aabbccddeeff", EdmLiteralKind.DEFAULT, null,
      UUID.class));
  assertEquals(uuid, instance.valueOfString("AABBCCDD-AABB-CCDD-EEFF-AABBCCDDEEFF", EdmLiteralKind.JSON, null,
      UUID.class));
  assertEquals(uuid, instance.valueOfString("guid'AABBCCDD-aabb-ccdd-eeff-AABBCCDDEEFF'", EdmLiteralKind.URI, null,
      UUID.class));
  
  //OLINGO-883 prefix is case insensitive
  assertEquals(uuid, instance.valueOfString("GuId'AABBCCDD-aabb-ccdd-eeff-AABBCCDDEEFF'", EdmLiteralKind.URI, null,
      UUID.class));
  
  expectErrorInValueOfString(instance, "AABBCCDDAABBCCDDEEFFAABBCCDDEEFF", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "uid'AABBCCDD-aabb-ccdd-eeff-AABBCCDDEEFF'", EdmLiteralKind.URI, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);

  expectTypeErrorInValueOfString(instance, uuid.toString(), EdmLiteralKind.DEFAULT);
}
 
Example #10
Source File: AbstractSimpleType.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public final <T> T valueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  if (value == null) {
    if (facets == null || facets.isNullable() == null || facets.isNullable()) {
      return null;
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_NULL_NOT_ALLOWED);
    }
  }

  if (literalKind == null) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_KIND_MISSING);
  }

  return internalValueOfString(value, literalKind, facets, returnType);
}
 
Example #11
Source File: AbstractSimpleType.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public final String valueToString(final Object value, final EdmLiteralKind literalKind, final EdmFacets facets)
    throws EdmSimpleTypeException {
  if (value == null) {
    if (facets == null || facets.isNullable() == null || facets.isNullable()) {
      return null;
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_NULL_NOT_ALLOWED);
    }
  }

  if (literalKind == null) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_KIND_MISSING);
  }

  final String result = internalValueToString(value, literalKind, facets);
  return literalKind == EdmLiteralKind.URI ? toUriLiteral(result) : result;
}
 
Example #12
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse executeFunctionImportValue(final GetFunctionImportUriInfo uriInfo, final String contentType)
    throws ODataException {
  final EdmFunctionImport functionImport = uriInfo.getFunctionImport();
  final EdmSimpleType type = (EdmSimpleType) functionImport.getReturnType().getType();

  final Object data = dataSource.readData(
      functionImport,
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      null);

  if (data == null) {
    throw new ODataNotFoundException(ODataHttpException.COMMON);
  }

  ODataResponse response;
  if (type == EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance()) {
    response = EntityProvider.writeBinary(((BinaryData) data).getMimeType(), ((BinaryData) data).getData());
  } else {
    final String value = type.valueToString(data, EdmLiteralKind.DEFAULT, null);
    response = EntityProvider.writeText(value == null ? "" : value);
  }
  return ODataResponse.fromResponse(response).build();
}
 
Example #13
Source File: EdmSByte.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  Byte valueByte;
  try {
    valueByte = Byte.parseByte(value);
  } catch (final NumberFormatException e) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value), e);
  }

  if (returnType.isAssignableFrom(Byte.class)) {
    return returnType.cast(valueByte);
  } else if (returnType.isAssignableFrom(Short.class)) {
    return returnType.cast(valueByte.shortValue());
  } else if (returnType.isAssignableFrom(Integer.class)) {
    return returnType.cast(valueByte.intValue());
  } else if (returnType.isAssignableFrom(Long.class)) {
    return returnType.cast(valueByte.longValue());
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
  }
}
 
Example #14
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void checkValueOfNull() throws Exception {
  for (EdmSimpleTypeKind kind : EdmSimpleTypeKind.values()) {
    if (kind == EdmSimpleTypeKind.Null) {
      continue;
    }
    final EdmSimpleType instance = kind.getEdmSimpleTypeInstance();
    assertNull(instance.valueOfString(null, EdmLiteralKind.DEFAULT, null, instance.getDefaultType()));
    assertNull(instance.valueOfString(null, EdmLiteralKind.DEFAULT, getNullableFacets(true), instance
        .getDefaultType()));
    assertNull(instance.valueOfString(null, EdmLiteralKind.DEFAULT, getNullableFacets(null), instance
        .getDefaultType()));

    expectErrorInValueOfString(instance, null, EdmLiteralKind.DEFAULT, getNullableFacets(false),
        EdmSimpleTypeException.LITERAL_NULL_NOT_ALLOWED);
    expectErrorInValueOfString(instance, "", null, null, EdmSimpleTypeException.LITERAL_KIND_MISSING);
  }
}
 
Example #15
Source File: ProviderFacadeImplTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void readDeltaFeed() throws Exception {

  final String contentType = ContentType.APPLICATION_ATOM_XML_FEED.toContentTypeString();
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  InputStream content = getFileAsStream("feed_with_deleted_entries.xml");
  EntityProviderReadProperties properties = EntityProviderReadProperties.init().build();

  ODataDeltaFeed deltaFeed = new ProviderFacadeImpl().readDeltaFeed(contentType, entitySet, content, properties);
  assertNotNull(deltaFeed);
  assertNotNull(deltaFeed.getEntries());
  assertNotNull(deltaFeed.getFeedMetadata());
  assertEquals(1, deltaFeed.getEntries().size());
  assertEquals(1, deltaFeed.getDeletedEntries().size());
  assertEquals("http://host:123/odata/Rooms?$skiptoken=97", deltaFeed.getFeedMetadata().getDeltaLink());
  assertEquals("http://host:123/odata/Rooms('2')", deltaFeed.getDeletedEntries().get(0).getUri());

  Date when =
      EdmDateTimeOffset.getInstance().valueOfString("2014-01-14T18:11:06.682+01:00", EdmLiteralKind.DEFAULT, null,
          Date.class);
  assertEquals(when, deltaFeed.getDeletedEntries().get(0).getWhen());
}
 
Example #16
Source File: XmlFeedDeserializer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private DeletedEntryMetadataImpl readDeletedEntryMetadata(final XMLStreamReader reader)
    throws EntityProviderException {
  try {
    DeletedEntryMetadataImpl deletedEntryMetadata = new DeletedEntryMetadataImpl();

    String uri = reader.getAttributeValue(null, FormatXml.ATOM_TOMBSTONE_REF);
    String whenStr = reader.getAttributeValue(null, FormatXml.ATOM_TOMBSTONE_WHEN);
    Date when;
    when = EdmDateTimeOffset.getInstance().valueOfString(whenStr, EdmLiteralKind.DEFAULT, null,
        Date.class);

    deletedEntryMetadata.setUri(uri);
    deletedEntryMetadata.setWhen(when);
    return deletedEntryMetadata;
  } catch (EdmSimpleTypeException e) {
    throw new EntityProviderException(EntityProviderException.INVALID_DELETED_ENTRY_METADATA, e);
  }
}
 
Example #17
Source File: FunctionalVisitor.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Object visitLiteral(LiteralExpression literal, EdmLiteral edm_literal)
{
   try
   {
      // A literal is a Provider<?> (Functional Java)
      // Returns a Node<?> (Expression Tree)
      Object o = edm_literal.getType().valueOfString(
            edm_literal.getLiteral(),
            EdmLiteralKind.DEFAULT,
            null,
            edm_literal.getType().getDefaultType());
      return ExecutableExpressionTree.Node.createLeave(ConstantFactory.constantFactory(o));
   }
   catch (EdmException ex)
   {
      throw new RuntimeException(ex);
   }
}
 
Example #18
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void validate() throws Exception {
  for (EdmSimpleTypeKind kind : EdmSimpleTypeKind.values()) {
    if (kind == EdmSimpleTypeKind.Null) {
      continue;
    }
    final EdmSimpleType instance = kind.getEdmSimpleTypeInstance();
    assertTrue(instance.validate(null, null, null));
    assertTrue(instance.validate(null, null, getNullableFacets(null)));
    assertTrue(instance.validate(null, null, getNullableFacets(true)));
    assertFalse(instance.validate(null, null, getNullableFacets(false)));
    assertFalse(instance.validate("", null, null));
    assertFalse(instance.validate("ä", EdmLiteralKind.DEFAULT, getUnicodeFacets(false)));
    assertFalse(instance.validate("ä", EdmLiteralKind.URI, null));
  }

  assertTrue(EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance().validate("abcd", EdmLiteralKind.DEFAULT,
      getMaxLengthFacets(3)));
  assertFalse(EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance().validate("abcd", EdmLiteralKind.DEFAULT,
      getMaxLengthFacets(2)));

  assertTrue(EdmSimpleTypeKind.Decimal.getEdmSimpleTypeInstance().validate("1.2", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(null, null)));
  assertFalse(EdmSimpleTypeKind.Decimal.getEdmSimpleTypeInstance().validate("1.2", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(null, 0)));
}
 
Example #19
Source File: EdmSingle.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> String internalValueToString(final T value, final EdmLiteralKind literalKind, final EdmFacets facets)
    throws EdmSimpleTypeException {
  if (value instanceof Long || value instanceof Integer) {
    if (Math.abs(((Number) value).longValue()) < Math.pow(10, MAX_PRECISION)) {
      return value.toString();
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT.addContent(value));
    }
  } else if (value instanceof Short || value instanceof Byte) {
    return value.toString();
  } else if (value instanceof Double) {
    if (((Double) value).isInfinite()) {
      return value.toString().toUpperCase(Locale.ROOT).substring(0, value.toString().length() - 5);
    } else if (Float.isInfinite(((Double) value).floatValue())) {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT.addContent(value));
    } else {
      return Float.toString(((Double) value).floatValue());
    }
  } else if (value instanceof Float) {
    final String result = value.toString();
    return ((Float) value).isInfinite() ? result.toUpperCase(Locale.ROOT).substring(0, value.toString().length() - 5)
        : result;
  } else if (value instanceof BigDecimal) {
    if (((BigDecimal) value).precision() <= MAX_PRECISION && Math.abs(((BigDecimal) value).scale()) <= MAX_SCALE) {
      return ((BigDecimal) value).toString();
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT.addContent(value));
    }
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(value.getClass()));
  }
}
 
Example #20
Source File: EdmInt32.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> T internalValueOfString(final String value, final EdmLiteralKind literalKind, final EdmFacets facets,
    final Class<T> returnType) throws EdmSimpleTypeException {
  Integer valueInteger;
  try {
    valueInteger = Integer.parseInt(value);
  } catch (final NumberFormatException e) {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT.addContent(value), e);
  }

  if (returnType.isAssignableFrom(Integer.class)) {
    return returnType.cast(valueInteger);
  } else if (returnType.isAssignableFrom(Byte.class)) {
    if (valueInteger >= Byte.MIN_VALUE && valueInteger <= Byte.MAX_VALUE) {
      return returnType.cast(valueInteger.byteValue());
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType));
    }
  } else if (returnType.isAssignableFrom(Short.class)) {
    if (valueInteger >= Short.MIN_VALUE && valueInteger <= Short.MAX_VALUE) {
      return returnType.cast(valueInteger.shortValue());
    } else {
      throw new EdmSimpleTypeException(EdmSimpleTypeException.LITERAL_UNCONVERTIBLE_TO_VALUE_TYPE.addContent(value,
          returnType));
    }
  } else if (returnType.isAssignableFrom(Long.class)) {
    return returnType.cast(valueInteger.longValue());
  } else {
    throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
  }
}
 
Example #21
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void checkNull() throws Exception {
  for (EdmSimpleTypeKind kind : EdmSimpleTypeKind.values()) {
    if (kind == EdmSimpleTypeKind.Null) {
      continue;
    }
    final EdmSimpleType instance = kind.getEdmSimpleTypeInstance();
    assertNull(instance.valueToString(null, EdmLiteralKind.DEFAULT, null));
    assertNull(instance.valueToString(null, EdmLiteralKind.DEFAULT, getNullableFacets(true)));
    assertNull(instance.valueToString(null, EdmLiteralKind.DEFAULT, getNullableFacets(null)));

    expectErrorInValueToString(instance, null, EdmLiteralKind.DEFAULT, getNullableFacets(false),
        EdmSimpleTypeException.VALUE_NULL_NOT_ALLOWED);
  }
}
 
Example #22
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void valueToStringBinary() throws Exception {
  final byte[] binary = new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD, (byte) 0xEE, (byte) 0xFF };
  final EdmSimpleType instance = EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance();

  assertEquals("qrvM3e7/", instance.valueToString(binary, EdmLiteralKind.DEFAULT, null));
  assertEquals("qrvM3e7/", instance.valueToString(binary, EdmLiteralKind.JSON, null));
  assertEquals("binary'AABBCCDDEEFF'", instance.valueToString(binary, EdmLiteralKind.URI, null));

  assertEquals("qrvM3e7/", instance.valueToString(binary, EdmLiteralKind.DEFAULT, getMaxLengthFacets(6)));
  assertEquals("qrvM3e7/", instance.valueToString(binary, EdmLiteralKind.JSON, getMaxLengthFacets(6)));
  assertEquals("binary'AABBCCDDEEFF'", instance.valueToString(binary, EdmLiteralKind.URI, getMaxLengthFacets(6)));
  assertEquals("qrvM3e7/", instance.valueToString(binary, EdmLiteralKind.DEFAULT,
      getMaxLengthFacets(Integer.MAX_VALUE)));
  assertEquals("binary'AABBCCDDEEFF'", instance.valueToString(binary, EdmLiteralKind.URI,
      getMaxLengthFacets(Integer.MAX_VALUE)));
  assertEquals("qrvM3e7/", instance.valueToString(binary, EdmLiteralKind.DEFAULT, getMaxLengthFacets(null)));

  assertEquals("qg==", instance.valueToString(new Byte[] { new Byte((byte) 170) }, EdmLiteralKind.DEFAULT, null));

  expectErrorInValueToString(instance, binary, EdmLiteralKind.DEFAULT, getMaxLengthFacets(3),
      EdmSimpleTypeException.VALUE_FACETS_NOT_MATCHED);
  expectErrorInValueToString(instance, binary, EdmLiteralKind.JSON, getMaxLengthFacets(3),
      EdmSimpleTypeException.VALUE_FACETS_NOT_MATCHED);
  expectErrorInValueToString(instance, binary, EdmLiteralKind.URI, getMaxLengthFacets(3),
      EdmSimpleTypeException.VALUE_FACETS_NOT_MATCHED);

  expectErrorInValueToString(instance, 0, EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED);
  expectErrorInValueToString(instance, binary, null, null, EdmSimpleTypeException.LITERAL_KIND_MISSING);
}
 
Example #23
Source File: Processor.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public ODataResponse executeFunctionImportValue(GetFunctionImportUriInfo uri_info, String content_type)
      throws ODataException
{
   EdmFunctionImport function_import = uri_info.getFunctionImport();
   Map<String, EdmLiteral> params = uri_info.getFunctionImportParameters();

   // FIXME: returned type might not be a simple type ...
   EdmSimpleType type = (EdmSimpleType) function_import.getReturnType().getType();

   AbstractOperation op = Model.getServiceOperation(function_import.getName());

   fr.gael.dhus.database.object.User current_user =
         ApplicationContextProvider.getBean(SecurityService.class).getCurrentUser();
   if (!op.canExecute(current_user))
   {
      throw new NotAllowedException();
   }

   Object res = op.execute(params);

   /* To handle binary results (NYI):
   if (type == EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance()) {
      response = EntityProvider.writeBinary(
         ((BinaryData) data).getMimeType(),    // BinaryData is an meta-object holding the data
         ((BinaryData) data).getData()         // and its mime type
       );
   }//*/
   final String value = type.valueToString(res, EdmLiteralKind.DEFAULT, null);

   return EntityProvider.writeText(value == null ? "" : value);
}
 
Example #24
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void valueOfStringInt64() throws Exception {
  final EdmSimpleType instance = EdmSimpleTypeKind.Int64.getEdmSimpleTypeInstance();

  assertEquals(Short.valueOf((short) 1), instance.valueOfString("1", EdmLiteralKind.DEFAULT, null, Short.class));
  assertEquals(Integer.valueOf(2), instance.valueOfString("2", EdmLiteralKind.JSON, null, Integer.class));
  assertEquals(Long.valueOf(-1234567890123456789L), instance.valueOfString("-1234567890123456789L",
      EdmLiteralKind.URI, null, Long.class));
  assertEquals(BigInteger.ONE, instance.valueOfString("1", EdmLiteralKind.DEFAULT, null, BigInteger.class));
  assertEquals(Long.valueOf(0), instance.valueOfString("0l", EdmLiteralKind.URI, null, Long.class));
  assertEquals(Byte.valueOf((byte) 0), instance.valueOfString("0L", EdmLiteralKind.URI, null, Byte.class));

  expectErrorInValueOfString(instance, "-12345678901234567890", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "1.0", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "1.0L", EdmLiteralKind.URI, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "0M", EdmLiteralKind.URI, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "0x42", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);

  expectTypeErrorInValueOfString(instance, "1", EdmLiteralKind.DEFAULT);
  expectUnconvertibleErrorInValueOfString(instance, "-129", Byte.class);
  expectUnconvertibleErrorInValueOfString(instance, "128", Byte.class);
  expectUnconvertibleErrorInValueOfString(instance, "-32769", Short.class);
  expectUnconvertibleErrorInValueOfString(instance, "32768", Short.class);
  expectUnconvertibleErrorInValueOfString(instance, "-2147483649", Integer.class);
  expectUnconvertibleErrorInValueOfString(instance, "2147483648", Integer.class);
}
 
Example #25
Source File: EdmGuid.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private boolean validateLiteralInternal(String value, EdmLiteralKind literalKind) {
  String cleanValue = null;
  if (literalKind == EdmLiteralKind.URI && value.toLowerCase().startsWith("guid'") && value.endsWith("'")) {
    cleanValue = value.substring(5, value.length() - 1);
  } else {
    cleanValue = value;
  }
  return validateLiteral(cleanValue);
}
 
Example #26
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void expectTypeErrorInValueOfString(final EdmSimpleType instance, final String value,
    final EdmLiteralKind literalKind) {
  try {
    instance.valueOfString(value, literalKind, null, Class.class);
    fail("Expected exception not thrown");
  } catch (EdmSimpleTypeException e) {
    assertNotNull(e.getMessageReference());
    assertEquals(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.getKey(), e.getMessageReference().getKey());
  }
}
 
Example #27
Source File: JPAFunctionContext.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Object convertArgument(final EdmLiteral edmLiteral, final EdmFacets facets, final Class<?> targetType)
    throws EdmSimpleTypeException {
  Object value = null;
  if (edmLiteral != null) {
    EdmSimpleType edmType = edmLiteral.getType();
    value = edmType.valueOfString(edmLiteral.getLiteral(), EdmLiteralKind.DEFAULT, facets, targetType);
  }
  return value;
}
 
Example #28
Source File: EdmDecimal.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean validate(final String value, final EdmLiteralKind literalKind, final EdmFacets facets) {
  if (value == null) {
    return facets == null || facets.isNullable() == null || facets.isNullable();
  }

  if (literalKind == null) {
    return false;
  }

  return validateLiteral(value, literalKind) && validatePrecisionAndScale(value, facets);
}
 
Example #29
Source File: ODataJPAResponseBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private EdmProperty getEdmPropertyForSelect() {
  EdmSimpleType edmType = EasyMock.createMock(EdmSimpleType.class);
  EasyMock.expect(edmType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
  Facets facets = new Facets().setNullable(false);
  try {
    EasyMock.expect(edmType.valueToString(new Integer(2), EdmLiteralKind.URI, facets)).andStubReturn("2");
    EasyMock.expect(edmType.valueToString(new Integer(2), EdmLiteralKind.DEFAULT, facets)).andStubReturn("2");
  } catch (EdmSimpleTypeException e1) {
    fail("There is an exception in mocking EdmType object " + e1.getMessage());
  }
  EasyMock.replay(edmType);
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  JPAEdmMappingImpl edmMapping = EasyMock.createMock(JPAEdmMappingImpl.class);
  EasyMock.expect(edmMapping.getInternalName()).andStubReturn("soId");
  EasyMock.expect(edmMapping.getMediaResourceMimeTypeKey()).andReturn(null);
  EasyMock.expect(((JPAEdmMappingImpl) edmMapping).isVirtualAccess()).andStubReturn(false);
  EasyMock.replay(edmMapping);
  try {
    EasyMock.expect(edmProperty.getName()).andStubReturn("ID");
    EasyMock.expect(edmProperty.getType()).andStubReturn(edmType);
    EasyMock.expect(edmProperty.getMapping()).andStubReturn(edmMapping);
    EasyMock.expect(edmProperty.getFacets()).andStubReturn(facets);
    EasyMock.expect(edmProperty.getCustomizableFeedMappings()).andStubReturn(null);
    EasyMock.expect(edmProperty.getMimeType()).andStubReturn(null);
    EasyMock.replay(edmProperty);

  } catch (EdmException e) {
    fail("There is an exception in mocking some object " + e.getMessage());
  }

  return edmProperty;

}
 
Example #30
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> mapFunctionParameters(final Map<String, EdmLiteral> functionImportParameters)
    throws EdmSimpleTypeException {
  if (functionImportParameters == null) {
    return Collections.emptyMap();
  } else {
    Map<String, Object> parameterMap = new HashMap<String, Object>();
    for (final String parameterName : functionImportParameters.keySet()) {
      final EdmLiteral literal = functionImportParameters.get(parameterName);
      final EdmSimpleType type = literal.getType();
      parameterMap.put(parameterName, type.valueOfString(literal.getLiteral(), EdmLiteralKind.DEFAULT, null, type
          .getDefaultType()));
    }
    return parameterMap;
  }
}