ImportExportCompatibilityCheck
Represents a class object to perform compatibility checks between the client API and Relativity.
using Relativity.DataExchange.Logger;
using Relativity.DataExchange.Resources;
using Relativity.Logging;
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Relativity.DataExchange
{
internal class ImportExportCompatibilityCheck : IImportExportCompatibilityCheck
{
private readonly RelativityInstanceInfo instanceInfo;
private readonly ILog log;
private readonly IApplicationVersionService applicationVersionService;
private readonly Version minRelativityVersion;
private readonly Version requiredWebApiVersion;
private readonly Version webApiStartFromRelativityVersion;
private readonly IAppSettings appSettings;
private readonly IAppSettingsInternal appSettingsInternal;
private readonly IRunningContext runningContext;
public ImportExportCompatibilityCheck(RelativityInstanceInfo instanceInfo, IApplicationVersionService applicationVersionService, IRunningContext runningContext, ILog log)
: this(instanceInfo, applicationVersionService, log, VersionConstants.MinRelativityVersion, VersionConstants.RequiredWebApiVersion, VersionConstants.WebApiStartFromRelativityVersion, runningContext, AppSettings.Instance, AppSettings.Instance as IAppSettingsInternal)
{
}
public ImportExportCompatibilityCheck(RelativityInstanceInfo instanceInfo, IApplicationVersionService applicationVersionService, ILog log, Version minRelativityVersion, Version requiredWebApiVersion, Version webApiStartFromRelativityVersion, IRunningContext runningContext, IAppSettings appSettings, IAppSettingsInternal appSettingsInternal)
{
this.webApiStartFromRelativityVersion = webApiStartFromRelativityVersion;
this.instanceInfo = instanceInfo.ThrowIfNull("instanceInfo");
this.log = log.ThrowIfNull<ILog>("log");
this.applicationVersionService = applicationVersionService.ThrowIfNull("applicationVersionService");
this.minRelativityVersion = minRelativityVersion.ThrowIfNull("minRelativityVersion");
this.requiredWebApiVersion = requiredWebApiVersion.ThrowIfNull("requiredWebApiVersion");
this.runningContext = runningContext;
this.appSettings = appSettings.ThrowIfNull("appSettings");
this.appSettingsInternal = appSettingsInternal.ThrowIfNull("appSettingsInternal");
}
[AsyncStateMachine(typeof(<ValidateAsync>d__11))]
public Task ValidateAsync(CancellationToken token)
{
<ValidateAsync>d__11 stateMachine = default(<ValidateAsync>d__11);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.token = token;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<PerformValidationAsync>d__12))]
private Task PerformValidationAsync(CancellationToken token, Version relativityVersion)
{
<PerformValidationAsync>d__12 stateMachine = default(<PerformValidationAsync>d__12);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.token = token;
stateMachine.relativityVersion = relativityVersion;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<ValidateWebApiVersion>d__13))]
private Task ValidateWebApiVersion(CancellationToken token, Version relativityVersion)
{
<ValidateWebApiVersion>d__13 stateMachine = default(<ValidateWebApiVersion>d__13);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.token = token;
stateMachine.relativityVersion = relativityVersion;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
private void VerifyRelativityVersionFormat(Version relativityVersion)
{
if (relativityVersion.Major <= 0) {
log.LogError("The Relativity version {RelativityVersion} is invalid and import/export service cannot be used for Relativity instance {RelativityHost}.", new object[2] {
relativityVersion,
instanceInfo.Host.Secure()
});
string message = Strings.RelativtyMinVersionInvalidNoAppNameExceptionMessage;
if (!string.IsNullOrEmpty(appSettings.ApplicationName))
message = string.Format(CultureInfo.CurrentCulture, Strings.RelativtyMinVersionInvalidExceptionMessage, appSettings.ApplicationName);
if (ShouldThrowException(message))
throw new RelativityNotSupportedException(message, relativityVersion);
}
}
private bool ShouldThrowException(string message)
{
bool enforceVersionCompatibilityCheck = appSettingsInternal.EnforceVersionCompatibilityCheck;
if (!enforceVersionCompatibilityCheck)
log.LogWarning("The version compatibility check failed but the application settings are configured to disable enforcement. Message: {VersionCompatibilityMessage}", new object[1] {
message
});
return enforceVersionCompatibilityCheck;
}
private void VerifyImportExportWebApiVersion(Version relativityVersion, Version importExportWebApiVersion)
{
if (importExportWebApiVersion.Major <= 0) {
log.LogError("The import/export WebAPI version {ImportExportWebApiVersion} is invalid and import/export service cannot be used.", new object[1] {
importExportWebApiVersion
});
string message = Strings.ImportExportWebApiVersionInvalidNoAppNameExceptionMessage;
if (!string.IsNullOrEmpty(appSettings.ApplicationName))
message = string.Format(CultureInfo.CurrentCulture, Strings.ImportExportWebApiVersionInvalidExceptionMessage, appSettings.ApplicationName);
if (ShouldThrowException(message))
throw new RelativityNotSupportedException(message, relativityVersion);
}
if (requiredWebApiVersion.Major != importExportWebApiVersion.Major) {
log.LogError("The import/export WebAPI version {ImportExportWebApiVersion} isn't compatible with the required client API version {RequiredWebApiVersion}.", new object[2] {
importExportWebApiVersion,
requiredWebApiVersion
});
string message2 = Strings.ImportExportWebApiVersionNotSupportedNoAppNameExceptionMessage;
if (!string.IsNullOrEmpty(appSettings.ApplicationName))
message2 = string.Format(CultureInfo.CurrentCulture, Strings.ImportExportWebApiVersionNotSupportedExceptionMessage, appSettings.ApplicationName);
if (ShouldThrowException(message2))
throw new RelativityNotSupportedException(message2, relativityVersion);
}
}
private void ValidateRelativityVersion(Version relativityVersion)
{
if (relativityVersion < minRelativityVersion) {
string message = string.Format(CultureInfo.CurrentCulture, Strings.RelativtyMinVersionNoAppNameExceptionMessage, relativityVersion, minRelativityVersion);
if (!string.IsNullOrEmpty(appSettings.ApplicationName))
message = string.Format(CultureInfo.CurrentCulture, Strings.RelativtyMinVersionExceptionMessage, appSettings.ApplicationName, relativityVersion, minRelativityVersion);
if (ShouldThrowException(message))
throw new RelativityNotSupportedException(message, relativityVersion);
}
}
}
}