/// <summary> /// Gets back a resource from a xml sample file. /// </summary> /// <param name="fileName">The XML filename, omit the extension</param> public static Resource GetXmlSample(string fileName) { var fhirSource = GetXml(fileName); var parser = new Hl7.Fhir.Serialization.FhirXmlParser(); return(parser.Parse <Resource>(fhirSource)); }
public List <AuDefinition> GetResourceList() { List <AuDefinition> AuDefinitionList = new List <AuDefinition>(); HttpClient downloader = new HttpClient(); Stream stream = null; stream = downloader.GetStreamAsync(definitionUrl).Result; using (ZipInputStream s = new ZipInputStream(stream)) { ZipEntry entry; while ((entry = s.GetNextEntry()) != null) { if (entry.Name.EndsWith(".xml", StringComparison.CurrentCultureIgnoreCase)) { //Console.WriteLine($"Modified: {entry.DateTime.ToShortDateString()} {entry.DateTime.ToShortTimeString()}"); if (entry.IsFile) { var buffer = new MemoryStream(); s.CopyTo(buffer); buffer.Seek(0, SeekOrigin.Begin); var sr = SerializationUtil.XmlReaderFromStream(buffer); Resource resource = new Hl7.Fhir.Serialization.FhirXmlParser().Parse <Resource>(sr); AuDefinition AuDef = new AuDefinition(); AuDef.FileName = entry.Name; AuDef.FhirId = resource.Id; AuDef.ResourceType = resource.ResourceType; AuDef.Resource = resource; AuDefinitionList.Add(AuDef); Console.WriteLine("Resource File Name : {0}", entry.Name); } } } // Close can be ommitted as the using statement will do it automatically // but leaving it here reminds you that is should be done. s.Close(); } return(AuDefinitionList); }
protected void ParseResponseBody() { var tpXml = this.Body; Patient Patient = null; if (this.Format == FhirApi.FhirFormat.Xml) { var parser = new Hl7.Fhir.Serialization.FhirXmlParser(); Patient = parser.Parse <Patient>(tpXml); } else { var parser = new Hl7.Fhir.Serialization.FhirJsonParser(); Patient = parser.Parse <Patient>(tpXml); } IElementNavigator PocoPatient = new PocoNavigator(Patient); this.ApiPatient = new ApiPatient(PocoPatient); }
public PatientFixture() { var parser = new Hl7.Fhir.Serialization.FhirXmlParser(); var tpXml = TestData.ReadTextFile("fp-test-patient.xml"); var patient = parser.Parse <Patient>(tpXml); TestInput = new PocoNavigator(patient); tpXml = TestData.ReadTextFile("questionnaire-example.xml"); var quest = parser.Parse <Questionnaire>(tpXml); Questionnaire = new PocoNavigator(quest); tpXml = TestData.ReadTextFile("uuid.profile.xml"); var uuid = parser.Parse <StructureDefinition>(tpXml); UuidProfile = new PocoNavigator(uuid); Xdoc = new XDocument(new XElement("group", new XAttribute("name", "CSharpTests"))); }
protected void ParseResponseBody() { var tpXml = this.Body; Bundle Bundle = null; if (this.Format == FhirApi.FhirFormat.Xml) { var parser = new Hl7.Fhir.Serialization.FhirXmlParser(); Bundle = parser.Parse <Bundle>(tpXml); } else { var parser = new Hl7.Fhir.Serialization.FhirJsonParser(); Bundle = parser.Parse <Bundle>(tpXml); } IElementNavigator PocoBundel = new PocoNavigator(Bundle); foreach (var RelatedPerson in PocoBundel.Select(@"Bundle.entry.select(resource as RelatedPerson)")) { ApiRelatedPerson ApiRelatedPerson = new ApiRelatedPerson(RelatedPerson); ApiRelatedPersonList.Add(ApiRelatedPerson); } }
private Resource DeSerializeFromXml(System.Xml.XmlReader XmlReader) { Hl7.Fhir.Serialization.FhirXmlParser FhirXmlParser = new Hl7.Fhir.Serialization.FhirXmlParser(); return(FhirXmlParser.Parse <Resource>(XmlReader)); }
// Process request and craft response. public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback callback) { if (request.Method == "OPTIONS") { // This is the CORS request, and that's good base.StatusCode = 200; // base.Headers.Add("Access-Control-Allow-Origin", "*"); base.Headers.Add("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS"); base.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, authorization"); callback.Continue(); return(CefReturnValue.Continue); } var uri = new Uri(request.Url); Console.WriteLine($"-----------------\r\n{request.Url}"); // Check the bearer header if (!string.IsNullOrEmpty(_launchContext.Bearer)) { string bearer = request.GetHeaderByName("authorization"); if (bearer != "Bearer " + _launchContext.Bearer) { base.StatusCode = (int)HttpStatusCode.Unauthorized; callback.Continue(); return(CefReturnValue.Continue); } } try { // This is a regular request Hl7.Fhir.Rest.FhirClient server = new Hl7.Fhir.Rest.FhirClient("http://localhost:4178"); server.OnAfterResponse += (sender, args) => { base.Charset = args.RawResponse.CharacterSet; foreach (string header in args.RawResponse.Headers.AllKeys) { base.Headers.Add(header, args.RawResponse.Headers[header]); } }; server.PreferredFormat = Hl7.Fhir.Rest.ResourceFormat.Json; string redirectedUrl = server.Endpoint.OriginalString.TrimEnd('/') + uri.PathAndQuery; System.Diagnostics.Trace.WriteLine($"{redirectedUrl}"); var headers = new List <KeyValuePair <string, IEnumerable <string> > >(); ModelBaseInputs <IServiceProvider> requestDetails = new ModelBaseInputs <IServiceProvider>(null, null, request.Method, uri, new Uri($"https://{AuthProtocolSchemeHandlerFactory.FhirFacadeAddress(_launchContext)}"), null, headers, null); if (request.Method == "GET") { if (uri.LocalPath == "/metadata") { _facade.GetConformance(requestDetails, Rest.SummaryType.False).ContinueWith <CapabilityStatement>(r => { if (r.Exception != null) { System.Diagnostics.Trace.WriteLine($"Error: {r.Exception.Message}"); if (r.Exception.InnerException is Hl7.Fhir.Rest.FhirOperationException fe) { base.StatusCode = (int)fe.Status; if (fe.Outcome != null) { base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(fe.Outcome)); base.MimeType = "application/fhir+json"; } callback.Continue(); System.Diagnostics.Trace.WriteLine($"Error (inner): {fe.Message}"); return(null); } } base.StatusCode = 200; // As per the documentation http://hl7.org/fhir/smart-app-launch/conformance/index.html CapabilityStatement cs = r.Result; // Update the security node with our internal security node if (cs.Rest[0].Security == null) { cs.Rest[0].Security = new Hl7.Fhir.Model.CapabilityStatement.SecurityComponent(); } Hl7.Fhir.Model.CapabilityStatement.SecurityComponent security = cs.Rest[0].Security; if (!security.Service.Any(cc => cc.Coding.Any(c => c.System == "http://hl7.org/fhir/restful-security-service" && c.Code == "SMART-on-FHIR"))) { security.Service.Add(new Hl7.Fhir.Model.CodeableConcept("http://hl7.org/fhir/restful-security-service", "SMART-on-FHIR")); } var extension = security.GetExtension("http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris"); if (extension == null) { extension = new Extension() { Url = "http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris" }; security.Extension.Add(extension); } // remove the existing authentications, and put in our own extension.Extension.Clear(); extension.AddExtension("token", new FhirUri($"https://{AuthProtocolSchemeHandlerFactory.AuthAddress(_launchContext)}/token")); extension.AddExtension("authorize", new FhirUri($"https://{AuthProtocolSchemeHandlerFactory.AuthAddress(_launchContext)}/authorize")); base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(r.Result)); Console.WriteLine($"Success: {base.Stream.Length}"); base.MimeType = "application/fhir+json"; callback.Continue(); return(r.Result); }); return(CefReturnValue.ContinueAsync); } if (!uri.LocalPath.StartsWith("/$") && !uri.LocalPath.StartsWith("/_") && uri.LocalPath.Length > 2) { // This is not an operation or history, so it must be a resource type string resourceType = uri.LocalPath.Substring(1); if (resourceType.Contains("/")) { resourceType = resourceType.Substring(0, resourceType.IndexOf("/")); } var rs = _facade.GetResourceService(requestDetails, resourceType); if (rs == null) { System.Diagnostics.Trace.WriteLine($"Error: resource type not handled {resourceType}"); base.StatusCode = (int)HttpStatusCode.BadRequest; // base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(fe.Outcome)); // base.MimeType = "application/fhir+json"; callback.Continue(); return(CefReturnValue.Continue); } // GET for a specific resource ResourceIdentity ri = new ResourceIdentity(uri); if (ri.IsRestResourceIdentity()) { rs.Get(ri.Id, ri.VersionId, SummaryType.False).ContinueWith(r => { if (r.Exception != null) { System.Diagnostics.Trace.WriteLine($"Error: {r.Exception.Message}"); if (r.Exception.InnerException is Hl7.Fhir.Rest.FhirOperationException fe) { base.StatusCode = (int)fe.Status; if (fe.Outcome != null) { base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(fe.Outcome)); base.MimeType = "application/fhir+json"; } callback.Continue(); System.Diagnostics.Trace.WriteLine($"Error (inner): {fe.Message}"); return(null); } } if (r.Result.HasAnnotation <HttpStatusCode>()) { base.StatusCode = (int)r.Result.Annotation <HttpStatusCode>(); } else { base.StatusCode = 200; } base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(r.Result)); Console.WriteLine($"Success: {base.Stream.Length}"); base.MimeType = "application/fhir+json"; callback.Continue(); return(r.Result); }); return(CefReturnValue.ContinueAsync); } // Search for the resource type var parameters = TupledParameters(uri, SearchQueryParameterNames); int?pagesize = GetIntParameter(uri, FhirParameter.COUNT); var summary = GetSummaryParameter(uri); rs.Search(parameters, pagesize, summary).ContinueWith(r => { if (r.Exception != null) { System.Diagnostics.Trace.WriteLine($"Error: {r.Exception.Message}"); if (r.Exception.InnerException is Hl7.Fhir.Rest.FhirOperationException fe) { base.StatusCode = (int)fe.Status; if (fe.Outcome != null) { base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(fe.Outcome)); base.MimeType = "application/fhir+json"; } callback.Continue(); System.Diagnostics.Trace.WriteLine($"Error (inner): {fe.Message}"); return(null); } } if (r.Result.HasAnnotation <HttpStatusCode>()) { base.StatusCode = (int)r.Result.Annotation <HttpStatusCode>(); } else { base.StatusCode = 200; } base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(r.Result)); Console.WriteLine($"Success: {base.Stream.Length}"); base.MimeType = "application/fhir+json"; callback.Continue(); return(r.Result); }); return(CefReturnValue.ContinueAsync); } System.Threading.Tasks.Task <Hl7.Fhir.Model.Resource> t = server.GetAsync(redirectedUrl).ContinueWith <Hl7.Fhir.Model.Resource>(r => { if (r.Exception != null) { System.Diagnostics.Trace.WriteLine($"Error: {r.Exception.Message}"); if (r.Exception.InnerException is Hl7.Fhir.Rest.FhirOperationException fe) { base.StatusCode = (int)fe.Status; if (fe.Outcome != null) { base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(fe.Outcome)); base.MimeType = "application/fhir+json"; } callback.Continue(); System.Diagnostics.Trace.WriteLine($"Error (inner): {fe.Message}"); return(null); } } base.StatusCode = 200; if (r.Result is Hl7.Fhir.Model.CapabilityStatement cs) { // As per the documentation http://hl7.org/fhir/smart-app-launch/conformance/index.html // Update the security node with our internal security node if (cs.Rest[0].Security == null) { cs.Rest[0].Security = new Hl7.Fhir.Model.CapabilityStatement.SecurityComponent(); } Hl7.Fhir.Model.CapabilityStatement.SecurityComponent security = cs.Rest[0].Security; if (!security.Service.Any(cc => cc.Coding.Any(c => c.System == "http://hl7.org/fhir/restful-security-service" && c.Code == "SMART-on-FHIR"))) { security.Service.Add(new Hl7.Fhir.Model.CodeableConcept("http://hl7.org/fhir/restful-security-service", "SMART-on-FHIR")); } var extension = security.GetExtension("http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris"); if (extension == null) { extension = new Extension() { Url = "http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris" }; security.Extension.Add(extension); } // remove the existing authentications, and put in our own extension.Extension.Clear(); extension.AddExtension("token", new FhirUri($"https://{AuthProtocolSchemeHandlerFactory.AuthAddress(_launchContext)}/token")); extension.AddExtension("authorize", new FhirUri($"https://{AuthProtocolSchemeHandlerFactory.AuthAddress(_launchContext)}/authorize")); } base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(r.Result)); Console.WriteLine($"Success: {base.Stream.Length}"); base.MimeType = "application/fhir+json"; callback.Continue(); return(r.Result); }); } if (request.Method == "POST") { if (uri.LocalPath == "/") { Bundle b = null; if (request.PostData != null) { var data = request.PostData.Elements.FirstOrDefault(); var body = data.GetBody(); if (request.GetHeaderByName("Content-Type").Contains("xml")) { b = new Hl7.Fhir.Serialization.FhirXmlParser().Parse <Bundle>(body); } else { b = new Hl7.Fhir.Serialization.FhirJsonParser().Parse <Bundle>(body); } } _facade.ProcessBatch(requestDetails, b).ContinueWith(r => { if (r.Exception != null) { System.Diagnostics.Trace.WriteLine($"Error: {r.Exception.Message}"); if (r.Exception.InnerException is Hl7.Fhir.Rest.FhirOperationException fe) { base.StatusCode = (int)fe.Status; if (fe.Outcome != null) { base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(fe.Outcome)); base.MimeType = "application/fhir+json"; } callback.Continue(); System.Diagnostics.Trace.WriteLine($"Error (inner): {fe.Message}"); return(null); } } if (r.Result.HasAnnotation <HttpStatusCode>()) { base.StatusCode = (int)r.Result.Annotation <HttpStatusCode>(); } else { base.StatusCode = 200; } base.Stream = new MemoryStream(new Hl7.Fhir.Serialization.FhirJsonSerializer(new Hl7.Fhir.Serialization.SerializerSettings() { Pretty = true }).SerializeToBytes(r.Result)); Console.WriteLine($"Success: {base.Stream.Length}"); base.MimeType = "application/fhir+json"; callback.Continue(); return(r.Result); }); return(CefReturnValue.ContinueAsync); } System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); client.DefaultRequestHeaders.Add("Accept", request.GetHeaderByName("Accept")); // client.DefaultRequestHeaders.Add("Content-Type", request.GetHeaderByName("Content-Type")); HttpContent content = null; if (request.PostData != null) { var data = request.PostData.Elements.FirstOrDefault(); var body = data.GetBody(); content = new System.Net.Http.StringContent(body, System.Text.Encoding.UTF8, request.GetHeaderByName("Content-Type")); } else { content = new System.Net.Http.StreamContent(null); } client.PostAsync(redirectedUrl, content).ContinueWith((System.Threading.Tasks.Task <HttpResponseMessage> r) => { if (r.Exception != null) { Console.WriteLine($"Error: {r.Exception.Message}"); //if (r.Exception.InnerException is Hl7.Fhir.Rest.FhirOperationException fe) //{ // base.StatusCode = (int)fe.Status; // if (fe.Outcome != null) // base.Stream = new MemoryStream(r.Result.Content.ReadAsStringAsync().GetAwaiter().GetResult()); // callback.Continue(); // System.Diagnostics.Trace.WriteLine($"Error (inner): {fe.Message}"); // return; //} } base.StatusCode = (int)r.Result.StatusCode; base.Stream = r.Result.Content.ReadAsStreamAsync().GetAwaiter().GetResult(); Console.WriteLine($"Success: {base.Stream.Length}"); base.MimeType = r.Result.Content.Headers.ContentType.MediaType; callback.Continue(); return; }); } if (request.Method == "PUT") { System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); client.DefaultRequestHeaders.Add("Accept", request.GetHeaderByName("Accept")); // client.DefaultRequestHeaders.Add("Content-Type", request.GetHeaderByName("Content-Type")); HttpContent content = null; if (request.PostData != null) { var data = request.PostData.Elements.FirstOrDefault(); var body = data.GetBody(); content = new System.Net.Http.StringContent(body, System.Text.Encoding.UTF8, request.GetHeaderByName("Content-Type")); } else { content = new System.Net.Http.StreamContent(null); } client.PutAsync(redirectedUrl, content).ContinueWith((System.Threading.Tasks.Task <HttpResponseMessage> r) => { if (r.Exception != null) { Console.WriteLine($"Error: {r.Exception.Message}"); //if (r.Exception.InnerException is Hl7.Fhir.Rest.FhirOperationException fe) //{ // base.StatusCode = (int)fe.Status; // if (fe.Outcome != null) // base.Stream = new MemoryStream(r.Result.Content.ReadAsStringAsync().GetAwaiter().GetResult()); // callback.Continue(); // System.Diagnostics.Trace.WriteLine($"Error (inner): {fe.Message}"); // return; //} } base.StatusCode = (int)r.Result.StatusCode; base.Stream = r.Result.Content.ReadAsStreamAsync().GetAwaiter().GetResult(); Console.WriteLine($"Success: {base.Stream.Length}"); base.MimeType = r.Result.Content.Headers.ContentType.MediaType; callback.Continue(); return; }); } return(CefReturnValue.ContinueAsync); } catch (Exception ex) { Console.WriteLine($"{ex.Message}"); callback.Dispose(); return(CefReturnValue.Cancel); } }
/// <summary> /// Process the specified excel file into a dataset file /// </summary> private static void Main(string[] args) { try { Console.WriteLine("SanteDB Vocabulary Importer v{0} ({1})", Assembly.GetEntryAssembly().GetName().Version, Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyInformationalVersionAttribute>().InformationalVersion); Console.WriteLine("Copyright (C) 2015-2022 See NOTICE for contributors"); var parms = new ParameterParser <ConsoleParameters>().Parse(args); Dataset retVal = new Dataset() { Id = parms.Name ?? "Imported Dataset", Action = new List <DataInstallAction>() }; // Process a FHIR if (parms.Fhir || parms.Csv) { if (!File.Exists(parms.SourceFile)) { throw new FileNotFoundException($"{parms.SourceFile} not found"); } if (parms.Fhir) { using (var fhirFileStream = File.OpenRead(parms.SourceFile)) { using (var xreader = XmlReader.Create(fhirFileStream)) { var fsz = new Hl7.Fhir.Serialization.FhirXmlParser(); var cs = fsz.Parse <Hl7.Fhir.Model.CodeSystem>(xreader); retVal.Id = $"Import FHIR Code System {cs.Id}"; var csId = Guid.NewGuid(); retVal.Action.Add(new DataUpdate() { InsertIfNotExists = true, Element = new CodeSystem() { Key = csId, Authority = CamelCase(cs.Name), Url = cs.Url, Oid = cs.Identifier.First(i => i.System == "urn:ietf:rfc:3986").Value, VersionText = cs.Version, Name = cs.Title } }); if (parms.CreateConcept) { var setId = Guid.NewGuid(); retVal.Action.Add(new DataUpdate() { InsertIfNotExists = true, Element = new ConceptSet() { Key = setId, Name = cs.Name, Mnemonic = CamelCase(cs.Name), Url = cs.Url, Oid = cs.Identifier.First(i => i.System == "urn:ietf:rfc:3986").Value } }); retVal.Action.AddRange(cs.Concept.SelectMany(o => ConvertToReferenceTerm(o, csId, setId, cs.Name))); } else { retVal.Action.AddRange(cs.Concept.SelectMany(o => ConvertToReferenceTerm(o, csId, null, null))); } } } } else if (parms.Csv) { // Open excel file stream using (var excelFileStream = File.OpenRead(parms.SourceFile)) { using (var importWkb = new XLWorkbook(excelFileStream, new LoadOptions() { RecalculateAllFormulas = false })) { retVal.Action = importWkb.Worksheets.SelectMany(o => o.Rows()).SelectMany(o => CreateReferenceTermInstruction(o, parms)).ToList(); } } } if (parms.CreateConcept) { retVal.Action.Add(new DataUpdate() { InsertIfNotExists = true, Element = new ConceptSet() { ConceptsXml = retVal.Action.Where(o => o.Element is Concept).Select(o => o.Element.Key.Value).ToList(), Mnemonic = parms.Prefix, Name = "A new Code System", Oid = "" } }); } if (parms.OutputFile == "-") { new XmlSerializer(typeof(Dataset)).Serialize(Console.Out, retVal); } else { using (var fs = File.Create(parms.OutputFile)) { new XmlSerializer(typeof(Dataset)).Serialize(fs, retVal); } } } else { new ParameterParser <ConsoleParameters>().WriteHelp(Console.Out); } } catch (Exception e) { Console.Error.WriteLine("Error processing file: {0}", e); } }