Ejemplo n.º 1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "patch", "delete", Route = "fhirproxy/{res?}/{id?}/{hist?}/{vid?}")] HttpRequest req,
            ILogger log, ClaimsPrincipal principal, string res, string id, string hist, string vid)
        {
            if (!Utils.isServerAccessAuthorized(req))
            {
                return(new ContentResult()
                {
                    Content = Utils.genOOErrResponse("auth-access", req.Headers[Utils.AUTH_STATUS_MSG_HEADER].First()), StatusCode = (int)System.Net.HttpStatusCode.Unauthorized, ContentType = "application/json"
                });
            }

            //Load Request Body
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            //Call Configured Pre-Processor Modules
            ProxyProcessResult prerslt = ProxyProcessManager.RunPreProcessors(requestBody, req, log, principal, res, id, hist, vid);

            if (!prerslt.Continue)
            {
                //Pre-Processor didn't like something or exception was called so return
                FHIRResponse preresp = prerslt.Response;
                if (preresp == null)
                {
                    string       errmsg = (string.IsNullOrEmpty(prerslt.ErrorMsg) ? "No message" : prerslt.ErrorMsg);
                    FHIRResponse fer    = new FHIRResponse();
                    fer.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                    fer.Content    = Utils.genOOErrResponse("internalerror", $"A Proxy Pre-Processor halted execution for an unknown reason. Check logs. Message is {errmsg}");
                    return(generateJSONResult(fer));
                }
                return(generateJSONResult(preresp));
            }

            log.LogInformation("Calling FHIR Server...");

            //Proxy the call to the FHIR Server
            FHIRResponse serverresponse = FHIRClientFactory.callFHIRServer(prerslt.Request, req, log, res, id, hist, vid);

            //Call Configured Post-Processor Modules
            ProxyProcessResult postrslt = ProxyProcessManager.RunPostProcessors(serverresponse, req, log, principal, res, id, hist, vid);


            if (postrslt.Response == null)
            {
                string errmsg = (string.IsNullOrEmpty(postrslt.ErrorMsg) ? "No message" : postrslt.ErrorMsg);
                postrslt.Response            = new FHIRResponse();
                postrslt.Response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                postrslt.Response.Content    = Utils.genOOErrResponse("internalerror", $"A Proxy Post-Processor halted execution for an unknown reason. Check logs. Message is {errmsg}");
            }
            //Reverse Proxy Response
            postrslt.Response = Utils.reverseProxyResponse(postrslt.Response, req, res);
            //return ActionResult
            if (postrslt.Response.StatusCode == HttpStatusCode.NoContent)
            {
                return(null);
            }
            return(generateJSONResult(postrslt.Response));
        }
        public static async Task <ProxyProcessResult> RunPostProcessors(FHIRResponse response, HttpRequest req, ILogger log, ClaimsPrincipal principal, string res, string id, string hist, string vid)
        {
            ProxyProcessResult rslt = new ProxyProcessResult();

            //Default to server response
            rslt.Response = response;
            //Get Configured PostProcessors
            string pps = System.Environment.GetEnvironmentVariable("POST_PROCESSOR_TYPES");

            if (string.IsNullOrEmpty(pps))
            {
                return(rslt);
            }
            string[] types = pps.Split(",");
            foreach (string cls in types)
            {
                try
                {
                    string         ic = cls;
                    DateTimeOffset os = DateTimeOffset.Now;
                    if (cls.Contains(":"))
                    {
                        string[] x = cls.Split(":");
                        ic = x[0];
                        int exp = DEF_EXP_MINS;
                        int.TryParse(x[1], out exp);
                        os = os.AddMinutes(exp);
                    }
                    else
                    {
                        os = os.AddMinutes(DEF_EXP_MINS);
                    }
                    IProxyPostProcess ip = (IProxyPostProcess)cache.GetOrAdd(cls, () => GetInstance(ic), os);
                    log.LogInformation($"ProxyProcessManager is running {cls} post-process...");
                    rslt = await ip.Process(rslt.Response, req, log, principal, res, id, hist, vid);

                    if (!rslt.Continue)
                    {
                        return(rslt);
                    }
                }
                catch (InvalidCastException ece)
                {
                    log.LogWarning($"{cls} does not seem to implement IProxyPostProcess and will not be executed:{ece.Message}");
                }
                catch (Exception e)
                {
                    log.LogError(e, $"Error trying to instanciate/execute post-process {cls}: {e.Message}");
                    FHIRResponse fhirresp = new FHIRResponse();
                    fhirresp.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                    fhirresp.Content    = Utils.genOOErrResponse("internalerror", $"Error trying to instanciate/execute post-process {cls}: {e.Message}");
                    return(new ProxyProcessResult(false, "internalerror", "", fhirresp));
                }
            }
            return(rslt);
        }