Java Code Examples for org.apache.olingo.commons.api.edm.EdmEntitySet#getName()

The following examples show how to use org.apache.olingo.commons.api.edm.EdmEntitySet#getName() . 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: Storage.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Entity readEntityByBindingLink(final String entityId, final EdmEntitySet edmEntitySet, 
    final String rawServiceUri) throws ODataApplicationException {
  
  UriResourceEntitySet entitySetResource = null;
  try {
    entitySetResource = odata.createUriHelper().parseEntityId(edm, entityId, rawServiceUri);
    
    if(!entitySetResource.getEntitySet().getName().equals(edmEntitySet.getName())) {
      throw new ODataApplicationException("Execpted an entity-id for entity set " + edmEntitySet.getName() 
        + " but found id for entity set " + entitySetResource.getEntitySet().getName(), 
        HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
    }
  } catch (DeserializerException e) {
    throw new ODataApplicationException(entityId + " is not a valid entity-Id", 
        HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
  }

  return readEntityData(entitySetResource.getEntitySet(), entitySetResource.getKeyPredicates());
}
 
Example 2
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Entity readEntityByBindingLink(final String entityId, final EdmEntitySet edmEntitySet, 
    final String rawServiceUri) throws ODataApplicationException {
  
  UriResourceEntitySet entitySetResource = null;
  try {
    entitySetResource = odata.createUriHelper().parseEntityId(edm, entityId, rawServiceUri);
    
    if(!entitySetResource.getEntitySet().getName().equals(edmEntitySet.getName())) {
      throw new ODataApplicationException("Execpted an entity-id for entity set " + edmEntitySet.getName() 
        + " but found id for entity set " + entitySetResource.getEntitySet().getName(), 
        HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
    }
  } catch (DeserializerException e) {
    throw new ODataApplicationException(entityId + " is not a valid entity-Id", 
        HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
  }

  return readEntityData(entitySetResource.getEntitySet(), entitySetResource.getKeyPredicates());
}
 
Example 3
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo,
    ContentType responseFormat) throws ODataApplicationException, SerializerException {

  // 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
  List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
  // in our example, the first segment is the EntitySet
  UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
  EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

  // 2nd: fetch the data from backend for this requested EntitySetName
  // it has to be delivered as EntitySet object
  EntityCollection entitySet = storage.readEntitySetData(edmEntitySet);

  // 3rd: create a serializer based on the requested format (json)
  ODataSerializer serializer = odata.createSerializer(responseFormat);

  // and serialize the content: transform from the EntitySet object to InputStream
  EdmEntityType edmEntityType = edmEntitySet.getEntityType();
  ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

  final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
  EntityCollectionSerializerOptions opts =
      EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
  SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, edmEntityType, entitySet, opts);

  // Finally: configure the response object: set the body, headers and status code
  response.setContent(serializedContent.getContent());
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 4
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public List<Entity> readNavigationEntities(EdmEntitySet entitySet) {
  
  EntityCollection entityCollection = data.get(entitySet.getName());
  List<Entity> entities = new ArrayList<Entity>();
  Entity otherEntity = entitySet.getName() == "ESAllPrim" ? data.get("ESDelta").getEntities().get(0) :
    data.get("ESAllPrim").getEntities().get(0);
  EntityCollection ec1=new EntityCollection();
  Entity entity1 = new Entity();
  entity1.setId(entityCollection.getEntities().get(0).getId());//added navigation
  entity1.addProperty(entityCollection.getEntities().get(0).getProperty(edm.getEntityContainer()
      .getEntitySet(entitySet.getName()).getEntityType().getPropertyNames().get(0)));
  Link link = new Link();
  Entity entity2 = new Entity();
  entity2.setId(otherEntity.getId());
  ec1.getEntities().add(entity2);
  link.setInlineEntitySet(ec1);
  link.setTitle("NavPropertyETAllPrimMany");
  entity1.getNavigationLinks().add(link);
  
  Entity entity3 = new Entity();
  EntityCollection ec2=new EntityCollection();
  entity3.setId(entityCollection.getEntities().get(0).getId());//added navigation
  DeletedEntity delentity = new DeletedEntity();
  delentity.setId(otherEntity.getId());
  delentity.setReason(Reason.deleted);
  ec2.getEntities().add(delentity);
  Link delLink = new Link();
  delLink.setInlineEntitySet(ec2);
  delLink.setTitle("NavPropertyETAllPrimMany");
  entity3.getNavigationLinks().add(delLink);
  entities.add(otherEntity);
  entities.add(entity1);
  entities.add(entity3);
  return entities;
}
 
Example 5
Source File: CarsProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public void readEntityCollection(final ODataRequest request, ODataResponse response, final UriInfo uriInfo,
    final ContentType requestedContentType) throws ODataApplicationException, SerializerException {
  // First we have to figure out which entity set to use
  final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());

  // Second we fetch the data for this specific entity set from the mock database and transform it into an EntitySet
  // object which is understood by our serialization
  EntityCollection entitySet = dataProvider.readAll(edmEntitySet);

  // Next we create a serializer based on the requested format. This could also be a custom format but we do not
  // support them in this example
  ODataSerializer serializer = odata.createSerializer(requestedContentType);

  // Now the content is serialized using the serializer.
  final ExpandOption expand = uriInfo.getExpandOption();
  final SelectOption select = uriInfo.getSelectOption();
  final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
  InputStream serializedContent = serializer.entityCollection(edm, edmEntitySet.getEntityType(), entitySet,
      EntityCollectionSerializerOptions.with()
          .id(id)
          .contextURL(isODataMetadataNone(requestedContentType) ? null :
              getContextUrl(edmEntitySet, false, expand, select, null))
          .count(uriInfo.getCountOption())
          .expand(expand).select(select)
          .build()).getContent();

  // Finally we set the response data, headers and status code
  response.setContent(serializedContent);
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
}
 
Example 6
Source File: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch the entity set name which has to be shown in @Id annotation
 * @param metadata
 * @param fullQualifiedName
 * @return
 */
private String getNavigatedEntitySetName(ServiceMetadata metadata, String fullQualifiedName) {
  List<EdmEntitySet> entitySets = metadata.getEdm().getEntityContainer().getEntitySets();
  for (EdmEntitySet entitySet : entitySets) {
    if (entitySet.getEntityType().getFullQualifiedName()
        .getFullQualifiedNameAsString().equals(fullQualifiedName)) {
      return entitySet.getName();
    }
  }
  return null;
}
 
Example 7
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException {

		// 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
		List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
		UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); // in our example, the first segment is the EntitySet
		EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

		// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
		EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);

		// 3rd: create a serializer based on the requested format (json)
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		// and serialize the content: transform from the EntitySet object to InputStream
		EdmEntityType edmEntityType = edmEntitySet.getEntityType();
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

		final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
		EntityCollectionSerializerOptions opts =
				EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
		SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, entityCollection, opts);
		InputStream serializedContent = serializerResult.getContent();

		// 4th: configure the response object: set the body, headers and status code
		response.setContent(serializedContent);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	}
 
