示例#1
0
 public DirectResponse(DirectRequest request)
     : base(request)
 {
     this.TransactionId = request.TransactionId;
     this.Action = request.Action;
     this.Method = request.Method;
 }
        internal virtual void WriteResponse(DirectRequest directRequest, HttpResponseBase response)
        {
            var            method = DirectProvider.GetCurrent().GetMethod(directRequest.Action, directRequest.Method);
            DirectResponse directResponse;

            if (method.EventName != null)
            {
                directResponse = new DirectEventResponse(directRequest)
                {
                    Name     = method.EventName,
                    Data     = Data,
                    Settings = Settings
                };
            }
            else
            {
                directResponse = new DirectDataResponse(directRequest)
                {
                    Result   = Data,
                    Settings = Settings
                };
            }

            directResponse.Write(response, ContentType, ContentEncoding);
        }
        public DirectValueProvider(DirectRequest directRequest, ParameterDescriptor[] parameterDescriptors) {
            int paramCount = parameterDescriptors.Length;
            Object[] data = directRequest.Data;

            DirectMethod directMethod = DirectProvider.GetCurrent().GetMethod(directRequest.Action, directRequest.Method);
            bool usesNamedArguments = (directMethod != null && directMethod.UsesNamedArguments);

            if (paramCount > 0) {
                if (usesNamedArguments) { // named arguments. match params by name
                    var dataObj = data[0] as JObject;

                    for (int i = 0; i < paramCount; i++) {
                        string pName = parameterDescriptors[i].ParameterName;
                        Type pType = parameterDescriptors[i].ParameterType;
                        JToken value = dataObj != null ? dataObj.SelectToken(pName) : null;
                        object rawValue = null;

                        if (value != null && value.Type != JTokenType.Null && value.Type != JTokenType.Undefined) {
                            Type vType = value.GetType();
                            if (vType == typeof(JObject) && pType != typeof(JObject) ||
                                vType == typeof(JArray) && pType != typeof(JArray)) {

                                rawValue = JsonConvert.DeserializeObject(value.ToString(), pType);
                            } else {
                                rawValue = Convert.ChangeType(value.ToString(), pType);
                            }
                        }

                        string attemptedValue = Convert.ToString(rawValue, CultureInfo.InvariantCulture);
                        _values.Add(pName, new ValueProviderResult(rawValue, attemptedValue, CultureInfo.InvariantCulture));
                    }
                } else {
                    for (int i = 0; i < parameterDescriptors.Length; i++) {
                        object rawValue = data[i];

                        if (rawValue != null) {
                            Type vType = rawValue.GetType();
                            Type pType = parameterDescriptors[i].ParameterType;

                            // Deserialize only objects and arrays and let MVC handle everything else.
                            if (vType == typeof(JObject) && pType != typeof(JObject) ||
                                vType == typeof(JArray) && pType != typeof(JArray)) {

                                rawValue = JsonConvert.DeserializeObject(rawValue.ToString(), pType);
                            }
                        }

                        string attemptedValue = Convert.ToString(rawValue, CultureInfo.InvariantCulture);
                        _values.Add(parameterDescriptors[i].ParameterName,
                                    new ValueProviderResult(rawValue, attemptedValue, CultureInfo.InvariantCulture));
                    }
                }
            }
        }
        public DirectErrorResponse(DirectRequest request, Exception exception) : base(request) {
            ErrorMessage = exception.Message;
            ErrorData = exception.Data.Count > 0 ? exception.Data : null;
#if DEBUG
            Where = GetErrorLocation(exception);
#else
            if (ProviderConfiguration.GetConfiguration().Debug) {
                Where = GetErrorLocation(exception);
            }
#endif
            if (request.IsFormPost) {
                Result = new {success = false};
            }
        }
