public static void HandleNotSuccessRequest(IRestResponse response)
        {
            JToken token = null;

            try
            {
                token = JObject.Parse(response.Content);
            }
            catch (System.Exception)
            {
                throw new ClientException <ErrorData>(null, response.StatusDescription);
            }

            var messageToken = (string)token.SelectToken("message", false);

            var exceptionType = (string)token.SelectToken("exceptionType", false);

            if (exceptionType != null)
            {
                var messageDetail = (string)token.SelectToken("messageDetail", false);
                if (exceptionType.Contains("InvalidSessionException"))
                {
                    throw new InvalidSessionException(new InvalidSessionErrorData
                    {
                        Message = messageToken, SessionGuid = Guid.Parse(messageDetail)
                    }, messageToken);
                }
            }

            var invalidState = token.SelectToken("modelState", false);

            if (invalidState != null)
            {
                var validationErrorData = new ValidationErrorData()
                {
                    Message    = messageToken,
                    ModelState = (dynamic)invalidState
                };
                throw new ClientException <ValidationErrorData>(validationErrorData, response.StatusDescription);
            }

            var exceptionMessage = (string)token.SelectToken("exceptionMessage", false);

            if (exceptionMessage != null)
            {
                var stackTrace      = (string)token.SelectToken("stackTrace", false);
                var serverErrorData = new ServerErrorData()
                {
                    Message       = exceptionMessage,
                    ExceptionType = exceptionType,
                    StackTrace    = stackTrace
                };
                throw new ClientException <ServerErrorData>(serverErrorData, response.StatusDescription);
            }


            if (messageToken != null)
            {
                var errorData = new ErrorData()
                {
                    Message = messageToken
                };
                throw new ClientException <ErrorData>(errorData, response.StatusDescription);
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage SubmitEntry()
        {
            int id;

            if (int.TryParse(HttpContext.Current.Request.Form["_id"], out id) == false)
            {
                return(ValidationErrorResponse("Could not find _id in the request data"));
            }

            var content = Umbraco.TypedContent(id);

            if (content == null)
            {
                return(ValidationErrorResponse("Could not any content with id {0}", id));
            }

            // set the correct culture for this request
            var domain = Services.DomainService.GetAssignedDomains(content.AncestorOrSelf(1).Id, true).FirstOrDefault();

            if (domain != null && string.IsNullOrEmpty(domain.LanguageIsoCode) == false)
            {
                var culture = new CultureInfo(domain.LanguageIsoCode);
                Thread.CurrentThread.CurrentCulture   = culture;
                Thread.CurrentThread.CurrentUICulture = culture;
            }

            if (Umbraco.MemberHasAccess(content.Path) == false)
            {
                return(Request.CreateUserNoAccessResponse());
            }

            var property = content.ContentType.PropertyTypes.FirstOrDefault(p => p.PropertyEditorAlias == FormModel.PropertyEditorAlias);

            if (property == null)
            {
                return(ValidationErrorResponse("Could not find any form property on content with id {0}", id));
            }

            FormModel formModel = null;

            try
            {
                formModel = content.GetPropertyValue <FormModel>(property.PropertyTypeAlias);
            }
            catch (Exception ex)
            {
                return(ValidationErrorResponse("Could not extract the form property on content with id {0}: {1}", id, ex.Message));
            }

            var result = formModel.CollectSubmittedValues(content, false);

            if (result == false)
            {
                var errorData = new ValidationErrorData
                {
                    InvalidFields     = formModel.AllValueFields().Where(f => f.Invalid).ForClientSide(),
                    FailedValidations = formModel.Validations.Where(v => v.Invalid).ForClientSide()
                };
                return(ValidationErrorResponse(errorData));
            }

            var successData = new SubmissionSuccessData();

            if (formModel.SuccessPageId <= 0)
            {
                return(SubmissionSuccessResponse(successData));
            }

            var successPage = Umbraco.TypedContent(formModel.SuccessPageId);

            if (successPage == null || !Umbraco.MemberHasAccess(successPage.Path))
            {
                return(SubmissionSuccessResponse(successData));
            }

            successData.RedirectUrl           = Umbraco.NiceUrl(formModel.SuccessPageId);
            successData.RedirectUrlWithDomain = Umbraco.NiceUrlWithDomain(formModel.SuccessPageId);
            return(SubmissionSuccessResponse(successData));
        }
Exemplo n.º 3
0
        public HttpResponseMessage SubmitEntry()
        {
            int id;
            if (int.TryParse(HttpContext.Current.Request.Form["_id"], out id) == false)
            {
                return ValidationErrorResponse("Could not find _id in the request data");
            }
            var rowId = Guid.Empty;
            Guid.TryParse(HttpContext.Current.Request.Form["_rowId"], out rowId);

            var content = Umbraco.TypedContent(id);
            if (content == null)
            {
                return ValidationErrorResponse("Could not any content with id {0}", id);
            }

            // set the correct culture for this request
            var domain = Services.DomainService.GetAssignedDomains(content.AncestorOrSelf(1).Id, true).FirstOrDefault();
            if(domain != null && string.IsNullOrEmpty(domain.LanguageIsoCode) == false)
            {
                var culture = new CultureInfo(domain.LanguageIsoCode);
                Thread.CurrentThread.CurrentCulture = culture;
                Thread.CurrentThread.CurrentUICulture = culture;
            }

            if (Umbraco.MemberHasAccess(content.Path) == false)
            {
                return Request.CreateUserNoAccessResponse();
            }

            var property = content.ContentType.PropertyTypes.FirstOrDefault(p => p.PropertyEditorAlias == FormModel.PropertyEditorAlias);
            if (property == null)
            {
                return ValidationErrorResponse("Could not find any form property on content with id {0}", id);
            }

            FormModel formModel = null;
            try
            {
                formModel = content.GetPropertyValue<FormModel>(property.PropertyTypeAlias);
            }
            catch (Exception ex)
            {
                return ValidationErrorResponse("Could not extract the form property on content with id {0}: {1}", id, ex.Message);
            }
            if(rowId != Guid.Empty)
            {
                formModel.LoadValues(content, rowId);
            }
            var result = formModel.CollectSubmittedValues(content, false);
            if (result == false)
            {
                var errorData = new ValidationErrorData
                {
                    InvalidFields = formModel.AllValueFields().Where(f => f.Invalid).ForFrontEnd(),
                    FailedValidations = formModel.Validations.Where(v => v.Invalid).ForFrontEnd()
                };
                return ValidationErrorResponse(errorData);
            }

            var successData = new SubmissionSuccessData(formModel.RowId);
            if (formModel.SuccessPageId <= 0)
            {
                return SubmissionSuccessResponse(successData);
            }

            var successPage = Umbraco.TypedContent(formModel.SuccessPageId);
            if (successPage == null || !Umbraco.MemberHasAccess(successPage.Path))
            {
                return SubmissionSuccessResponse(successData);
            }

            successData.RedirectUrl = Umbraco.NiceUrl(formModel.SuccessPageId);
            successData.RedirectUrlWithDomain = Umbraco.NiceUrlWithDomain(formModel.SuccessPageId);
            return SubmissionSuccessResponse(successData);
        }
Exemplo n.º 4
0
 private HttpResponseMessage ValidationErrorResponse(ValidationErrorData errorData)
 {
     return(Request.CreateResponse(HttpStatusCode.BadRequest, errorData, GetFormatter()));
 }
Exemplo n.º 5
0
 private HttpResponseMessage ValidationErrorResponse(ValidationErrorData errorData)
 {
     return Request.CreateResponse(HttpStatusCode.BadRequest, errorData, GetFormatter());
 }