Example 8
Source File: DemoEntityCollectionProcessor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, 
        UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException {

    // 1st we have retrieve the requested EntitySet from the uriInfo object 
    // (representation of the parsed service URI)
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // In our example, the first segment is the EntitySet
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2nd: fetch the data from backend for this requested EntitySetName 
    // it has to be delivered as EntitySet object
    EntityCollection entitySet = getData(edmEntitySet);

    // 3rd: create a serializer based on the requested format (json)
    ODataSerializer serializer = odata.createSerializer(responseFormat);
    
    // 4th: Now serialize the content: transform from the EntitySet object to InputStream
    EdmEntityType edmEntityType = edmEntitySet.getEntityType();
    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

    final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
    EntityCollectionSerializerOptions opts =
            EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
    SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, 
        edmEntityType, entitySet, opts);

    // Finally: configure the response object: set the body, headers and status code
    response.setContent(serializedContent.getContent());
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 9
Source File: QueryHandler.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * This method creates next url link.
 *
 * @param rawRequestUri Request uri
 * @param entitySet     EntitySet
 * @param page          Page num
 * @return uri
 * @throws ODataApplicationException
 */
private static URI createNextLink(final String rawRequestUri, final EdmEntitySet entitySet, final int page)
        throws ODataApplicationException {
    String nextLink = rawRequestUri + "/" + entitySet.getName() + "?$skiptoken=" + page;
    try {
        return new URI(nextLink);
    } catch (final URISyntaxException e) {
        throw new ODataApplicationException("Exception while constructing next link",
                                            HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ROOT, e);
    }
}
 
