Ejemplo n.º 1
0
  @Override
  public void readEntity(
      final ODataRequest request,
      ODataResponse response,
      final UriInfo uriInfo,
      final ContentType requestedContentType)
      throws ODataApplicationException, SerializerException {
    // First we have to figure out which entity set the requested entity is in
    final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());

    // Next we fetch the requested entity from the database
    Entity entity;
    try {
      entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet);
    } catch (DataProviderException e) {
      throw new ODataApplicationException(e.getMessage(), 500, Locale.ENGLISH);
    }

    if (entity == null) {
      // If no entity was found for the given key we throw an exception.
      throw new ODataApplicationException(
          "No entity found for this key", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
    } else {
      // If an entity was found we proceed by serializing it and sending it to the client.
      ODataSerializer serializer = odata.createSerializer(requestedContentType);
      final ExpandOption expand = uriInfo.getExpandOption();
      final SelectOption select = uriInfo.getSelectOption();
      InputStream serializedContent =
          serializer
              .entity(
                  edm,
                  edmEntitySet.getEntityType(),
                  entity,
                  EntitySerializerOptions.with()
                      .contextURL(
                          isODataMetadataNone(requestedContentType)
                              ? null
                              : getContextUrl(edmEntitySet, true, expand, select, null))
                      .expand(expand)
                      .select(select)
                      .build())
              .getContent();
      response.setContent(serializedContent);
      response.setStatusCode(HttpStatusCode.OK.getStatusCode());
      response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
    }
  }
Ejemplo n.º 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());
  }