Пример #1
0
 private static IHttpResponse ListPackages(IHttpContext context, RouteMatchData routeMatchData)
 {
     return(LiteralHttpResponse.FormatHttpResponse(
                HttpStatusCode.OK,
                HttpConstants.ContentTypeText,
                "list"));
 }
Пример #2
0
 public void Initialize(
     IHttpContext httpContext,
     RouteMatchData routeMatchData)
 {
     this.httpContext    = httpContext;
     this.routeMatchData = routeMatchData;
 }
Пример #3
0
        public IHttpResponse Handle(IHttpContext context, RouteMatchData routeMatchData)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat(
                    "Calling controller+method '{0}.{1}'",
                    typeof(TController).FullName,
                    handlerFunc.Method.Name);
            }

            TController controller = controllerRegistry.FetchController <TController>();

            try
            {
                controller.Initialize(context, routeMatchData);
                return(handlerFunc(controller));
            }
            finally
            {
                if (controller != null)
                {
                    controllerRegistry.ReleaseController(controller);
                }
            }
        }
Пример #4
0
 private static IHttpResponse GetPackage(IHttpContext context, RouteMatchData routeMatchData)
 {
     return(LiteralHttpResponse.FormatHttpResponse(
                HttpStatusCode.OK,
                HttpConstants.ContentTypeText,
                "get{0}",
                routeMatchData.GetParameter("id")));
 }
Пример #5
0
        public void RouteDoesNotMatch()
        {
            Route route = new Route("product/(?<productId>[^//]+)/part/(?<partId>[^//]+)$");

            DummyHttpContext context = new DummyHttpContext(
                "http://google.com/service/",
                "/service/product/10/part");
            RouteMatchData matchData = route.Match(context);

            Assert.IsNull(matchData);
        }
Пример #6
0
        public void RouteIsMatch(string queryString)
        {
            Route route = new Route("product/(?<productId>[^//]+)/part/(?<partId>[^//]+)$");

            DummyHttpContext context = new DummyHttpContext(
                "http://google.com/service",
                "/product/10/part/20",
                queryString);
            RouteMatchData matchData = route.Match(context);

            Assert.IsNotNull(matchData);
            Assert.AreEqual("10", matchData.GetParameter("productId"));
            Assert.AreEqual("20", matchData.GetParameter("partId"));
        }
Пример #7
0
        public IHttpResponse RouteRestRequest(IHttpContext context)
        {
            foreach (KeyValuePair <Route, IRouteHandler> pair in routes)
            {
                RouteMatchData matchData = pair.Key.Match(context);
                if (matchData == null)
                {
                    continue;
                }

                return(pair.Value.Handle(context, matchData));
            }

            throw new RestBadRequestException(new Uri(context.RequestUrl), null, null);
        }
Пример #8
0
        public IHttpResponse Handle(IHttpContext context, RouteMatchData routeMatchData)
        {
            string serviceName = typeof(TService).FullName;

            if (log.IsDebugEnabled)
            {
                log.DebugFormat(
                    "Calling service+method '{0}.{1}'",
                    serviceName,
                    handlerFunc.Method.Name);
            }

            TService service;

            try
            {
                service = servicesRegistry.FetchService <TService>();
            }
            catch (Exception ex)
            {
                log.FatalFormat("Cannot instantiate REST service '{0}'. Reason: {1}", serviceName, ex);
                throw;
            }

            try
            {
                service.Initialize(context, routeMatchData);
                return(handlerFunc(service));
            }
            catch (Exception ex)
            {
                log.ErrorFormat("REST service '{0}' failed. Reason: {1}", serviceName, ex);
                throw;
            }
            finally
            {
                if (service != null)
                {
                    servicesRegistry.ReleaseService(service);
                }
            }
        }