Esempio n. 1
0
        public void RegisterEventOnRedirect(ProcessInboundRulesResult results)
        {
            if (!Tracker.Enabled)
                return;

            if (!Tracker.IsActive)
                Tracker.StartTracking();

            try
            {

                foreach (var result in results.ProcessedResults.Where(e => e.RuleMatched))
                {
                    var itemId = result.ItemId;

                    var redirectItem = Sitecore.Context.Database.GetItem(new ID(itemId));

                    if (redirectItem != null)
                    {
                        var pageEventModel = new Sitecore.Analytics.Model.PageEventData()
                        {
                            PageEventDefinitionId = RedirectEventItemId,
                            ItemId = itemId,
                            Name = "UrlRewrite Redirect",
                            DateTime = DateTime.UtcNow,
                            Text = string.Format("Redirected from {0} to {1} using {2} [{3}].", result.OriginalUri, result.RewrittenUri, redirectItem.Name, itemId)
                        };

                        var pageEventData = new Sitecore.Analytics.Data.PageEventData(pageEventModel);

                        Tracker.Current.CurrentPage.Item = new Sitecore.Analytics.Model.ItemData
                        {
                            Id = itemId,
                            Language = redirectItem.Language.Name,
                            Version = redirectItem.Version.Number
                        };

                        Tracker.Current.CurrentPage.Register(pageEventData);
                        Tracker.Current.Interaction.AcceptModifications();
                    }
                }

            }
            catch (Exception ex)
            {
                Log.Error(this, ex, "Exception occurred during tracking.");
            }
        }
Esempio n. 2
0
        public ProcessInboundRulesResult ProcessRequestUrl(Uri requestUri, List<InboundRule> inboundRules)
        {
            if (inboundRules == null)
            {
                throw new ArgumentNullException("inboundRules");
            }

            var originalUri = requestUri;

            Log.Debug(this, "Processing url: {0}", originalUri);

            var ruleResult = new InboundRuleResult
            {
                RewrittenUri = originalUri
            };

            var processedResults = new List<InboundRuleResult>();

            foreach (var inboundRule in inboundRules)
            {
                ruleResult = ProcessInboundRule(ruleResult.RewrittenUri, inboundRule);
                processedResults.Add(ruleResult);

                if (!ruleResult.RuleMatched)
                    continue;

                if (ruleResult.RuleMatched && ruleResult.StopProcessing)
                {
                    break;
                }
            }

            // TODO: log more information about what actually hapenned - this currently only reflects rewrites/redirects
            Log.Debug(this, "Processed originalUrl: {0} redirectedUrl: {1}", originalUri, ruleResult.RewrittenUri);

            var finalResult = new ProcessInboundRulesResult(originalUri, processedResults);

            return finalResult;
        }
Esempio n. 3
0
 public static void TrackRedirect(ProcessInboundRulesResult results)
 {
     tracking.RegisterEventOnRedirect(results);
 }
        public void ExecuteResult(HttpContextBase httpContext, ProcessInboundRulesResult ruleResult)
        {
            var httpRequest = httpContext.Request;
            var httpResponse = httpContext.Response;

            var responseHeaders = ruleResult.ProcessedResults.SelectMany(e => e.ResponseHeaders);
            var replacements = new RewriteHelper.Replacements
            {
                RequestHeaders = RequestHeaders,
                RequestServerVariables = RequestServerVariables
            };

            httpResponse.Clear();

            foreach (var responseHeader in responseHeaders)
            {
                var responseHeaderValue = RewriteHelper.ReplaceTokens(replacements, responseHeader.Value);
                httpResponse.Headers.Set(responseHeader.VariableName, responseHeaderValue);
            }

            if (ruleResult.FinalAction is IBaseRedirect)
            {
                var redirectAction = ruleResult.FinalAction as IBaseRedirect;
                int statusCode;

                if (redirectAction.StatusCode.HasValue)
                {
                    statusCode = (int)(redirectAction.StatusCode.Value);
                }
                else
                {
                    statusCode = (int)HttpStatusCode.MovedPermanently;
                }

                httpResponse.RedirectLocation = ruleResult.RewrittenUri.ToString();
                httpResponse.StatusCode = statusCode;

                if (redirectAction.HttpCacheability.HasValue)
                {
                    httpResponse.Cache.SetCacheability(redirectAction.HttpCacheability.Value);
                }

                //ReportingService.QueueReport(ruleResult.ProcessedResults.Where(e => e.RuleMatched), Sitecore.Context.Database);
            }
            else if (ruleResult.FinalAction is IBaseRewrite)
            {

                var rewrittenUrl = ruleResult.RewrittenUri;

                var isLocal = String.Equals(httpRequest.Url.Host, rewrittenUrl.Host,
                            StringComparison.OrdinalIgnoreCase);

                if (!isLocal)
                {
                    throw new ApplicationException("Rewrite Url must be a local URL");
                }

                httpContext.Server.TransferRequest(rewrittenUrl.PathAndQuery, true, httpRequest.HttpMethod, RequestHeaders, true);
            }
            else if (ruleResult.FinalAction is AbortRequest)
            {
                // do nothing
            }
            else if (ruleResult.FinalAction is CustomResponse)
            {
                var customResponse = ruleResult.FinalAction as CustomResponse;

                httpResponse.TrySkipIisCustomErrors = true;

                httpResponse.StatusCode = customResponse.StatusCode;
                httpResponse.StatusDescription = customResponse.ErrorDescription;

                // TODO: Implement Status Reason?
                //httpResponse.??? = customResponse.Reason;

                if (customResponse.SubStatusCode.HasValue)
                {
                    httpResponse.SubStatusCode = customResponse.SubStatusCode.Value;
                }

            }

            httpResponse.End();
        }