summaryrefslogtreecommitdiff
path: root/wcf/src/Apache/Qpid/Channel/AmqpChannelHelpers.cs
blob: b431689c4ddd7e28f5fb9a1cbc769b088414b021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

namespace Apache.Qpid.Channel
{
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.Globalization;

    using Apache.Qpid.AmqpTypes;

    /// <summary>
    /// Collection of constants used by the Amqp Channel classes
    /// </summary>
    static class AmqpConstants
    {
        internal const string Scheme = "amqp";
        internal const string AmqpBindingSectionName = "system.serviceModel/bindings/amqpBinding";
        internal const string AmqpBinaryBindingSectionName = "system.serviceModel/bindings/amqpBinaryBinding";
        internal const string AmqpTransportSectionName = "amqpTransport";
    }
 
    static class AmqpConfigurationStrings
    {
        public const string BrokerHost = "host";
        public const string BrokerPort = "port";
        public const string TransferMode = "transferMode";
        public const string Brokers = "brokers";
        public const string Shared = "shared";
        public const string PrefetchLimit = "prefetchLimit";
        public const string MaxBufferPoolSize = "maxBufferPoolSize";
        public const string MaxReceivedMessageSize = "maxReceivedMessageSize";
        public const string Security = "security";
        public const string SecurityMode = "mode";
        public const string SecurityTransport = "transport";
        public const string SecurityTransportCredentialType = "credentialType";
        public const string SecurityTransportUseSSL = "useSSL";
        public const string SecurityTransportDefaultCredential = "defaultCredential";
        public const string SecurityTransportIgnoreEndpointCredentials = "ignoreEndpointCredentials";
        public const string CredentialUserName = "userName";
        public const string CredentialPassword = "password";
    }

    static class AmqpDefaults
    {
        internal const string BrokerHost = "localhost";
        internal const int BrokerPort = 5672;
        internal const TransferMode TransferMode = System.ServiceModel.TransferMode.Buffered;
        internal const long MaxBufferPoolSize = 64 * 1024;
        internal const int MaxReceivedMessageSize = 5 * 1024 * 1024; //64 * 1024;
    }

    // parking spot for properties that may be shared by separate channels on a single AMQP connection
    internal class AmqpChannelProperties
    {
        string brokerHost;
        int brokerPort;
        TransferMode transferMode;
        AmqpProperties defaultMessageProperties;
        AmqpSecurityMode amqpSecurityMode;
        AmqpTransportSecurity amqpTransportSecurity;
        AmqpCredential amqpCredential;
        long maxBufferPoolSize;
        int maxReceivedMessageSize;

        internal AmqpChannelProperties()
        {
            this.brokerHost = AmqpDefaults.BrokerHost;
            this.brokerPort = AmqpDefaults.BrokerPort;
            this.transferMode = AmqpDefaults.TransferMode;
            this.defaultMessageProperties = null;
            this.amqpSecurityMode = AmqpSecurityMode.None;
            this.amqpTransportSecurity = null;
            this.amqpCredential = null;
            this.maxBufferPoolSize = AmqpDefaults.MaxBufferPoolSize;
            this.maxReceivedMessageSize = AmqpDefaults.MaxReceivedMessageSize;
        }

        public AmqpChannelProperties Clone()
        {
            AmqpChannelProperties props = (AmqpChannelProperties) this.MemberwiseClone();
            if (this.defaultMessageProperties != null)
            {
                props.defaultMessageProperties = this.defaultMessageProperties.Clone();
            }

            if (this.amqpTransportSecurity != null)
            {
                props.amqpTransportSecurity = this.amqpTransportSecurity.Clone();
            }

            if (this.amqpCredential != null)
            {
                this.amqpCredential = this.amqpCredential.Clone();
            }

            return props;
        }

        internal string BrokerHost
        {
            get { return this.brokerHost; }
            set { this.brokerHost = value; }
        }

        internal int BrokerPort
        {
            get { return this.brokerPort; }
            set { this.brokerPort = value; }
        }

        internal TransferMode TransferMode
        {
            get { return this.transferMode; }
            set { this.transferMode = value; }
        }

        internal AmqpProperties DefaultMessageProperties
        {
            get { return this.defaultMessageProperties; }
            set { this.defaultMessageProperties = value; }
        }

        internal AmqpSecurityMode AmqpSecurityMode
        {
            get { return this.amqpSecurityMode; }
            set { this.amqpSecurityMode = value; }
        }

        internal AmqpTransportSecurity AmqpTransportSecurity
        {
            get { return this.amqpTransportSecurity; }
            set { this.amqpTransportSecurity = value; }
        }

        internal AmqpCredential AmqpCredential
        {
            get { return this.amqpCredential; }
            set { this.amqpCredential = value; }
        }

        internal long MaxBufferPoolSize
        {
            get { return this.maxBufferPoolSize; }
            set { this.maxBufferPoolSize = value; }
        }

        internal int MaxReceivedMessageSize
        {
            get { return this.maxReceivedMessageSize; }
            set { this.maxReceivedMessageSize = value; }
        }
    }

    static class AmqpChannelHelpers
    {
        internal static void ValidateTimeout(TimeSpan timeout)
        {
            if (timeout < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("timeout", timeout, "Timeout must be greater than or equal to TimeSpan.Zero. To disable timeout, specify TimeSpan.MaxValue.");
            }
        }

        internal static void FindAuthenticationCredentials(AmqpChannelProperties channelProperties,
            BindingContext bindingContext)
        {
            AmqpTransportSecurity tsec = channelProperties.AmqpTransportSecurity;
            if (tsec == null)
            {
                // no auth
                return;
            }

            if (tsec.CredentialType == AmqpCredentialType.Anonymous)
            {
                // no auth
                return;
            }

            // credentials search order: specific AmqpCredentials, specific
            // ClientCredentials (if applicable), binding's default credentials

            AmqpCredential amqpCred = bindingContext.BindingParameters.Find<AmqpCredential>();
            if (amqpCred != null)
            {
                channelProperties.AmqpCredential = amqpCred.Clone();
                return;
            }

            if (!tsec.IgnoreEndpointClientCredentials)
            {
                ClientCredentials cliCred = bindingContext.BindingParameters.Find<ClientCredentials>();
                if (cliCred != null)
                {
                    if (cliCred.UserName != null)
                    {
                        if (cliCred.UserName.UserName != null)
                        {
                            channelProperties.AmqpCredential = new AmqpCredential(cliCred.UserName.UserName,
                                cliCred.UserName.Password);
                            return;
                        }
                    }
                }
            }

            if (tsec.DefaultCredential != null)
            {
                channelProperties.AmqpCredential = tsec.DefaultCredential.Clone();
            }
        }
    }
}