/// <summary> /// <see cref="Sif.Framework.Service.Registration.IRegistrationService.Register(Sif.Framework.Model.Infrastructure.Environment)">Register</see> /// </summary> public void Register(ref Environment environment) { if (!Registered) { if (sessionService.HasSession(environment.ApplicationInfo.ApplicationKey, environment.SolutionId, environment.UserToken, environment.InstanceId)) { if (log.IsDebugEnabled) { log.Debug("Session token already exists for this object service (Consumer/Provider)."); } string storedSessionToken = sessionService.RetrieveSessionToken(environment.ApplicationInfo.ApplicationKey, environment.SolutionId, environment.UserToken, environment.InstanceId); AuthorisationToken = AuthenticationUtils.GenerateBasicAuthorisationToken(storedSessionToken, settings.SharedSecret); string storedEnvironmentUrl = sessionService.RetrieveEnvironmentUrl(environment.ApplicationInfo.ApplicationKey, environment.SolutionId, environment.UserToken, environment.InstanceId); string environmentXml = HttpUtils.GetRequest(storedEnvironmentUrl, AuthorisationToken); if (log.IsDebugEnabled) { log.Debug("Environment XML from GET request ..."); } if (log.IsDebugEnabled) { log.Debug(environmentXml); } environmentType environmentTypeToDeserialise = SerialiserFactory.GetXmlSerialiser <environmentType>().Deserialise(environmentXml); Environment environmentResponse = MapperFactory.CreateInstance <environmentType, Environment>(environmentTypeToDeserialise); sessionToken = environmentResponse.SessionToken; environmentUrl = environmentResponse.InfrastructureServices[InfrastructureServiceNames.environment].Value; if (log.IsDebugEnabled) { log.Debug("Environment URL is " + environmentUrl + "."); } if (!storedSessionToken.Equals(sessionToken) || !storedEnvironmentUrl.Equals(environmentUrl)) { AuthorisationToken = AuthenticationUtils.GenerateBasicAuthorisationToken(sessionToken, settings.SharedSecret); sessionService.RemoveSession(storedSessionToken); sessionService.StoreSession(environmentResponse.ApplicationInfo.ApplicationKey, sessionToken, environmentUrl, environmentResponse.SolutionId, environmentResponse.UserToken, environmentResponse.InstanceId); } environment = environmentResponse; } else { if (log.IsDebugEnabled) { log.Debug("Session token does not exist for this object service (Consumer/Provider)."); } string initialToken = AuthenticationUtils.GenerateBasicAuthorisationToken(environment.ApplicationInfo.ApplicationKey, settings.SharedSecret); environmentType environmentTypeToSerialise = MapperFactory.CreateInstance <Environment, environmentType>(environment); string body = SerialiserFactory.GetXmlSerialiser <environmentType>().Serialise(environmentTypeToSerialise); string environmentXml = HttpUtils.PostRequest(settings.EnvironmentUrl, initialToken, body); if (log.IsDebugEnabled) { log.Debug("Environment XML from POST request ..."); } if (log.IsDebugEnabled) { log.Debug(environmentXml); } try { environmentType environmentTypeToDeserialise = SerialiserFactory.GetXmlSerialiser <environmentType>().Deserialise(environmentXml); Environment environmentResponse = MapperFactory.CreateInstance <environmentType, Environment>(environmentTypeToDeserialise); sessionToken = environmentResponse.SessionToken; environmentUrl = environmentResponse.InfrastructureServices[InfrastructureServiceNames.environment].Value; if (log.IsDebugEnabled) { log.Debug("Environment URL is " + environmentUrl + "."); } AuthorisationToken = AuthenticationUtils.GenerateBasicAuthorisationToken(sessionToken, settings.SharedSecret); sessionService.StoreSession(environment.ApplicationInfo.ApplicationKey, sessionToken, environmentUrl, environmentResponse.SolutionId, environmentResponse.UserToken, environmentResponse.InstanceId); environment = environmentResponse; } catch (Exception) { if (environmentUrl != null) { HttpUtils.DeleteRequest(environmentUrl, AuthorisationToken); } else if (!String.IsNullOrWhiteSpace(TryParseEnvironmentUrl(environmentXml))) { HttpUtils.DeleteRequest(TryParseEnvironmentUrl(environmentXml), AuthorisationToken); } throw; } } Registered = true; } }