Ejemplo n.º 1
0
        private void ProcessServiceRequest(HttpRequestEventArgs e, RequestCommandWrapper command)
        {
            JObject responseObject;

            switch (command.action)
            {
            case "get_properties":
                responseObject = ProcessGetProperties(command);
                break;

            case "set_property":
                responseObject = ProcessSetProperty(command);
                break;

            case "execute":
                responseObject = ProcessExecute(command);
                break;

            default:
                throw new ApplicationException("unsupported action '" + command.action + "'");
            }
            HttpResponse response = e.Response;

            MakeResponse(response, HttpStatusCode.OK, responseObject);
        }
Ejemplo n.º 2
0
        private JObject ProcessExecute(RequestCommandWrapper command)
        {
            Debug.Assert(command.action == "execute");

            string objectName;

            GetTokenAsScalar <string>(command.jObject, "object_name", out objectName);
            string actionName;

            GetTokenAsScalar <string>(command.jObject, "action_name", out actionName);
            JObject parameters;

            GetTokenAsJObject(command.jObject, "parameters", out parameters);

            if (objectName == null ||
                actionName == null ||
                parameters == null)
            {
                throw new ArgumentException("You must set object_name, action_name and parameters fields");
            }
            ;

            JObject ret = new JObject();

            m_schemaLock.EnterReadLock();
            try
            {
                object obj;
                if (!m_objectNames.TryGetValue(objectName, out obj))
                {
                    throw new ArgumentException("Unknown object '" + objectName + "'");
                }
                ;

                ObjectDescription descr = m_objects[obj];

                ActionDescription actionDescr;
                if (!descr.actions.TryGetValue(actionName, out actionDescr))
                {
                    throw new ArgumentException("Unknown action '" + actionName + "'");
                }
                ;

                JValue value = actionDescr.Call(obj, parameters);
                ret.Add("value", value);
            }
            finally
            {
                m_schemaLock.ExitReadLock();
            };

            ret.Add("request_id", command.requestId);
            ret.Add("result", "ok");
            return(ret);
        }
Ejemplo n.º 3
0
        private JObject ProcessSetProperty(RequestCommandWrapper command)
        {
            Debug.Assert(command.action == "set_property");

            string objectName;

            GetTokenAsScalar <string>(command.jObject, "object_name", out objectName);
            string propertyName;

            GetTokenAsScalar <string>(command.jObject, "property_name", out propertyName);
            object value;

            GetTokenAsScalar <object>(command.jObject, "value", out value);

            if (objectName == null ||
                propertyName == null)
            {
                throw new ArgumentException("You must set object_name and property_name fields");
            }
            ;

            m_schemaLock.EnterReadLock();
            try
            {
                object obj;
                if (!m_objectNames.TryGetValue(objectName, out obj))
                {
                    throw new ArgumentException("Unknown object '" + objectName + "'");
                }
                ;

                ObjectDescription descr = m_objects[obj];

                PropertyOrFieldDescription propDescr;
                if (!descr.properties.TryGetValue(propertyName, out propDescr))
                {
                    throw new ArgumentException("Unknown property '" + propertyName + "'");
                }
                ;

                propDescr.SetValue(obj, value);
            }
            finally
            {
                m_schemaLock.ExitReadLock();
            };

            JObject ret = new JObject();

            ret.Add("request_id", command.requestId);
            ret.Add("result", "ok");
            return(ret);
        }
Ejemplo n.º 4
0
        private void RequestReceived(object sender, HttpRequestEventArgs e)
        {
            HttpRequest request = e.Request;

            if (m_auth != null)
            {
                if (!TryAuth(request, m_auth))
                {
                    m_logger.Trace("[" + request.UserHostAddress + "][error] Auth false, returning Unauthorized");
                    e.Response.StatusCode        = (int)HttpStatusCode.Unauthorized;
                    e.Response.StatusDescription = HttpResponseCodeStatus.Get(HttpStatusCode.Unauthorized);
                    e.Response.Headers.Add("WWW-Authenticate", "Basic realm=\"" + this.m_name.Replace("\"", "") + "\"");
                    return;
                }
                ;
            }
            ;

            string requestId = null;

            // Method allowed
            if (request.HttpMethod != "POST")
            {
                m_logger.Trace("[" + request.UserHostAddress + "][error] Wrong request method");
                MakeReponseError(e.Response, requestId, "Wrong request method", HttpStatusCode.Forbidden);
                return;
            }
            ;

            try
            {
                string path = request.Path;
                string body = ReadInput(request);
                if (String.IsNullOrWhiteSpace(body))
                {
                    ProcessSchemaRequest(e);
                    return;
                }

                RequestCommandWrapper command = new RequestCommandWrapper(body, m_jsonSettings);
                requestId = command.requestId;
                ProcessServiceRequest(e, command);
            }
            catch (Exception ex)
            {
                bool needError = !(ex is ArgumentException);
                m_logger.Log(needError? LogLevel.Error : LogLevel.Warn, "[" + request.UserHostAddress + "][error] Exception: \n" + ex.ToString());
                MakeReponseError(e.Response, requestId, "exception: " + ex.Message);
                return;
            };
        }
Ejemplo n.º 5
0
        private JObject ProcessGetProperties(RequestCommandWrapper command)
        {
            Debug.Assert(command.action == "get_properties");

            JObject ret = new JObject();

            ret.Add("request_id", command.requestId);
            ret.Add("result", "ok");

            string objectName;

            GetTokenAsScalar <string>(command.jObject, "object_name", out objectName);
            string propertyName;

            GetTokenAsScalar <string>(command.jObject, "property_name", out propertyName);

            JObject objects = new JObject();

            m_schemaLock.EnterReadLock();
            try
            {
                if (objectName == null)
                {
                    foreach (KeyValuePair <object, ObjectDescription> descr in m_objects)
                    {
                        JObject obj = new JObject();
                        foreach (KeyValuePair <string, PropertyOrFieldDescription> pair in descr.Value.properties)
                        {
                            obj.Add(pair.Key, pair.Value.GetValue(descr.Key));
                        }
                        objects.Add(descr.Value.name, obj);
                    }
                }
                else
                {
                    object obj;
                    if (m_objectNames.TryGetValue(objectName, out obj))
                    {
                        ObjectDescription descr = m_objects[obj];
                        JObject           jObj  = new JObject();
                        if (propertyName == null)
                        {
                            foreach (KeyValuePair <string, PropertyOrFieldDescription> pair in descr.properties)
                            {
                                jObj.Add(pair.Key, pair.Value.GetValue(obj));
                            }
                        }
                        else
                        {
                            PropertyOrFieldDescription propDescr;
                            if (descr.properties.TryGetValue(propertyName, out propDescr))
                            {
                                jObj.Add(propDescr.name, propDescr.GetValue(obj));
                            }
                            else
                            {
                                throw new ArgumentException("Unknown property '" + propertyName + "'");
                            };
                        }
                        objects.Add(descr.name, jObj);
                    }
                    else
                    {
                        throw new ArgumentException("Unknown object '" + objectName + "'");
                    }
                }
            }
            finally
            {
                m_schemaLock.ExitReadLock();
            };

            ret.Add("objects", objects);
            return(ret);
        }