public void Setup() { TestApplicationContext.TestAssembly = typeof(TestRelatedPersonResourceHandler).Assembly; TestApplicationContext.Initialize(TestContext.CurrentContext.TestDirectory); this.m_serviceManager = ApplicationServiceContext.Current.GetService <IServiceManager>(); var testConfiguration = new FhirServiceConfigurationSection { Resources = new List <string> { "Patient" }, OperationHandlers = new List <TypeReferenceConfiguration>(), ExtensionHandlers = new List <TypeReferenceConfiguration>(), ProfileHandlers = new List <TypeReferenceConfiguration>(), MessageHandlers = new List <TypeReferenceConfiguration> { new TypeReferenceConfiguration(typeof(BirthPlaceExtension)) } }; using (AuthenticationContext.EnterSystemContext()) { FhirResourceHandlerUtil.Initialize(testConfiguration, this.m_serviceManager); ExtensionUtil.Initialize(testConfiguration); } }
/// <summary> /// Initialize based on configuration /// </summary> public static void Initialize(FhirServiceConfigurationSection m_configuration, IServiceManager serviceManager) { // Configuration if (m_configuration.Resources?.Any() == true) { foreach (var t in serviceManager.CreateInjectedOfAll <IFhirResourceHandler>()) { if (m_configuration.Resources.Any(r => r == t.ResourceType.ToString())) { RegisterResourceHandler(t); } else if (t is IDisposable disp) { disp.Dispose(); } } } else { // Old configuration foreach (var t in m_configuration.ResourceHandlers.Select(o => o.Type).Where(c => c != null)) { var rh = serviceManager.CreateInjected(t); if (rh is IFhirResourceHandler resourceHandler) { RegisterResourceHandler(resourceHandler); } } } }
public void Setup() { // Force load of the DLL var p = FbCharset.Ascii; TestApplicationContext.TestAssembly = typeof(TestRelatedPersonResourceHandler).Assembly; TestApplicationContext.Initialize(TestContext.CurrentContext.TestDirectory); this.m_serviceManager = ApplicationServiceContext.Current.GetService <IServiceManager>(); var testConfiguration = new FhirServiceConfigurationSection { Resources = new List <string> { "Encounter", "Bundle", "Patient", "Organization" }, OperationHandlers = new List <TypeReferenceConfiguration>(), ExtensionHandlers = new List <TypeReferenceConfiguration>(), ProfileHandlers = new List <TypeReferenceConfiguration>(), MessageHandlers = new List <TypeReferenceConfiguration> { new TypeReferenceConfiguration(typeof(EncounterResourceHandler)), new TypeReferenceConfiguration(typeof(BundleResourceHandler)), new TypeReferenceConfiguration(typeof(PatientResourceHandler)) } }; using (AuthenticationContext.EnterSystemContext()) { FhirResourceHandlerUtil.Initialize(testConfiguration, this.m_serviceManager); ExtensionUtil.Initialize(testConfiguration); } }
public void Setup() { TestApplicationContext.TestAssembly = typeof(TestOrganizationResourceHandler).Assembly; TestApplicationContext.Initialize(TestContext.CurrentContext.TestDirectory); m_serviceManager = ApplicationServiceContext.Current.GetService <IServiceManager>(); TestApplicationContext.Current.AddBusinessRule <Core.Model.Roles.Patient>(typeof(SamplePatientBusinessRulesService)); var testConfiguration = new FhirServiceConfigurationSection { Resources = new List <string> { "Patient" }, MessageHandlers = new List <TypeReferenceConfiguration> { new TypeReferenceConfiguration(typeof(PatientResourceHandler)) } }; using (AuthenticationContext.EnterSystemContext()) { FhirResourceHandlerUtil.Initialize(testConfiguration, m_serviceManager); ExtensionUtil.Initialize(testConfiguration); } }
public void Setup() { // Force load of the DLL var p = FbCharset.Ascii; TestApplicationContext.TestAssembly = typeof(TestOrganizationResourceHandler).Assembly; TestApplicationContext.Initialize(TestContext.CurrentContext.TestDirectory); m_serviceManager = ApplicationServiceContext.Current.GetService <IServiceManager>(); var testConfiguration = new FhirServiceConfigurationSection { Resources = new List <string> { "Immunization", "Patient", "Encounter", "Practitioner" }, MessageHandlers = new List <TypeReferenceConfiguration> { new TypeReferenceConfiguration(typeof(ImmunizationResourceHandler)) } }; using (AuthenticationContext.EnterSystemContext()) { FhirResourceHandlerUtil.Initialize(testConfiguration, m_serviceManager); ExtensionUtil.Initialize(testConfiguration); } }
public void Setup() { TestApplicationContext.TestAssembly = typeof(TestRelatedPersonResourceHandler).Assembly; TestApplicationContext.Initialize(TestContext.CurrentContext.TestDirectory); this.m_serviceManager = ApplicationServiceContext.Current.GetService <IServiceManager>(); var testConfiguration = new FhirServiceConfigurationSection { Resources = new List <string> { "Patient", "Practitioner", "Observation" } }; TestUtil.CreateAuthority("TEST", "1.2.3.4", "http://santedb.org/fhir/test", "TEST_HARNESS", this.AUTH); using (AuthenticationContext.EnterSystemContext()) { FhirResourceHandlerUtil.Initialize(testConfiguration, this.m_serviceManager); ExtensionUtil.Initialize(testConfiguration); //add practitioner to be used as performer var practitioner = TestUtil.GetFhirMessage("ObservationPerformer") as Practitioner; var practitionerResourceHandler = FhirResourceHandlerUtil.GetResourceHandler(ResourceType.Practitioner); // create the practitioner using the resource handler this.m_practitioner = (Practitioner)practitionerResourceHandler.Create(practitioner, TransactionMode.Commit); //add patient to be used subject var patient = TestUtil.GetFhirMessage("ObservationSubject") as Patient; var patientResourceHandler = FhirResourceHandlerUtil.GetResourceHandler(ResourceType.Patient); // create the patient using the resource handler this.m_patient = (Patient)patientResourceHandler.Create(patient, TransactionMode.Commit); //add a general observation to be used for multiple tests var observation = TestUtil.GetFhirMessage("SetupObservation") as Observation; observation.Subject = new ResourceReference($"urn:uuid:{this.m_patient.Id}"); observation.Performer.Add(new ResourceReference($"urn:uuid:{this.m_practitioner.Id}")); var observationResourceHandler = FhirResourceHandlerUtil.GetResourceHandler(ResourceType.Observation); this.m_observation = (Observation)observationResourceHandler.Create(observation, TransactionMode.Commit); } }
/// <summary> /// Creates a new dispatcher for the channel /// </summary> public Dispatcher(Guid channelKey, Uri endpoint, IDictionary <String, String> settings) { this.Key = channelKey; this.Endpoint = endpoint; this.Settings = settings; var configManager = ApplicationServiceContext.Current.GetService <IConfigurationManager>(); this.m_fhirConfiguration = configManager.GetSection <FhirServiceConfigurationSection>(); this.m_configuration = configManager.GetSection <FhirDispatcherConfigurationSection>()?.Targets.Find(o => o.Endpoint == endpoint.ToString()); settings.TryGetValue("Content-Type", out string contentType); // The client for this object this.m_client = new FhirClient(this.Endpoint, new FhirClientSettings() { ParserSettings = new Hl7.Fhir.Serialization.ParserSettings() { AllowUnrecognizedEnums = true, PermissiveParsing = true, AcceptUnknownMembers = true }, PreferredFormat = ContentType.GetResourceFormatFromFormatParam(contentType ?? "xml"), PreferCompressedResponses = true, VerifyFhirVersion = false, Timeout = 5000 }); foreach (var kv in this.Settings.Where(z => z.Key != "Content-Type" && !z.Key.StartsWith("$"))) { this.m_client.RequestHeaders.Add(kv.Key, kv.Value); } if (this.m_configuration?.Authenticator != null) { var authenticator = this.m_configuration.Authenticator.Type.CreateInjected() as IFhirClientAuthenticator; authenticator.AttachClient(this.m_client, this.m_configuration, settings); } }
/// <summary> /// Get the index /// </summary> public Stream Index() { this.ThrowIfNotReady(); try { RestOperationContext.Current.OutgoingResponse.ContentType = "text/html"; RestOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", "filename=\"index.html\""); RestOperationContext.Current.OutgoingResponse.SetLastModified(DateTime.UtcNow); FhirServiceConfigurationSection config = ApplicationServiceContext.Current.GetService <IConfigurationManager>().GetSection <FhirServiceConfigurationSection>(); if (!String.IsNullOrEmpty(config.LandingPage)) { using (var fs = File.OpenRead(config.LandingPage)) { MemoryStream ms = new MemoryStream(); int br = 1024; byte[] buffer = new byte[1024]; while (br == 1024) { br = fs.Read(buffer, 0, 1024); ms.Write(buffer, 0, br); } ms.Seek(0, SeekOrigin.Begin); return(ms); } } else { return(typeof(FhirServiceBehavior).Assembly.GetManifestResourceStream("SanteDB.Messaging.FHIR.index.htm")); } } catch (IOException) { throw new FileNotFoundException(); } }
public void Setup() { TestApplicationContext.TestAssembly = typeof(TestOrganizationResourceHandler).Assembly; TestApplicationContext.Initialize(TestContext.CurrentContext.TestDirectory); this.m_serviceManager = ApplicationServiceContext.Current.GetService <IServiceManager>(); var testConfiguration = new FhirServiceConfigurationSection { Resources = new List <string> { "Medication" }, MessageHandlers = new List <TypeReferenceConfiguration> { new TypeReferenceConfiguration(typeof(MedicationResourceHandler)) } }; using (AuthenticationContext.EnterSystemContext()) { FhirResourceHandlerUtil.Initialize(testConfiguration, m_serviceManager); ExtensionUtil.Initialize(testConfiguration); } }
public void Setup() { // Force load of the DLL var p = FbCharset.Ascii; TestApplicationContext.TestAssembly = typeof(TestRelatedPersonResourceHandler).Assembly; TestApplicationContext.Initialize(TestContext.CurrentContext.TestDirectory); this.m_serviceManager = ApplicationServiceContext.Current.GetService <IServiceManager>(); var testConfiguration = new FhirServiceConfigurationSection { Resources = new List <string> { "Practitioner" } }; using (AuthenticationContext.EnterSystemContext()) { FhirResourceHandlerUtil.Initialize(testConfiguration, this.m_serviceManager); ExtensionUtil.Initialize(testConfiguration); } }
/// <summary> /// Configure the service /// </summary> public void Configure(SanteDBConfiguration configuration, IDictionary <string, string> settings) { var restConfiguration = configuration.GetSection <SanteDB.Rest.Common.Configuration.RestConfigurationSection>(); if (restConfiguration == null) { throw new ConfigurationException("Error retrieving REST configuration", configuration); } var fhirRestConfiguration = restConfiguration.Services.FirstOrDefault(o => o.ServiceType == typeof(IFhirServiceContract)); if (fhirRestConfiguration == null) // add fhir rest config { fhirRestConfiguration = new RestServiceConfiguration() { Behaviors = new List <RestServiceBehaviorConfiguration>() { new RestServiceBehaviorConfiguration(typeof(TokenAuthorizationAccessBehavior)) }, Name = "FHIR", Endpoints = new List <RestEndpointConfiguration>() { this.CreateEndpoint("http://0.0.0.0:8080/fhir") } }; RestServiceConfiguration.Load(typeof(IFhirServiceContract).Assembly.GetManifestResourceStream("SanteDB.Messaging.FHIR.Configuration.Default.xml")); restConfiguration.Services.Add(fhirRestConfiguration); } var fhirConfiguration = configuration.GetSection <FhirServiceConfigurationSection>(); if (fhirConfiguration == null) { fhirConfiguration = new FhirServiceConfigurationSection() { ResourceBaseUri = "http://127.0.0.1:8080/fhir", ResourceHandlers = typeof(FhirDockerFeature).Assembly.ExportedTypes.Where(o => typeof(IFhirResourceHandler).IsAssignableFrom(o) && !o.IsAbstract && o.IsClass).Select(o => new TypeReferenceConfiguration(o)).ToList() }; configuration.AddSection(fhirConfiguration); } // Listen address if (settings.TryGetValue(ListenUriSetting, out string listen)) { if (!Uri.TryCreate(listen, UriKind.Absolute, out Uri listenUri)) { throw new ArgumentOutOfRangeException($"{listen} is not a valid URL"); } // Setup the endpoint fhirRestConfiguration.Endpoints.Clear(); fhirRestConfiguration.Endpoints.Add(new RestEndpointConfiguration() { Address = listen, ContractXml = typeof(IFhirServiceContract).AssemblyQualifiedName, Behaviors = new List <RestEndpointBehaviorConfiguration>() { new RestEndpointBehaviorConfiguration(typeof(MessageLoggingEndpointBehavior)), new RestEndpointBehaviorConfiguration(typeof(MessageCompressionEndpointBehavior)), new RestEndpointBehaviorConfiguration(typeof(AcceptLanguageEndpointBehavior)) } }); } // Authentication if (settings.TryGetValue(AuthenticationSetting, out string auth)) { if (!this.authSettings.TryGetValue(auth.ToUpperInvariant(), out Type authType)) { throw new ArgumentOutOfRangeException($"Don't understand auth option {auth} allowed values {String.Join(",", this.authSettings.Keys)}"); } // Add behavior if (authType != null) { fhirRestConfiguration.Behaviors.Add(new RestServiceBehaviorConfiguration() { Type = authType }); } else { fhirRestConfiguration.Behaviors.RemoveAll(o => this.authSettings.Values.Any(v => v == o.Type)); } } // Has the user set CORS? if (settings.TryGetValue(CorsSetting, out string cors)) { if (!Boolean.TryParse(cors, out bool enabled)) { throw new ArgumentOutOfRangeException($"{cors} is not a valid boolean value"); } // Cors is disabled? if (!enabled) { fhirRestConfiguration.Endpoints.ForEach(ep => ep.Behaviors.RemoveAll(o => o.Type == typeof(CorsEndpointBehavior))); } else { fhirRestConfiguration.Endpoints.ForEach(ep => ep.Behaviors.RemoveAll(o => o.Type == typeof(CorsEndpointBehavior))); fhirRestConfiguration.Endpoints.ForEach(ep => ep.Behaviors.Add(new RestEndpointBehaviorConfiguration() { Type = typeof(CorsEndpointBehavior) })); } } // Base URI if (settings.TryGetValue(BaseUriSetting, out string baseUri)) { fhirConfiguration.ResourceBaseUri = baseUri; } // Custom resource list? if (settings.TryGetValue(ResourceSetting, out string resource)) { fhirConfiguration.Resources = new List <string>(); foreach (var res in resource.Split(';')) { if (!Enum.TryParse <Hl7.Fhir.Model.ResourceType>(res, out Hl7.Fhir.Model.ResourceType rt)) { throw new ArgumentOutOfRangeException($"{res} is not a valid FHIR resource"); } // Add resource setting fhirConfiguration.Resources.Add(res); } } // Custom operation list? if (settings.TryGetValue(OperationSetting, out string operations)) { fhirConfiguration.Operations = new List <string>(); foreach (var res in operations.Split(';')) { fhirConfiguration.Operations.Add(res); } } // Custom profile list? if (settings.TryGetValue(ProfileSetting, out string profiles)) { fhirConfiguration.Profiles = new List <string>(); foreach (var res in profiles.Split(';')) { fhirConfiguration.Profiles.Add(res); } } // Custom settings if (settings.TryGetValue(ExtensionSetting, out string extensions)) { fhirConfiguration.Extensions = new List <string>(); foreach (var res in extensions.Split(';')) { fhirConfiguration.Extensions.Add(res); } } // Custom message list? if (settings.TryGetValue(MessageSetting, out string messages)) { fhirConfiguration.Messages = new List <string>(); foreach (var res in messages.Split(';')) { fhirConfiguration.Messages.Add(res); } } // Add services var serviceConfiguration = configuration.GetSection <ApplicationServiceContextConfigurationSection>().ServiceProviders; if (!serviceConfiguration.Any(s => s.Type == typeof(FhirMessageHandler))) { serviceConfiguration.Add(new TypeReferenceConfiguration(typeof(FhirMessageHandler))); serviceConfiguration.Add(new TypeReferenceConfiguration(typeof(FhirDataInitializationService))); } }
/// <summary> /// Creates a new instance of the FHIR message dispatch formatter /// </summary> public FhirMessageDispatchFormatter() { this.m_configuration = ApplicationServiceContext.Current.GetService <IConfigurationManager>().GetSection <FhirServiceConfigurationSection>(); }
/// <summary> /// Creates a profile utility /// </summary> public static void Initialize(FhirServiceConfigurationSection configuration) { var svcManager = ApplicationServiceContext.Current.GetService <IServiceManager>(); if (configuration.BehaviorModifiers?.Any() == true) { s_behaviorModifiers = configuration.BehaviorModifiers.Select(t => svcManager.CreateInjected(t.Type)) .OfType <IFhirRestBehaviorModifier>() .ToList(); } else { s_behaviorModifiers = svcManager .CreateInjectedOfAll <IFhirRestBehaviorModifier>() .ToList(); } if (configuration.ExtensionHandlers?.Any() == true) { s_extensionHandlers = configuration.ExtensionHandlers.Select(t => svcManager.CreateInjected(t.Type)) .OfType <IFhirExtensionHandler>() .ToList(); } else if (configuration.Extensions?.Any() == true) { s_extensionHandlers = svcManager.CreateInjectedOfAll <IFhirExtensionHandler>() .Where(o => configuration.Extensions.Contains(o.Uri.ToString())) .ToList(); } else { s_extensionHandlers = svcManager .CreateInjectedOfAll <IFhirExtensionHandler>() .ToList(); } if (configuration.OperationHandlers?.Any() == true) { s_operationHandlers = configuration.OperationHandlers.Select(t => svcManager.CreateInjected(t.Type)) .OfType <IFhirOperationHandler>() .Union(svcManager.CreateInjectedOfAll <IFhirOperationHandler>(typeof(ExtensionUtil).Assembly)) .ToList(); } else if (configuration.Operations?.Any() == true) { s_operationHandlers = svcManager .CreateInjectedOfAll <IFhirOperationHandler>() .Where(o => configuration.Operations.Contains(o.Uri.ToString()) || configuration.Operations.Contains(o.Name)) .ToList(); } else { s_operationHandlers = svcManager .CreateInjectedOfAll <IFhirOperationHandler>() .ToList(); } if (configuration.ProfileHandlers?.Any() == true) { s_profileHandlers = configuration.ProfileHandlers.Select(t => svcManager.CreateInjected(t.Type)) .OfType <IFhirProfileValidationHandler>() .ToList(); } else if (configuration.Profiles?.Any() == true) { s_profileHandlers = svcManager .CreateInjectedOfAll <IFhirProfileValidationHandler>() .Where(o => configuration.Profiles.Contains(o.ProfileUri.ToString())) .ToList(); } else { s_profileHandlers = svcManager .CreateInjectedOfAll <IFhirProfileValidationHandler>() .ToList(); } s_behaviorModifiers = svcManager .CreateInjectedOfAll <IFhirRestBehaviorModifier>() .ToList(); // Message operations if (configuration.MessageHandlers?.Any() == true) { s_messageOperations = configuration.MessageHandlers.Select(t => svcManager.CreateInjected(t.Type)) .OfType <IFhirMessageOperation>() .ToDictionary(o => o.EventUri, o => o); } else if (configuration.Messages?.Any() == true) { s_messageOperations = svcManager .CreateInjectedOfAll <IFhirMessageOperation>() .Where(o => configuration.Messages.Contains(o.EventUri.ToString())) .ToDictionary(o => o.EventUri, o => o); } else { s_messageOperations = svcManager .CreateInjectedOfAll <IFhirMessageOperation>() .ToDictionary(o => o.EventUri, o => o); } }
/// <summary> /// Constructor, load configuration /// </summary> public FhirMessageHandler(IServiceManager serviceManager) { this.m_serviceManager = serviceManager; this.m_configuration = ApplicationServiceContext.Current.GetService <IConfigurationManager>().GetSection <FhirServiceConfigurationSection>(); }