Example 10
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo,
    ContentType responseFormat) throws ODataApplicationException, SerializerException {

  // 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
  List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
  // in our example, the first segment is the EntitySet
  UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
  EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

  // 2nd: fetch the data from backend for this requested EntitySetName
  // it has to be delivered as EntitySet object
  EntityCollection entitySet = storage.readEntitySetData(edmEntitySet);

  // 3rd: create a serializer based on the requested format (json)
  ODataSerializer serializer = odata.createSerializer(responseFormat);

  // and serialize the content: transform from the EntitySet object to InputStream
  EdmEntityType edmEntityType = edmEntitySet.getEntityType();
  ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

  final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
  EntityCollectionSerializerOptions opts =
      EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
  SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, edmEntityType, entitySet, opts);

  // Finally: configure the response object: set the body, headers and status code
  response.setContent(serializedContent.getContent());
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 11
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException {

		// 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
		List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
		UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); // in our example, the first segment is the EntitySet
		EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

		// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
		EntityIterator iterator = storage.readEntitySetDataStreamed(edmEntitySet);

		// 3rd: create a serializer based on the requested format (json)
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		// and serialize the content: transform from the EntitySet object to InputStream
		EdmEntityType edmEntityType = edmEntitySet.getEntityType();
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

		final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
		EntityCollectionSerializerOptions opts =
				EntityCollectionSerializerOptions.with().id(id)
						.writeContentErrorCallback(errorCallback)
						.contextURL(contextUrl).build();

		SerializerStreamResult serializerResult = serializer.entityCollectionStreamed(serviceMetadata,
				edmEntityType, iterator, opts);

		// 4th: configure the response object: set the body, headers and status code
		response.setODataContent(serializerResult.getODataContent());
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	}
 
Example 12
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException {

    // 1st we have retrieve the requested EntitySet from the uriInfo object (representation of the parsed service URI)
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); // in our example, the first segment is the EntitySet
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2nd: fetch the data from backend for this requested EntitySetName // it has to be delivered as EntitySet object
    EntityCollection entitySet = getData(edmEntitySet);

    // 3rd: create a serializer based on the requested format (json)
    ODataSerializer serializer = odata.createSerializer(responseFormat);
    
    // 4th: Now serialize the content: transform from the EntitySet object to InputStream
    EdmEntityType edmEntityType = edmEntitySet.getEntityType();
    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

    final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
    EntityCollectionSerializerOptions opts =
        EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
    SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, edmEntityType, entitySet, opts);

    // Finally: configure the response object: set the body, headers and status code
    response.setContent(serializedContent.getContent());
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
  }
 
