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);
  }
 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;
 }
示例#5
0
  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);
    }
  }
  @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()));
  }