Example #1
0
        public void Intercept(IInvocation invocation)
        {
            // Calls the decorated instance.
            invocation.Proceed();


            if (invocation.GetConcreteMethod().Name ==
                NameOfHelper.MethodName <IUrlTrackingService>(x => x.TrackAsync(null)))
            {
                // TrackAsync called
                var trackTask = (Task <UrlTrackingResult>)invocation.ReturnValue;

                // Filtering bots
                if (HttpContext.Current != null)
                {
                    string userAgent = HttpContext.Current.Request.UserAgent;
                    if (!string.IsNullOrEmpty(userAgent))
                    {
                        if (_bots.Any(bot => userAgent.IndexOf(bot, StringComparison.InvariantCultureIgnoreCase) >= 0))
                        {
                            return;
                        }
                    }
                }

                // Checking result
                trackTask.ContinueWith(async t =>
                {
                    UrlTrackingResult trackingResult = t.Result;
                    if (!trackingResult.IsAccountable)
                    {
                        // skip non tariffing redirects
                        return;
                    }

                    try
                    {
                        DomainTrackingStat trackingStat = _mappingEngine.Map <UrlTrackingResult, DomainTrackingStat>(trackingResult);

                        // counting url
                        await _service.CountAsync(trackingStat);
                    }
                    catch (Exception e)
                    {
                        Trace.TraceError(string.Format("Could not count external url '{0}': {1}",
                                                       trackingResult.Redirect, e));
                    }
                });
            }
        }
        public async Task <HttpResponseMessage> Get(string id)
        {
            UrlTrackingResult result = await _service.TrackAsync(new DomainTrackingUrl
            {
                Key        = id,
                RequestUri = Request.RequestUri
            });

            // redirecting
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Redirect);

            response.Headers.Location = result.Redirect;

            return(response);
        }
Example #3
0
        public async Task <UrlTrackingResult> TrackAsync(DomainTrackingUrl trackingUrl)
        {
            if (string.IsNullOrEmpty(trackingUrl.Id))
            {
                trackingUrl.Id = _urlShortenerService.Decode(trackingUrl.Key).ToString(CultureInfo.InvariantCulture);
            }

            TrackingUrlEntity entity = _mappingEngine.Map <DomainTrackingUrl, TrackingUrlEntity>(trackingUrl);

            entity = await _trackingUrlRepository.GetAsync(entity);

            if (entity == null)
            {
                throw new NotFoundException(string.Format("Could not find tracking url with id {0} and key {1}",
                                                          trackingUrl.Id, trackingUrl.Key));
            }

            // Build portal url link
            string projectUrl = _projectUriProvider.GetUri(entity.ProjectId);

            // We should keep requested schema in redirect link
            var projectUri = new UriBuilder(projectUrl)
            {
                Scheme = trackingUrl.RequestUri.Scheme,
                Port   = trackingUrl.RequestUri.Port
            };

            // Default redirect to Portal
            var result = new UrlTrackingResult
            {
                ProjectId      = entity.ProjectId,
                SubscriptionId = entity.SubscriptionId,
                IsAccountable  = false,
                Redirect       = projectUri.Uri
            };

            DomainCompany       company;
            CompanySubscription subscription;

            try
            {
                company = await _companyService.FindBySubscriptionAsync(entity.SubscriptionId);

                subscription = await _subscriptionService.GetAsync(entity.SubscriptionId);
            }
            catch (ForbiddenException)
            {
                // blocked company or subscription - portal redirect
                return(result);
            }
            catch (NotFoundException)
            {
                // deleted or unexisting company or subscription - portal redirect
                return(result);
            }


            // Checking custom subscription type
            if (subscription.Type == SubscriptionType.Pro || subscription.Type == SubscriptionType.Custom)
            {
                // always redirect to client site
                result.Redirect      = new Uri(entity.RedirectUrl);
                result.IsAccountable = true;
            }
            else if (subscription.Type == SubscriptionType.Basic)
            {
                // checking company balance
                if (subscription.IsManuallyEnabled || subscription.HasTrialClicks || (await _balanceService.GetBalanceAsync(company.Id)) > 0)
                {
                    result.Redirect      = new Uri(entity.RedirectUrl);
                    result.IsAccountable = true;
                }
            }

            return(result);
        }