Example 13
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 14
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();
      // 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, uriResourceNavigation);
    }
  } 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 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
  EdmEntityType responseEdmEntityType = null;

  // 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();
      if (!edmNavigationProperty.containsTarget()) {
     // from Categories(1) to Products
        responseEdmEntitySet = Util.getNavigationTargetEntitySet(startEdmEntitySet, edmNavigationProperty);
      } else {
        responseEdmEntitySet = startEdmEntitySet;
        responseEdmEntityType = targetEntityType;
      }

      // 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);
  }

  ContextURL contextUrl = null;
  EdmEntityType edmEntityType = null;
  // 3rd: create and configure a serializer
  if (isContNav(uriInfo)) {
    contextUrl = ContextURL.with().entitySetOrSingletonOrType(request.getRawODataPath()).build();
    edmEntityType = responseEdmEntityType;
  } else {
    contextUrl = ContextURL.with().entitySet(responseEdmEntitySet).build(); 
    edmEntityType = responseEdmEntitySet.getEntityType();
  }
  final String id = request.getRawBaseUri() + "/" + responseEdmEntitySet.getName();
  EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with()
      .contextURL(contextUrl).id(id).build();
  
  ODataSerializer serializer = odata.createSerializer(responseFormat);
  SerializerResult serializerResult = serializer.entityCollection(this.srvMetadata, 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: 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 17
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();
      // 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, uriResourceNavigation);
    }
  } 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 18
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 {

	// 1st: retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// in our example, the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
	EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);
	
	// 3rd: Check if filter system query option is provided and apply the expression if necessary
	FilterOption filterOption = uriInfo.getFilterOption();
	if(filterOption != null) {
		// Apply $filter system query option
		try {
		      List<Entity> entityList = entityCollection.getEntities();
		      Iterator<Entity> entityIterator = entityList.iterator();
		      
		      // Evaluate the expression for each entity
		      // If the expression is evaluated to "true", keep the entity otherwise remove it from the entityList
		      while (entityIterator.hasNext()) {
		    	  // To evaluate the the expression, create an instance of the Filter Expression Visitor and pass
		    	  // the current entity to the constructor
		    	  Entity currentEntity = entityIterator.next();
		    	  Expression filterExpression = filterOption.getExpression();
		    	  FilterExpressionVisitor expressionVisitor = new FilterExpressionVisitor(currentEntity);
		    	  
		    	  // Start evaluating the expression
		    	  Object visitorResult = filterExpression.accept(expressionVisitor);
		    	  
		    	  // The result of the filter expression must be of type Edm.Boolean
		    	  if(visitorResult instanceof Boolean) {
		    		  if(!Boolean.TRUE.equals(visitorResult)) {
		    		    // The expression evaluated to false (or null), so we have to remove the currentEntity from entityList
		    		    entityIterator.remove();
		    		  }
		    	  } else {
		    		  throw new ODataApplicationException("A filter expression must evaulate to type Edm.Boolean", 
		    		      HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ENGLISH);
		    	  }
		      }

		    } catch (ExpressionVisitException e) {
		      throw new ODataApplicationException("Exception in filter evaluation",
		          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH);
		    }
	}
	
	// 4th: create a serializer based on the requested format (json)
	ODataSerializer serializer = odata.createSerializer(responseFormat);

	// and serialize the content: transform from the EntitySet object to InputStream
	EdmEntityType edmEntityType = edmEntitySet.getEntityType();
	ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

	final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
	EntityCollectionSerializerOptions opts = EntityCollectionSerializerOptions.with()
			.contextURL(contextUrl).id(id).build();
	SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, entityCollection,
			opts);

	InputStream serializedContent = serializerResult.getContent();

	// 5th: configure the response object: set the body, headers and status code
	response.setContent(serializedContent);
	response.setStatusCode(HttpStatusCode.OK.getStatusCode());
	response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example 19
Source File: UriHelperImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public String buildCanonicalURL(final EdmEntitySet edmEntitySet, final Entity entity) throws SerializerException {
  return edmEntitySet.getName() + '(' + buildKeyPredicate(edmEntitySet.getEntityType(), entity) + ')';
}
 
Example 20
Source File: ProductsEntityCollectionProcessor.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Override
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, ODataLibraryException {
    // 1st we have retrieve the requested EntitySet from the uriInfo object (representation of the parsed service URI)
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet)resourcePaths.get(0); // in our example, the first segment is the EntitySet
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2nd: fetch the data from backend for this requested EntitySetName
    // it has to be delivered as EntitySet object
    EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);

    // 3rd: apply System Query Options
    // modify the result set according to the query options, specified by the end user
    List<Entity> entityList = entityCollection.getEntities();
    EntityCollection returnEntityCollection = new EntityCollection();

    // handle $count: always return the original number of entities, without considering $top and $skip
    CountOption countOption = uriInfo.getCountOption();
    if (countOption != null) {
        boolean isCount = countOption.getValue();
        if (isCount) {
            returnEntityCollection.setCount(entityList.size());
        }
    }

    applyQueryOptions(uriInfo, entityList, returnEntityCollection);

    // 3rd: create a serializer based on the requested format (json)
    ODataSerializer serializer = odata.createSerializer(responseFormat);

    // 4th: Now serialize the content: transform from the EntitySet object to InputStream
    EdmEntityType edmEntityType = edmEntitySet.getEntityType();
    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

    final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
    EntityCollectionSerializerOptions opts =
        EntityCollectionSerializerOptions
            .with().id(id)
            .count(countOption)
            .contextURL(contextUrl)
            .build();

    SerializerResult serializerResult = serializer.entityCollection(serviceMetadata,
                                                                    edmEntityType,
                                                                    returnEntityCollection,
                                                                    opts);
    InputStream serializedContent = serializerResult.getContent();

    // Finally: configure the response object: set the body, headers and status code
    response.setContent(serializedContent);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}