Exemple #1
0
        private void WriteErrorResponse(HttpApplication application, HttpStatusCode status, string reason)
        {
            HttpContext context = application.Context;

            context.Response.ContentType     = "application/json";
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.StatusCode      = (int)status;
            string response = GOServerException.FormatJsonResponseString((int)status, reason, reason);              // Setting message = reason partly for backwards compatibility, mainly. Probably more correct to send empty message string here.

            context.Response.Write(response);
            application.CompleteRequest();
        }
Exemple #2
0
        public void ThrowAccessDenied(GOServerException exception)
        {
            // If is an anonymous caller and the current user token has expired, package the authorization exception inside a expiredSecurityToken exception
            // makes for a more meaningful response to caller
            var claims = GetCurrentUserClaims();

            if (claims != null &&
                claims.IsAnonymousClaim &&
                IsCurrentUserTokenExpired)
            {
                throw new GOServerException("expiredSecurityToken", "expiredSecurityToken", exception, (int)HttpStatusCode.Forbidden);
            }
            else
            {
                throw exception;
            }
        }
Exemple #3
0
        public virtual GOUserDataObject Save(
            GOUserContainer theDataObjectToSave,
            LambdaExpression securityFilterExpression = null,
            List <string> includes = null,
            bool skipSecurity      = false,
            Parameters parameters  = null)
        {
            var uri     = new Uri(_serviceUrl);
            var request = WebRequest.Create(uri);

            request.ContentType = "application/x-www-form-urlencoded";
            request.Method      = "POST";
            request.Timeout     = GetRequestTimeout(parameters);

            using (var stream = request.GetRequestStream())
            {
                string objectToSaveAsString = JsonConvert.SerializeObject(theDataObjectToSave, JsonSerializerSettings);

                var byteArray = Encoding.UTF8.GetBytes("entity=" + HttpUtility.UrlEncode(objectToSaveAsString));
                stream.Write(byteArray, 0, byteArray.Length);

                if (includes != null && includes.Any())
                {
                    byteArray = Encoding.UTF8.GetBytes("&include=" + HttpUtility.UrlEncode(String.Join(",", includes)));
                    stream.Write(byteArray, 0, byteArray.Length);
                }

                // get user token for currently authenticated user if any
                var userToken = UserIdentity.UserToken;

                if (!String.IsNullOrEmpty(userToken))
                {
                    byteArray = Encoding.UTF8.GetBytes("&_user_token=" + userToken);
                    stream.Write(byteArray, 0, byteArray.Length);
                }

                // Add custom query params
                WriteQueryParams(stream);

                try
                {
                    using (var response = request.GetResponse())
                    {
                        if (response == null)
                        {
                            throw new PulpException("Unable to get the response from " + uri.ToString());
                        }

                        using (var responseStream = response.GetResponseStream())
                        {
                            if (responseStream == null)
                            {
                                throw new PulpException("Unable to get the response stream from " + uri.ToString());
                            }

                            var sr        = new StreamReader(responseStream);
                            var container = JsonConvert.DeserializeObject <GOUserContainer>(sr.ReadToEnd(), JsonSerializerSettings);

                            if (container == null)
                            {
                                if (theDataObjectToSave.ExtractGOUser().IsMarkedForDeletion)
                                {
                                    return(null);
                                }
                                else
                                {
                                    throw new GOServerException("No GOUser in the response to SDK DataProvider Save(). Not expecting a null response here!");
                                }
                            }
                            else
                            {
                                container.ObjectsDataSet.EnsureInitialized();
                                container.ObjectsDataSet.ReconstructIndexes();

                                response.Close();
                                return(container.ExtractGOUser());
                            }
                        }
                    }
                }
                catch (WebException we)
                {
                    // See if we can decode GOServerException
                    var error = GOServerException.FromWebResponse(we);

                    if (error != null)
                    {
                        throw error;
                    }

                    throw;
                }
            }
        }
