Java Code Examples for org.apache.olingo.odata2.api.edm.EdmSimpleType#valueToString()

The following examples show how to use org.apache.olingo.odata2.api.edm.EdmSimpleType#valueToString() . 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: 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 2
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 3
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 4
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 5
Source File: EdmURIBuilderImpl.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public EdmURIBuilder appendFunctionImportParameters(Map<EdmParameter, Object> functionImportParams) {
  try {
    if (functionImportParams != null) {
      for (Map.Entry<EdmParameter, Object> param : functionImportParams.entrySet()) {
        EdmParameter edmParam = param.getKey();
        EdmSimpleType edmType = (EdmSimpleType) edmParam.getType();
        Object value = param.getValue();
        if (value instanceof String) {
          value = value.toString();
        } 
        value = edmType.valueToString(value, EdmLiteralKind.URI, edmParam.getFacets());
        functionImportParameters.put(edmParam.getName(), value);
      }
    }
  } catch (EdmException e) {
    throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR
  }
  return this;
}
 
Example 6
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 7
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private <T> String constructETag(final EdmEntitySet entitySet, final T data) throws ODataException {
  final EdmEntityType entityType = entitySet.getEntityType();
  String eTag = null;
  for (final String propertyName : entityType.getPropertyNames()) {
    final EdmProperty property = (EdmProperty) entityType.getProperty(propertyName);
    if (property.getFacets() != null && property.getFacets().getConcurrencyMode() == EdmConcurrencyMode.Fixed) {
      final EdmSimpleType type = (EdmSimpleType) property.getType();
      final String component = type.valueToString(valueAccess.getPropertyValue(data, property),
          EdmLiteralKind.DEFAULT, property.getFacets());
      eTag = eTag == null ? component : eTag + Edm.DELIMITER + component;
    }
  }
  return eTag == null ? null : "W/\"" + eTag + "\"";
}
 
Example 8
Source File: XmlPropertyEntityProducer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Appends a simple-property value to the XML stream.
 * @param writer the XML stream writer
 * @param prop property informations
 * @param value the value of the property
 * @throws XMLStreamException
 * @throws EdmException
 * @throws EntityProviderProducerException 
 */
private void appendProperty(final XMLStreamWriter writer, final EntityPropertyInfo prop, final Object value)
    throws XMLStreamException, EdmException, EntityProviderProducerException {
  Object contentValue = value;
  String mimeType = null;
  if (prop.getMimeType() != null) {
    mimeType = prop.getMimeType();
  } else if (prop.getMapping() != null && prop.getMapping().getMediaResourceMimeTypeKey() != null) {
    mimeType = (String) extractChildValue(value, prop.getMapping().getMediaResourceMimeTypeKey());
    contentValue = extractChildValue(value, prop.getName());
  }

  if (mimeType != null) {
    writer.writeAttribute(Edm.NAMESPACE_M_2007_08, FormatXml.M_MIME_TYPE, mimeType);
  }

  final EdmSimpleType type = (EdmSimpleType) prop.getType();
  if (includeSimplePropertyType) {
    String fqnTypeName = type.getNamespace() + Edm.DELIMITER + type.getName();
    writer.writeAttribute(Edm.NAMESPACE_M_2007_08, FormatXml.ATOM_TYPE, fqnTypeName);
  }

  final EdmFacets facets = validateFacets ? prop.getFacets() : null;
  String valueAsString = null;
  try {
    valueAsString = type.valueToString(contentValue, EdmLiteralKind.DEFAULT, facets);
  } catch (EdmSimpleTypeException e) {
      throw new EntityProviderProducerException(EdmSimpleTypeException.getMessageReference(
          e.getMessageReference()).updateContent(
              e.getMessageReference().getContent(), prop.getName()), e);
  }
  if (valueAsString == null) {
    writer.writeAttribute(Edm.NAMESPACE_M_2007_08, FormatXml.ATOM_NULL, FormatXml.ATOM_VALUE_TRUE);
  } else {
    writer.writeCharacters(valueAsString);
  }
}
 
Example 9
Source File: AtomEntryEntityProducer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected static String createETag(final EntityInfoAggregator eia, final Map<String, Object> data)
    throws EntityProviderException {
  String propertyName = "";
  try {
    String etag = null;

    Collection<EntityPropertyInfo> propertyInfos = eia.getETagPropertyInfos();
    for (EntityPropertyInfo propertyInfo : propertyInfos) {
      propertyName = propertyInfo.getName();
      EdmType edmType = propertyInfo.getType();
      if (edmType instanceof EdmSimpleType) {
        EdmSimpleType edmSimpleType = (EdmSimpleType) edmType;
        if (etag == null) {
          etag =
              edmSimpleType.valueToString(data.get(propertyInfo.getName()), EdmLiteralKind.DEFAULT, propertyInfo
                  .getFacets());
        } else {
          etag =
              etag
                  + Edm.DELIMITER
                  + edmSimpleType.valueToString(data.get(propertyInfo.getName()), EdmLiteralKind.DEFAULT,
                      propertyInfo.getFacets());
        }
      }
    }

    if (etag != null) {
      etag = "W/\"" + etag + "\"";
    }

    return etag;
  } catch (EdmSimpleTypeException e) {
    throw new EntityProviderProducerException(EdmSimpleTypeException.getMessageReference(
        e.getMessageReference()).updateContent(e.getMessageReference().getContent(), propertyName), e);
  }
}
 
