Exemple #1
0
        public void ProcessRequest(PiccoloContext context)
        {
            string payload = null;

            try
            {
                var stopRequestProcessing = _eventDispatcher.RaiseRequestProcessingEvent(context);
                if (stopRequestProcessing)
                {
                    return;
                }

                var lookupResult = _requestRouter.FindRequestHandler(context.RequestVerb, context.ApplicationPath, context.RequestUri);
                if (lookupResult.IsSuccessful)
                {
                    var requestHandler = _configuration.ObjectFactory.CreateInstance <IRequestHandler>(lookupResult.RequestHandlerType);
                    var requestPayload = _payloadDeserialiser.DeserialisePayload(requestHandler, context.RequestVerb, context.RequestPayload);
                    if (requestPayload != null)
                    {
                        string errorMessage;
                        if ((errorMessage = _payloadValidatorInvoker.ValidatePayload(lookupResult.RequestHandlerType, context.RequestVerb, requestPayload)) != null)
                        {
                            InjectResponse(context, HttpStatusCode.BadRequest, "Bad Request", null, errorMessage);
                            return;
                        }
                    }

                    var responseMessage = _requestHandlerInvoker.Execute(requestHandler, context.RequestVerb, lookupResult.RouteParameters, context.RequestQueryParameters, context.Data, requestPayload);

                    payload = SerialisePayload(responseMessage.Content);
                    InjectResponse(context, responseMessage.StatusCode, responseMessage.ReasonPhrase, responseMessage.Headers.Location, payload);
                }
                else
                {
                    var responseMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
                    InjectResponse(context, responseMessage.StatusCode, responseMessage.ReasonPhrase, null, null);
                }
            }
            catch (RouteParameterDatatypeMismatchException rpdmex)
            {
                _eventDispatcher.RaiseRequestFaultedEvent(context, rpdmex);

                var responseMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
                if (context.Http.IsDebuggingEnabled)
                {
                    payload = SerialisePayload(new ObjectContent(rpdmex));
                }

                InjectResponse(context, responseMessage.StatusCode, responseMessage.ReasonPhrase, null, payload);
            }
            catch (MalformedParameterException mpex)
            {
                _eventDispatcher.RaiseRequestFaultedEvent(context, mpex);

                var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest);
                if (context.Http.IsDebuggingEnabled)
                {
                    payload = SerialisePayload(new ObjectContent(mpex));
                }

                InjectResponse(context, responseMessage.StatusCode, responseMessage.ReasonPhrase, null, payload);
            }
            catch (MissingPayloadException mpex)
            {
                _eventDispatcher.RaiseRequestFaultedEvent(context, mpex);

                var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest);
                payload = SerialisePayload(new ObjectContent(new { error = "Payload missing" }));

                InjectResponse(context, responseMessage.StatusCode, responseMessage.ReasonPhrase, null, payload);
            }
            catch (MalformedPayloadException mpex)
            {
                _eventDispatcher.RaiseRequestFaultedEvent(context, mpex);

                if (context.Http.IsDebuggingEnabled)
                {
                    payload = SerialisePayload(new ObjectContent(mpex));
                }

                InjectResponse(context, (HttpStatusCode)422, "Unprocessable Entity", null, payload);
            }
            catch (Exception ex)
            {
                _eventDispatcher.RaiseRequestFaultedEvent(context, ex);

                var responseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                if (context.Http.IsDebuggingEnabled)
                {
                    payload = SerialisePayload(new ObjectContent(ex));
                }

                InjectResponse(context, responseMessage.StatusCode, responseMessage.ReasonPhrase, null, payload);
            }
            finally
            {
                _eventDispatcher.RaiseRequestProcessedEvent(context, payload);
            }
        }