internal object GetParameterValue(ParsedUri parsedUri)
        {
            var value          = GetValueFromUri(parsedUri);
            var convertedValue = ConvertValueToParameterType(_parameterType, value);

            return(convertedValue);
        }
Example #2
0
        public bool TryParse(string uriFormatUri, out ParsedUri parsedUri)
        {
            var match = _splitRegex.Match(uriFormatUri);

            if (!match.Success)
            {
                parsedUri = null;
                return(false);
            }

            var uriGroup = match.Groups["uri"];

            if (!uriGroup.Success)
            {
                parsedUri = null;
                return(false);
            }

            var pathParts = ParsePathParts(uriGroup.Value);

            var parameters = ParseParameterGroup(match.Groups["parameters"]);
            var fragment   = match.Groups["fragment"].Success ? match.Groups["fragment"].Value : string.Empty;

            parsedUri = new ParsedUri(pathParts, parameters, fragment);
            return(true);
        }
        internal bool Match(ParsedUri uri)
        {
            if (_matchUri.PathParts.Count != uri.PathParts.Count)
            {
                return(false);
            }

            for (var i = 0; i < _matchUri.PathParts.Count; i++)
            {
                var fromPart = _matchUri.PathParts[i];
                var toPart   = uri.PathParts[i];
                if (fromPart.PartType == PathPart.PathPartType.Argument)
                {
                    continue;
                }

                if (!fromPart.Value.Equals(toPart.Value, StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }
            }

            if (uri.Parameters.Count < _matchUri.Parameters.Count)
            {
                return(false);
            }

            return(_matchUri.Parameters.All(x => uri.Parameters.Any(y => y.Name.Equals(x.Name, StringComparison.OrdinalIgnoreCase))));
        }
        protected override object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri)
        {
            var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType);

            object[] parameters;
            try
            {
                parameters = info.GetParametersFromUri(requestUri).ToArray();
            }
            catch (FormatException)
            {
                return _responseFactory.CreateBadRequest();
            }

            return info.MethodInfo.Invoke(
                    instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()),
                    parameters);
        }
        internal RestControllerMethodInfo(
            MethodInfo methodInfo,
            Func <object[]> constructorArgs,
            TypeWrapper typeWrapper)
        {
            constructorArgs.GuardNull(nameof(constructorArgs));
            _uriParser = new UriParser();
            _matchUri  = GetUriFromMethod(methodInfo);

            ReturnTypeWrapper         = typeWrapper;
            ControllerConstructorArgs = constructorArgs;
            MethodInfo = methodInfo;

            _validParameterTypes = GetValidParameterTypes();
            _parameterGetters    = GetParameterGetters(methodInfo);
            Verb = GetVerb();

            Type contentParameterType;

            HasContentParameter  = TryGetContentParameterType(methodInfo, out contentParameterType);
            ContentParameterType = contentParameterType;
        }
        protected override object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri)
        {
            var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType);

            object contentObj = null;
            try
            {
                if (request.HttpServerRequest.Content != null)
                {
                    contentObj = _contentSerializer.FromContent(
                        request.ContentEncoding.GetString(request.HttpServerRequest.Content),
                        request.ContentMediaType,
                        info.ContentParameterType);
                }
            }
            catch (JsonReaderException)
            {
                return _responseFactory.CreateBadRequest();
            }
            catch (InvalidOperationException)
            {
                return _responseFactory.CreateBadRequest();
            }

            object[] parameters = null;
            try
            {
                parameters = info.GetParametersFromUri(requestUri).Concat(new[] { contentObj }).ToArray();
            }
            catch (FormatException)
            {
                return _responseFactory.CreateBadRequest();
            }

            return info.MethodInfo.Invoke(
                    instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()),
                    parameters);
        }
Example #7
0
        public bool TryParse(string uriFormatUri, out ParsedUri parsedUri)
        {
            var match = _splitRegex.Match(uriFormatUri);
            if (!match.Success)
            {
                parsedUri = null;
                return false;
            }

            var uriGroup = match.Groups["uri"];
            if (!uriGroup.Success)
            {
                parsedUri = null;
                return false;
            }

            var pathParts = ParsePathParts(uriGroup.Value);

            var parameters = ParseParameterGroup(match.Groups["parameters"]);
            var fragment = match.Groups["fragment"].Success ? match.Groups["fragment"].Value : string.Empty;

            parsedUri = new ParsedUri(pathParts, parameters, fragment);
            return true;
        }
