summaryrefslogtreecommitdiff
path: root/dotnet/Qpid.Client/Client/AmqBrokerInfo.cs
blob: 591c5b941f935d1a1139a7a4bccd1598fe18f4fd (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 *
 * 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.
 *
 */
using System;
using System.Collections;
using System.Text;
using Apache.Qpid.Client.Qms;

namespace Apache.Qpid.Client
{
    public class AmqBrokerInfo : IBrokerInfo
    {
        public readonly string URL_FORMAT_EXAMPLE =
            "<transport>://<hostname>[:<port Default=\""+BrokerInfoConstants.DEFAULT_PORT+"\">][?<option>='<value>'[,<option>='<value>']]";

        public const long DEFAULT_CONNECT_TIMEOUT = 30000L;

        private string _host = "localhost";
        private int _port = 5672;
        private string _transport = "amqp";
        private Hashtable _options = new Hashtable();
        private SslOptions _sslOptions;

        public AmqBrokerInfo()
        {
        }

        public AmqBrokerInfo(string url)
        {
            // URL should be of format tcp://host:port?option='value',option='value'
            try
            {
                Uri connection = new Uri(url);

                String transport = connection.Scheme;

                // Handles some defaults to minimise changes to existing broker URLS e.g. localhost
                if (transport != null)
                {
                    transport = transport.ToLower();
                    //todo this list of valid transports should be enumerated somewhere
                    if ((!(transport.Equals("vm") || transport.Equals("tcp"))))
                    {
                        if (transport.Equals("localhost"))
                        {
                            connection = new Uri(BrokerInfoConstants.DEFAULT_TRANSPORT + "://" + url);
                            transport = connection.Scheme;
                        }
                        else
                        {
                            if (url[transport.Length] == ':' && url[transport.Length + 1] != '/')
                            {
                                //Then most likely we have a host:port value
                                connection = new Uri(BrokerInfoConstants.DEFAULT_TRANSPORT + "://" + url);
                                transport = connection.Scheme;
                            }
                            else
                            {
                                URLHelper.parseError(0, transport.Length, "Unknown transport", url);
                            }
                        }
                    }
                }
                else
                {
                    //Default the transport
                    connection = new Uri(BrokerInfoConstants.DEFAULT_TRANSPORT + "://" + url);
                    transport = connection.Scheme;
                }

                if (transport == null)
                {
                    URLHelper.parseError(-1, "Unknown transport:'" + transport + "'" +
                                             " In broker URL:'" + url + "' Format: " + URL_FORMAT_EXAMPLE, "");
                }

                Transport = transport;

                String host = connection.Host;
                if (!host.Equals("default")) Host = host;

                int port = connection.Port;

                if (port == -1)
                {
                    // Fix for when there is port data but it is not automatically parseable by getPort().
                    String auth = connection.Authority;

                    if (auth != null && auth.IndexOf(':') != -1)
                    {
                        int start = auth.IndexOf(":") + 1;
                        int end = start;
                        bool looking = true;
                        bool found = false;
                        //Walk the authority looking for a port value.
                        while (looking)
                        {
                            try
                            {
                                end++;
                                int.Parse(auth.Substring(start, end-start+1));

                                if (end >= auth.Length)
                                {
                                    looking = false;
                                    found = true;
                                }
                            }
                            catch (FormatException)
                            {
                                looking = false;
                            }

                        }
                        if (found)
                        {
                            Port = int.Parse(auth.Substring(start, end-start+1));
                        }
                        else
                        {
                            URLHelper.parseError(connection.ToString().IndexOf(connection.Authority) + end - 1,
                                                 "Illegal character in port number", connection.ToString());
                        }
                    }
                    else
                    {
                        Port = BrokerInfoConstants.DEFAULT_PORT;
                    }
                }
                else
                {
                    Port = port;
                }

                String queryString = connection.Query;
                if (queryString.Length > 0 && queryString[0] == '?')
                {
                    queryString = queryString.Substring(1);
                }

                URLHelper.parseOptions(_options, queryString);

                //Fragment is #string (not used)
            }
            catch (UriFormatException uris)
            {
                throw uris;
//                if (uris is UrlSyntaxException)
//                {
//                    throw uris;
//                }
//
//                URLHelper.parseError(uris.getIndex(), uris.getReason(), uris.getInput());
            }
        }

        public AmqBrokerInfo(string transport, string host, int port, bool useSSL) : this()
        {
            _transport = transport;
            _host = host;
            _port = port;

            if (useSSL)
            {
                SetOption(BrokerInfoConstants.OPTIONS_SSL, "true");
            }
        }

        public AmqBrokerInfo(string transport, string host, int port, SslOptions sslConfig)
           : this()
        {
           _transport = transport;
           _host = host;
           _port = port;
            
           if ( sslConfig != null )
           {
              SetOption(BrokerInfoConstants.OPTIONS_SSL, "true");
              _sslOptions = sslConfig;
           }
        }


        public string Host
        {
            get { return _host; }
            set { _host = value; }
        }

        public int Port
        {
            get { return _port; }
            set { _port = value; }
        }

        public string Transport
        {
            get { return _transport; }
            set { _transport = value; }
        }

        public SslOptions SslOptions
        {
            get { return _sslOptions; }
        }

        public string GetOption(string key)
        {
            return (string)_options[key];
        }

        public void SetOption(string key, string value)
        {
            _options[key] = value;
        }

        public long Timeout
        {
            get
            {
                if ( _options.ContainsKey(BrokerInfoConstants.OPTIONS_CONNECT_TIMEOUT) )
                {
                    try
                    {
                        return long.Parse(GetOption(BrokerInfoConstants.OPTIONS_CONNECT_TIMEOUT));
                    } catch ( FormatException )
                    {
                        //Do nothing as we will use the default below.
                    }
                }
                return BrokerInfoConstants.DEFAULT_CONNECT_TIMEOUT;
            }
            set
            {
                SetOption(BrokerInfoConstants.OPTIONS_CONNECT_TIMEOUT, value.ToString());
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(_transport);
            sb.Append("://");

            if (!(StringEqualsIgnoreCase(_transport, "vm")))
            {
                sb.Append(_host);
            }

            sb.Append(':');
            sb.Append(_port);

            sb.Append(URLHelper.printOptions(_options));

            return sb.ToString();
        }
        
		public override bool Equals(object obj)
		{
	        if (!(obj is IBrokerInfo))
	        {
	            return false;
	        }
	
	        IBrokerInfo bd = (IBrokerInfo) obj;
	        return StringEqualsIgnoreCase(_host, bd.Host) &&
	        	_port == bd.Port &&
                StringEqualsIgnoreCase(_transport, bd.Transport) &&
                UseSSL == bd.UseSSL;
        }
    	
		public override int GetHashCode()
		{
			return _host.ToLower().GetHashCode() ^ _port.GetHashCode();
		}

        // TODO: move to util class.
        private bool StringEqualsIgnoreCase(string one, string two)
        {
            return one.ToLower().Equals(two.ToLower());
        }

        public bool UseSSL
        {
            get
            {
                // To be friendly to users we should be case insensitive.
                // or simply force users to conform to OPTIONS_SSL
                // todo make case insensitive by trying ssl Ssl sSl ssL SSl SsL sSL SSL

                if ( _options.ContainsKey(BrokerInfoConstants.OPTIONS_SSL) )
                {
                    return StringEqualsIgnoreCase(GetOption(BrokerInfoConstants.OPTIONS_SSL), "true");
                }

                return false;
            }
            set
            {
                SetOption(BrokerInfoConstants.OPTIONS_SSL, value.ToString());
            }
        }
    }
}