Exemplo n.º 1
0
        /// <summary>
        /// Send Json filtered with SecurityContext
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        protected ActionResult JsonResult <T>(T model)
        {
            AtomJavaScriptSerializer ajs = new AtomJavaScriptSerializer(Repository.SecurityContext, true);
            string content = ajs.Serialize(model);

            return(Content(content, "application/json"));
        }
Exemplo n.º 2
0
        ///// <summary>
        /////
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="name"></param>
        ///// <param name="optional"></param>
        ///// <param name="defValue"></param>
        ///// <returns></returns>
        //public T FormValue<T>(string name, bool optional = false, T defValue = default(T)) {
        //    object val = null;
        //    if (!FormModel.TryGetValue(name, out val)) {
        //        if(!optional)
        //            throw new InvalidOperationException(name + " is required");
        //        return defValue;
        //    }
        //    if (val == null)
        //        return defValue;
        //    return (T)Convert.ChangeType(val, typeof(T));
        //}



        private void ParseDates(Dictionary <string, object> data)
        {
            foreach (var item in data.ToList())
            {
                Dictionary <string, object> childObject = item.Value as Dictionary <string, object>;
                if (childObject != null)
                {
                    ParseDates(childObject);
                    continue;
                }

                string val = item.Value as string;
                if (val == null)
                {
                    continue;
                }

                if (val.StartsWith("/Date(") && val.EndsWith(")/"))
                {
                    // change date..
                    object date = AtomJavaScriptSerializer.ToDateTime(val);
                    data[item.Key] = date;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/json; charset=utf-8";
            AtomJavaScriptSerializer js = new AtomJavaScriptSerializer(this.SecurityContext, true);

            if (Total > 0)
            {
                var obj = new { items = Query.ToList(), total = Total, merge = true };
                context.HttpContext.Response.Write(js.Serialize(obj));
            }
            else
            {
                var items = Query.ToList();
                context.HttpContext.Response.Write(js.Serialize(items));
            }
        }
Exemplo n.º 4
0
        private object DeserializeValue(object value)
        {
            if (value == null)
            {
                return(null);
            }

            string val = value as string;

            if (val != null)
            {
                //if (val.StartsWith("/DateISO"))
                //{
                //    int i = val.IndexOf('(');
                //    val = val.Substring(i + 1);
                //    i = val.LastIndexOf(')');
                //    val = val.Substring(0, i);
                //    return DateTime.Parse(val, null, System.Globalization.DateTimeStyles.RoundtripKind);
                //}
                if (val.StartsWith("/Date"))
                {
                    return(AtomJavaScriptSerializer.ToDateTime(val));
                }
                return(val);
            }

            if (value is Dictionary <string, object> )
            {
                IJavaScriptDeserializable d = new AtomDictionary();
                d.Deserialize(value as Dictionary <string, object>);
                return(d);
            }

            if (value is System.Collections.IEnumerable)
            {
                List <object> items = new List <object>();
                foreach (var item in (System.Collections.IEnumerable)value)
                {
                    items.Add(DeserializeValue(item));
                }

                return(items);
            }

            return(value);
        }
Exemplo n.º 5
0
        public ActionResult Update(long id)
        {
            if (!User.Identity.IsAuthenticated || User.Identity.AuthenticationType != "WSClient")
            {
                throw new UnauthorizedAccessException();
            }

            using (DB.CreateSecurityScope(null))
            {
                var job = DB.WSJobs.FirstOrDefault(x => x.JobID == id);
                if (job.AssigneeID != User.Identity.Name)
                {
                    throw new UnauthorizedAccessException();
                }
                job.JobStatus = FormValue <string>("JobStatus");
                job.ResultUrl = FormValue <string>("ResultUrl");
                job.EndTime   = DateTime.UtcNow;

                DB.SaveChanges();

                if (!string.IsNullOrWhiteSpace(job.TriggerEmail))
                {
                }

                if (!string.IsNullOrWhiteSpace(job.TriggerUrl))
                {
                    try {
                        using (WebClient client = new WebClient()) {
                            client.Headers[HttpRequestHeader.ContentType] = "application/json";
                            AtomJavaScriptSerializer js = new AtomJavaScriptSerializer(DefaultSecurityContext.Instance);
                            string result = js.Serialize(job);
                            client.UploadString(job.TriggerUrl, "POST", result);
                        }
                    }
                    catch
                    {
                    }
                }

                return(JsonResult(job));
            }
        }
Exemplo n.º 6
0
        private Expression ParseLinq(Expression root, Type type, string key, object value)
        {
            string[] tokens = key.Split(':', ' ').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).ToArray();

            string field = tokens[0];

            string operation = "==";

            if (tokens.Length > 1)
            {
                operation = tokens[1];
            }

            operation = operation.ToLower();

            foreach (var item in field.Split('.'))
            {
                PropertyInfo p = null;
                if (item.Equals("$id", StringComparison.OrdinalIgnoreCase))
                {
                    p = type.GetEntityProperties(true).FirstOrDefault().Property;
                }
                else
                {
                    p = type.GetProperty(item);
                }
                type = p.PropertyType;
                root = Expression.Property(root, p);
            }

            switch (operation)
            {
            case "any":
                return(ParseCollectionLinq("Any", root, type, value));

            case "all":
                return(ParseCollectionLinq("All", root, type, value));

            case "in":
                return(ParseCollectionLinq("Contains", Expression.Constant(CreateList(value, type)), type, root));

            case "!in":
                return(Expression.Not(ParseCollectionLinq("Contains", Expression.Constant(CreateList(value, type)), type, root)));

            case "!any":
                return(Expression.Not(ParseCollectionLinq("Any", root, type, value)));

            case "!all":
                return(Expression.Not(ParseCollectionLinq("All", root, type, value)));
            }

            Expression ve = Expression.Constant(null);

            Type vt = Nullable.GetUnderlyingType(type);

            if (value != null)
            {
                Type valueType = value.GetType();
                if (valueType.IsArray || valueType == type)
                {
                    ve = Expression.Constant(value);
                }
                else if (value is ArrayList)
                {
                    ve = Expression.Constant(value);
                }
                else
                {
                    if (vt != null)
                    {
                        if (vt == typeof(DateTime) && value.GetType() == typeof(string))
                        {
                            ve = Expression.Constant(AtomJavaScriptSerializer.ToDateTime(value as string), type);
                        }
                        else
                        {
                            value = Convert.ChangeType(value, vt);
                            ve    = Expression.Convert(Expression.Constant(value), type);
                        }
                    }
                    else
                    {
                        value = Convert.ChangeType(value, type);
                        ve    = Expression.Constant(value);
                    }
                }
            }

            switch (operation)
            {
            case "==":
                return(Expression.Equal(root, ve));

            case "!=":
                return(Expression.NotEqual(root, ve));

            case ">":
                return(Expression.GreaterThan(root, ve));

            case ">=":
                return(Expression.GreaterThanOrEqual(root, ve));

            case "<":
                return(Expression.LessThan(root, ve));

            case "<=":
                return(Expression.LessThanOrEqual(root, ve));

            case "between":
                if (value == null)
                {
                    return(null);
                }
                var objArray = new List <object>(CreateList(value, typeof(object)).Cast <object>());
                if (objArray.Any(x => x == null))
                {
                    return(null);
                }
                if (vt == typeof(DateTime))
                {
                    objArray = objArray.Select(x => (object)AtomJavaScriptSerializer.ToDateTime(x as string)).ToList();
                }
                return(Expression.And(
                           Expression.GreaterThanOrEqual(root, Expression.Constant(objArray.FirstOrDefault(), type)),
                           Expression.LessThanOrEqual(root, Expression.Constant(objArray.LastOrDefault(), type))));

            case "contains":
                return(Expression.Call(root, StringContainsMethod, ve));

            case "startswith":
                return(Expression.Call(root, StringStartsWithMethod, ve));

            case "endswith":
                return(Expression.Call(root, StringEndsWithMethod, ve));

            case "!contains":
                return(Expression.Not(Expression.Call(root, StringContainsMethod, ve)));

            case "!startswith":
                return(Expression.Not(Expression.Call(root, StringStartsWithMethod, ve)));

            case "!endswith":
                return(Expression.Not(Expression.Call(root, StringEndsWithMethod, ve)));

            default:
                break;
            }
            throw new ArgumentException(operation + " not supported");
        }
        private static void FetchResult(ControllerContext context)
        {
            TimeSpan ts = TimeSpan.FromSeconds(10);

            var User     = context.HttpContext.User;
            var Response = context.HttpContext.Response;

            string username = User.Identity.Name;

            long jobID;

            AtomJavaScriptSerializer js = new AtomJavaScriptSerializer(null);

            Response.Output.WriteLine("{ 'JobID': 0, 'Message': 'Welcome' }");
            Response.Flush();

            Update();

            do
            {
                if (!Response.IsClientConnected)
                {
                    break;
                }


                if (!Jobs.TryTake(out jobID, ts))
                {
                    Response.Output.WriteLine("{ 'JobID': 0, 'Message':'None' }");
                    Response.Flush();
                    Update();
                    continue;
                }

                using (WSClientModelEntities db = new WSClientModelEntities())
                {
                    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(60)))
                    {
                        DateTime utcNow = DateTime.UtcNow;

                        WSJob job = db.WSJobs.Where(Queued()).FirstOrDefault(x => x.JobID == jobID);
                        if (job == null)
                        {
                            continue;
                        }

                        job.AssigneeID = username;
                        job.JobStatus  = "Assigned";
                        job.EndTime    = DateTime.UtcNow;

                        var s = js.Serialize(new {
                            job.JobID,
                            job.JobStatus,
                            job.WSDLUrl,
                            job.IsDemo,
                            job.OutputType,
                            job.OutputTarget,
                            job.OutputPrefix,
                            job.OutputPackage
                        });



                        Response.Output.WriteLine(s);
                        Response.Flush();

                        db.SaveChanges();

                        scope.Complete();
                    }
                }
            } while (true);
        }