示例#5
0
        private void ExecuteRequest(RequestContext requestContext, DirectRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request", DirectResources.Common_DirectRequestIsNull);
            }

            HttpContextBase httpContext = requestContext.HttpContext;
            RouteData       routeData   = requestContext.RouteData;

            routeData.Values["controller"] = request.Action;
            routeData.Values["action"]     = request.Method;
            httpContext.Items[DirectRequest.DirectRequestKey] = request;
            var controller = (Controller)_factory.CreateController(requestContext, request.Action);

            DirectAction action = GetAction(request.Action);

            if (action == null)
            {
                throw new NullReferenceException(String.Format(DirectResources.DirectProvider_ActionNotFound, request.Action));
            }

            DirectMethod method = action.GetMethod(request.Method);

            if (method == null)
            {
                throw new NullReferenceException(String.Format(DirectResources.DirectProvider_MethodNotFound, request.Method, action.Name));
            }

            if (!method.IsFormHandler && method.Params == null)
            {
                if (request.Data == null && method.Len > 0 || request.Data != null && request.Data.Length != method.Len)
                {
                    throw new ArgumentException(String.Format(DirectResources.DirectProvider_WrongNumberOfArguments, request.Method, request.Action));
                }
            }

            try {
                controller.ActionInvoker = new DirectMethodInvoker();
                (controller as IController).Execute(requestContext);
            } catch (DirectException exception) {
                var errorResponse = new DirectErrorResponse(request, exception);
                errorResponse.Write(httpContext.Response);
            } finally {
                _factory.ReleaseController(controller);
            }

            httpContext.Items.Remove(DirectRequest.DirectRequestKey);
        }
        public DirectErrorResponse(DirectRequest request, Exception exception)
            : base(request)
        {
            this.ErrorMessage = exception.Message;
            this.ErrorData = exception.Data.Count > 0 ? exception.Data : null;

            if (DirectConfig.Debug) {
                string stackTrace = exception.StackTrace.Replace("\r\n", "<br />").Replace("at ", "*&#160;");
                this.Where = String.Format("[{0}]<br />{1}", exception.GetType(), stackTrace);
            }

            if (request.IsFormPost) {
                this.Result = new {success = false};
            }
        }
示例#7
0
        public DirectErrorResponse(DirectRequest request, Exception exception) : base(request)
        {
            ErrorMessage = exception.Message;
            ErrorData    = exception.Data.Count > 0 ? exception.Data : null;
#if DEBUG
            Where = GetErrorLocation(exception);
#else
            if (ProviderConfiguration.GetConfiguration().Debug)
            {
                Where = GetErrorLocation(exception);
            }
#endif
            if (request.IsFormPost)
            {
                Result = new { success = false };
            }
        }
        internal virtual void WriteResponse(DirectRequest directRequest, HttpResponseBase response) {
            var method = DirectProvider.GetCurrent().GetMethod(directRequest.Action, directRequest.Method);
            DirectResponse directResponse;

            if (method.EventName != null) {
                directResponse = new DirectEventResponse(directRequest) {
                    Name = method.EventName,
                    Data = Data,
                    Settings = Settings
                };
            } else {
                directResponse = new DirectDataResponse(directRequest) {
                    Result = Data,
                    Settings = Settings
                };
            }

            directResponse.Write(response, ContentType, ContentEncoding);
        }
示例#9
0
        public void Execute(RequestContext requestContext)
        {
            HttpContextBase httpContext = requestContext.HttpContext;

            _factory = ControllerBuilder.Current.GetControllerFactory();

            if (httpContext.Request.Form.Count == 0)
            {
                // Raw HTTP Post request(s)

                var    reader = new StreamReader(httpContext.Request.InputStream, new UTF8Encoding());
                string json   = reader.ReadToEnd();

                if (json.StartsWith("[") && json.EndsWith("]"))
                {
                    // Batch of requests
                    var requests = JsonConvert.DeserializeObject <List <DirectRequest> >(json);
                    httpContext.Response.Write("[");
                    foreach (DirectRequest request in requests)
                    {
                        ExecuteRequest(requestContext, request);
                        if (request != requests[requests.Count - 1])
                        {
                            httpContext.Response.Write(",");
                        }
                    }
                    httpContext.Response.Write("]");
                }
                else
                {
                    // Single request
                    var request = JsonConvert.DeserializeObject <DirectRequest>(json);
                    ExecuteRequest(requestContext, request);
                }
            }
            else
            {
                // Form Post request
                var request = new DirectRequest(httpContext.Request);
                ExecuteRequest(requestContext, request);
            }
        }
 public DirectEventResponse(DirectRequest request) : base(request) { }
示例#11
0
        private void ExecuteRequest(RequestContext requestContext, DirectRequest request)
        {
            HttpContextBase httpContext = requestContext.HttpContext;
            RouteData routeData = requestContext.RouteData;

            routeData.Values["controller"] = request.Action;
            routeData.Values["action"] = request.Method;
            httpContext.Items[DirectRequest.DirectRequestKey] = request;
            var controller = (Controller)_factory.CreateController(requestContext, request.Action);

            DirectAction action = this.GetAction(request.Action);
            if (action == null) {
                throw new NullReferenceException(String.Format(DirectResources.DirectProvider_ActionNotFound, request.Action));
            }

            DirectMethod method = action.GetMethod(request.Method);
            if (method == null) {
                throw new NullReferenceException(String.Format(DirectResources.DirectProvider_MethodNotFound, request.Method, action.Name));
            }

            if (!method.IsFormHandler) {
                if (request.Data == null && method.Len > 0 || request.Data != null && request.Data.Length != method.Len) {
                    throw new ArgumentException(DirectResources.DirectProvider_WrongNumberOfArguments);
                }
            }

            try {
                controller.ActionInvoker = new DirectMethodInvoker();
                (controller as IController).Execute(requestContext);
            } catch (DirectException exception) {
                var errorResponse = new DirectErrorResponse(request, exception);
                errorResponse.Write(httpContext.Response);
            } finally {
                _factory.ReleaseController(controller);
            }

            httpContext.Items.Remove(DirectRequest.DirectRequestKey);
        }
