ResponseGenerated() public static method

public static ResponseGenerated ( IResponse response, string cacheKey = null ) : ResponseResult
response IResponse
cacheKey string
return ResponseResult
Ejemplo n.º 1
0
        public Task <ResponseResult> GetResponseAsync(HttpContextBase context, IEnumerable <RouteMatchResult> routeMatchResults)
        {
            context.ThrowIfNull("context");
            routeMatchResults.ThrowIfNull("routeMatchResults");

            return(ResponseResult.ResponseGenerated(new Response().NotFound()).AsCompletedTask());
        }
Ejemplo n.º 2
0
        public Task <ResponseResult> GetResponseAsync(HttpContextBase context, IEnumerable <RouteMatchResult> routeMatchResults)
        {
            context.ThrowIfNull("context");
            routeMatchResults.ThrowIfNull("routeMatchResults");

            RouteMatchResult[] unmatchedResults = routeMatchResults.Where(arg => arg.MatchResult.ResultType == MatchResultType.RouteNotMatched).ToArray();

            if (!unmatchedResults.Any())
            {
                return(ResponseResult.ResponseNotGenerated().AsCompletedTask());
            }

            RouteMatchResult[] unmatchedResultsThatMatchedOnUrlRelativePath = unmatchedResults.Where(arg1 => RouteMatchedUrlRelativePath(arg1.MatchResult)).ToArray();
            int minimumUnmatchedRestrictions = unmatchedResultsThatMatchedOnUrlRelativePath.Any() ? unmatchedResultsThatMatchedOnUrlRelativePath.Min(arg => arg.MatchResult.UnmatchedRestrictions.Count()) : 0;

            RouteMatchResult[] closestMatches = unmatchedResultsThatMatchedOnUrlRelativePath.Where(arg => arg.MatchResult.UnmatchedRestrictions.Count() == minimumUnmatchedRestrictions).ToArray();

            if (closestMatches.Length != 1)
            {
                return(ResponseResult.ResponseNotGenerated().AsCompletedTask());
            }

            RouteMatchResult closestMatch = closestMatches[0];

            IRestriction[]      unmatchedRestrictions = closestMatch.MatchResult.UnmatchedRestrictions.ToArray();
            MethodRestriction[] methodRestrictions    = unmatchedRestrictions.OfType <MethodRestriction>().ToArray();

            if (methodRestrictions.Any())
            {
                IEnumerable <string> methods = methodRestrictions
                                               .Select(arg => arg.Method)
                                               .Distinct(StringComparer.OrdinalIgnoreCase)
                                               .OrderBy(arg => arg);

                return(ResponseResult.ResponseGenerated(new Response().MethodNotAllowed().Header("Allow", String.Join(", ", methods))).AsCompletedTask());
            }
            if (unmatchedRestrictions.OfType <HeaderRestriction <AcceptHeader> >().Any())
            {
                return(ResponseResult.ResponseGenerated(new Response().NotAcceptable()).AsCompletedTask());
            }
            if (unmatchedRestrictions.OfType <HeaderRestriction <AcceptCharsetHeader> >().Any())
            {
                return(ResponseResult.ResponseGenerated(new Response().NotAcceptable()).AsCompletedTask());
            }
            if (unmatchedRestrictions.OfType <HeaderRestriction <AcceptEncodingHeader> >().Any())
            {
                return(ResponseResult.ResponseGenerated(new Response().NotAcceptable()).AsCompletedTask());
            }
            if (unmatchedRestrictions.OfType <HeaderRestriction <ContentEncodingHeader> >().Any())
            {
                return(ResponseResult.ResponseGenerated(new Response().UnsupportedMediaType()).AsCompletedTask());
            }

            return(ResponseResult.ResponseNotGenerated().AsCompletedTask());
        }
Ejemplo n.º 3
0
        public async Task <ResponseResult> GetResponseAsync(HttpContextBase context, IEnumerable <RouteMatchResult> routeMatchResults)
        {
            context.ThrowIfNull("context");
            routeMatchResults.ThrowIfNull("routeMatchResults");

            routeMatchResults = routeMatchResults.ToArray();

            RouteMatchResult[] matchedResults = routeMatchResults.Where(arg => arg.MatchResult.ResultType == MatchResultType.RouteMatched).ToArray();

            if (!matchedResults.Any())
            {
                return(ResponseResult.ResponseNotGenerated());
            }

            int maximumMatchedRestrictions = matchedResults.Max(arg => arg.MatchResult.MatchedRestrictions.Count());

            RouteMatchResult[] bestMatches = matchedResults.Where(arg => arg.MatchResult.MatchedRestrictions.CountEqual(maximumMatchedRestrictions)).ToArray();

            if (bestMatches.Length > 1)
            {
                return(ResponseResult.ResponseGenerated(new Response().MultipleChoices()));
            }
            if (bestMatches.Length == 0)
            {
                return(ResponseResult.ResponseNotGenerated());
            }

            RouteMatchResult   bestMatch          = bestMatches[0];
            AuthenticateResult authenticateResult = await bestMatch.Route.AuthenticateAsync(context.Request, context.Response);

            if (authenticateResult.ResultType == AuthenticateResultType.AuthenticationFailed)
            {
                return(ResponseResult.ResponseGenerated(authenticateResult.FailedResponse ?? new Response().Unauthorized()));
            }

            Task <IResponse> responseTask = bestMatch.Route.ProcessResponseAsync(context);

            return(ResponseResult.ResponseGenerated(responseTask, bestMatch.MatchResult.CacheKey));
        }