Java Code Examples for org.apache.olingo.server.api.uri.UriResourceEntitySet#getKeyPredicates()

The following examples show how to use org.apache.olingo.server.api.uri.UriResourceEntitySet#getKeyPredicates() . 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: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example 2
Source File: ProductEntityProcessor.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo) throws ODataApplicationException {
    // 1. Retrieve the entity set which belongs to the requested entity
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // Note: only in our example we can assume that the first segment is the
    // EntitySet
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2. delete the data in backend
    List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
    for (UriParameter kp : keyPredicates) {
        LOG.info("Deleting entity {} ( {} )", kp.getName(), kp.getText());
    }

    storage.deleteEntityData(edmEntitySet, keyPredicates);

    // 3. configure the response object
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example 3
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example 4
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void updateEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
                          ContentType requestFormat, ContentType responseFormat)
						throws ODataApplicationException, DeserializerException, SerializerException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// 2. update the data in backend
	// 2.1. retrieve the payload from the PUT request for the entity to be updated 
	InputStream requestInputStream = request.getBody();
	ODataDeserializer deserializer = odata.createDeserializer(requestFormat);
	DeserializerResult result = deserializer.entity(requestInputStream, edmEntityType);
	Entity requestEntity = result.getEntity();
	// 2.2 do the modification in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	// Note that this updateEntity()-method is invoked for both PUT or PATCH operations
	HttpMethod httpMethod = request.getMethod();
	storage.updateEntityData(edmEntitySet, keyPredicates, requestEntity, httpMethod);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example 5
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example 6
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example 7
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example 8
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
     throws ODataApplicationException, SerializerException {
	
	// 1. retrieve the Entity Type 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
	
	// 2. retrieve the data from backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
	
	// 3. serialize
	EdmEntityType entityType = edmEntitySet.getEntityType();
	
	ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build();
 	// expand and select currently not supported
	EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();

	ODataSerializer serializer = this.odata.createSerializer(responseFormat);
	SerializerResult result = serializer.entity(serviceMetadata, entityType, entity, options);
	
	//4. configure the response object
	response.setContent(result.getContent());
	response.setStatusCode(HttpStatusCode.OK.getStatusCode());
	response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 9
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
						throws ODataApplicationException, SerializerException {

	// 1. retrieve the Entity Type
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. retrieve the data from backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);

	// 3. serialize
	EdmEntityType entityType = edmEntitySet.getEntityType();

	ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build();
 	// expand and select currently not supported
	EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();

	ODataSerializer serializer = this.odata.createSerializer(responseFormat);
	SerializerResult serializerResult = serializer.entity(serviceMetadata, entityType, entity, options);
	InputStream entityStream = serializerResult.getContent();

	//4. configure the response object
	response.setContent(entityStream);
	response.setStatusCode(HttpStatusCode.OK.getStatusCode());
	response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 10
Source File: ProductEntityProcessor.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, SerializerException {

    // 1. retrieve the Entity Type
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // Note: only in our example we can assume that the first segment is the
    // EntitySet
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2. retrieve the data from backend
    List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
    Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
    LOG.info("Reading entity {}", entity.getSelfLink());

    // 3. serialize
    EdmEntityType entityType = edmEntitySet.getEntityType();

    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build();
    // expand and select currently not supported
    EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();

    ODataSerializer serializer = this.odata.createSerializer(responseFormat);
    SerializerResult serializerResult = serializer.entity(serviceMetadata, entityType, entity, options);
    InputStream entityStream = serializerResult.getContent();

    // 4. configure the response object
    response.setContent(entityStream);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 11
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
						throws ODataApplicationException, SerializerException {

	// 1. retrieve the Entity Type
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. retrieve the data from backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);

	// 3. serialize
	EdmEntityType entityType = edmEntitySet.getEntityType();

	ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build();
 	// expand and select currently not supported
	EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();

	ODataSerializer serializer = this.odata.createSerializer(responseFormat);
	SerializerResult serializerResult = serializer.entity(serviceMetadata, entityType, entity, options);
	InputStream entityStream = serializerResult.getContent();

	//4. configure the response object
	response.setContent(entityStream);
	response.setStatusCode(HttpStatusCode.OK.getStatusCode());
	response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
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
  // 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 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: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response,
    UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, SerializerException {
  
  EdmEntitySet responseEdmEntitySet = null; // we'll need this to build the ContextURL
  EntityCollection responseEntityCollection = null; // we'll need this to set the response body

  // 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
  List<UriResource> resourceParts = uriInfo.getUriResourceParts();
  int segmentCount = resourceParts.size();

  UriResource uriResource = resourceParts.get(0); // in our example, the first segment is the EntitySet
  if (!(uriResource instanceof UriResourceEntitySet)) {
    throw new ODataApplicationException("Only EntitySet is supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
  }

  UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) uriResource;
  EdmEntitySet startEdmEntitySet = uriResourceEntitySet.getEntitySet();

  if (segmentCount == 1) { // this is the case for: DemoService/DemoService.svc/Categories
    responseEdmEntitySet = startEdmEntitySet; // the response body is built from the first (and only) entitySet

    // 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
    responseEntityCollection = storage.readEntitySetData(startEdmEntitySet);
  } else if (segmentCount == 2) { // in case of navigation: DemoService.svc/Categories(3)/Products

    UriResource lastSegment = resourceParts.get(1); // in our example we don't support more complex URIs
    if (lastSegment instanceof UriResourceNavigation) {
      UriResourceNavigation uriResourceNavigation = (UriResourceNavigation) lastSegment;
      EdmNavigationProperty edmNavigationProperty = uriResourceNavigation.getProperty();
      EdmEntityType targetEntityType = edmNavigationProperty.getType();
      // from Categories(1) to Products
      responseEdmEntitySet = Util.getNavigationTargetEntitySet(startEdmEntitySet, edmNavigationProperty);

      // 2nd: fetch the data from backend
      // first fetch the entity where the first segment of the URI points to
      List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
      // e.g. for Categories(3)/Products we have to find the single entity: Category with ID 3
      Entity sourceEntity = storage.readEntityData(startEdmEntitySet, keyPredicates);
      // error handling for e.g. DemoService.svc/Categories(99)/Products
      if (sourceEntity == null) {
        throw new ODataApplicationException("Entity not found.",
            HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
      }
      // then fetch the entity collection where the entity navigates to
      // note: we don't need to check uriResourceNavigation.isCollection(),
      // because we are the EntityCollectionProcessor
      responseEntityCollection = storage.getRelatedEntityCollection(sourceEntity, targetEntityType);
    }
  } else { // this would be the case for e.g. Products(1)/Category/Products
    throw new ODataApplicationException("Not supported",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
  }

  // 3rd: create and configure a serializer
  ContextURL contextUrl = ContextURL.with().entitySet(responseEdmEntitySet).build();
  final String id = request.getRawBaseUri() + "/" + responseEdmEntitySet.getName();
  EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with()
      .contextURL(contextUrl).id(id).build();
  EdmEntityType edmEntityType = responseEdmEntitySet.getEntityType();

  ODataSerializer serializer = odata.createSerializer(responseFormat);
  SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType,
      responseEntityCollection, opts);

  // 4th: configure the response object: set the body, headers and status code
  response.setContent(serializerResult.getContent());
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
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());
	}
}
 
Example 17
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 18
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 19
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 20
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());
	}
}