| | | 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.ServiceModel.Channels; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.ServiceModel.Channels |
| | | 8 | | { |
| | | 9 | | public class KafkaBinding : Binding |
| | | 10 | | { |
| | | 11 | | private KafkaSecurity _security; |
| | | 12 | | private KafkaTransportBindingElement _transport; |
| | | 13 | | private TextMessageEncodingBindingElement _textEncoding; |
| | | 14 | | private BinaryMessageEncodingBindingElement _binaryEncoding; |
| | | 15 | | |
| | 39 | 16 | | private KafkaMessageEncoding _messageEncoding = KafkaMessageEncoding.Text; |
| | | 17 | | |
| | 39 | 18 | | public KafkaBinding() |
| | | 19 | | { |
| | 39 | 20 | | Initialize(); |
| | 39 | 21 | | } |
| | | 22 | | |
| | | 23 | | public KafkaBinding(KafkaSecurityMode securityMode) |
| | 0 | 24 | | : this() |
| | | 25 | | { |
| | 0 | 26 | | _security.Mode = securityMode; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public override BindingElementCollection CreateBindingElements() |
| | | 30 | | { |
| | 331 | 31 | | BindingElementCollection elements = new(); |
| | | 32 | | |
| | 331 | 33 | | SecurityBindingElement securityBindingElement = _security.CreateMessageSecurity(); |
| | 331 | 34 | | if (securityBindingElement != null) |
| | | 35 | | { |
| | 0 | 36 | | elements.Add(securityBindingElement); |
| | | 37 | | } |
| | | 38 | | |
| | 331 | 39 | | MessageEncodingBindingElement encodingBindingElement = MessageEncoding switch |
| | 331 | 40 | | { |
| | 9 | 41 | | KafkaMessageEncoding.Binary => _binaryEncoding, |
| | 322 | 42 | | KafkaMessageEncoding.Text => _textEncoding, |
| | 0 | 43 | | _ => _textEncoding |
| | 331 | 44 | | }; |
| | | 45 | | |
| | 331 | 46 | | elements.Add(encodingBindingElement); |
| | | 47 | | |
| | 331 | 48 | | _security.ApplySecurity(_transport); |
| | | 49 | | |
| | 331 | 50 | | elements.Add(_transport); |
| | | 51 | | |
| | 331 | 52 | | return elements.Clone(); |
| | | 53 | | } |
| | | 54 | | |
| | 4 | 55 | | public override string Scheme => KafkaConstants.Scheme; |
| | | 56 | | |
| | | 57 | | |
| | | 58 | | public KafkaMessageEncoding MessageEncoding |
| | | 59 | | { |
| | 335 | 60 | | get => _messageEncoding; |
| | | 61 | | set |
| | | 62 | | { |
| | 2 | 63 | | if (!KafkaMessageEncodingHelper.IsDefined(value)) |
| | | 64 | | { |
| | 0 | 65 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 66 | | } |
| | | 67 | | |
| | 2 | 68 | | _messageEncoding = value; |
| | 2 | 69 | | } |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | public KafkaSecurity Security |
| | | 73 | | { |
| | 4 | 74 | | get => _security; |
| | 4 | 75 | | set => _security = value ?? throw new ArgumentNullException(nameof(value)); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | private void Initialize() |
| | | 79 | | { |
| | 39 | 80 | | _security = new KafkaSecurity(); |
| | 39 | 81 | | _transport = new KafkaTransportBindingElement(); |
| | 39 | 82 | | _textEncoding = new TextMessageEncodingBindingElement(); |
| | 39 | 83 | | _binaryEncoding = new BinaryMessageEncodingBindingElement(); |
| | 39 | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |