/// <summary> /// Writes a set of links (Uris) in response to a $links query; includes optional count and next-page-link information. /// </summary> /// <param name="writer">The <see cref="XmlWriter"/> to write to.</param> /// <param name="links">The associated entity links to write.</param> internal static void WriteAssociatedEntityLinks(XmlWriter writer, ODataAssociatedEntityLinks links) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(writer != null, "writer != null"); Debug.Assert(links != null, "links != null"); // <links> ... writer.WriteStartElement(string.Empty, AtomConstants.ODataLinksElementName, AtomConstants.ODataNamespace); // xmlns= writer.WriteAttributeString(AtomConstants.XmlnsNamespacePrefix, AtomConstants.ODataNamespace); if (links.InlineCount.HasValue) { // <m:count> WriteCount(writer, links.InlineCount.Value, true); } IEnumerable <ODataAssociatedEntityLink> associatedEntityLinks = links.Links; if (associatedEntityLinks != null) { foreach (ODataAssociatedEntityLink link in associatedEntityLinks) { WriteAssociatedEntityLink(writer, link, false); } } if (links.NextLink != null) { // <d:next> writer.WriteElementString(string.Empty, AtomConstants.ODataNextLinkElementName, AtomConstants.ODataNamespace, UriUtils.UriToString(links.NextLink)); } // </links> writer.WriteEndElement(); }
/// <summary> /// Writes a set of links (Uris) in response to a $links query; includes optional count and next-page-link information. /// </summary> /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param> /// <param name="baseUri">The base Uri used for writing.</param> /// <param name="associatedEntityLinks">The set of associated entity links to write out.</param> /// <param name="includeResultsWrapper">True if the 'results' wrapper should be included into the payload; otherwise false.</param> private static void WriteAssociatedEntityLinks(JsonWriter jsonWriter, Uri baseUri, ODataAssociatedEntityLinks associatedEntityLinks, bool includeResultsWrapper) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(jsonWriter != null, "jsonWriter != null"); Debug.Assert(associatedEntityLinks != null, "links != null"); if (includeResultsWrapper) { // { jsonWriter.StartObjectScope(); } if (associatedEntityLinks.InlineCount.HasValue) { Debug.Assert(includeResultsWrapper, "Expected 'includeResultsWrapper' to be true if a count is specified."); jsonWriter.WriteName(JsonConstants.ODataCountName); jsonWriter.WriteValue(associatedEntityLinks.InlineCount.Value); } if (includeResultsWrapper) { // "results": jsonWriter.WriteDataArrayName(); } jsonWriter.StartArrayScope(); IEnumerable <ODataAssociatedEntityLink> entityLinks = associatedEntityLinks.Links; if (entityLinks != null) { foreach (ODataAssociatedEntityLink link in entityLinks) { WriteAssociatedEntityLink(jsonWriter, baseUri, link); } } jsonWriter.EndArrayScope(); if (associatedEntityLinks.NextLink != null) { // "__next": ... Debug.Assert(includeResultsWrapper, "Expected 'includeResultsWrapper' to be true if a next page link is specified."); jsonWriter.WriteName(JsonConstants.ODataNextLinkName); jsonWriter.WriteValue(UriUtils.UriToString(associatedEntityLinks.NextLink)); } if (includeResultsWrapper) { jsonWriter.EndObjectScope(); } }
/// <summary> /// Writes a set of links (Uris) in response to a $links query; includes optional count and next-page-link information. /// </summary> /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param> /// <param name="baseUri">The base Uri used for writing.</param> /// <param name="associatedEntityLinks">The set of associated entity links to write out.</param> /// <param name="version">The protocol version used for writing.</param> /// <param name="writingResponse">Flag indicating whether a request or a response is being written.</param> internal static void WriteAssociatedEntityLinks(JsonWriter jsonWriter, Uri baseUri, ODataAssociatedEntityLinks associatedEntityLinks, ODataVersion version, bool writingResponse) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(jsonWriter != null, "jsonWriter != null"); Debug.Assert(associatedEntityLinks != null, "associatedEntityLinks != null"); ODataJsonWriterUtils.WriteDataWrapper( jsonWriter, writingResponse, () => WriteAssociatedEntityLinks(jsonWriter, baseUri, associatedEntityLinks, version >= ODataVersion.V2 && writingResponse)); }
/// <summary> /// Write a set of top-level links to the given stream. This method creates an /// async buffered stream, writes the links to it and returns an <see cref="AsyncWriter"/> /// that can be used to flush and close/dispose the stream. /// </summary> /// <param name="stream">The stream to write the links to.</param> /// <param name="links">The associated entity links to write as message payload.</param> /// <returns>An <see cref="AsyncWriter"/> that can be used to flush and close/dispose the stream.</returns> private AsyncWriter WriteAssociatedEntityLinksImplementation(Stream stream, ODataAssociatedEntityLinks links) { Debug.Assert(this.writerPayloadKind != ODataPayloadKind.Unsupported, "Expected payload kind, format and encoding to be set by now."); return this.WriteTopLevelContent( stream, (xmlWriter) => ODataAtomWriterUtils.WriteAssociatedEntityLinks(xmlWriter, links), (jsonWriter) => ODataJsonWriterUtils.WriteAssociatedEntityLinks(jsonWriter, this.settings.BaseUri, links, this.settings.Version, this.writingResponse), Strings.ODataMessageWriter_InvalidContentTypeForWritingLinks, InternalErrorCodes.ODataMessageWriter_WriteAssociatedEntityLinks); }
/// <summary> /// Writes the result of a $links query as the message payload. /// </summary> /// <param name="links">The associated entity links to write as message payload.</param> /// <returns>A func which performs the actual writing given the stream to write to.</returns> private Func<Stream, AsyncWriter> WriteAssociatedEntityLinksImplementation(ODataAssociatedEntityLinks links) { // NOTE: we decided to not stream links for now but only make writing them async. this.VerifyWriterNotUsed(); ExceptionUtils.CheckArgumentNotNull(links, "links"); if (links.InlineCount.HasValue) { // Check that Count is not set for requests if (!this.writingResponse) { throw new ODataException(Strings.ODataMessageWriter_InlineCountInRequest); } ODataVersionChecker.CheckInlineCount(this.settings.Version); } if (links.NextLink != null) { // Check that NextPageLink is not set for requests if (!this.writingResponse) { throw new ODataException(Strings.ODataMessageWriter_NextPageLinkInRequest); } ODataVersionChecker.CheckServerPaging(this.settings.Version); } // Set the content type header here since all headers have to be set before getting the stream this.SetOrVerifyHeaders(ODataPayloadKind.AssociatedEntityLink); return (stream) => this.WriteAssociatedEntityLinksImplementation(stream, links); }
/// <summary> /// Asynchronously writes the result of a $links query as the message payload. /// </summary> /// <param name="links">The associated entity links to write as message payload.</param> /// <returns>A task representing the asynchronous writing of the associated entity links.</returns> public Task WriteAssociatedEntityLinksAsync(ODataAssociatedEntityLinks links) { return this.WriteToStreamAsync(this.WriteAssociatedEntityLinksImplementation(links)); }
/// <summary> /// Writes the result of a $links query as the message payload. /// </summary> /// <param name="links">The associated entity links to write as message payload.</param> public void WriteAssociatedEntityLinks(ODataAssociatedEntityLinks links) { this.WriteToStream(this.WriteAssociatedEntityLinksImplementation(links)); }
/// <summary> /// Writes a set of links (Uris) in response to a $links query; includes optional count and next-page-link information. /// </summary> /// <param name="writer">The <see cref="XmlWriter"/> to write to.</param> /// <param name="links">The associated entity links to write.</param> internal static void WriteAssociatedEntityLinks(XmlWriter writer, ODataAssociatedEntityLinks links) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(writer != null, "writer != null"); Debug.Assert(links != null, "links != null"); // <links> ... writer.WriteStartElement(string.Empty, AtomConstants.ODataLinksElementName, AtomConstants.ODataNamespace); // xmlns= writer.WriteAttributeString(AtomConstants.XmlnsNamespacePrefix, AtomConstants.ODataNamespace); if (links.InlineCount.HasValue) { // <m:count> WriteCount(writer, links.InlineCount.Value, true); } IEnumerable<ODataAssociatedEntityLink> associatedEntityLinks = links.Links; if (associatedEntityLinks != null) { foreach (ODataAssociatedEntityLink link in associatedEntityLinks) { WriteAssociatedEntityLink(writer, link, false); } } if (links.NextLink != null) { // <d:next> writer.WriteElementString(string.Empty, AtomConstants.ODataNextLinkElementName, AtomConstants.ODataNamespace, UriUtils.UriToString(links.NextLink)); } // </links> writer.WriteEndElement(); }
/// <summary> /// Writes a set of links (Uris) in response to a $links query; includes optional count and next-page-link information. /// </summary> /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param> /// <param name="baseUri">The base Uri used for writing.</param> /// <param name="associatedEntityLinks">The set of associated entity links to write out.</param> /// <param name="includeResultsWrapper">True if the 'results' wrapper should be included into the payload; otherwise false.</param> private static void WriteAssociatedEntityLinks(JsonWriter jsonWriter, Uri baseUri, ODataAssociatedEntityLinks associatedEntityLinks, bool includeResultsWrapper) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(jsonWriter != null, "jsonWriter != null"); Debug.Assert(associatedEntityLinks != null, "links != null"); if (includeResultsWrapper) { // { jsonWriter.StartObjectScope(); } if (associatedEntityLinks.InlineCount.HasValue) { Debug.Assert(includeResultsWrapper, "Expected 'includeResultsWrapper' to be true if a count is specified."); jsonWriter.WriteName(JsonConstants.ODataCountName); jsonWriter.WriteValue(associatedEntityLinks.InlineCount.Value); } if (includeResultsWrapper) { // "results": jsonWriter.WriteDataArrayName(); } jsonWriter.StartArrayScope(); IEnumerable<ODataAssociatedEntityLink> entityLinks = associatedEntityLinks.Links; if (entityLinks != null) { foreach (ODataAssociatedEntityLink link in entityLinks) { WriteAssociatedEntityLink(jsonWriter, baseUri, link); } } jsonWriter.EndArrayScope(); if (associatedEntityLinks.NextLink != null) { // "__next": ... Debug.Assert(includeResultsWrapper, "Expected 'includeResultsWrapper' to be true if a next page link is specified."); jsonWriter.WriteName(JsonConstants.ODataNextLinkName); jsonWriter.WriteValue(UriUtils.UriToString(associatedEntityLinks.NextLink)); } if (includeResultsWrapper) { jsonWriter.EndObjectScope(); } }