Example 10
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void expectErrorInValueToString(final EdmSimpleType instance, final Object value,
    final EdmLiteralKind literalKind, final EdmFacets facets, final MessageReference messageReference) {
  try {
    instance.valueToString(value, literalKind, facets);
    fail("Expected exception not thrown");
  } catch (EdmSimpleTypeException e) {
    assertNotNull(e.getMessageReference());
    assertEquals(messageReference.getKey(), e.getMessageReference().getKey());
  }
}
 
Example 11
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private <T> String constructETag(final EdmEntitySet entitySet, final T data) throws ODataException {
  final EdmEntityType entityType = entitySet.getEntityType();
  String eTag = null;
  for (final String propertyName : entityType.getPropertyNames()) {
    final EdmProperty property = (EdmProperty) entityType.getProperty(propertyName);
    if (property.getFacets() != null && property.getFacets().getConcurrencyMode() == EdmConcurrencyMode.Fixed) {
      final EdmSimpleType type = (EdmSimpleType) property.getType();
      final String component = type.valueToString(valueAccess.getPropertyValue(data, property),
          EdmLiteralKind.DEFAULT, property.getFacets());
      eTag = eTag == null ? component : eTag + Edm.DELIMITER + component;
    }
  }
  return eTag == null ? null : "W/\"" + eTag + "\"";
}
 
Example 12
Source File: XmlPropertyEntitySerializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Appends a simple-property value to the XML stream.
 * @param writer the XML stream writer
 * @param prop property informations
 * @param value the value of the property
 * @throws XMLStreamException
 * @throws EdmException
 * @throws EntityProviderProducerException 
 */
private void appendProperty(final XMLStreamWriter writer, final EntityPropertyInfo prop, final Object value)
    throws XMLStreamException, EdmException, EntityProviderProducerException {
  Object contentValue = value;
  String mimeType = null;
  if (prop.getMimeType() != null) {
    mimeType = prop.getMimeType();
  } else if (prop.getMapping() != null && prop.getMapping().getMediaResourceMimeTypeKey() != null) {
    mimeType = (String) extractChildValue(value, prop.getMapping().getMediaResourceMimeTypeKey());
    contentValue = extractChildValue(value, prop.getName());
  }

  if (mimeType != null) {
    writer.writeAttribute(Edm.NAMESPACE_M_2007_08, FormatXml.M_MIME_TYPE, mimeType);
  }

  final EdmSimpleType type = (EdmSimpleType) prop.getType();
  final EdmFacets facets = validateFacets ? prop.getFacets() : null;
  String valueAsString = null;
  try {
    valueAsString = type.valueToString(contentValue, EdmLiteralKind.DEFAULT, facets);
  } catch (EdmSimpleTypeException e) {
      throw new EntityProviderProducerException(EdmSimpleTypeException.getMessageReference(
          e.getMessageReference()).updateContent(
              e.getMessageReference().getContent(), prop.getName()), e);
  }
  if (valueAsString == null) {
    writer.writeAttribute(Edm.NAMESPACE_M_2007_08, FormatXml.ATOM_NULL, FormatXml.ATOM_VALUE_TRUE);
  } else {
    writer.writeCharacters(valueAsString);
  }
}
 
Example 13
Source File: EdmURIBuilderImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * @param property
 * @param value
 * @return
 * @throws EdmException
 * @throws EdmSimpleTypeException
 */
private String getKey(EdmProperty property, Object value, boolean isSegment) 
    throws EdmException {
  String key = "";
  EdmSimpleType edmType = (EdmSimpleType) property.getType();
  if (value instanceof String) {
    value = Encoder.encode(value.toString()); //NOSONAR
  }
  if (!isSegment) {
    key = "(" + edmType.valueToString(value, EdmLiteralKind.URI, property.getFacets()) + ")";
  } else {
    key = edmType.valueToString(value, EdmLiteralKind.URI, property.getFacets());
  }
  return key;
}
 
