-
Notifications
You must be signed in to change notification settings - Fork 137
/
ConnectionInfo.cs
410 lines (346 loc) · 14.2 KB
/
ConnectionInfo.cs
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using System.Xml;
using System.Xml.Serialization;
using SolarWinds.InformationService.Contract2;
using SolarWinds.InformationService.Contract2.PubSub;
using SolarWinds.InformationService.InformationServiceClient;
using SwqlStudio.Properties;
using SwqlStudio.Subscriptions;
namespace SwqlStudio
{
public class ConnectionInfo : IDisposable
{
public string ServerType { get; set; }
private string _server;
private string _username;
private string _password;
private InfoServiceProxy _proxy;
private readonly InfoServiceBase _infoServiceType;
public event EventHandler<EventArgs> ConnectionClosed;
public event EventHandler<EventArgs> ConnectionClosing;
public ConnectionInfo(string server, string username, string password, string serverType)
{
ServerType = serverType;
_server = server;
_username = username;
_password = password;
_infoServiceType = InfoServiceFactory.Create(serverType, username, password);
QueryParameters = new PropertyBag();
}
public Binding Binding
{
get { return _infoServiceType.Binding; }
}
public string Server
{
get { return _server; }
set { _server = value; }
}
public string UserName
{
get { return _username; }
set { _username = value; }
}
public string Password
{
get { return _password; }
set { _password = value; }
}
public bool CanCreateSubscription { get; set; }
public bool SupportsActiveSubscriptions
{
get { return _infoServiceType.SupportsActiveSubscriber; }
}
public string Title
{
get
{
return String.Format("{0} : {1}{2}", Server, ServerType, FormattedUserName);
}
}
private string FormattedUserName
{
get
{
if (!string.IsNullOrEmpty(UserName))
return string.Format(" [{0}]", UserName);
return UserName;
}
}
public InfoServiceProxy Proxy
{
get { return _proxy; }
}
public InformationServiceConnection Connection { get; private set; }
public static List<ServerType> AvailableServerTypes
{
get
{
List<ServerType> serverTypes = new List<ServerType>
{
new ServerType() { Type = "Orion (v3)", IsAuthenticationRequired = true },
new ServerType() { Type = "Orion (v3) AD", IsAuthenticationRequired = false },
new ServerType() { Type = "Orion (v3) Certificate", IsAuthenticationRequired = false },
new ServerType() { Type = "Orion (v3) over HTTPS", IsAuthenticationRequired = true },
new ServerType() { Type = "Orion (v2)", IsAuthenticationRequired = true },
new ServerType() { Type = "Orion (v2) AD", IsAuthenticationRequired = false },
new ServerType() { Type = "Orion (v2) Certificate", IsAuthenticationRequired = false },
new ServerType() { Type = "Orion (v2) over HTTPS", IsAuthenticationRequired = true },
new ServerType() { Type = "EOC", IsAuthenticationRequired = true },
new ServerType() { Type = "NCM", IsAuthenticationRequired = true },
new ServerType() { Type = "NCM (Windows Authentication)", IsAuthenticationRequired = false },
new ServerType() { Type = "NCM Integration", IsAuthenticationRequired = true },
new ServerType() { Type = "Java over HTTP", IsAuthenticationRequired = true }
};
if (Settings.Default.ShowCompressedModes)
{
serverTypes.AddRange(new[]
{
new ServerType() { Type = "Orion (v2) Compressed", IsAuthenticationRequired = true },
new ServerType() { Type = "Orion (v2) AD Compressed", IsAuthenticationRequired = false },
new ServerType() { Type = "Orion (v3) Compressed", IsAuthenticationRequired = true },
new ServerType() { Type = "Orion (v3) AD Compressed", IsAuthenticationRequired = false },
});
}
return serverTypes;
}
}
public PropertyBag QueryParameters { get; set; }
public void Connect()
{
if (_proxy == null || (_proxy != null && (_proxy.Channel.State == CommunicationState.Closed || _proxy.Channel.State == CommunicationState.Faulted)))
{
if (_proxy != null)
_proxy.Dispose();
_proxy = _infoServiceType.CreateProxy(_server);
_proxy.OperationTimeout = TimeSpan.FromMinutes(Settings.Default.OperationTimeout);
_proxy.ChannelFactory.Endpoint.Behaviors.Add(new LogHeaderReaderBehavior());
_proxy.Open();
}
Connection = new InformationServiceConnection((IInformationService)_proxy);
Connection.Open();
}
public bool IsConnected
{
get { return _proxy != null && _proxy.ClientChannel.State == CommunicationState.Opened; }
}
internal NotificationDeliveryServiceProxy CreateActiveListenerProxy(INotificationSubscriber listener)
{
if (!_infoServiceType.SupportsActiveSubscriber)
throw new InvalidOperationException("This connection type doesn't support active subscriptions");
return _infoServiceType.CreateNotificationDeliveryServiceProxy(_server, listener);
}
private void EnsureConnection()
{
if (!IsConnected)
Connect();
}
public IEnumerable<T> Query<T>(string swql) where T : new()
{
EnsureConnection();
using (var context = new InformationServiceContext(_proxy))
using (var serviceQuery = context.CreateQuery<T>(swql))
{
var enumerator = serviceQuery.GetEnumerator();
while (enumerator.MoveNext())
{
yield return enumerator.Current;
}
}
}
public DataTable Query(string swql)
{
XmlDocument dummy;
XmlDocument dummy2;
return Query(swql, out dummy, out dummy2);
}
public DataTable Query(string swql, out XmlDocument queryPlan, out XmlDocument queryStats)
{
XmlDocument tmpQueryPlan = null; // can't reference out parameter from closure
XmlDocument tmpQueryStats = null; // can't reference out parameter from closure
DataTable result = DoWithExceptionTranslation(
delegate
{
EnsureConnection();
using (InformationServiceCommand command = new InformationServiceCommand(swql, Connection) { ApplicationTag = "SWQL Studio" })
{
foreach (var param in QueryParameters)
command.Parameters.AddWithValue(param.Key, param.Value);
InformationServiceDataAdapter dataAdapter = new InformationServiceDataAdapter(command);
DataTable resultDataTable = new DataTable();
dataAdapter.Fill(resultDataTable);
tmpQueryPlan = dataAdapter.QueryPlan;
tmpQueryStats = dataAdapter.QueryStats;
return resultDataTable;
}
});
queryPlan = tmpQueryPlan;
queryStats = tmpQueryStats;
return result;
}
public static void DoWithExceptionTranslation(Action action)
{
DoWithExceptionTranslation(delegate
{
action();
return 0;
});
}
public static T DoWithExceptionTranslation<T>(Func<T> action, bool retryOnConnectionError = true)
{
string msg;
Exception inner;
bool couldRetry = false;
try
{
return action();
}
catch (FaultException<InfoServiceFaultContract> ex)
{
msg = ex.Detail.Message;
inner = ex;
}
catch (SecurityNegotiationException ex)
{
msg = ex.Message;
inner = ex;
}
catch (FaultException ex)
{
msg = ex.InnerException?.Message ?? ex.Message;
inner = ex.InnerException ?? ex;
}
catch (MessageSecurityException ex)
{
if (ex.InnerException is FaultException fault)
{
msg = fault.Message;
inner = fault;
couldRetry = fault.Code?.SubCode?.Name == "BadContextToken";
}
else
{
msg = ex.Message;
inner = ex;
}
}
catch (CommunicationObjectFaultedException ex)
{
msg = ex.Message;
inner = ex;
couldRetry = true;
}
catch (Exception ex)
{
msg = ex.Message;
inner = ex;
}
if (couldRetry && retryOnConnectionError)
{
return DoWithExceptionTranslation(action, false);
}
throw new ApplicationException(msg, inner);
}
public XmlDocument QueryXml(string query, out XmlDocument queryPlan, out List<ErrorMessage> errorMessages, out XmlDocument queryStats)
{
EnsureConnection();
Message results;
errorMessages = null;
using (new SwisSettingsContext { DataProviderTimeout = TimeSpan.FromSeconds(30), ApplicationTag = "SWQL Studio", AppendErrors = true })
{
results = _proxy.Query(new QueryXmlRequest(query, QueryParameters));
}
XmlReader reader = results.GetReaderAtBodyContents();
var body = new XmlDocument(reader.NameTable);
body.Load(reader);
var nsmgr = new XmlNamespaceManager(reader.NameTable);
nsmgr.AddNamespace("is", Constants.Namespace);
bool hasErrors = false;
if (results.Headers.FindHeader("hasErrors", Constants.Namespace) > -1)
{
hasErrors = results.Headers.GetHeader<bool>("hasErrors", Constants.Namespace);
}
if (hasErrors)
{
XmlNode errorsNode = body.SelectSingleNode("/is:QueryXmlResponse/is:QueryXmlResult/errors", nsmgr);
if (errorsNode != null)
{
errorMessages = new List<ErrorMessage>();
foreach (XmlNode node in errorsNode.ChildNodes)
{
XmlSerializer serializer = new XmlSerializer(typeof(ErrorMessage));
ErrorMessage message = (ErrorMessage)serializer.Deserialize(new StringReader(node.OuterXml));
if (message != null)
errorMessages.Add(message);
}
errorsNode.ParentNode.RemoveChild(errorsNode);
}
}
// Extract query plan if present
XmlNode queryPlanNode = body.SelectSingleNode("/is:QueryXmlResponse/is:QueryXmlResult/is:queryResult/is:queryPlan", nsmgr);
if (queryPlanNode != null)
{
queryPlan = new XmlDocument();
queryPlan.LoadXml(queryPlanNode.OuterXml);
queryPlanNode.ParentNode.RemoveChild(queryPlanNode);
}
else
{
queryPlan = null;
}
queryStats = null;
return body;
}
public void Dispose()
{
Close();
}
public void Close()
{
if (_proxy != null)
{
var listeners = ConnectionClosing;
listeners?.Invoke(this, EventArgs.Empty);
_proxy.Dispose();
_proxy = null;
listeners = ConnectionClosed;
listeners?.Invoke(this, EventArgs.Empty);
}
}
internal ConnectionInfo Copy()
{
return new ConnectionInfo(_server, _username, _password, _infoServiceType.ServiceType)
{
QueryParameters = QueryParameters
};
}
protected bool Equals(ConnectionInfo other)
{
return string.Equals(_server, other._server) && string.Equals(_username, other._username) && string.Equals(ServerType, other.ServerType);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((ConnectionInfo)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = _server.GetHashCode();
hashCode = (hashCode * 397) ^ _username.GetHashCode();
hashCode = (hashCode * 397) ^ ServerType.GetHashCode();
return hashCode;
}
}
}
}