Beispiel #1
0
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            base.OnActionExecuted(context);

            double executionMS = timeKeeper.Stop().TotalMilliseconds;

            logger.Info($"Execution require {executionMS} ms");

            Trace.Write(new TraceItem()
            {
                Millis = executionMS
            },
                        context.HttpContext);

            HandlerProcessor.Process(Settings.List("PostExecutionHandler"), context.HttpContext);
        }
Beispiel #2
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);

            Context.Current.Reset();

            string scheme      = HttpContext.Request.Scheme;
            string host        = HttpContext.Request.Host.Value;
            string path        = HttpContext.Request.Path;
            string queryString = HttpContext.Request.QueryString.HasValue ? HttpContext.Request.QueryString.Value : "";

            string url = $"{scheme}://{host}{path}{queryString}";

            logger.Info($"Request {url} with session id {Session.Id}");

            HandlerProcessor.Process(Settings.List("PreExecutionHandler"), context.HttpContext);
        }
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);

            Context.Current.Reset();

            string remoteIp = HttpContext.Connection.RemoteIpAddress.ToString();

            if (!CanAccess(remoteIp))
            {
                throw new SecurityException($"Forbidden Request from Remote IP address: {remoteIp}");
            }

            string scheme      = HttpContext.Request.Scheme;
            string host        = HttpContext.Request.Host.Value;
            string path        = HttpContext.Request.Path;
            string queryString = HttpContext.Request.QueryString.HasValue ? HttpContext.Request.QueryString.Value : "";

            string url = $"{scheme}://{host}{path}{queryString}";

            logger.Info($"Request {url} with session id {Session.Id}");

            HandlerProcessor.Process(Settings.List("PreExecutionHandler"), context.HttpContext);
        }
