Java Code Examples for org.apache.olingo.server.api.serializer.ODataSerializer#primitive()

The following examples show how to use org.apache.olingo.server.api.serializer.ODataSerializer#primitive() . 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: TechnicalPrimitiveComplexProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private SerializerResult serializeProperty(final Entity entity, final EdmEntitySet edmEntitySet,
    final List<String> path, final Property property, final EdmProperty edmProperty,
    final EdmType type, final EdmReturnType returnType,
    final RepresentationType representationType, final ContentType responseFormat,
    final ExpandOption expand, final SelectOption select) throws ODataLibraryException {
  ODataSerializer serializer = odata.createSerializer(responseFormat);
  final ContextURL contextURL = isODataMetadataNone(responseFormat) ? null :
      getContextUrl(edmEntitySet, entity, path, type, representationType, expand, select);
  SerializerResult result = null;
  switch (representationType) {
  case PRIMITIVE:
    result = serializer.primitive(serviceMetadata, (EdmPrimitiveType) type, property,
        PrimitiveSerializerOptions.with().contextURL(contextURL)
            .nullable(edmProperty == null ? returnType.isNullable() : edmProperty.isNullable())
            .maxLength(edmProperty == null ? returnType.getMaxLength() : edmProperty.getMaxLength())
            .precision(edmProperty == null ? returnType.getPrecision() : edmProperty.getPrecision())
            .scale(edmProperty == null ? returnType.getScale() : edmProperty.getScale())
            .unicode(edmProperty == null ? null : edmProperty.isUnicode())
            .build());
    break;
  case COMPLEX:
    result = serializer.complex(serviceMetadata, (EdmComplexType) type, property,
        ComplexSerializerOptions.with().contextURL(contextURL)
            .expand(expand).select(select)
            .build());
    break;
  case COLLECTION_PRIMITIVE:
    result = serializer.primitiveCollection(serviceMetadata, (EdmPrimitiveType) type, property,
        PrimitiveSerializerOptions.with().contextURL(contextURL)
            .nullable(edmProperty == null ? returnType.isNullable() : edmProperty.isNullable())
            .maxLength(edmProperty == null ? returnType.getMaxLength() : edmProperty.getMaxLength())
            .precision(edmProperty == null ? returnType.getPrecision() : edmProperty.getPrecision())
            .scale(edmProperty == null ? returnType.getScale() : edmProperty.getScale())
            .unicode(edmProperty == null ? null : edmProperty.isUnicode())
            .build());
    break;
  case COLLECTION_COMPLEX:
    result = serializer.complexCollection(serviceMetadata, (EdmComplexType) type, property,
        ComplexSerializerOptions.with().contextURL(contextURL)
            .expand(expand).select(select)
            .build());
    break;
  default:
    break;
  }
  return result;
}
 