示例#12
0
        public void Execute(RequestContext requestContext)
        {
            HttpContextBase httpContext = requestContext.HttpContext;
            _factory = ControllerBuilder.Current.GetControllerFactory();

            if (httpContext.Request.Form.Count == 0) {
                // Raw HTTP Post request(s)

                var reader = new StreamReader(httpContext.Request.InputStream, new UTF8Encoding());
                var json = reader.ReadToEnd();

                if (json.StartsWith("[") && json.EndsWith("]")) {
                    // Batch of requests
                    var requests = JsonConvert.DeserializeObject<List<DirectRequest>>(json);
                    httpContext.Response.Write("[");
                    foreach (var request in requests) {
                        ExecuteRequest(requestContext, request);
                        if (request != requests[requests.Count - 1]) {
                            httpContext.Response.Write(",");
                        }
                    }
                    httpContext.Response.Write("]");
                } else {
                    // Single request
                    var request = JsonConvert.DeserializeObject<DirectRequest>(json);
                    ExecuteRequest(requestContext, request);
                }
            } else {
                // Form Post request

                var request = new DirectRequest(httpContext.Request);
                ExecuteRequest(requestContext, request);
            }
        }
        public DirectValueProvider(DirectRequest directRequest, ParameterDescriptor[] parameterDescriptors)
        {
            int paramCount = parameterDescriptors.Length;

            Object[] data = directRequest.Data;

            DirectMethod directMethod       = DirectProvider.GetCurrent().GetMethod(directRequest.Action, directRequest.Method);
            bool         usesNamedArguments = (directMethod != null && directMethod.UsesNamedArguments);

            if (paramCount > 0)
            {
                if (usesNamedArguments)   // named arguments. match params by name
                {
                    var dataObj = data[0] as JObject;

                    for (int i = 0; i < paramCount; i++)
                    {
                        string pName = parameterDescriptors[i].ParameterName;
                        Type   pType = parameterDescriptors[i].ParameterType;
                        JToken value = dataObj != null?dataObj.SelectToken(pName) : null;

                        object rawValue = null;

                        if (value != null && value.Type != JTokenType.Null && value.Type != JTokenType.Undefined)
                        {
                            Type vType = value.GetType();
                            if (vType == typeof(JObject) && pType != typeof(JObject) ||
                                vType == typeof(JArray) && pType != typeof(JArray))
                            {
                                rawValue = JsonConvert.DeserializeObject(value.ToString(), pType);
                            }
                            else
                            {
                                rawValue = Convert.ChangeType(value.ToString(), pType);
                            }
                        }

                        string attemptedValue = Convert.ToString(rawValue, CultureInfo.InvariantCulture);
                        _values.Add(pName, new ValueProviderResult(rawValue, attemptedValue, CultureInfo.InvariantCulture));
                    }
                }
                else
                {
                    for (int i = 0; i < parameterDescriptors.Length; i++)
                    {
                        object rawValue = data[i];

                        if (rawValue != null)
                        {
                            Type vType = rawValue.GetType();
                            Type pType = parameterDescriptors[i].ParameterType;

                            // Deserialize only objects and arrays and let MVC handle everything else.
                            if (vType == typeof(JObject) && pType != typeof(JObject) ||
                                vType == typeof(JArray) && pType != typeof(JArray))
                            {
                                rawValue = JsonConvert.DeserializeObject(rawValue.ToString(), pType);
                            }
                        }

                        string attemptedValue = Convert.ToString(rawValue, CultureInfo.InvariantCulture);
                        _values.Add(parameterDescriptors[i].ParameterName,
                                    new ValueProviderResult(rawValue, attemptedValue, CultureInfo.InvariantCulture));
                    }
                }
            }
        }
示例#14
0
 protected DirectResponse(DirectRequest request) {
     IsFileUpload = request.IsFileUpload;
 }
 public DirectDataResponse(DirectRequest request) : base(request) {
     TransactionId = request.TransactionId;
     Action = request.Action;
     Method = request.Method;
 }
示例#16
0
 public DirectEventResponse(DirectRequest request) : base(request)
 {
 }
 public DirectDataResponse(DirectRequest request) : base(request)
 {
     TransactionId = request.TransactionId;
     Action        = request.Action;
     Method        = request.Method;
 }