コード例 #1
0
        internal object GetParameterValue(ParsedUri parsedUri)
        {
            var value          = GetValueFromUri(parsedUri);
            var convertedValue = ConvertValueToParameterType(_parameterType, value);

            return(convertedValue);
        }
コード例 #2
0
ファイル: UriParser.cs プロジェクト: bbSkygazer/IotWebServer
        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);
        }
コード例 #3
0
        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))));
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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;
        }
コード例 #6
0
        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);
        }
コード例 #7
0
ファイル: UriParser.cs プロジェクト: Jark/restup
        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;
        }
コード例 #8
0
ファイル: ParameterValueGetter.cs プロジェクト: Jark/restup
 internal object GetParameterValue(ParsedUri parsedUri)
 {
     var value = GetValueFromUri(parsedUri);
     var convertedValue = ConvertValueToParameterType(_parameterType, value);
     return convertedValue;
 }
コード例 #9
0
        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}.");
        }
コード例 #10
0
 protected abstract object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri);
コード例 #11
0
        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.");
        }
コード例 #12
0
 protected override string GetValueFromUri(ParsedUri parsedUri)
 {
     return(parsedUri.PathParts[_pathIndex].Value);
 }
コード例 #13
0
        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));
        }
コード例 #14
0
 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));
        }
コード例 #16
0
 protected override string GetValueFromUri(ParsedUri parsedUri)
 {
     return parsedUri.Parameters.FirstOrDefault(x => x.Name.Equals(_queryParameterName, StringComparison.OrdinalIgnoreCase)).Value;
 }
コード例 #17
0
 protected abstract string GetValueFromUri(ParsedUri parsedUri);
コード例 #18
0
 protected override string GetValueFromUri(ParsedUri parsedUri)
 {
     return(parsedUri.Parameters.FirstOrDefault(x => x.Name.Equals(_queryParameterName, StringComparison.OrdinalIgnoreCase)).Value);
 }
コード例 #19
0
ファイル: ParameterValueGetter.cs プロジェクト: Jark/restup
 protected abstract string GetValueFromUri(ParsedUri parsedUri);
コード例 #20
0
 protected override string GetValueFromUri(ParsedUri parsedUri)
 {
     return parsedUri.PathParts[_pathIndex].Value;
 }