public override void OnActionExecuted(HttpActionExecutedContext filterContext)
        {
            try
            {
                MiniSessionManager.Instance.CommitChanges(filterContext.Exception);
                if (AllowPartialResponse)
                {
                    TryToCreatePartialResponse(filterContext);
                }
            }
            catch (Exception x)
            {
                filterContext.Exception = x;
            }
            HandleException(filterContext);
            var timer = (Stopwatch)filterContext.Request.Properties["logtimer"];

            timer.Stop();
            _elapsed = timer.Elapsed;
            if (!LogEnabled)
            {
                return;
            }
            IdentityHelper.LogAction(
                filterContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                filterContext.ActionContext.ActionDescriptor.ActionName,
                filterContext.Exception == null,
                filterContext.Exception?.Message);
            if (!(bool)filterContext.Request.Properties["requestIsLogged"])
            {
                APILogger?.LogExposedAPIAccess(_id, filterContext.ActionContext, _elapsed, false);
                filterContext.Request.Properties["requestIsLogged"] = true;
            }
        }
示例#2
0
        }// end OnActionExecutedAsync()

        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            log4net.LogManager.GetLogger(this.GetType()).Debug("start: OnActionExecuting");

            if (actionContext == null)
            {
                throw new ArgumentNullException("actionContext");
            }

            if (!IsCachingAllowed(actionContext, AnonymousOnly))
            {
                return;
            }

            var timer = (Stopwatch)actionContext.Request.Properties["logtimer"];

            if (timer == null)
            {
                timer = new Stopwatch();
                timer.Start();
            }

            var config = actionContext.Request.GetConfiguration();

            EnsureCacheTimeQuery();
            EnsureCache(config, actionContext.Request);

            var cacheKeyGenerator = config.CacheOutputConfiguration().GetCacheKeyGenerator(actionContext.Request, typeof(CustomCacheKeyGenerator));

            if (cacheKeyGenerator is CustomCacheKeyGenerator)
            {
                ((CustomCacheKeyGenerator)cacheKeyGenerator).CachePerUser = CachePerUser;
                ((CustomCacheKeyGenerator)cacheKeyGenerator).ApiName      = ApiName;
                ((CustomCacheKeyGenerator)cacheKeyGenerator).HeadersToInlcudeInCacheKey = HeadersToInlcudeInCacheKey;
            }

            var responseMediaType = GetExpectedMediaType(config, actionContext);

            actionContext.Request.Properties[CurrentRequestMediaType] = responseMediaType;
            var cachekey = cacheKeyGenerator.MakeCacheKey(actionContext, responseMediaType, ExcludeQueryStringFromCacheKey);

            if (!_webApiCache.Contains(cachekey))
            {
                return;
            }

            if (actionContext.Request.Headers.IfNoneMatch != null)
            {
                var etag = _webApiCache.Get <string>(cachekey + Constants.EtagKey);
                if (etag != null)
                {
                    if (actionContext.Request.Headers.IfNoneMatch.Any(x => x.Tag == etag))
                    {
                        var time          = CacheTimeQuery.Execute(DateTime.UtcNow);
                        var quickResponse = actionContext.Request.CreateResponse(HttpStatusCode.NotModified);
                        ApplyCacheHeaders(quickResponse, time);
                        actionContext.Response = quickResponse;

                        // Log Api Access
                        if (LogEnabled && !(bool)actionContext.Request.Properties["requestIsLogged"])
                        {
                            timer.Stop();
                            APILogger?.LogExposedAPIAccess(_id, actionContext, timer.Elapsed, true);
                            actionContext.Request.Properties["requestIsLogged"] = true;
                        }

                        return;
                    }
                }
            }

            var val = _webApiCache.Get <byte[]>(cachekey);

            if (val == null)
            {
                return;
            }

            var contenttype = _webApiCache.Get <MediaTypeHeaderValue>(cachekey + Constants.ContentTypeKey) ?? responseMediaType;

            actionContext.Response         = actionContext.Request.CreateResponse();
            actionContext.Response.Content = new ByteArrayContent(val);

            actionContext.Response.Content.Headers.ContentType = contenttype;

            var responseEtag = _webApiCache.Get <string>(cachekey + Constants.EtagKey);

            if (responseEtag != null)
            {
                SetEtag(actionContext.Response, responseEtag);
            }

            var cacheTime = CacheTimeQuery.Execute(DateTime.UtcNow);

            ApplyCacheHeaders(actionContext.Response, cacheTime);
            log4net.LogManager.GetLogger(this.GetType()).Debug("end: OnActionExecuting");

            // Log Api Access
            if (LogEnabled && !(bool)actionContext.Request.Properties["requestIsLogged"])
            {
                timer.Stop();
                APILogger?.LogExposedAPIAccess(_id, actionContext, timer.Elapsed, true);
                actionContext.Request.Properties["requestIsLogged"] = true;
            }
        }