Exemplo n.º 1
0
        protected bool ProcessPut(HttpListenerContext context)
        {
            PutEventArgs <Dictionary <string, object> > eventArgs = new PutEventArgs <Dictionary <string, object> >()
            {
                AskedDate = DateTime.Now, AskedUrl = context.Request.Url
            };
            bool result;

            if (AllowPut)
            {
                result = ProcessPutSql(context, PostQuery(context.Request.QueryString));
            }
            else
            {
                result = Process403(context);
            }
            eventArgs.EndDate = DateTime.Now;
            eventArgs.ResponseHttpStatusCode = (HttpStatusCode)context.Response.StatusCode;
            OnPutAction(eventArgs);
            return(result);
        }
Exemplo n.º 2
0
 protected virtual void OnPutAction(PutEventArgs <string> e)
 {
     OnPut?.Invoke(this, e);
 }
Exemplo n.º 3
0
 protected void OnPutAction(PutEventArgs <Dictionary <string, object> > e)
 {
     OnPut?.Invoke(this, e);
 }
Exemplo n.º 4
0
 protected virtual void OnPutAction(PutEventArgs <ObjectType> e)
 {
     OnPut?.Invoke(this, e);
 }
Exemplo n.º 5
0
        protected bool ProcessPut(HttpListenerContext context)
        {
            PutEventArgs <ObjectType> eventArgs = new PutEventArgs <ObjectType>()
            {
                AskedDate = DateTime.Now, AskedUrl = context.Request.Url
            };
            bool result = true;

            try
            {
                if (!context.Request.HasEntityBody)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                    result = false;
                }
                else
                {
                    string parameters = null;
                    using (System.IO.Stream body = context.Request.InputStream)
                    {
                        using (System.IO.StreamReader reader = new System.IO.StreamReader(body, context.Request.ContentEncoding))
                        {
                            parameters = reader.ReadToEnd();
                        }
                    }

                    if (string.IsNullOrWhiteSpace(parameters))
                    {
                        context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        result = false;
                    }
                    else
                    {
                        ObjectType objFromParameters = JsonSerializer.Deserialize <ObjectType>(parameters);
                        if (objFromParameters == null)
                        {
                            context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                            result = false;
                        }
                        else
                        {
                            eventArgs.New = objFromParameters;
                            ObjectType objFromCollection = Item.FirstOrDefault(item => item.GetType().GetProperties().Where(prop => System.Attribute.IsDefined(prop, typeof(PrimaryKeyAttribute))).All(prop => prop.GetValue(item).Equals(prop.GetValue(objFromParameters))));
                            if (objFromCollection == null)
                            {
                                context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                                result = false;
                            }
                            else
                            {
                                eventArgs.Old = objFromCollection;
                                int index = Item.IndexOf(objFromCollection);
                                Item[index] = objFromParameters;
                            }
                        }
                    }
                }
            }
            catch
            {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                result = false;
            }

            eventArgs.EndDate = DateTime.Now;
            eventArgs.ResponseHttpStatusCode = (HttpStatusCode)context.Response.StatusCode;
            OnPutAction(eventArgs);

            PrepareResponse(context);
            return(result);
        }