Beispiel #1
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;
        }
Beispiel #2
0
        private static void ParsePath(Dictionary <string, object> dictionary, ApiContext context)
        {
            if (context.Route.Url.Contains("{id}") ||
                context.Route.Url.Contains("{path}") || context.Route.Url.Contains("{email}") ||
                context.Route.Url.Contains("{username}") || context.Route.Url.Contains("{code}") ||
                context.Route.Url.Contains("{domainName}") || context.Route.Url.Contains("{secret}") ||
                context.Route.Url.Contains("{folderName}") || context.Route.Url.Contains("{pageSize}"))
            {
                List <string> requestSegments = RouteResolver.GetSignificantSegments(context.Uri.Segments);

                if (requestSegments.Contains("Epinion.Clarity.Web_deploy"))
                {
                    requestSegments.Remove("Epinion.Clarity.Web_deploy");
                }

                List <string> routeSegments = RouteResolver.GetSignificantSegments(context.Route.Url.Split('/'));

                for (int i = 0; i < routeSegments.Count; i++)
                {
                    if (routeSegments[i] == "{id}")
                    {
                        long id;
                        if (!long.TryParse(requestSegments[i], out id))
                        {
                            id = -100000;
                            throw new System.FormatException();
                        }
                        dictionary.Add("id", id);
                    }
                    else if (routeSegments[i] == "{path}")
                    {
                        string path = requestSegments[i];
                        dictionary.Add("path", path);
                    }
                    else if (routeSegments[i] == "{email}")
                    {
                        string path = requestSegments[i];
                        dictionary.Add("email", path);
                    }
                    else if (routeSegments[i] == "{username}")
                    {
                        string path = requestSegments[i];
                        dictionary.Add("username", path);
                    }
                    else if (routeSegments[i] == "{code}")
                    {
                        string path = requestSegments[i];
                        dictionary.Add("code", path);
                    }
                    else if (routeSegments[i] == "{domainName}")
                    {
                        string path = requestSegments[i];
                        dictionary.Add("domainName", path);
                    }
                    else if (routeSegments[i] == "{secret}")
                    {
                        string path = requestSegments[i];
                        dictionary.Add("secret", path);
                    }
                    else if (routeSegments[i] == "{folderName}")
                    {
                        string path = requestSegments[i];
                        dictionary.Add("folderName", path);
                    }
                    else if (routeSegments[i] == "{pageSize}")
                    {
                        int pageSize = Int32.Parse(requestSegments[i]);
                        dictionary.Add("pageSize", pageSize);
                    }
                }
            }
        }