/// <summary> /// Gets the default media type for a given payload kind in a given format. /// </summary> /// <param name="payloadKind">The kind of payload to write.</param> /// <param name="specifiedFormat">The format in which to write the payload (can be ODataFormat.Default).</param> /// <param name="actualFormat">The default format for the specified payload kind (is never ODataFormat.Default)</param> /// <returns>The default media type for the given payload kind and format.</returns> private static MediaType GetDefaultMediaType(ODataPayloadKind payloadKind, ODataFormat specifiedFormat, out ODataFormat actualFormat) { // get the supported and default media types for the specified payload kind MediaTypeWithFormat[] supportedMediaTypes = mediaTypesForPayloadKind[(int)payloadKind]; for (int i = 0; i < supportedMediaTypes.Length; ++i) { // NOTE: the supportedMediaTypes are sorted (desc) by format and media type; so the // default format and media type is the first entry in the array MediaTypeWithFormat supportedMediaType = supportedMediaTypes[i]; if (specifiedFormat == ODataFormat.Default || supportedMediaType.Format == specifiedFormat) { actualFormat = supportedMediaType.Format; return(supportedMediaType.MediaType); } } throw new ODataException(Strings.ODataUtils_DidNotFindDefaultMediaType(specifiedFormat.ToString())); }
/// <summary> /// Converts a <see cref="ODataCollectionValue"/> to a string for use in a Url. /// </summary> /// <param name="collectionValue">Instance to convert.</param> /// <param name="model">Model to be used for validation. User model is optional. The EdmLib core model is expected as a minimum.</param> /// <param name="version">Version to be compliant with. Collection requires >= V3.</param> /// <param name="format">ODataFormat to use for the format of the literal.</param> /// <returns>A string representation of <paramref name="collectionValue"/> to be added to a Url.</returns> internal static string ConvertToUriCollectionLiteral(ODataCollectionValue collectionValue, IEdmModel model, ODataVersion version, ODataFormat format) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(collectionValue, "collectionValue"); ExceptionUtils.CheckArgumentNotNull(model, "model"); ODataVersionChecker.CheckCollectionValue(version); StringBuilder builder = new StringBuilder(); using (TextWriter textWriter = new StringWriter(builder, CultureInfo.InvariantCulture)) { ODataMessageWriterSettings messageWriterSettings = new ODataMessageWriterSettings() { Version = version, Indent = false }; if (format == ODataFormat.VerboseJson) { WriteJsonVerboseLiteral( model, messageWriterSettings, textWriter, (serializer) => serializer.WriteCollectionValue( collectionValue, null /*metadataTypeReference*/, false /*isOpenPropertyType*/)); } else if (format == ODataFormat.Json) { WriteJsonLightLiteral( model, messageWriterSettings, textWriter, (serializer) => serializer.WriteCollectionValue( collectionValue, null /*metadataTypeReference*/, false /*isTopLevelProperty*/, true /*isInUri*/, false /*isOpenPropertyType*/)); } else { throw new ArgumentException(ODataErrorStrings.ODataUriUtils_ConvertToUriLiteralUnsupportedFormat(format.ToString())); } } return(builder.ToString()); }
/// <summary> /// Creates an <see cref="ODataCollectionWriter"/> for the specified message and its stream. /// </summary> /// <param name="format">The serialization format to create the writer for.</param> /// <param name="encoding">The encoding to create the writer with.</param> /// <param name="stream">The response stream to write to.</param> /// <param name="settings">Writer settings to use.</param> /// <param name="metadataProvider">The metadata provider to use.</param> /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param> /// <returns>The newly created <see cref="ODataCollectionWriter"/> instance.</returns> /// <remarks>This is used to create the collection writer once we've obtained the stream from the response.</remarks> private static ODataCollectionWriter CreateCollectionWriter( ODataFormat format, Encoding encoding, Stream stream, ODataWriterSettings settings, DataServiceMetadataProviderWrapper metadataProvider, bool synchronous) { if (settings.BaseUri != null && !settings.BaseUri.IsAbsoluteUri) { throw new ODataException(Strings.ODataWriter_BaseUriMustBeNullOrAbsolute(UriUtils.UriToString(settings.BaseUri))); } switch (format) { case ODataFormat.Json: return(new ODataJsonCollectionWriter(stream, settings, encoding, metadataProvider, synchronous)); case ODataFormat.Atom: return(new ODataAtomCollectionWriter(stream, settings, encoding, metadataProvider, synchronous)); case ODataFormat.Default: Debug.Assert(false, "Should never get here as content-type negotiation should not return Default format for collection."); throw new ODataException(Strings.ODataCollectionWriter_CannotCreateCollectionWriterForFormat(format.ToString())); default: throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataCollectionWriter_CreateCollectionWriter_UnreachableCodePath)); } }
/// <summary> /// Creates an <see cref="ODataWriter"/> for the specified message and its stream. /// </summary> /// <param name="format">The serialization format to create the writer for.</param> /// <param name="encoding">The encoding to create the writer with.</param> /// <param name="stream">The response stream to write to.</param> /// <param name="settings">Writer settings to use.</param> /// <param name="writingResponse">True if we are writing a response message; false for request messages.</param> /// <param name="metadataProvider">The metadata provider to use.</param> /// <param name="writingFeed">True when creating a writer to write a feed; false when creating a writer to write an entry.</param> /// <param name="synchronous">True if the writer is created for synchronous operation; false for asynchronous.</param> /// <returns>The newly created <see cref="ODataWriter"/> instance.</returns> /// <remarks>This is used to create the writer once we've obtained the stream from the response.</remarks> private static ODataWriter CreateWriter( ODataFormat format, Encoding encoding, Stream stream, ODataWriterSettings settings, bool writingResponse, DataServiceMetadataProviderWrapper metadataProvider, bool writingFeed, bool synchronous) { if (settings.BaseUri != null && !settings.BaseUri.IsAbsoluteUri) { throw new ODataException(Strings.ODataWriter_BaseUriMustBeNullOrAbsolute(UriUtils.UriToString(settings.BaseUri))); } switch (format) { case ODataFormat.Json: return new ODataJsonWriter(stream, settings, encoding, writingResponse, metadataProvider, writingFeed, synchronous); case ODataFormat.Atom: return new ODataAtomWriter(stream, settings, encoding, writingResponse, metadataProvider, writingFeed, synchronous); case ODataFormat.Default: Debug.Assert(false, "Should never get here as content-type negotiation should not return Default format for entry or feed."); throw new ODataException(Strings.ODataWriter_CannotCreateWriterForFormat(format.ToString())); default: throw new ODataException(Strings.General_InternalError(InternalErrorCodes.ODataWriter_CreateWriter_UnreachableCodePath)); } }
/// <summary> /// Gets the default media type for a given payload kind in a given format. /// </summary> /// <param name="payloadKind">The kind of payload to write.</param> /// <param name="specifiedFormat">The format in which to write the payload (can be ODataFormat.Default).</param> /// <param name="actualFormat">The default format for the specified payload kind (is never ODataFormat.Default)</param> /// <returns>The default media type for the given payload kind and format.</returns> private static MediaType GetDefaultMediaType(ODataPayloadKind payloadKind, ODataFormat specifiedFormat, out ODataFormat actualFormat) { // get the supported and default media types for the specified payload kind MediaTypeWithFormat[] supportedMediaTypes = mediaTypesForPayloadKind[(int)payloadKind]; for (int i = 0; i < supportedMediaTypes.Length; ++i) { // NOTE: the supportedMediaTypes are sorted (desc) by format and media type; so the // default format and media type is the first entry in the array MediaTypeWithFormat supportedMediaType = supportedMediaTypes[i]; if (specifiedFormat == ODataFormat.Default || supportedMediaType.Format == specifiedFormat) { actualFormat = supportedMediaType.Format; return supportedMediaType.MediaType; } } throw new ODataException(Strings.ODataUtils_DidNotFindDefaultMediaType(specifiedFormat.ToString())); }