| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Text; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Dispatcher |
| | | 11 | | { |
| | | 12 | | internal class DemultiplexingDispatchMessageFormatter : IDispatchMessageFormatter |
| | | 13 | | { |
| | | 14 | | private readonly IDispatchMessageFormatter _defaultFormatter; |
| | | 15 | | private readonly Dictionary<WebContentFormat, IDispatchMessageFormatter> _formatters; |
| | | 16 | | private string _supportedFormats; |
| | | 17 | | |
| | 160 | 18 | | public DemultiplexingDispatchMessageFormatter(IDictionary<WebContentFormat, IDispatchMessageFormatter> formatter |
| | | 19 | | { |
| | 160 | 20 | | if (formatters == null) |
| | | 21 | | { |
| | 0 | 22 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(formatters)); |
| | | 23 | | } |
| | | 24 | | |
| | 160 | 25 | | _formatters = new Dictionary<WebContentFormat, IDispatchMessageFormatter>(); |
| | 960 | 26 | | foreach (WebContentFormat key in formatters.Keys) |
| | | 27 | | { |
| | 320 | 28 | | _formatters.Add(key, formatters[key]); |
| | | 29 | | } |
| | | 30 | | |
| | 160 | 31 | | _defaultFormatter = defaultFormatter; |
| | 160 | 32 | | } |
| | | 33 | | |
| | | 34 | | public void DeserializeRequest(Message message, object[] parameters) |
| | | 35 | | { |
| | 5 | 36 | | if (message == null) |
| | | 37 | | { |
| | 0 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | IDispatchMessageFormatter selectedFormatter; |
| | 5 | 42 | | if (TryGetEncodingFormat(message, out WebContentFormat format)) |
| | | 43 | | { |
| | 5 | 44 | | _formatters.TryGetValue(format, out selectedFormatter); |
| | 5 | 45 | | if (selectedFormatter == null) |
| | | 46 | | { |
| | 0 | 47 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | else |
| | | 51 | | { |
| | 0 | 52 | | selectedFormatter = _defaultFormatter; |
| | 0 | 53 | | if (selectedFormatter == null) |
| | | 54 | | { |
| | 0 | 55 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | 5 | 59 | | selectedFormatter.DeserializeRequest(message, parameters); |
| | 5 | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) => throw Diagno |
| | | 63 | | |
| | | 64 | | internal static string GetSupportedFormats(IEnumerable<WebContentFormat> formats) |
| | | 65 | | { |
| | 0 | 66 | | StringBuilder sb = new StringBuilder(); |
| | 0 | 67 | | int i = 0; |
| | 0 | 68 | | foreach (WebContentFormat format in formats) |
| | | 69 | | { |
| | 0 | 70 | | if (i > 0) |
| | | 71 | | { |
| | 0 | 72 | | sb.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator); |
| | 0 | 73 | | sb.Append(" "); |
| | | 74 | | } |
| | 0 | 75 | | sb.Append("'" + format.ToString() + "'"); |
| | 0 | 76 | | ++i; |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | return sb.ToString(); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | internal static bool TryGetEncodingFormat(Message message, out WebContentFormat format) |
| | | 83 | | { |
| | 5 | 84 | | message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out object prop); |
| | 5 | 85 | | if (!(prop is WebBodyFormatMessageProperty formatProperty)) |
| | | 86 | | { |
| | 0 | 87 | | format = WebContentFormat.Default; |
| | 0 | 88 | | return false; |
| | | 89 | | } |
| | | 90 | | |
| | 5 | 91 | | format = formatProperty.Format; |
| | | 92 | | |
| | 5 | 93 | | return true; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | private string GetSupportedFormats() |
| | | 97 | | { |
| | 0 | 98 | | if (_supportedFormats == null) |
| | | 99 | | { |
| | 0 | 100 | | _supportedFormats = GetSupportedFormats(_formatters.Keys); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | return _supportedFormats; |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |