-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnector.cs
More file actions
368 lines (333 loc) · 16.6 KB
/
Connector.cs
File metadata and controls
368 lines (333 loc) · 16.6 KB
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
using Dynamicweb.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.EndpointMonitoring;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Licensing;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Logging;
using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Extensibility.Notifications;
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Http;
using System.Net.Mail;
using System.Runtime.ExceptionServices;
using System.Xml;
using static Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Notifications.Communication;
namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Connectors
{
/// <summary>
/// Main class to interact with a remote ERP.
/// </summary>
internal static class Connector
{
/// <summary>
/// The maximum retry count
/// </summary>
private static readonly int MaxRetryCount = 3;
/// <summary>
/// The retry interval
/// </summary>
private static readonly int RetryInterval = 500;
private static readonly ConcurrentDictionary<string, DateTime> InstanceIdTooManyRequestsCollection = new ConcurrentDictionary<string, DateTime>();
private static int TooManyRequestsTimeoutSeconds = 60;
public static bool EnableThrowExceptions
{
get
{
return Core.Converter.ToBoolean(Context.Current?.Items?["DynamicwebLiveIntegrationAddInThrowExceptions"]);
}
set
{
if (Context.Current?.Items != null)
{
Context.Current.Items["DynamicwebLiveIntegrationAddInThrowExceptions"] = value;
}
}
}
static Connector()
{
int timeout = SystemConfiguration.Instance.GetInt32($"{Constants.LiveIntegrationSettingsKey}/TooManyRequestsTimeoutSeconds");
if (timeout > 0)
{
TooManyRequestsTimeoutSeconds = timeout;
}
}
private static ConnectorBase GetConnector(Settings settings, Logger logger, SubmitType submitType, Order order = null)
{
if (settings == null || string.IsNullOrEmpty(settings.Endpoint))
{
return new WebServiceConnector(settings, logger, submitType, order);
}
else
{
return new EndpointConnector(settings, logger, submitType, order);
}
}
/// <summary>
/// Calculates the order.
/// </summary>
/// <param name="orderXml">The order XML.</param>
/// <param name="order">The order</param>
/// <param name="createOrder">if set to <c>true</c> [create order].</param>
/// <param name="error">The error.</param>
/// <returns>XmlDocument.</returns>
public static XmlDocument CalculateOrder(Settings settings, string orderXml, Order order, bool createOrder, out Exception error, Logger logger, SubmitType submitType)
{
error = null;
XmlDocument document = null;
try
{
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.Connector.CalculateOrder START");
// only retry if is not create order, for create order schedule task will send later
document = Communicate(settings, orderXml, $"CalculateOrder (ID: {order.Id}, CreateOrder: {createOrder})", logger, submitType, !createOrder, true, order);
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.Connector.CalculateOrder END");
}
catch (Exception ex)
{
logger.Log(ErrorLevel.ConnectionError,
$"Error CalculateOrder Order Id:'{order.Id}' CreateOrder:'{createOrder}' Message:'{ex.Message}'.");
error = ex;
}
if (EnableThrowExceptions && error != null)
{
throw error;
}
return document;
}
/// <summary>
/// Clears the cache.
/// </summary>
public static void ClearCache()
{
EndpointMonitoringService.ClearEndpoints();
}
/// <summary>
/// Gets the products information.
/// </summary>
/// <param name="productsXml">The products XML.</param>
/// <param name="retry">Retry request on error.</param>
/// <returns>XmlDocument.</returns>
public static XmlDocument GetProductsInfo(Settings settings, string productsXml, Logger logger, out HttpStatusCode httpStatusCode, bool retry = false)
{
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.Connector.GetProductsInfo START");
var result = Communicate(settings, productsXml, "GetProductsInfo", logger, SubmitType.Live, out httpStatusCode, retry);
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.Connector.GetProductsInfo END");
return result;
}
/// <summary>
/// Determines whether the web service connection is available.
/// </summary>
public static bool IsWebServiceConnectionAvailable(Settings settings, SubmitType submitType)
{
return IsWebServiceConnectionAvailable(settings, new Logger(settings), submitType);
}
/// <summary>
/// Determines whether the web service connection is available.
/// </summary>
public static bool IsWebServiceConnectionAvailable(Settings settings, Logger logger, SubmitType submitType)
{
if (settings is null)
{
return true;
}
else
{
var connector = GetConnector(settings, logger, submitType);
return connector.IsWebServiceConnectionAvailable();
}
}
/// <summary>
/// Retrieves data from the request string.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>XmlDocument.</returns>
public static XmlDocument RetrieveDataFromRequestString(Settings settings, string request, Logger logger, SubmitType submitType)
{
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.Connector.RetrieveDataFromRequestString START");
var result = Communicate(settings, request, "RetrieveDataFromRequestString", logger, submitType);
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.Connector.RetrieveDataFromRequestString END");
return result;
}
/// <summary>
/// Communicates the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="referenceName">Name of the reference.</param>
/// <param name="retry">if set to <c>true</c> [retry].</param>
/// <param name="throwException">if set to <c>true</c> [throw exception].</param>
/// <returns>XmlDocument.</returns>
private static XmlDocument Communicate(Settings settings, string request, string referenceName, Logger logger, SubmitType submitType, bool retry = true, bool throwException = false, Order order = null)
{
return Communicate(settings, request, referenceName, logger, submitType, out _, retry, throwException, order);
}
/// <summary>
/// Communicates the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="referenceName">Name of the reference.</param>
/// <param name="retry">if set to <c>true</c> [retry].</param>
/// <param name="throwException">if set to <c>true</c> [throw exception].</param>
/// <returns>XmlDocument.</returns>
private static XmlDocument Communicate(Settings settings, string request, string referenceName, Logger logger, SubmitType submitType, out HttpStatusCode httpStatusCode, bool retry = true, bool throwException = false, Order order = null)
{
XmlDocument result = null;
int retryCount = 0;
httpStatusCode = HttpStatusCode.OK;
var connector = GetConnector(settings, logger, submitType, order);
if (InstanceIdTooManyRequestsCollection.TryGetValue(settings.InstanceId, out DateTime lastErrorTime) &&
DateTime.Now.Subtract(lastErrorTime).TotalSeconds < TooManyRequestsTimeoutSeconds)
{
logger.Log(ErrorLevel.DebugInfo, $"Skip due to 429 error timeout: {referenceName} Request: '{request}'.");
return null;
}
// if ERP is down and in retry mode wait!
while (!connector.IsWebServiceConnectionAvailable())
{
if (!retry || retryCount >= MaxRetryCount)
{
return null;
}
++retryCount;
System.Threading.Thread.Sleep(RetryInterval);
}
var endpoint = connector.GetEndpoint();
string url = endpoint?.GetUrl();
if (!LicenseService.IsLicenseValid(url))
{
logger.Log(ErrorLevel.DebugInfo, $"Request send failed. License is not valid.");
Diagnostics.ExecutionTable.Current.Add($"DynamicwebLiveIntegration: Request send failed. License is not valid.");
return null;
}
Exception exception;
do
{
exception = null;
string erpXmlResponse = null;
try
{
NotificationManager.Notify(OnBeforeErpCommunication, new OnBeforeErpCommunicationArgs(request, referenceName, settings, logger));
logger.Log(ErrorLevel.DebugInfo, $"Request {referenceName} sent: '{request}'.");
Diagnostics.ExecutionTable.Current.Add($"DynamicwebLiveIntegration: Request {referenceName} sent: '{WebUtility.HtmlEncode(request)}'.");
erpXmlResponse = connector.Execute(endpoint, request);
if (!string.IsNullOrEmpty(erpXmlResponse))
{
logger.Log(ErrorLevel.DebugInfo, $"Response {referenceName} received: '{erpXmlResponse}'.");
Diagnostics.ExecutionTable.Current.Add($"DynamicwebLiveIntegration: Response {referenceName} received: '{WebUtility.HtmlEncode(erpXmlResponse)}'.");
if (!Helpers.ParseResponseToXml(erpXmlResponse, logger, out result))
{
result = null;
exception = new Exception($"Response is not valid XML: {erpXmlResponse}");
}
if (result != null && !LicenseService.ValidateLicense(url, result, logger))
{
logger.Log(ErrorLevel.DebugInfo, $"Response failed. License is not valid.");
Diagnostics.ExecutionTable.Current.Add($"DynamicwebLiveIntegration: Response failed. License is not valid.");
result = null;
retry = false;
}
}
else
{
logger.Log(ErrorLevel.ResponseError, $"Response {referenceName} returned null.");
Diagnostics.ExecutionTable.Current.Add($"DynamicwebLiveIntegration: Response {referenceName} returned null.");
}
}
catch (Exception ex)
{
exception = ex;
logger.Log(ErrorLevel.ConnectionError, $"An error occurred while calling {referenceName} from Web Service: '{ex.Message}'.");
Diagnostics.ExecutionTable.Current.Add($"DynamicwebLiveIntegration: An error occurred while calling {referenceName} from Web Service: '{ex.Message}'.");
HandleException(connector, settings, endpoint, ex, out httpStatusCode, ref retry);
NotificationManager.Notify(OnAfterErpException, new OnAfterErpExceptionArgs(request, erpXmlResponse, referenceName, ex, settings, logger));
}
finally
{
NotificationManager.Notify(OnAfterErpCommunication, new OnAfterErpCommunicationArgs(request, erpXmlResponse, result, referenceName, exception, settings, logger));
}
// no retry or xml reply from ERP then leave
if (!retry || result != null)
{
break;
}
// in retry mode, wait some time
++retryCount;
System.Threading.Thread.Sleep(RetryInterval);
}
while (retryCount < MaxRetryCount);
if ((throwException || EnableThrowExceptions) && exception != null)
{
throw exception;
}
return result;
}
private static void HandleException(ConnectorBase connector, Settings settings, EndpointInfo endpoint, Exception ex, out HttpStatusCode httpStatusCode, ref bool retry)
{
httpStatusCode = HttpStatusCode.OK;
bool skipError = false;
//Do not ping Endpoints with Internal Server Error 500 (that means that the request was received but generating response failed)
if (ex?.InnerException is not null)
{
if (ex.InnerException is WebException webException)
{
if (webException is not null && webException.Response is not null && webException.Response is HttpWebResponse httpWebResponse)
{
httpStatusCode = httpWebResponse.StatusCode;
if (httpWebResponse is not null && httpWebResponse.StatusCode == HttpStatusCode.InternalServerError)
{
skipError = true;
retry = false;
}
}
}
else if (ex.InnerException is HttpRequestException httpReqException && httpReqException?.StatusCode is not null)
{
httpStatusCode = (HttpStatusCode)httpReqException.StatusCode;
if (httpReqException.StatusCode == HttpStatusCode.InternalServerError)
{
skipError = true;
retry = false;
}
}
}
if (!skipError && ex is not null && ex is HttpRequestException httpRequestException && httpRequestException?.StatusCode is not null)
{
httpStatusCode = (HttpStatusCode)httpRequestException.StatusCode;
if (httpRequestException.StatusCode == HttpStatusCode.InternalServerError)
{
skipError = true;
retry = false;
}
}
//ConnectionError: An error occurred while calling GetProductsInfo from Web Service: 'The remote server returned an error: (429).'
if (!skipError && !string.IsNullOrEmpty(ex.Message) && ex.Message.Contains("(429)"))
{
skipError = true;
retry = false;
InstanceIdTooManyRequestsCollection.TryAdd(settings.InstanceId, DateTime.Now);
}
if (!skipError)
{
connector.Error(endpoint);
}
}
internal static string RetrievePDF(Settings settings, string requestString, SubmitType submitType)
{
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.Connector.RetrievePDF START");
string base64EncodedPDF;
var logger = new Logger(settings);
try
{
logger.Log(ErrorLevel.DebugInfo, string.Format("Request RetrievePDF sent: '{0}'.", requestString));
base64EncodedPDF = GetConnector(settings, logger, submitType).Execute(requestString);
logger.Log(ErrorLevel.DebugInfo, string.Format("Response RetrievePDF received: '{0}'.", base64EncodedPDF));
}
catch (Exception ex)
{
logger.Log(ErrorLevel.ResponseError, string.Format("Response RetrievePDF returned error: '{0}'.", ex.Message));
throw;
}
Diagnostics.ExecutionTable.Current.Add("DynamicwebLiveIntegration.Connector.RetrievePDF END");
return base64EncodedPDF;
}
}
}