Exemplo n.º 1
0
        public void ProcessRequest(HttpContext context)
        {
            InvokeHandler handler;

            if (context.Request.ContentType.Contains("json"))
            {
                switch (context.Request.HttpMethod)
                {
                case "GET":
                    handler = RestMethodInfo.Get;
                    break;

                case "POST":
                    handler = RestMethodInfo.Post;
                    break;

                case "PUT":
                    handler = RestMethodInfo.Put;
                    break;

                case "DELETE":
                    handler = RestMethodInfo.Delete;
                    break;

                default:
                    handler = null;
                    break;
                }
                if (handler == null)
                {
                    throw new HttpException(405, string.Format("Method '{0}' not allowed for {1}", context.Request.HttpMethod, RestMethodInfo.RequestType.Name));
                }
                object req;
                if (context.Request.HttpMethod == "POST")
                {
                    req = Parser.Deserialize(context.Request.InputStream, RestMethodInfo.RequestType);
                }
                else
                {
                    string data = "{" + context.Request.QueryString.ToString() + "}";
                    req = Parser.Deserialize(data, RestMethodInfo.RequestType);
                }
                //Get Rest class from cache.
                GXRestService target = RestMap[RestMethodInfo.RestClassType] as GXRestService;
                if (target == null)
                {
                    target = GXJsonParser.CreateInstance(RestMethodInfo.RestClassType) as GXRestService;
                    RestMap[RestMethodInfo.RestClassType] = target;
                }
                //Update user and DB info.

                //If proxy is used.
                string add = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                if (add == null)
                {
                    add = context.Request.UserHostAddress;
                }
                target.Host = Host;
                target.User = context.User;
                target.Db   = Connection;
                object tmp   = handler(target, req);
                string reply = Parser.Serialize(tmp);
                context.Response.Write(reply);
                context.Response.ContentType = "json";
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parse query DTO and return response DTO as string.
        /// </summary>
        /// <param name="method">Http method.</param>
        /// <param name="path">Command to execute.</param>
        /// <param name="data">Command data.</param>
        /// <returns>DTO result as string.</returns>
        static string GetReply(Hashtable messageMap, IPrincipal user, GXServer server, string hostAddress, string method, string path, string data)
        {
            InvokeHandler    handler;
            string           command;
            GXRestMethodInfo mi;

            lock (messageMap)
            {
                mi = GXGeneral.GetTypes(messageMap, path, out command);
            }
            if (mi == null)
            {
                throw new HttpException(405, string.Format("Rest method '{0}' not implemented.", command));
            }
            switch (method.ToUpper())
            {
            case "GET":
                handler = mi.Get;
                break;

            case "POST":
                handler = mi.Post;
                break;

            case "PUT":
                handler = mi.Put;
                break;

            case "DELETE":
                handler = mi.Delete;
                break;

            default:
                handler = null;
                break;
            }
            if (handler == null)
            {
                throw new HttpException(405, string.Format("Method '{0}' not allowed for {1}", method, command));
            }
            object req = server.Parser.Deserialize("{" + data + "}", mi.RequestType);
            //Get Rest class from cache.
            GXRestService target = server.RestMap[mi.RestClassType] as GXRestService;

            if (target == null)
            {
                target = GXJsonParser.CreateInstance(mi.RestClassType) as GXRestService;
                server.RestMap[mi.RestClassType] = target;
            }
            target.Host            = server.Host;
            target.User            = user;
            target.Db              = server.Connection;
            target.UserHostAddress = hostAddress;
            object tmp = handler(target, req);

            if (tmp == null)
            {
                throw new HttpException(405, string.Format("Command '{0}' returned null.", command));
            }
            return(server.Parser.SerializeToHttp(tmp));
        }