Beispiel #4
0
        public async Task Invoke(HttpContext context)
        {
            TimeKeeper timeKeeper = new TimeKeeper();

            string contentType = null;
            string content     = null;
            int    statusCode  = 200;

            try
            {
                LoadUser(context);

                string resourceName = context.Request.Path.Value;
                string body         = new StreamReader(context.Request.Body).ReadToEndAsync().Result;

                logger.Info($"User: {Context.Current.User?.Username}");
                logger.Info($"Resource: {resourceName}");
                logger.Debug($"Body: {body}");
                logger.Info($"Session ID: {context.Session.Id}");
                logger.Info($"HTTP method: {context.Request.Method}");

                ApiInputHandler input = new ApiInputHandler()
                {
                    Body     = body,
                    Context  = context,
                    Resource = resourceName
                };

                if (context.Request.Method == "OPTIONS")
                {
                    //TODO improve this poor code :D
                    contentType = ContentType.TextPlain;
                    content     = ":)";
                }
                else
                {
                    HandlerProcessor.Process(Settings.List("PreExecutionHandler"), context);

                    ApiHandlerOutput output = Process(input);

                    HandlerProcessor.Process(Settings.List("PostExecutionHandler"), context);

                    if (output.Type == ApiHandlerOutputType.JSon)
                    {
                        contentType = ContentType.ApplicationJson;
                        content     = JsonHelper.Serialize(output.Output);
                    }
                    else if (output.Type == ApiHandlerOutputType.TextAsJson)
                    {
                        contentType = ContentType.ApplicationJson;
                        content     = output.Output as string;
                    }
                    else if (output.Type == ApiHandlerOutputType.Text)
                    {
                        contentType = ContentType.TextPlain;
                        content     = output.Output as string;
                    }
                    else
                    {
                        throw new Exception(Messages.WrongApiOutput);
                    }
                }
            }

            #region catches

            catch (ApiInvalidArgumentException e)
            {
                logger.Error(e);

                statusCode  = 501;
                contentType = ContentType.ApplicationJson;
                string message = propagateApplicationErrorInFault ? e.Message : Messages.GenericFailure;
                content = JsonHelper.Serialize(new { Message = message });

                Trace.Write(new TraceItem()
                {
                    Error = e.Message, Millis = timeKeeper.Stop().TotalMilliseconds
                }, context);
            }
            catch (ApiNotFoundException e)
            {
                logger.Error(e);

                statusCode  = 404;
                contentType = ContentType.ApplicationJson;
                string message = propagateApplicationErrorInFault ? e.Message : Messages.GenericFailure;
                content = JsonHelper.Serialize(new { Message = message });

                Trace.Write(new TraceItem()
                {
                    Error = e.Message, Millis = timeKeeper.Stop().TotalMilliseconds
                }, context);
            }
            catch (ApiTokenExpiredException e)
            {
                logger.Error(e);

                statusCode  = 401;
                contentType = ContentType.ApplicationJson;
                string message = propagateApplicationErrorInFault ? e.Message : Messages.GenericFailure;
                content = JsonHelper.Serialize(new { Message = message });

                Trace.Write(new TraceItem()
                {
                    Error = e.Message, Millis = timeKeeper.Stop().TotalMilliseconds
                }, context);
            }
            catch (ApiAccessDeniedException e)
            {
                logger.Error(e);

                statusCode  = 401;
                contentType = ContentType.ApplicationJson;
                string message = propagateApplicationErrorInFault ? e.Message : Messages.GenericFailure;
                content = JsonHelper.Serialize(new { Message = message });

                Trace.Write(new TraceItem()
                {
                    Error = e.Message, Millis = timeKeeper.Stop().TotalMilliseconds
                }, context);
            }
            catch (MissingRequiredProperty e)
            {
                logger.Error(e);

                statusCode  = 400;
                contentType = ContentType.ApplicationJson;
                string message = propagateApplicationErrorInFault ? e.Message : Messages.GenericFailure;
                content = JsonHelper.Serialize(new { Message = message });

                Trace.Write(new TraceItem()
                {
                    Error = e.Message, Millis = timeKeeper.Stop().TotalMilliseconds
                }, context);
            }
            catch (CustomJsonException e)
            {
                logger.Error(e);

                statusCode  = 502; // TODO check this code
                contentType = ContentType.ApplicationJson;
                string message = propagateApplicationErrorInFault ? e.Message : Messages.GenericFailure;
                content = JsonHelper.Serialize(new { Message = message });

                Trace.Write(new TraceItem()
                {
                    Error = e.Message, Millis = timeKeeper.Stop().TotalMilliseconds
                }, context);
            }
            catch (SecurityException e)
            {
                logger.Error(e);

                statusCode  = 403; // TODO check this code
                contentType = ContentType.ApplicationJson;
                string message = propagateApplicationErrorInFault ? e.Message : Messages.GenericFailure;
                content = JsonHelper.Serialize(new { Message = message });

                Trace.Write(new TraceItem()
                {
                    Error = e.Message, Millis = timeKeeper.Stop().TotalMilliseconds
                }, context);
            }
            catch (Exception e)
            {
                logger.Error(e);

                statusCode  = 500;
                contentType = ContentType.ApplicationJson;
                string message = propagateApplicationErrorInFault ? e.Message : Messages.GenericFailure;
                content = JsonHelper.Serialize(new { Message = message });

                Trace.Write(new TraceItem()
                {
                    Error = e.Message, Millis = timeKeeper.Stop().TotalMilliseconds
                }, context);
            }

            #endregion

            finally
            {
                logger.Debug($"ContentType: {contentType}");
                logger.Debug($"Content: {content}");
                logger.Info($"statusCode: {statusCode}");

                context.Response.ContentType = contentType;
                context.Response.StatusCode  = statusCode;

                await context.Response.WriteAsync(content);

                double ms = timeKeeper.Stop().TotalMilliseconds;

                Trace.Write(new TraceItem()
                {
                    Millis = ms
                }, context);

                logger.Info($"Request completed in {ms} ms");
            }
        }
Beispiel #5
0
 public double GetBrokenLineLength(BrokenLinePoint[] brokenLine)
 {
     return(HandlerProcessor.Process <GetBrokenLineLengthHandler, double>(h => h.Handle(brokenLine)));
 }