Exemple #4
0
        public virtual void Delete(
            GOUserDataObject theDataObjectToDelete,
            LambdaExpression securityFilterExpression = null,
            IObjectsDataSet context = null,
            Parameters parameters   = null,
            bool skipSecurity       = false /* skipSecurity has no effect here */)
        {
            // FC Anyone know why the append .json string? Seems incompatible with EntityApiHandler.ProceeDelete (so I'm removing it)
            // var uri = new Uri(_serviceUrl + "DeleteGOUser.json");
            var uri = new Uri(_serviceUrl);

            // Honour dry-run
            bool isDryRun = parameters != null && parameters.ContainsKey(ParameterKeys.DryDelete) && (bool)parameters[ParameterKeys.DryDelete] == true;

            if (isDryRun)
            {
                uri = new Uri(_serviceUrl + "?" + ApiRequest.RequestParameter.DryDelete + "=true");
            }

            // custom query params
            uri = AppendQueryParams(uri);

            var request = WebRequest.Create(uri);

            request.ContentType = "application/x-www-form-urlencoded";
            request.Method      = "DELETE";
            request.Timeout     = GetRequestTimeout(parameters);


            using (var stream = request.GetRequestStream())
            {
                string objectToDeleteAsString = JsonConvert.SerializeObject(new GOUserContainer(theDataObjectToDelete), JsonSerializerSettings);

                var byteArray = Encoding.UTF8.GetBytes("entity=" + HttpUtility.UrlEncode(objectToDeleteAsString));
                stream.Write(byteArray, 0, byteArray.Length);

                // get user token for currently authenticated user if any
                var userToken = UserIdentity.UserToken;

                if (!String.IsNullOrEmpty(userToken))
                {
                    byteArray = Encoding.UTF8.GetBytes("&_user_token=" + userToken);
                    stream.Write(byteArray, 0, byteArray.Length);
                }

                try
                {
                    using (var response = request.GetResponse())
                    {
                        if (response == null)
                        {
                            throw new PulpException("Unable to get the response from " + uri.ToString());
                        }

                        using (var responseStream = response.GetResponseStream())
                        {
                            if (responseStream == null)
                            {
                                throw new PulpException("Unable to get the response stream from " + uri.ToString());
                            }

                            // if this is a dry-run request, read the response and return to caller via the parameters
                            if (isDryRun)
                            {
                                var encoding = Encoding.UTF8;
                                using (var reader = new StreamReader(responseStream, encoding))
                                {
                                    parameters[ParameterKeys.DeleteStackJsonEncoded] = reader.ReadToEnd();
                                }
                            }
                        }

                        response.Close();
                    }
                }
                catch (WebException we)
                {
                    // See if we can decode GOServerException
                    var error = GOServerException.FromWebResponse(we);

                    if (error != null)
                    {
                        throw error;
                    }

                    throw;
                }
            }
        }
