-
Notifications
You must be signed in to change notification settings - Fork 15
/
ConnectionProperties.cs
155 lines (133 loc) · 3.98 KB
/
ConnectionProperties.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
using LINQPad.Extensibility.DataContext;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Xml.Linq;
namespace OData4.LINQPadDriver
{
public class ConnectionProperties
{
private readonly IConnectionInfo _connectionInfo;
private readonly XElement _driverData;
public ConnectionProperties(IConnectionInfo connectionInfo)
{
_connectionInfo = connectionInfo;
_driverData = connectionInfo.DriverData;
}
public bool Persist
{
get => _connectionInfo.Persist;
set => _connectionInfo.Persist = value;
}
public string Uri
{
get => (string)_driverData.Element("Uri");
set => _driverData.SetElementValue("Uri", value);
}
public string Domain
{
get => (string)_driverData.Element("Domain");
set => _driverData.SetElementValue("Domain", value);
}
public string UserName
{
get => (string)_driverData.Element("UserName");
set => _driverData.SetElementValue("UserName", value);
}
public string Password
{
get => _connectionInfo.Decrypt((string)_driverData.Element("Password") ?? "");
set => _driverData.SetElementValue("Password", _connectionInfo.Encrypt(value));
}
public bool UseProxy
{
get => (bool?)_driverData.Element("UseProxy") ?? false;
set => _driverData.SetElementValue("UseProxy", value.ToString());
}
public bool AcceptInvalidCertificate
{
get => (bool?)_driverData.Element("AcceptInvalidCertificate") ?? false;
set => _driverData.SetElementValue("AcceptInvalidCertificate", value.ToString());
}
public AuthenticationType AuthenticationType
{
get => (AuthenticationType?)(int?)_driverData.Element("AuthenticationType") ?? AuthenticationType.None;
set => _driverData.SetElementValue("AuthenticationType", (int)value);
}
public bool LogMethod
{
get => (bool?)_driverData.Element("LogMethod") ?? true;
set => _driverData.SetElementValue("LogMethod", value.ToString());
}
public bool LogHeaders
{
get => (bool?)_driverData.Element("LogHeaders") ?? true;
set => _driverData.SetElementValue("LogHeaders", value.ToString());
}
public IEnumerable<KeyValuePair<string, string>> CustomHeaders
{
get
{
var element = _driverData.Element("CustomHeaders");
if (element == null)
return new KeyValuePair<string, string>[0];
return element.Elements("Header")
.Select(x => new KeyValuePair<string, string>((string)x.Attribute("Name") ?? "",
(string)x.Attribute("Value") ?? ""))
.Where(x => !string.IsNullOrWhiteSpace(x.Key));
}
set
{
var headers = value?.Where(x => !string.IsNullOrWhiteSpace(x.Key))
.Select(x => new XElement("Header",
new XAttribute("Name", x.Key.Trim()),
new XAttribute("Value", (x.Value ?? "").Trim())));
_driverData.Elements("CustomHeaders").Remove();
_driverData.Add(new XElement("CustomHeaders", headers));
}
}
public string ClientCertificateFile
{
get => (string)_driverData.Element("ClientCertificateFile");
set => _driverData.SetElementValue("ClientCertificateFile", value);
}
public ICredentials GetCredentials()
{
switch (AuthenticationType)
{
case AuthenticationType.None:
return null;
case AuthenticationType.Windows:
return CredentialCache.DefaultCredentials;
case AuthenticationType.Basic:
return !string.IsNullOrEmpty(UserName)
? new NetworkCredential(UserName, Password, Domain ?? string.Empty)
: CredentialCache.DefaultNetworkCredentials;
case AuthenticationType.ClientCertificate:
return null;
default:
throw new ArgumentOutOfRangeException();
}
}
public IWebProxy GetWebProxy()
{
var proxy = WebRequest.GetSystemWebProxy();
if (UseProxy)
{
proxy = LINQPad.Util.GetIWebProxy() ?? proxy;
}
return proxy;
}
public NameValueCollection GetCustomHeaders()
{
var headers = new NameValueCollection();
foreach (var header in CustomHeaders)
{
headers.Add(header.Key, header.Value);
}
return headers;
}
}
}