6
6
using Microsoft . AspNetCore . Authorization ;
7
7
using Microsoft . AspNetCore . Http ;
8
8
using Microsoft . AspNetCore . Mvc ;
9
+ using Microsoft . Extensions . Logging ;
9
10
using Microsoft . Extensions . Options ;
10
11
using VirtoCommerce . Platform . Core ;
11
12
using VirtoCommerce . Platform . Core . Settings ;
@@ -21,40 +22,67 @@ public class LicensingController : Controller
21
22
private readonly PlatformOptions _platformOptions ;
22
23
private readonly ISettingsManager _settingsManager ;
23
24
private readonly LicenseProvider _licenseProvider ;
24
-
25
- public LicensingController ( IOptions < PlatformOptions > platformOptions , ISettingsManager settingsManager , LicenseProvider licenseProvider )
25
+ private readonly ILogger < LicensingController > _logger ;
26
+ private readonly IHttpClientFactory _httpClientFactory ;
27
+
28
+ public LicensingController (
29
+ IOptions < PlatformOptions > platformOptions ,
30
+ ISettingsManager settingsManager ,
31
+ LicenseProvider licenseProvider ,
32
+ ILogger < LicensingController > logger ,
33
+ IHttpClientFactory httpClientFactory )
26
34
{
27
35
_platformOptions = platformOptions . Value ;
28
36
_settingsManager = settingsManager ;
29
37
_licenseProvider = licenseProvider ;
38
+ _logger = logger ;
39
+ _httpClientFactory = httpClientFactory ;
30
40
}
31
41
32
42
[ HttpPost ]
33
43
[ Route ( "activateByCode" ) ]
34
44
public async Task < ActionResult < License > > ActivateByCode ( [ FromBody ] string activationCode )
35
45
{
46
+ _logger . LogInformation ( "Activation by code started." ) ;
47
+
36
48
if ( ! Regex . IsMatch ( activationCode , "^([a-zA-Z_0-9-])+$" ) )
37
49
{
50
+ _logger . LogWarning ( "Invalid activation code" ) ;
38
51
return BadRequest ( new { Message = $ "Activation code \" { activationCode } \" is invalid" } ) ;
39
52
}
40
53
41
54
License license = null ;
42
55
43
- using ( var httpClient = new HttpClient ( ) )
56
+ var activationUrl = new Uri ( _platformOptions . LicenseActivationUrl + activationCode ) ;
57
+ try
44
58
{
45
- var activationUrl = new Uri ( _platformOptions . LicenseActivationUrl + activationCode ) ;
59
+ var httpClient = _httpClientFactory . CreateClient ( ) ;
60
+ httpClient . DefaultRequestHeaders . UserAgent . ParseAdd ( PlatformConstants . UserAgent ) ;
61
+ httpClient . DefaultRequestHeaders . Add ( "Accept" , "*/*" ) ;
62
+
63
+ _logger . LogInformation ( "Sending request to activation URL: {ActivationUrl}" , _platformOptions . LicenseActivationUrl ) ;
46
64
var httpResponse = await httpClient . GetAsync ( activationUrl ) ;
47
65
48
66
if ( httpResponse . IsSuccessStatusCode )
49
67
{
50
68
var rawLicense = await httpResponse . Content . ReadAsStringAsync ( ) ;
69
+ _logger . LogInformation ( "License content '{LicenseContent}' received successful." , rawLicense ) ;
51
70
license = License . Parse ( rawLicense , _platformOptions . LicensePublicKeyResourceName ) ;
71
+ _logger . LogInformation ( "License activation successful." ) ;
72
+ }
73
+ else
74
+ {
75
+ _logger . LogWarning ( "License activation failed using {ActivationUrl} by code with status code: {StatusCode}" , _platformOptions . LicenseActivationUrl , httpResponse . StatusCode ) ;
52
76
}
53
- }
54
77
55
- if ( license != null )
78
+ if ( license != null )
79
+ {
80
+ await DisableTrial ( ) ;
81
+ }
82
+ }
83
+ catch ( Exception ex )
56
84
{
57
- await DisableTrial ( ) ;
85
+ _logger . LogError ( ex , "Error occurred during license activation using {ActivationUrl} by code." , _platformOptions . LicenseActivationUrl ) ;
58
86
}
59
87
60
88
return Ok ( license ) ;
0 commit comments