Exemple #5
0
        public virtual DataObjectCollection <GOUserDataObject> GetCollection(
            LambdaExpression securityFilterExpression = null,
            string filterPredicate   = null,
            object[] filterArguments = null,
            string orderByPredicate  = null,
            int pageNumber           = 0,
            int pageSize             = 0,
            List <string> includes   = null,
            IObjectsDataSet context  = null,
            Parameters parameters    = null,
            bool skipSecurity        = false /* skipSecurity has no effect here */)
        {
            var pFilter = filterPredicate;

            if (filterArguments != null)
            {
                for (var i = 0; i < filterArguments.Length; i++)
                {
                    var toReplace = (filterArguments[i] is string || filterArguments[i] is Guid) ? "\"" + filterArguments[i].ToString() + "\"" : filterArguments[i].ToString();
                    pFilter = pFilter.Replace("@" + i, toReplace);
                }
            }
            pFilter = !String.IsNullOrEmpty(filterPredicate) ? "&filter=" + HttpUtility.UrlEncode(pFilter) : "";

            var pPath = (includes != null && includes.Any()) ? "&include=" + HttpUtility.UrlEncode(String.Join(",", includes)) : "";

            string orderBy = null;

            if (!String.IsNullOrEmpty(orderByPredicate))
            {
                string[] toks = orderByPredicate.Split(new char[] { ' ' });

                if (toks.Length > 2)
                {
                    throw new GOServerException("OrderBy predicate, expected format is '{sortColumnName} {sortOrder}'");
                }

                orderBy = $"&sortColumn={toks[0]}{(toks.Length > 1 ? ($"&sortOrder={toks[1]}") : "")}";
            }

            var pPageNumber = "?pageNumber=" + pageNumber;
            var pPageSize   = pageSize > 0 ? "&pageSize=" + pageSize : "";

            // get user token for currently authenticated user if any
            var    userToken  = UserIdentity.UserToken;
            string pUserToken = null;

            if (!String.IsNullOrEmpty(userToken))
            {
                pUserToken = "&_user_token=" + userToken;
            }

            var uriparameters = $"{pPageNumber}{pPageSize}{orderBy}{pFilter}{pPath}{pUserToken}";

            var uri = new Uri(String.Concat(_serviceUrl, "list", uriparameters));

            // custom query params
            uri = AppendQueryParams(uri);

            var request = WebRequest.Create(uri);

            request.ContentType = "application/x-www-form-urlencoded";
            request.Timeout     = GetRequestTimeout(parameters);

            try
            {
                using (var response = request.GetResponse())
                {
                    if (response == null)
                    {
                        throw new PulpException("Unable to get the response from " + uri.ToString());
                    }

                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            throw new PulpException("Unable to get the response stream from " + uri.ToString());
                        }


                        using (var sr = new StreamReader(responseStream))
                        {
                            var container = JsonConvert.DeserializeObject <GOUserCollectionContainer>(sr.ReadToEnd(), JsonSerializerSettings);
                            container.ObjectsDataSet.EnsureInitialized();
                            container.ObjectsDataSet.ReconstructIndexes();

                            response.Close();
                            return(container.ExtractGOUserItems());
                        }
                    }
                }
            }
            catch (WebException we)
            {
                // See if we can decode GOServerException
                var error = GOServerException.FromWebResponse(we);

                if (error != null)
                {
                    throw error;
                }

                throw;
            }
        }
Exemple #6
0
        public virtual GOUserDataObject Get(
            GOUserDataObject theDataObjectToGet,
            LambdaExpression securityFilterExpression = null,
            List <string> includes  = null,
            IObjectsDataSet context = null,
            Parameters parameters   = null,
            bool skipSecurity       = false /* skipSecurity has no effect here */)
        {
            var pPath  = (includes != null && includes.Any()) ? "?include=" + HttpUtility.UrlEncode(String.Join(",", includes)) : "";
            var pksUrl = "";

            pksUrl += Uri.EscapeDataString(theDataObjectToGet.Id.ToString()) + "/";
            pksUrl.TrimEnd('/');

            // get user token for currently authenticated user if any
            var userToken = UserIdentity.UserToken;

            if (!String.IsNullOrEmpty(userToken))
            {
                pPath  = !String.IsNullOrEmpty(pPath) ? pPath + "&_user_token=" : "?_user_token=";
                pPath += userToken;
            }

            var uri = new Uri(String.Concat(_serviceUrl, "byid/", pksUrl, pPath));

            // custom query params
            uri = AppendQueryParams(uri);

            var request = WebRequest.Create(uri);

            request.ContentType = "application/x-www-form-urlencoded";
            request.Timeout     = GetRequestTimeout(parameters);

            try
            {
                using (var response = request.GetResponse())
                {
                    if (response == null)
                    {
                        throw new PulpException("Unable to get the response from " + uri.ToString());
                    }

                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            throw new PulpException("Unable to get the response stream from " + uri.ToString());
                        }

                        using (var sr = new StreamReader(responseStream))
                        {
                            var container = JsonConvert.DeserializeObject <GOUserContainer>(sr.ReadToEnd(), JsonSerializerSettings);

                            container.ObjectsDataSet.EnsureInitialized();
                            container.ObjectsDataSet.ReconstructIndexes();

                            response.Close();
                            return(container.ExtractGOUser());
                        }
                    }
                }
            }
            catch (WebException we)
            {
                // See if we can decode GOServerException
                var error = GOServerException.FromWebResponse(we);

                if (error != null)
                {
                    throw error;
                }

                throw;
            }
        }