예제 #1
0
 private Entity readEntityInternal(final UriInfoResource uriInfo, final EdmEntitySet entitySet)
     throws DataProvider.DataProviderException {
   // This method will extract the key values and pass them to the data provider
   final UriResourceEntitySet resourceEntitySet =
       (UriResourceEntitySet) uriInfo.getUriResourceParts().get(0);
   return dataProvider.read(entitySet, resourceEntitySet.getKeyPredicates());
 }
예제 #2
0
  @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();
    InputStream serializedContent =
        serializer
            .entityCollection(
                edm,
                edmEntitySet.getEntityType(),
                entitySet,
                EntityCollectionSerializerOptions.with()
                    .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());
  }