Example #8
0
 internal object GetParameterValue(ParsedUri parsedUri)
 {
     var value = GetValueFromUri(parsedUri);
     var convertedValue = ConvertValueToParameterType(_parameterType, value);
     return convertedValue;
 }
        private static ParameterValueGetter GetParameterGetter(ParameterInfo parameterInfo, ParsedUri matchUri)
        {
            var methodName         = parameterInfo.Name;
            var firstPathPartMatch = matchUri.PathParts
                                     .Select((x, i) => new { Part = x, Index = i })
                                     .Where(x => x.Part.PartType == PathPart.PathPartType.Argument)
                                     .FirstOrDefault(x => methodName.Equals(x.Part.Value, StringComparison.OrdinalIgnoreCase));

            var parameterType = parameterInfo.ParameterType;

            if (firstPathPartMatch != null)
            {
                return(new PathParameterValueGetter(methodName, parameterType, firstPathPartMatch.Index));
            }

            var firstQueryParameterMatch = matchUri.Parameters
                                           .FirstOrDefault(x => methodName.Equals(x.Value, StringComparison.OrdinalIgnoreCase));

            if (firstQueryParameterMatch != null)
            {
                return(new QueryParameterValueGetter(methodName, parameterType, firstQueryParameterMatch));
            }

            throw new Exception($"Method {methodName} not found in rest controller method uri {matchUri}.");
        }
 protected abstract object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri);
        public async Task <IRestResponse> ExecuteMethodAsync(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri)
        {
            var methodInvokeResult = ExecuteAnonymousMethod(info, request, requestUri);

            switch (info.ReturnTypeWrapper)
            {
            case RestControllerMethodInfo.TypeWrapper.None:
                return(await Task.FromResult((IRestResponse)methodInvokeResult));

            case RestControllerMethodInfo.TypeWrapper.AsyncOperation:
                return(await ConvertToTask((dynamic)methodInvokeResult));

            case RestControllerMethodInfo.TypeWrapper.Task:
                return(await(dynamic) methodInvokeResult);
            }

            throw new Exception($"ReturnTypeWrapper of type {info.ReturnTypeWrapper} not known.");
        }
Example #12
0
 protected override string GetValueFromUri(ParsedUri parsedUri)
 {
     return(parsedUri.PathParts[_pathIndex].Value);
 }
        protected override object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri)
        {
            var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType);

            object[] parameters;
            try
            {
                parameters = info.GetParametersFromUri(requestUri).ToArray();
            }
            catch (FormatException)
            {
                return(_responseFactory.CreateBadRequest());
            }

            return(info.MethodInfo.Invoke(
                       instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()),
                       parameters));
        }
 internal IEnumerable <object> GetParametersFromUri(ParsedUri uri)
 {
     return(_parameterGetters.Select(x => x.GetParameterValue(uri)).ToArray());
 }
        protected override object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri)
        {
            var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType);

            object contentObj = null;

            try
            {
                if (request.HttpServerRequest.Content != null)
                {
                    contentObj = _contentSerializer.FromContent(
                        request.ContentEncoding.GetString(request.HttpServerRequest.Content),
                        request.ContentMediaType,
                        info.ContentParameterType);
                }
            }
            catch (JsonReaderException)
            {
                return(_responseFactory.CreateBadRequest());
            }
            catch (InvalidOperationException)
            {
                return(_responseFactory.CreateBadRequest());
            }

            object[] parameters = null;
            try
            {
                parameters = info.GetParametersFromUri(requestUri).Concat(new[] { contentObj }).ToArray();
            }
            catch (FormatException)
            {
                return(_responseFactory.CreateBadRequest());
            }

            return(info.MethodInfo.Invoke(
                       instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()),
                       parameters));
        }
 protected override string GetValueFromUri(ParsedUri parsedUri)
 {
     return parsedUri.Parameters.FirstOrDefault(x => x.Name.Equals(_queryParameterName, StringComparison.OrdinalIgnoreCase)).Value;
 }
 protected abstract string GetValueFromUri(ParsedUri parsedUri);
 protected override string GetValueFromUri(ParsedUri parsedUri)
 {
     return(parsedUri.Parameters.FirstOrDefault(x => x.Name.Equals(_queryParameterName, StringComparison.OrdinalIgnoreCase)).Value);
 }
Example #19
0
 protected abstract string GetValueFromUri(ParsedUri parsedUri);
Example #20
0
 protected override string GetValueFromUri(ParsedUri parsedUri)
 {
     return parsedUri.PathParts[_pathIndex].Value;
 }