예제 #1
0
        /// <summary>
        /// Handles requests for deferred responses.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="responder"></param>
        private void ProcessRequestForDeferredResponse(RequestReceivedEventArgs args, IResponder responder)
        {
            var jobId = args.QueryString[JobIdParameterName] ?? args.Request.FormValues[JobIdParameterName];

            if (!String.IsNullOrEmpty(jobId))
            {
                string jsonText;

                var deferredResponses = _DeferredResponses;
                DeferredMethodResponse response;
                if (deferredResponses.TryGetValue(jobId, out response))
                {
                    jsonText = response.JsonResponseText;
                }
                else
                {
                    var jsonResponse = new JsonResponse();
                    jsonResponse.Response = new DeferredExecutionResult()
                    {
                        JobId = jobId,
                    };
                    jsonText = JsonConvert.SerializeObject(jsonResponse);
                }

                responder.SendText(args.Request, args.Response, jsonText, Encoding.UTF8, MimeType.Json, cacheSeconds: 0);
            }
        }
예제 #2
0
        /// <summary>
        /// Handles a potential request for a JSON file.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="responder"></param>
        /// <returns></returns>
        public bool ProcessJsonRequest(RequestReceivedEventArgs args, IResponder responder)
        {
            var result = false;

            var mappedView = FindMappedView(args);

            if (mappedView != null)
            {
                mappedView.LastContactUtc = DateTime.UtcNow;

                var normalisedFile = NormaliseString(args.File);
                if (normalisedFile == NormaliseString(ViewHeartbeatMethod))
                {
                    result = true;
                }
                else if (normalisedFile == NormaliseString(GetDeferredResponseMethod))
                {
                    ProcessRequestForDeferredResponse(args, responder);
                    result = true;
                }
                else
                {
                    ExposedMethod exposedMethod;
                    if (mappedView.ExposedMethods.TryGetValue(normalisedFile, out exposedMethod))
                    {
                        var response = new JsonResponse();

                        try {
                            var parameters = MapParameters(exposedMethod, args);

                            if (!exposedMethod.WebAdminMethod.DeferExecution)
                            {
                                response.Response = exposedMethod.MethodInfo.Invoke(mappedView.View, parameters);
                            }
                            else
                            {
                                var deferredMethod = new DeferredMethod(args.UniqueId, mappedView, exposedMethod, parameters);
                                lock (_SyncLock) {
                                    var newMethods = CollectionHelper.ShallowCopy(_DeferredMethods);
                                    newMethods.Add(deferredMethod.RequestId, deferredMethod);
                                    _DeferredMethods = newMethods;
                                }

                                response.Response = new DeferredExecutionResult()
                                {
                                    JobId = deferredMethod.JobId,
                                };
                            }
                        } catch (Exception ex) {
                            try {
                                var log = Factory.Singleton.Resolve <ILog>().Singleton;
                                log.WriteLine("Caught exception during processing of request for {0}: {1}", args.Request.RawUrl, ex);
                            } catch {}
                            response.Exception = ex.Message;
                            response.Response  = null;
                        }

                        var jsonText = JsonConvert.SerializeObject(response);
                        responder.SendText(args.Request, args.Response, jsonText, Encoding.UTF8, MimeType.Json, cacheSeconds: 0);

                        result = true;
                    }
                }
            }

            return(result);
        }
예제 #3
0
 public void Responder_SendText_Throws_If_Passed_Null_Request()
 {
     _Responder.SendText(null, _Response.Object, "Hello", Encoding.UTF8, MimeType.Css);
 }