Exemple #1
0
        public void ProcessRequest(PiccoloContext context)
        {
            try
            {
                _eventDispatcher.RaiseRequestProcessingEvent(context);

                var lookupResult = _requestRouter.FindRequestHandler(context.RequestVerb, context.RequestUri);
                if (lookupResult.IsSuccessful)
                {
                    var requestHandler      = _configuration.ObjectFactory.CreateInstance <IRequestHandler>(lookupResult.RequestHandlerType);
                    var httpResponseMessage = _requestHandlerInvoker.Execute(requestHandler, context.RequestVerb, lookupResult.RouteParameters, context.RequestQueryParameters, context.RequestPayload);
                    InjectResponse(context, httpResponseMessage);
                }
                else
                {
                    InjectResponse(context, new HttpResponseMessage(HttpStatusCode.NotFound));
                }
            }
            catch (Exception ex)
            {
                _eventDispatcher.RaiseRequestFaultedEvent(context, ex);
                InjectResponse(context, new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }
            finally
            {
                _eventDispatcher.RaiseRequestProcessedEvent(context);
            }
        }
Exemple #2
0
        private void InjectResponse(PiccoloContext context, HttpResponseMessage responseMessage)
        {
            context.Http.Response.StatusCode        = (int)responseMessage.StatusCode;
            context.Http.Response.StatusDescription = responseMessage.ReasonPhrase;

            if (responseMessage.HasContent())
            {
                context.Http.Response.ContentType = "application/json";
                context.Http.Response.Write(SerialisePayload(responseMessage));
            }
        }
Exemple #3
0
        public void ProcessRequest(PiccoloContext context)
        {
            var lookupResult = _requestRouter.FindRequestHandler(context.RequestVerb, context.RequestUri);

            if (lookupResult.IsSuccessful)
            {
                var requestHandler      = _configuration.RequestHandlerFactory.CreateInstance(lookupResult.RequestHandlerType);
                var httpResponseMessage = _requestHandlerInvoker.Execute(requestHandler, context.RequestVerb, lookupResult.RouteParameters, context.RequestQueryParameters, context.RequestPayload);
                InjectResponse(context, httpResponseMessage);
            }
            else
            {
                InjectResponse(context, new HttpResponseMessage(HttpStatusCode.NotFound));
            }
        }
Exemple #4
0
        private static void InjectResponse(PiccoloContext context, HttpStatusCode statusCode, string reasonPhrase, Uri createdResourceLocation, string payload)
        {
            context.Http.Response.StatusCode        = (int)statusCode;
            context.Http.Response.StatusDescription = reasonPhrase;

            if (createdResourceLocation != null)
            {
                context.Http.Response.AddHeader("Location", createdResourceLocation.ToString());
            }

            if (payload != null)
            {
                context.Http.Response.ContentType = "application/json";
                context.Http.Response.Write(payload);
            }
        }
Exemple #5
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);
            }
        }