public void writeServiceDocument(final JsonGenerator gen) throws IOException {
    gen.writeStartObject();

    if (!isODataMetadataNone) {
      final String metadataUri =
          (serviceRoot == null ? "" : serviceRoot.endsWith("/") ? serviceRoot : (serviceRoot + "/"))
              + Constants.METADATA;
      gen.writeObjectField(Constants.JSON_CONTEXT, metadataUri);

      if (metadata != null
          && metadata.getServiceMetadataETagSupport() != null
          && metadata.getServiceMetadataETagSupport().getMetadataETag() != null) {
        gen.writeStringField(
            Constants.JSON_METADATA_ETAG,
            metadata.getServiceMetadataETagSupport().getMetadataETag());
      }
    }

    gen.writeArrayFieldStart(Constants.VALUE);

    final EdmEntityContainer container = metadata.getEdm().getEntityContainer();
    writeEntitySets(gen, container);
    writeFunctionImports(gen, container);
    writeSingletons(gen, container);
  }
  /**
   * Appends references, e.g., to the OData Core Vocabulary, as defined in the OData specification
   * and mentioned in its Common Schema Definition Language (CSDL) document.
   */
  private void appendReference(final XMLStreamWriter writer) throws XMLStreamException {
    for (final EdmxReference reference : serviceMetadata.getReferences()) {
      writer.writeStartElement(PREFIX_EDMX, REFERENCE, NS_EDMX);
      writer.writeAttribute(URI, reference.getUri().toASCIIString());

      List<EdmxReferenceInclude> includes = reference.getIncludes();
      for (EdmxReferenceInclude include : includes) {
        writer.writeStartElement(PREFIX_EDMX, INCLUDE, NS_EDMX);
        writer.writeAttribute(XML_NAMESPACE, include.getNamespace());
        if (include.getAlias() != null) {
          // Reference Aliases are ignored for now since they are not V2 compatible
          writer.writeAttribute(XML_ALIAS, include.getAlias());
        }
        writer.writeEndElement();
      }

      List<EdmxReferenceIncludeAnnotation> includeAnnotations = reference.getIncludeAnnotations();
      for (EdmxReferenceIncludeAnnotation includeAnnotation : includeAnnotations) {
        writer.writeStartElement(PREFIX_EDMX, INCLUDE_ANNOTATIONS, NS_EDMX);
        writer.writeAttribute(XML_TERM_NAMESPACE, includeAnnotation.getTermNamespace());
        if (includeAnnotation.getQualifier() != null) {
          writer.writeAttribute(XML_QUALIFIER, includeAnnotation.getQualifier());
        }
        if (includeAnnotation.getTargetNamespace() != null) {
          writer.writeAttribute(XML_TARGET_NAMESPACE, includeAnnotation.getTargetNamespace());
        }
        writer.writeEndElement();
      }

      writer.writeEndElement();
    }
  }
 private void appendDataServices(final XMLStreamWriter writer) throws XMLStreamException {
   writer.setDefaultNamespace(NS_EDM);
   writer.writeStartElement(NS_EDMX, DATA_SERVICES);
   for (EdmSchema schema : serviceMetadata.getEdm().getSchemas()) {
     appendSchema(writer, schema);
   }
   writer.writeEndElement();
 }
 public MetadataDocumentXmlSerializer(final ServiceMetadata serviceMetadata)
     throws SerializerException {
   if (serviceMetadata == null || serviceMetadata.getEdm() == null) {
     throw new SerializerException(
         "Service Metadata and EDM must not be null for a service.",
         SerializerException.MessageKeys.NULL_METADATA_OR_EDM);
   }
   this.serviceMetadata = serviceMetadata;
 }
 public ServiceDocumentJsonSerializer(
     final ServiceMetadata metadata, final String serviceRoot, final boolean isODataMetadataNone)
     throws SerializerException {
   if (metadata == null || metadata.getEdm() == null) {
     throw new SerializerException(
         "Service Metadata and EDM must not be null for a service.",
         SerializerException.MessageKeys.NULL_METADATA_OR_EDM);
   }
   this.metadata = metadata;
   this.serviceRoot = serviceRoot;
   this.isODataMetadataNone = isODataMetadataNone;
 }
  @Test
  public void writeServiceWithEmptyMockedEdm() throws Exception {
    final Edm edm = mock(Edm.class);
    EdmEntityContainer container = mock(EdmEntityContainer.class);
    when(container.getFullQualifiedName()).thenReturn(new FullQualifiedName("service", "test"));
    when(container.getEntitySets()).thenReturn(Collections.<EdmEntitySet>emptyList());
    when(container.getFunctionImports()).thenReturn(Collections.<EdmFunctionImport>emptyList());
    when(container.getSingletons()).thenReturn(Collections.<EdmSingleton>emptyList());
    when(edm.getEntityContainer()).thenReturn(container);
    ServiceMetadata metadata = mock(ServiceMetadata.class);
    when(metadata.getEdm()).thenReturn(edm);

    assertEquals(
        "<?xml version='1.0' encoding='UTF-8'?>"
            + "<app:service xmlns:atom=\"http://www.w3.org/2005/Atom\" "
            + "xmlns:app=\"http://www.w3.org/2007/app\" "
            + "xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\" "
            + "metadata:context=\"http://host/svc/$metadata\">"
            + "<app:workspace><atom:title>service.test</atom:title></app:workspace>"
            + "</app:service>",
        IOUtils.toString(serializer.serviceDocument(metadata, "http://host/svc").getContent()));
  }
  private void processInternal(final ODataRequest request, final ODataResponse response)
      throws ODataApplicationException, ODataLibraryException {
    final int measurementHandle =
        debugger.startRuntimeMeasurement("ODataHandler", "processInternal");

    response.setHeader(HttpHeader.ODATA_VERSION, ODataServiceVersion.V40.toString());
    try {
      validateODataVersion(request);
    } catch (final ODataHandlerException e) {
      debugger.stopRuntimeMeasurement(measurementHandle);
      throw e;
    }

    final int measurementUriParser = debugger.startRuntimeMeasurement("UriParser", "parseUri");
    try {
      uriInfo =
          new Parser()
              .parseUri(
                  request.getRawODataPath(),
                  request.getRawQueryPath(),
                  null,
                  serviceMetadata.getEdm());
    } catch (final ODataLibraryException e) {
      debugger.stopRuntimeMeasurement(measurementUriParser);
      debugger.stopRuntimeMeasurement(measurementHandle);
      throw e;
    }
    debugger.stopRuntimeMeasurement(measurementUriParser);

    final int measurementUriValidator =
        debugger.startRuntimeMeasurement("UriValidator", "validate");
    final HttpMethod method = request.getMethod();
    try {
      new UriValidator().validate(uriInfo, method);
    } catch (final UriValidationException e) {
      debugger.stopRuntimeMeasurement(measurementUriValidator);
      debugger.stopRuntimeMeasurement(measurementHandle);
      throw e;
    }
    debugger.stopRuntimeMeasurement(measurementUriValidator);

    final int measurementDispatcher =
        debugger.startRuntimeMeasurement("ODataDispatcher", "dispatch");
    try {
      new ODataDispatcher(uriInfo, this).dispatch(request, response);
    } finally {
      debugger.stopRuntimeMeasurement(measurementDispatcher);
      debugger.stopRuntimeMeasurement(measurementHandle);
    }
  }