Example 2
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
							UriInfo uriInfo, ContentType responseFormat)
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

	// 1.2. retrieve the requested (Edm) property
	// the last segment is the Property
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); 
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();


	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), 
		                                    Locale.ENGLISH);
	}

	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		InputStream propertyStream = serializerResult.getContent();

		//4. configure the response object
		response.setContent(propertyStream);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	} else {
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 3
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
							UriInfo uriInfo, ContentType responseFormat)
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

	// 1.2. retrieve the requested (Edm) property
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();


	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		InputStream propertyStream = serializerResult.getContent();

		//4. configure the response object
		response.setContent(propertyStream);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	} else {
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 4
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 5
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,UriInfo uriInfo, ContentType responseFormat) 
    throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

	// 1.2. retrieve the requested (Edm) property
	// the last segment is the Property
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); 
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();


	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), 
		                                    Locale.ENGLISH);
	}

	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		InputStream propertyStream = serializerResult.getContent();

		//4. configure the response object
		response.setContent(propertyStream);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	} else {
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 6
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 7
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
							UriInfo uriInfo, ContentType responseFormat)
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

	// 1.2. retrieve the requested (Edm) property
	// the last segment is the Property
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); 
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();


	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), 
		                                    Locale.ENGLISH);
	}

	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		InputStream propertyStream = serializerResult.getContent();

		//4. configure the response object
		response.setContent(propertyStream);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	} else {
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 8
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 9
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
    UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, SerializerException {

  // 1. Retrieve info from URI
  // 1.1. retrieve the info about the requested entity set
  List<UriResource> resourceParts = uriInfo.getUriResourceParts();
  // Note: only in our example we can rely that the first segment is the EntitySet
  UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
  EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
  // the key for the entity
  List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

  // 1.2. retrieve the requested (Edm) property
  // the last segment is the Property
  UriResourceProperty uriProperty = (UriResourceProperty) resourceParts.get(resourceParts.size() - 1);
  EdmProperty edmProperty = uriProperty.getProperty();
  String edmPropertyName = edmProperty.getName();
  // in our example, we know we have only primitive types in our model
  EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();

  // 2. retrieve data from backend
  // 2.1. retrieve the entity data, for which the property has to be read
  Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
  if (entity == null) { // Bad request
    throw new ODataApplicationException("Entity not found",
        HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
  }

  // 2.2. retrieve the property data from the entity
  Property property = entity.getProperty(edmPropertyName);
  if (property == null) {
    throw new ODataApplicationException("Property not found",
        HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
  }

  // 3. serialize
  Object value = property.getValue();
  if (value != null) {
    // 3.1. configure the serializer
    ODataSerializer serializer = odata.createSerializer(responseFormat);

    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
    PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
    // 3.2. serialize
    SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
    InputStream propertyStream = serializerResult.getContent();

    // 4. configure the response object
    response.setContent(propertyStream);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
  } else {
    // in case there's no value for the property, we can skip the serialization
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  }
}
 
Example 10
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
							UriInfo uriInfo, ContentType responseFormat)
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

	// 1.2. retrieve the requested (Edm) property
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();


	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}

	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		InputStream propertyStream = serializerResult.getContent();

		//4. configure the response object
		response.setContent(propertyStream);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	} else {
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 11
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 12
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 13
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
    UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, SerializerException {

  // 1. Retrieve info from URI
  // 1.1. retrieve the info about the requested entity set
  List<UriResource> resourceParts = uriInfo.getUriResourceParts();
  // Note: only in our example we can rely that the first segment is the EntitySet
  UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
  EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
  // the key for the entity
  List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

  // 1.2. retrieve the requested (Edm) property
  // the last segment is the Property
  UriResourceProperty uriProperty = (UriResourceProperty) resourceParts.get(resourceParts.size() - 1);
  EdmProperty edmProperty = uriProperty.getProperty();
  String edmPropertyName = edmProperty.getName();
  // in our example, we know we have only primitive types in our model
  EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();

  // 2. retrieve data from backend
  // 2.1. retrieve the entity data, for which the property has to be read
  Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
  if (entity == null) { // Bad request
    throw new ODataApplicationException("Entity not found",
        HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
  }

  // 2.2. retrieve the property data from the entity
  Property property = entity.getProperty(edmPropertyName);
  if (property == null) {
    throw new ODataApplicationException("Property not found",
        HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
  }

  // 3. serialize
  Object value = property.getValue();
  if (value != null) {
    // 3.1. configure the serializer
    ODataSerializer serializer = odata.createSerializer(responseFormat);

    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
    PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
    // 3.2. serialize
    SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
    InputStream propertyStream = serializerResult.getContent();

    // 4. configure the response object
    response.setContent(propertyStream);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
  } else {
    // in case there's no value for the property, we can skip the serialization
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  }
}
 
Example 14
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 15
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response,
							UriInfo uriInfo, ContentType responseFormat)
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0);
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();

	// 1.2. retrieve the requested (Edm) property
	// the last segment is the Property
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1);
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType();


	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
	}

	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
	}

	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult serializerResult = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		InputStream propertyStream = serializerResult.getContent();

		//4. configure the response object
		response.setContent(propertyStream);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	} else {
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}
 
Example 16
Source File: DemoPrimitiveProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readPrimitive(ODataRequest request, ODataResponse response, 
							UriInfo uriInfo, ContentType responseFormat) 
							throws ODataApplicationException, SerializerException {

	// 1. Retrieve info from URI
	// 1.1. retrieve the info about the requested entity set 
	List<UriResource> resourceParts = uriInfo.getUriResourceParts();
	// Note: only in our example we can rely that the first segment is the EntitySet
	UriResourceEntitySet uriEntityset = (UriResourceEntitySet) resourceParts.get(0); 
	EdmEntitySet edmEntitySet = uriEntityset.getEntitySet();
	// the key for the entity
	List<UriParameter> keyPredicates = uriEntityset.getKeyPredicates();
	
	// 1.2. retrieve the requested (Edm) property 
	UriResourceProperty uriProperty = (UriResourceProperty)resourceParts.get(resourceParts.size() -1); // the last segment is the Property
	EdmProperty edmProperty = uriProperty.getProperty();
	String edmPropertyName = edmProperty.getName();
	// in our example, we know we have only primitive types in our model
	EdmPrimitiveType edmPropertyType = (EdmPrimitiveType) edmProperty.getType(); 
	
	
	// 2. retrieve data from backend
	// 2.1. retrieve the entity data, for which the property has to be read
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	if (entity == null) { // Bad request
		throw new ODataApplicationException("Entity not found",
						HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	} 
	
	// 2.2. retrieve the property data from the entity
	Property property = entity.getProperty(edmPropertyName);
	if (property == null) {
		throw new ODataApplicationException("Property not found",
             HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
	}		
	
	// 3. serialize
	Object value = property.getValue();
	if (value != null) {
		// 3.1. configure the serializer
		ODataSerializer serializer = odata.createSerializer(responseFormat);
		
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).navOrPropertyPath(edmPropertyName).build();
		PrimitiveSerializerOptions options = PrimitiveSerializerOptions.with().contextURL(contextUrl).build();
		// 3.2. serialize
		SerializerResult result = serializer.primitive(serviceMetadata, edmPropertyType, property, options);
		
		//4. configure the response object
		response.setContent(result.getContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());		
	}else{
		// in case there's no value for the property, we can skip the serialization
		response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
	}
}