Exemplo n.º 1
0
        public IHttpContext GetNode(IHttpContext context)
        {
            try
            {
                var webParameter = WebServiceUtils.GetNameValueCollection(context);
                IotDataResponseFormat responseFormat = StringUtils.ToEnum(WebServiceUtils.GetQueryStringValue(webParameter, "format", DataStation.DefaultResponseFormat.ToString()), DataStation.DefaultResponseFormat);

                string path   = "";
                string nodeId = "";
                var    match  = Regex.Match(context.Request.PathInfo, @"/node(?<path>(/[a-zA-Z0-9_]+)*)/(?<nodeId>[a-zA-Z0-9_]+)$");
                if (match.Success)
                {
                    path   = match.Groups["path"].Value;
                    nodeId = match.Groups["nodeId"].Value;
                }

                if (!WebServiceUtils.CheckAuthentication(context, path))
                {
                    return(context);
                }

                if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(nodeId))
                {
                    context.Response.SendResponse(HttpStatusCode.BadRequest);
                }
                else
                {
                    IDataRepository dataRepository = DataStation.DataRepository;
                    INode           node           = dataRepository.GetNode(path, nodeId);

                    if (node != null)
                    {
                        if (responseFormat == IotDataResponseFormat.Xml)
                        {
                            WebResponse.SendNodeResponseAsXml(context, node);
                        }
                        else
                        {
                            WebResponse.SendNodeResponseAsJson(context, node);
                        }
                    }
                    else
                    {
                        context.Response.SendResponse(HttpStatusCode.BadRequest);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "Cannot GetXmlData!");
                context.Response.SendResponse(HttpStatusCode.InternalServerError);
            }
            return(context);
        }
Exemplo n.º 2
0
 public IHttpContext Index(IHttpContext context)
 {
     try
     {
         if (!WebServiceUtils.CheckAuthentication(context, ""))
         {
             return(context);
         }
         context.Response.SendResponse(HttpStatusCode.Ok, TemplateManager.Rander("PageLinks", GetServicePageList()), Encoding.UTF8);
     }
     catch (Exception e)
     {
         Logger.Error(e, "Cannot GetXmlData!");
         context.Response.SendResponse(HttpStatusCode.InternalServerError);
     }
     return(context);
 }
Exemplo n.º 3
0
        public IHttpContext GetFolder(IHttpContext context)
        {
            try
            {
                var webParameter = WebServiceUtils.GetNameValueCollection(context);
                IotDataResponseFormat responseFormat = StringUtils.ToEnum(WebServiceUtils.GetQueryStringValue(webParameter, "format", DataStation.DefaultResponseFormat.ToString()), DataStation.DefaultResponseFormat);
                int  depth        = StringUtils.GetIntValue(WebServiceUtils.GetQueryStringValue(webParameter, "depth", "1"), 1);
                bool includeNodes = StringUtils.GetValue(WebServiceUtils.GetQueryStringValue(webParameter, "includeNodes", "false"), false);

                string path  = "";
                var    match = Regex.Match(context.Request.PathInfo, @"/folder(?<path>(/[a-zA-Z0-9_]+)+)$");
                if (match.Success)
                {
                    path = match.Groups["path"].Value;
                }
                if (!WebServiceUtils.CheckAuthentication(context, path))
                {
                    return(context);
                }

                IDataRepository dataRepository = DataStation.DataRepository;
                var             folder         = dataRepository.GetFolder(path, depth, includeNodes);
                if (responseFormat == IotDataResponseFormat.Xml)
                {
                    WebResponse.SendFolderResponseAsXml(context, folder);
                }
                else
                {
                    WebResponse.SendFolderResponseAsJson(context, folder);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "Cannot GetXmlData!");
                context.Response.SendResponse(HttpStatusCode.InternalServerError);
            }
            return(context);
        }
Exemplo n.º 4
0
        public IHttpContext SetNode(IHttpContext context)
        {
            try
            {
                var webParameter = WebServiceUtils.GetNameValueCollection(context);
                IotDataResponseFormat responseFormat = StringUtils.ToEnum(WebServiceUtils.GetQueryStringValue(webParameter, "format", DataStation.DefaultResponseFormat.ToString()), DataStation.DefaultResponseFormat);

                string path   = "";
                string nodeId = "";
                var    match  = Regex.Match(context.Request.PathInfo, @"/node(?<path>(/[a-zA-Z0-9_]+)*)/(?<nodeId>[a-zA-Z0-9_]+)$");
                if (match.Success)
                {
                    path   = match.Groups["path"].Value;
                    nodeId = match.Groups["nodeId"].Value;
                }
                if (!WebServiceUtils.CheckAuthentication(context, path))
                {
                    return(context);
                }


                string jsonString = context.Request.Payload;

                Node node = Node.CreateFrom(JObject.Parse(jsonString));
                if (node == null)
                {
                    context.Response.SendResponse(HttpStatusCode.BadRequest);
                }
                else if (node.Id != nodeId)
                {
                    string message = $"Node ID is mismatched ('{nodeId}' != '{node.Id}').";
                    Logger.Warn(message);
                    context.Response.SendResponse(HttpStatusCode.BadRequest, message);
                }
                else
                {
                    IDataRepository dataRepository = DataStation.DataRepository;
                    if (dataRepository.SetNode(path, node))
                    {
                        if (responseFormat == IotDataResponseFormat.Xml)
                        {
                            WebResponse.SendNodeResponseAsXml(context, node);
                        }
                        else
                        {
                            WebResponse.SendNodeResponseAsJson(context, node);
                        }
                    }
                    else
                    {
                        context.Response.SendResponse(HttpStatusCode.BadRequest);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "Cannot SetNode!");
                context.Response.SendResponse(HttpStatusCode.InternalServerError);
            }
            return(context);
        }