private void appendEntitySets(final XMLStreamWriter writer, final List<EdmEntitySet> entitySets)
      throws XMLStreamException {
    for (EdmEntitySet entitySet : entitySets) {
      writer.writeStartElement(XML_ENTITY_SET);
      writer.writeAttribute(XML_NAME, entitySet.getName());
      writer.writeAttribute(
          XML_ENTITY_TYPE, getAliasedFullQualifiedName(entitySet.getEntityType(), false));
      if (!entitySet.isIncludeInServiceDocument()) {
        writer.writeAttribute(
            XML_INCLUDE_IN_SERVICE_DOCUMENT, "" + entitySet.isIncludeInServiceDocument());
      }

      appendNavigationPropertyBindings(writer, entitySet);
      writer.writeEndElement();
    }
  }
Ejemplo n.º 2
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.º 3
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());
  }
Ejemplo n.º 4
0
  private ContextURL getContextUrl(
      final EdmEntitySet entitySet,
      final boolean isSingleEntity,
      final ExpandOption expand,
      final SelectOption select,
      final String navOrPropertyPath)
      throws SerializerException {

    return ContextURL.with()
        .entitySet(entitySet)
        .selectList(
            odata
                .createUriHelper()
                .buildContextURLSelectList(entitySet.getEntityType(), expand, select))
        .suffix(isSingleEntity ? Suffix.ENTITY : null)
        .navOrPropertyPath(navOrPropertyPath)
        .build();
  }