Example 14
Source File: JsonPropertyEntityProducer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
protected static void appendPropertyValue(final JsonStreamWriter jsonStreamWriter,
                                          final EntityPropertyInfo propertyInfo, final Object value,
                                          boolean validatingFacets,
                                          boolean isDataBasedPropertySerialization) throws IOException, EdmException,
    EntityProviderException {
  if (propertyInfo.isComplex()) {
    if (value == null || value instanceof Map<?, ?>) {
      jsonStreamWriter.beginObject();
      appendPropertyMetadata(jsonStreamWriter, propertyInfo.getType());
      if (value == null && isDataBasedPropertySerialization) {
        jsonStreamWriter.endObject();
        return;
      }
      for (final EntityPropertyInfo childPropertyInfo : ((EntityComplexPropertyInfo) propertyInfo)
          .getPropertyInfos()) {
        final String name = childPropertyInfo.getName();
        if (isDataBasedPropertySerialization && !((Map<?,?>)value).containsKey(name)) {
          continue;
        } 
        jsonStreamWriter.separator();
        jsonStreamWriter.name(name);
        appendPropertyValue(jsonStreamWriter, childPropertyInfo,
            value == null ? null : ((Map<?, ?>) value).get(name), validatingFacets, isDataBasedPropertySerialization);
      }
      jsonStreamWriter.endObject();
    } else {
      throw new EntityProviderProducerException(EntityProviderException.ILLEGAL_ARGUMENT
          .addContent("A complex property must have a Map as data"));
    }
  } else {
    final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
    final Object contentValue = value instanceof Map ? ((Map<?, ?>) value).get(propertyInfo.getName()) : value;
    final EdmFacets facets = validatingFacets ? propertyInfo.getFacets(): null;
    String valueAsString = null;
    try {
    valueAsString = type.valueToString(contentValue, EdmLiteralKind.JSON, facets);
    } catch (EdmSimpleTypeException e) {
      throw new EntityProviderProducerException(EdmSimpleTypeException.getMessageReference(
          e.getMessageReference()).updateContent(e.getMessageReference().getContent(), 
              propertyInfo.getName()), e);
    }
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case String:
      jsonStreamWriter.stringValue(valueAsString);
      break;
    case Boolean:
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      jsonStreamWriter.unquotedValue(valueAsString);
      break;
    case DateTime:
    case DateTimeOffset:
      // Although JSON escaping is (and should be) done in the JSON
      // serializer, we backslash-escape the forward slash here explicitly
      // because it is not required to escape it in JSON but in OData.
      jsonStreamWriter.stringValueRaw(valueAsString == null ? null : valueAsString.replace("/", "\\/"));
      break;
    default:
      jsonStreamWriter.stringValueRaw(valueAsString);
      break;
    }
  }
}
 
Example 15
Source File: JsonPropertyEntitySerializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
protected static void appendPropertyValue(final JsonStreamWriter jsonStreamWriter,
                                          final EntityPropertyInfo propertyInfo, final Object value,
                                          boolean validatingFacets) throws IOException, EdmException,
    EntityProviderException { //NOSONAR
  if (propertyInfo.isComplex()) {
    if (value == null || value instanceof Map<?,?>) {
      jsonStreamWriter.beginObject();
      appendPropertyMetadata(jsonStreamWriter, propertyInfo.getType());
      if (value == null) {
        jsonStreamWriter.endObject();
        return;
      }
      for (final EntityPropertyInfo childPropertyInfo : ((EntityComplexPropertyInfo) propertyInfo)
          .getPropertyInfos()) {
        final String name = childPropertyInfo.getName();
        if ( !((Map<?,?>)value).containsKey(name)) { //NOSONAR
          continue;
        } 
        jsonStreamWriter.separator();
        jsonStreamWriter.name(name);
        appendPropertyValue(jsonStreamWriter, childPropertyInfo,
            value == null ? null : ((Map<?,?>) value).get(name), validatingFacets); //NOSONAR
      }
      jsonStreamWriter.endObject();
    } else {
      throw new EntityProviderProducerException(EntityProviderException.ILLEGAL_ARGUMENT
          .addContent("A complex property must have a Map as data"));
    }
  } else {
    final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
    final Object contentValue = value instanceof Entity ? ((Entity) value).
        getProperties().get(propertyInfo.getName()) : value;
    final EdmFacets facets = validatingFacets ? propertyInfo.getFacets(): null;
    String valueAsString = null;
    try {
    valueAsString = type.valueToString(contentValue, EdmLiteralKind.JSON, facets);
    } catch (EdmSimpleTypeException e) {
      throw new EntityProviderProducerException(EdmSimpleTypeException.getMessageReference(
          e.getMessageReference()).updateContent(e.getMessageReference().getContent(), 
              propertyInfo.getName()), e);
    }
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case String:
      jsonStreamWriter.stringValue(valueAsString);
      break;
    case Boolean:
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      jsonStreamWriter.unquotedValue(valueAsString);
      break;
    case DateTime:
    case DateTimeOffset:
      // Although JSON escaping is (and should be) done in the JSON
      // serializer, we backslash-escape the forward slash here explicitly
      // because it is not required to escape it in JSON but in OData.
      jsonStreamWriter.stringValueRaw(valueAsString == null ? null : valueAsString.replace("/", "\\/"));
      break;
    default:
      jsonStreamWriter.stringValueRaw(valueAsString);
      break;
    }
  }
}