示例#1
0
        public RestApiResult Invoke(IDictionary <string, object> parameters)
        {
            Type   type     = Method.DeclaringType;
            object instance = type.Assembly.CreateInstance(type.FullName);

            List <object> methodParameters = new List <object>();

            foreach (ParameterInfo parameter in Method.GetParameters())
            {
                methodParameters.Add(parameters[parameter.Name]);
            }

            RestApiResult result = Method.Invoke(instance, methodParameters.ToArray()) as RestApiResult;

            return(result);
        }
示例#2
0
        public void ProcessRequest(HttpContext httpContext)
        {
            string   requestMethod = httpContext.Request.HttpMethod.ToUpper();
            HttpVerb verb;

            switch (requestMethod)
            {
            case "POST": verb = HttpVerb.Post; break;

            case "PUT": verb = HttpVerb.Put; break;

            case "DELETE": verb = HttpVerb.Delete; break;

            case "GET": verb = HttpVerb.Get; break;

            default: throw new InvalidOperationException();
            }

            Route route = RouteResolver.Resolve(verb, httpContext.Request.Url);

            if (route != null)
            {
                StreamReader reader = new StreamReader(httpContext.Request.InputStream);

                ApiContext apiContext = new ApiContext
                {
                    Uri   = httpContext.Request.Url,
                    Route = route,
                };

                if ((route.Verb == HttpVerb.Post) && (route.Url.Contains("images") || route.Url.Contains("spss") && !route.Url.Contains("bulkimport") || route.Url.Contains("templates") || route.Url.Contains("importCSV")))
                {
                    apiContext.HttpFileCollection = new HttpFileCollectionWrapper(httpContext.Request.Files);
                }
                else
                {
                    apiContext.RequestBody = reader.ReadToEnd();
                }

                IDictionary <string, object> parameters = ParameterParser.Parse(apiContext);

                ClarityDB.CreateInstance();

                RestApiResult result;

                if (!Authentication.RouteRequiresAuthenticate(route) || Authentication.IsAuthenticated(route))
                {
                    result = route.Invoke(parameters);
                }
                else
                {
                    result = new RestApiResult {
                        StatusCode = HttpStatusCode.Unauthorized
                    };
                }

                ClarityDB.DestroyInstance();

                httpContext.Response.StatusCode = (int)result.StatusCode;

                if (result.Json != null)
                {
                    var settings = new JsonSerializerSettings {
                        Formatting = Formatting.None
                    };
                    string response = JsonConvert.SerializeObject(result.Json, Formatting.None, settings);
                    httpContext.Response.Write(response);
                }
            }
            else
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
            }

            httpContext.Response.TrySkipIisCustomErrors = true;
            httpContext.Response.SuppressFormsAuthenticationRedirect = true;
        }