public void InitWithKeyValuePairs()
 {
     JObject o = new JObject(new string[] { "one", "two", }, new object[] { 1, 2 });
     Assert.AreEqual(2, o.Count);
     Assert.AreEqual(1, o["one"]);
     Assert.AreEqual(2, o["two"]);
 }
 public void InitWithNullValues()
 {
     JObject o = new JObject(new string[] { "one", "two", "three" }, null);
     Assert.AreEqual(3, o.Count);
     Assert.IsTrue(JNull.LogicallyEquals(o["one"]));
     Assert.IsTrue(JNull.LogicallyEquals(o["two"]));
     Assert.IsTrue(JNull.LogicallyEquals(o["three"]));
 }
 public void InitWithExtraKeys()
 {
     JObject o = new JObject(new string[] { "one", "two", "three" }, new object[] { 1, 2 });
     Assert.AreEqual(3, o.Count);
     Assert.AreEqual(1, o["one"]);
     Assert.AreEqual(2, o["two"]);
     Assert.IsTrue(JNull.LogicallyEquals(o["three"]));
 }
 public void Names()
 {
     JObject o = new JObject();
     o.Put("one", 1);
     o.Put("two", 2);
     ICollection names = o.Names;
     IEnumerator e = names.GetEnumerator();
     e.MoveNext(); Assert.AreEqual("one", e.Current);
     e.MoveNext(); Assert.AreEqual("two", e.Current);
 }
 public void InitWithKeyValuePairsAccumulates()
 {
     JObject o = new JObject(new string[] { "one", "two", "three", "two" }, new object[] { 1, 2, 3, 4 });
     Assert.AreEqual(3, o.Count);
     Assert.AreEqual(1, o["one"]);
     IList two = o["two"] as IList;
     Assert.IsNotNull(two, "Member 'two' should be an ordered list of accumulated values.");
     Assert.AreEqual(2, two.Count, "Count of values under 'two'.");
     Assert.AreEqual(2, two[0]);
     Assert.AreEqual(4, two[1]);
     Assert.AreEqual(3, o["three"]);
 }
        public override void webExecute()
        {
            bool   isRefreshToken        = false;
            bool   isDevice              = false;
            bool   isExternalSDAuth      = false;
            String clientId              = cgiGet("client_id");
            String clientSecret          = cgiGet("client_secret");
            String grantType             = cgiGet("grant_type");
            String nativeToken           = cgiGet("native_token");
            String nativeVerifier        = cgiGet("native_verifier");
            String avoid_redirect        = cgiGet("avoid_redirect");
            String additional_parameters = cgiGet("additional_parameters");
            String refreshToken          = "";
            String userName              = string.Empty;
            String userPassword          = string.Empty;
            String scope = string.Empty;
            string URL   = string.Empty;
            bool   flag  = false;

            try
            {
                DataStoreUtil.LoadDataStores(context);

                if (grantType.Equals("refresh_token", StringComparison.OrdinalIgnoreCase))
                {
                    refreshToken   = cgiGet("refresh_token");
                    isRefreshToken = true;
                }
                else if (grantType.Equals("device", StringComparison.OrdinalIgnoreCase))
                {
                    isDevice = true;
                }
                else if (!string.IsNullOrEmpty(nativeToken))
                {
                    isExternalSDAuth = true;
                }
                else
                {
                    userName     = cgiGet("username");
                    userPassword = cgiGet("password");
                    scope        = cgiGet("scope");
                }

                OutData  gamout;
                GxResult result;
                if (isRefreshToken)
                {
                    result = GxSecurityProvider.Provider.refreshtoken(context, clientId, clientSecret, refreshToken, out gamout, out flag);
                }
                else if (isDevice)
                {
                    result = GxSecurityProvider.Provider.logindevice(context, clientId, clientSecret, out gamout, out flag);
                }
                else if (isExternalSDAuth)
                {
                    result = GxSecurityProvider.Provider.externalauthenticationfromsdusingtoken(context, grantType, nativeToken, nativeVerifier, clientId, clientSecret, ref scope, additional_parameters, out gamout, out flag);
                }
                else if (String.IsNullOrEmpty(additional_parameters))
                {
                    result = GxSecurityProvider.Provider.oauthauthentication(context, grantType, userName, userPassword, clientId, clientSecret, scope, out gamout, out URL, out flag);
                }
                else
                {
                    result = GxSecurityProvider.Provider.oauthauthentication(context, grantType, userName, userPassword, clientId, clientSecret, scope, additional_parameters, out gamout, out URL, out flag);
                }

                localHttpContext.Response.ContentType = MediaTypesNames.ApplicationJson;
                if (!flag)
                {
                    localHttpContext.Response.StatusCode = 401;
                    if (result != null)
                    {
                        string messagePermission = result.Description;
                        HttpHelper.SetResponseStatusAndJsonError(context.HttpContext, result.Code, messagePermission);
                        if (GXUtil.ContainsNoAsciiCharacter(messagePermission))
                        {
                            messagePermission = string.Format("{0}{1}", GxRestPrefix.ENCODED_PREFIX, Uri.EscapeDataString(messagePermission));
                        }
                        localHttpContext.Response.AddHeader(HttpHeader.AUTHENTICATE_HEADER, HttpHelper.OatuhUnauthorizedHeader(context.GetServerName(), result.Code, messagePermission));
                    }
                }
                else
                {
                    if (!isDevice && !isRefreshToken && (gamout == null || String.IsNullOrEmpty((string)gamout["gxTpr_Access_token"])))
                    {
                        if (string.IsNullOrEmpty(avoid_redirect))
                        {
                            localHttpContext.Response.StatusCode = 303;
                        }
                        else
                        {
                            localHttpContext.Response.StatusCode = 200;
                        }
                        localHttpContext.Response.AddHeader("location", URL);
                        Jayrock.Json.JObject jObj = new Jayrock.Json.JObject();
                        jObj.Put("Location", URL);
                        localHttpContext.Response.Write(jObj.ToString());
                    }
                    else
                    {
                        localHttpContext.Response.StatusCode = 200;
                        localHttpContext.Response.Write(gamout.JsonString);
                    }
                }
                context.CloseConnections();
            }
            catch (Exception e)
            {
                localHttpContext.Response.StatusCode = 404;
                localHttpContext.Response.Write(e.Message);
                GXLogging.Error(log, string.Format("Error in access_token service clientId:{0} clientSecret:{1} grantType:{2} userName:{3} scope:{4}", clientId, clientSecret, grantType, userName, scope), e);
            }
        }
        public override void webExecute()
        {
            try
            {
                if (context.isMultipartRequest())
                {
                    localHttpContext.Response.ContentType = MediaTypesNames.TextPlain;
                    var r         = new List <UploadFile>();
                    var fileCount = localHttpContext.Request.GetFileCount();
                    for (var i = 0; i < fileCount; i++)
                    {
                        var      hpf      = localHttpContext.Request.GetFile(i);
                        string   fileName = string.Empty;
                        string[] files    = hpf.FileName.Split(new char[] { '\\' });
                        if (files.Length > 0)
                        {
                            fileName = files[files.Length - 1];
                        }
                        else
                        {
                            fileName = hpf.FileName;
                        }

                        string ext           = FileUtil.GetFileType(fileName);
                        string savedFileName = FileUtil.getTempFileName(Preferences.getTMP_MEDIA_PATH(), FileUtil.GetFileName(fileName), string.IsNullOrEmpty(ext) ? "tmp" : ext);
                        GxFile gxFile        = new GxFile(Preferences.getTMP_MEDIA_PATH(), savedFileName);

                        gxFile.Create(hpf.InputStream);

                        GXFileWatcher.Instance.AddTemporaryFile(gxFile);

                        r.Add(new UploadFile()
                        {
                            name         = fileName,
                            size         = gxFile.GetLength(),
                            url          = gxFile.GetPath(),
                            type         = context.GetContentType(ext),
                            extension    = ext,
                            thumbnailUrl = gxFile.GetPath(),
                            path         = savedFileName
                        });
                    }
                    UploadFilesResult result = new UploadFilesResult()
                    {
                        files = r
                    };
                    var jsonObj = JSONHelper.Serialize(result);
                    localHttpContext.Response.Write(jsonObj);
                }
                else
                {
                    Stream istream     = localHttpContext.Request.GetInputStream();
                    String contentType = localHttpContext.Request.ContentType;
                    String ext         = context.ExtensionForContentType(contentType);

                    string fileName = FileUtil.getTempFileName(Preferences.getTMP_MEDIA_PATH(), "BLOB", string.IsNullOrEmpty(ext) ? "tmp" : ext);
                    GxFile file     = new GxFile(Preferences.getTMP_MEDIA_PATH(), fileName);
                    file.Create(istream);

                    Jayrock.Json.JObject obj = new Jayrock.Json.JObject();
                    fileName = file.GetURI();

                    String fileGuid  = Guid.NewGuid().ToString("N");
                    String fileToken = GxRestPrefix.UPLOAD_PREFIX + fileGuid;
                    CacheAPI.FilesCache.Set(fileGuid, fileName, GxRestPrefix.UPLOAD_TIMEOUT);
                    obj.Put("object_id", fileToken);
                    localHttpContext.Response.AddHeader("GeneXus-Object-Id", fileToken);
                    localHttpContext.Response.ContentType = MediaTypesNames.ApplicationJson;
                    localHttpContext.Response.StatusCode  = 201;
                    localHttpContext.Response.Write(obj.ToString());
                }
            }
            catch (Exception e)
            {
                SendResponseStatus(500, e.Message);
                HttpHelper.SetResponseStatusAndJsonError(localHttpContext, HttpStatusCode.InternalServerError.ToString(), e.Message);
            }
            finally
            {
                try
                {
                    context.CloseConnections();
                }
                catch
                {
                }
            }
        }
 public void WriteObjectArray()
 {
     JObject o = new JObject();
     o.Put("one", 1);
     o.Put("two", 2);
     o.Put("three", 3);
     Assert.AreEqual("[{\"one\":1,\"two\":2,\"three\":3},{\"one\":1,\"two\":2,\"three\":3},{\"one\":1,\"two\":2,\"three\":3}]", WriteValue(new object[] { o, o, o }));
 }
 public void InitWithExtraValues()
 {
     JObject o = new JObject(new string[] { "one", "two" }, new object[] { 1, 2, 3, 4 });
     Assert.AreEqual(2, o.Count);
     Assert.AreEqual(1, o["one"]);
     IList two = (IList) o["two"];
     Assert.AreEqual(3, two.Count, "Count of values under 'two'.");
     Assert.AreEqual(2, two[0]);
     Assert.AreEqual(3, two[1]);
     Assert.AreEqual(4, two[2]);
 }
 public void InitWithNullKeys()
 {
     JObject o = new JObject(null, new object[] { 1, 2, 3, 4 });
     Assert.AreEqual(1, o.Count);
     IList values = (IList) o[""];
     Assert.AreEqual(4, values.Count, "Count of values.");
     Assert.AreEqual(1, values[0]);
     Assert.AreEqual(2, values[1]);
     Assert.AreEqual(3, values[2]);
     Assert.AreEqual(4, values[3]);
 }
 public void StartObject()
 {
     OnStart();
     _currentObject = new JObject();
 }
        private object OnEnd(object current)
        {
            Debug.Assert(current != null);

            _currentArray = null;
            _currentObject = null;

            if (Stack.Count != 0)
            {
                object o = Stack.Pop();

                //
                // Pop last object as the current object or array and
                // assert that we've poppoed one of the two as a sanity check.
                //

                _currentObject = o as JObject;

                if (_currentObject == null)
                    _currentArray = o as JArray;

                Debug.Assert(_currentObject != null || _currentArray != null);
            }

            return current;
        }
        private void OnStart()
        {
            //
            // Remember the current array or object, whichever happens to be
            // the one this instance is building right now, by pushing it on to
            // the stack.
            //

            if (_currentObject != null)            
            {
                Stack.Push(_currentObject);
                _currentObject = null;
            }
            else if (_currentArray != null)
            {
                Stack.Push(_currentArray);
                _currentArray = null;
            }

            //
            // Sanity check.
            //
 
            Debug.Assert(_currentArray == null);
            Debug.Assert(_currentObject == null);
        }