public void Remove(IRoutingContext routingContext) { lock (updateLock) { var clone = new List<IRoutingContext>(container); if (clone.Remove(routingContext)) { container = clone; } } }
public bool TryGetRoutingContext(Guid peerId, out IRoutingContext context) { CopyOnModifyRoutingContextContainer routingContextContainer; if (routingContextByPeerId.TryGetValue(peerId, out routingContextContainer)) { context = routingContextContainer.TakeRandomOrNull(); return context != null; } context = null; return false; }
public void Add(IRoutingContext routingContext) { lock (updateLock) { var clone = new List<IRoutingContext>(container); if (!clone.Contains(routingContext)) { clone.Add(routingContext); } container = clone; } }
TimeToBeReceived(IRoutingContext context) { #region SetDeliveryConstraintDiscardIfNotReceivedBefore TimeSpan timeToBeReceived = TimeSpan.FromSeconds(25); context.Extensions.AddDeliveryConstraint(new DiscardIfNotReceivedBefore(timeToBeReceived)); #endregion #region ReadDeliveryConstraintDiscardIfNotReceivedBefore DiscardIfNotReceivedBefore constraint; context.Extensions.TryGetDeliveryConstraint(out constraint); timeToBeReceived = constraint.MaxTime; #endregion }
Recoverable(IRoutingContext context) { #region SetDeliveryConstraintNonDurable context.Extensions.AddDeliveryConstraint(new NonDurableDelivery()); #endregion #region ReadDeliveryConstraintNonDurable context.Extensions.TryGetDeliveryConstraint(out NonDurableDelivery constraint); #endregion }
public IHttpResponse Handle(IHttpContext httpContext) { try { string loginPath = "/login"; string registerPath = "/register"; //if (httpContext.Request.Path != loginPath // && httpContext.Request.Path != registerPath // && !httpContext.Request.Session.Containts(CurrentUserSessionKey)) //{ // return new RedirectResponse(loginPath); //} HttpRequestMethod requestMethod = httpContext.Request.RequestMethod; string requestPath = httpContext.Request.Path; var registeredRoutes = this.serverRouteConfig.Routes[requestMethod]; foreach (var registeredRoute in registeredRoutes) { string routePattern = registeredRoute.Key; IRoutingContext routingContext = registeredRoute.Value; Regex regex = new Regex(routePattern); Match match = regex.Match(requestPath); if (!match.Success) { continue; } var parameters = routingContext.Parameters; foreach (string parameter in parameters) { string parameterValue = match.Groups[parameter].Value; httpContext.Request.AddUrlParameter(parameter, parameterValue); } return(routingContext.RequestHandler.Handle(httpContext)); } } catch (Exception ex) { return(new InternalServerErrorResponse(ex)); } return(new NotFoundResponse()); }
Recoverable(IRoutingContext context) { #region SetDeliveryConstraintNonDurable context.Extensions.AddDeliveryConstraint(new NonDurableDelivery()); #endregion #region ReadDeliveryConstraintNonDurable NonDurableDelivery constraint; context.Extensions.TryGetDeliveryConstraint(out constraint); #endregion }
public TimeToBeReceived() { IRoutingContext context = null; #region SetDeliveryConstraintDiscardIfNotReceivedBefore TimeSpan timeToBeReceived = TimeSpan.FromSeconds(25); context.AddDeliveryConstraint(new DiscardIfNotReceivedBefore(timeToBeReceived)); #endregion #region ReadDeliveryConstraintDiscardIfNotReceivedBefore DiscardIfNotReceivedBefore constraint; context.TryGetDeliveryConstraint(out constraint); timeToBeReceived = constraint.MaxTime; #endregion }
public IHttpResponse Handle(IHttpContext context) { try { // Check if user is authenticated ICollection <string> anonymousPaths = this.serverRouteConfig.AnonymousPaths; //if (!anonymousPaths.Contains(context.Request.Path) && // (context.Request.Session == null || !context.Request.Session.Contains(SessionStore.CurrentUserKey))) //{ // return new RedirectResponse(anonymousPaths.First()); //} HttpRequestMethod requestMethod = context.Request.Method; string requestPath = context.Request.Path; IDictionary <string, IRoutingContext> registeredRoutes = this.serverRouteConfig.Routes[requestMethod]; foreach (var registeredRoute in registeredRoutes) { string routePattern = registeredRoute.Key; IRoutingContext routingContext = registeredRoute.Value; Regex routeRegex = new Regex(routePattern); Match match = routeRegex.Match(requestPath); if (!match.Success) { continue; } IEnumerable <string> parameters = routingContext.Parameters; foreach (string parameter in parameters) { string parameterValue = match.Groups[parameter].Value; context.Request.AddUrlParameter(parameter, parameterValue); } return(routingContext.Handler.Handle(context)); } } catch (Exception ex) { return(new InternalServerErrorResponse(ex)); } return(new NotFoundResponse()); }
public IObservable <IUrlResponse> GetResponse(IRoutingContext context) { Guard.ArgumentNotNull(context, "context"); Guard.ArgumentIsType(context.Request, typeof(ControllerActionRequest), "context.Request"); var _relayObservable = new LazyRelayObservable <IUrlResponse>((s) => { var _request = (ControllerActionRequest)context.Request; var _controller = (IController)TypeBuilder.BuildType(_controllerType); var _response = new ControllerContext(_request, ResponseStatus.Success, _controller, context.ParsedParameters); s.OnNext(_response); s.OnCompleted(); }); return(_relayObservable); }
public IHttpResponse Handle(IHttpRequest httpRequest) { string requestPath = httpRequest.Path; bool isSignInRequest = requestPath == Paths.logIn; bool loggedIn = AuthenticationCheck(httpRequest); if (!isSignInRequest && !loggedIn) { return(new RedirectResponse(Paths.logIn)); } var requestMethod = httpRequest.RequestMethod; var registeredRoutes = this.serverRouteConfig.Routes[requestMethod]; foreach (var registeredRoute in registeredRoutes) { string routePattern = registeredRoute.Key; IRoutingContext routingContext = registeredRoute.Value; Regex routeRegex = new Regex(routePattern); Match match = routeRegex.Match(requestPath); if (!match.Success) { continue; } var parameters = routingContext.Parameters; foreach (string parameter in parameters) { string parameterValue = match.Groups[parameter].Value; httpRequest.AddUrlParameter(parameter, parameterValue); } IHttpResponse response = routingContext.RequestHandler.Handle(httpRequest); return(response); } return(new NotFoundResponse()); }
public IObservable <IUrlResponse> GetResponse(IRoutingContext context) { // we get the route handler from the resource locator IRouteHandler _routeHandler = null; if (!ResourceLocator.TryGetResource <IRouteHandler>(_url, out _routeHandler)) { // we return that the handler was not found var _observer = new LazyRelayObservable <IUrlResponse>((s) => { s.OnNext(new UrlResponse(context.Request, ResponseStatus.HandlerNotFound, null, null)); s.OnCompleted(); }); return(_observer); } // we we resolve return(_routeHandler.GetResponse(context)); }
public IHttpResponse Handle(IHttpContext httpContext) { HttpRequestMethod requestMethod = httpContext.Request.Method; string requestPath = httpContext.Request.Path; var registeredRoutes = this.serverRouteConfig.Routes[requestMethod]; foreach (var registeredRoute in registeredRoutes) { // will return ^/users/(?<name>[a-z]+)$ string routePattern = registeredRoute.Key; IRoutingContext routingContext = registeredRoute.Value; Regex routeRegex = new Regex(routePattern); Match match = routeRegex.Match(requestPath); if (!match.Success) { continue; } // ^/users/(?<name>[a-z]+)$ -> name var parameters = routingContext.Parameters; // extract value for <name> foreach (string parameter in parameters) { // if we have named group in the regex string parameterValue = match.Groups[parameter].Value; httpContext.Request.AddUrlParameter(parameter, parameterValue); } return(routingContext.Handler.Handle(httpContext)); } return(new ViewResponse(HttpStatusCode.NotFound, new PageNotFoundView())); //return new NotFoundResponse(); }
public IHttpResponse Handle(IHttpContext httpContext) { var anonymousPaths = new[] { "/login", "/register" }; if (!anonymousPaths.Contains(httpContext.Request.Path) && (httpContext.Request.Session == null || !httpContext.Request.Session.IsAuthenticated())) { return(new RedirectResponse(anonymousPaths.First())); } var routesContext = this.serverRouteConfig.Routes[httpContext.Request.Method]; string path = httpContext.Request.Path; foreach (var route in routesContext) { string pattern = route.Key; IRoutingContext routingContext = route.Value; Regex regex = new Regex(pattern); Match match = regex.Match(path); if (!match.Success) { continue; } foreach (var parameter in routingContext.Parameters) { httpContext.Request.AddUrlParameter(parameter, match.Groups[parameter].Value); } return(routingContext.RequestHandler.Handle(httpContext)); } return(new ViewResponse(HttpStatusCode.NotFound, new NotFoundView())); }
protected RoutingRepository(RoutingDatabaseContext context) : base(context) { Context = context; }
public IHttpResponse Handle(IHttpContext context) { try { // Check if user is authenticated //string[] anonymousPaths = new[] { "/login", "/register" }; //if (context.Request.Path == null || // (!anonymousPaths.Contains(context.Request.Path) && // !context.Request.Session.Contains(SessionStore.CurrentUserKey))) //{ // return new RedirectResponse(anonymousPaths.First()); //} string[] anonymousPaths = new[] { "/login", "/register" }; if (!anonymousPaths.Contains(context.Request.Path) && (context.Request.Session == null || !context.Request.Session.Contains(SessionStore.CurrentUserKey))) { return(new RedirectResponse(anonymousPaths.First())); } HttpRequestMethod requestMethod = context.Request.Method; string requestPath = context.Request.Path; var registeredRoutes = this.serverRouteConfig.Routes[requestMethod]; foreach (var registeredRoute in registeredRoutes) { // will return ^/users/(?<name>[a-z]+)$ string routePattern = registeredRoute.Key; IRoutingContext routingContext = registeredRoute.Value; Regex routeRegex = new Regex(routePattern); Match match = routeRegex.Match(requestPath); if (!match.Success) { continue; } // ^/users/(?<name>[a-z]+)$ -> name var parameters = routingContext.Parameters; // extract value for <name> foreach (string parameter in parameters) { // if we have named group in the regex string parameterValue = match.Groups[parameter].Value; context.Request.AddUrlParameter(parameter, parameterValue); } return(routingContext.Handler.Handle(context)); } } catch (Exception ex) { return(new InternalServerErrorResponse(ex)); } return(new NotFoundResponse()); }
private static ResponseStatus OnResolveRoute(IUrlRequest request, bool throwException, out IRoutingContext context) { Guard.ArgumentNotNull(request, "request"); Guard.ArgumentValue((!ValidateUrl(request.RequestUrl, false)), "request", INVALID_REQUEST_URL); // default value context = null; // we check the url if (!ValidateUrl(request.RequestUrl, throwException)) { return(ResponseStatus.UrlInvalid); } // we get the route data first RouteData routeData = RouteTable.Routes.GetRouteData(request); if (routeData == null) { if (throwException) { throw new InvalidOperationException(NO_ROUTE_MATCHES); } return(ResponseStatus.UrlNotFound); } // we get the route handler IRouteHandler routeHandler = routeData.RouteHandler; if (routeHandler == null) { if (throwException) { throw new InvalidOperationException(NO_ROUTE_HANDLER); } return(ResponseStatus.HandlerNotFound); } // we merge the dictionaries (into the request parameters) var _requestParameters = new ParametersCollection(); // we add the data-tokens specified with route, n_ote these can be overriden if (routeData.DataTokens != null && routeData.DataTokens.Count > 0) { // we add the values, from the routes data, if it exists then we update the value foreach (var _parameter in routeData.DataTokens) { _requestParameters.Add(_parameter.Key, _parameter.Value); } } // we add the values found in the url merged with the default values, n_ote these can be overriden if (routeData.Values != null && routeData.Values.Count > 0) { // we add the values, from the routes data, if it exists then we update the value foreach (var _parameter in routeData.Values) { if (_requestParameters.ContainsKey(_parameter.Key)) { _requestParameters[_parameter.Key] = _parameter.Value; } else { _requestParameters.Add(_parameter.Key, _parameter.Value); } } } // and our passed in parameters will override any existing entry if (request.RequestParameters != null && request.RequestParameters.Count > 0) { foreach (var _parameter in request.RequestParameters) { if (_requestParameters.ContainsKey(_parameter.Key)) { _requestParameters[_parameter.Key] = _parameter.Value; } else { _requestParameters.Add(_parameter.Key, _parameter.Value); } } } // we setup the request cotext and get the response context = new RoutingContext(request, routeData, _requestParameters); return(ResponseStatus.Success); }
/// <summary> /// Adds a <see cref="DeliveryConstraint"/> to a <see cref="IRoutingContext"/>. /// </summary> public static void AddDeliveryConstraint(this IRoutingContext context, DeliveryConstraint constraint) { AddDeliveryConstraintInternal(context, constraint); }
public void Register(Guid peerId, IRoutingContext context) { CopyOnModifyRoutingContextContainer routingContextContainer = routingContextByPeerId.GetOrAdd( peerId, add => new CopyOnModifyRoutingContextContainer()); routingContextContainer.Add(context); }
/// <summary> /// Sets flag to: route to next route. /// </summary> public static void Continue(this IRoutingContext context) => context.Target = RouteTarget.Continue;
/// <summary> /// Sets flag to: stop all routing. /// </summary> public static void Stop(this IRoutingContext context) => context.Target = RouteTarget.Stop;
/// <summary> /// Sets flag to: skip remaining routes and route to next upper level route. /// </summary> public static void Break(this IRoutingContext context) => context.Target = RouteTarget.Break;
IObservable <IUrlResponse> IRouteHandler.GetResponse(IRoutingContext context) { return(GetResponse(context)); }
public IHttpResponse Handle(IHttpContext context) { try { // Add new session for the client if current session is null if (!context.HttpRequest.Cookies.ContainsKey(SessionRepository.CookieKey)) { string sessionId = Guid.NewGuid().ToString(); context.HttpRequest.Session = SessionRepository.GetSession(sessionId); } // Check if user is authenticated ICollection <string> anonymousPaths = this.serverRouteConfig.AnonymousPaths; bool isPathMatched = anonymousPaths.Any(ap => Regex.IsMatch(context.HttpRequest.Path, ap)); bool isUserAuthenticated = context.HttpRequest.Session.Contains(SessionRepository.CurrentUserKey); if (!isPathMatched && !isUserAuthenticated) { return(new RedirectResponse(anonymousPaths.First())); } // Add new shopping cart for guest users if (!context.HttpRequest.Session.Contains(ShoppingCart.CurrentShoppingCartSessionKey)) { context.HttpRequest.Session.AddSession(ShoppingCart.CurrentShoppingCartSessionKey, new ShoppingCart()); } HttpRequestMethod requestMethod = context.HttpRequest.RequestMethod; string requestPath = context.HttpRequest.Path; IDictionary <string, IRoutingContext> registeredRoutes = this.serverRouteConfig.Routes[requestMethod]; foreach (KeyValuePair <string, IRoutingContext> registeredRoute in registeredRoutes) { string routePattern = registeredRoute.Key; IRoutingContext routeContext = registeredRoute.Value; Regex routeRegex = new Regex(routePattern); Match match = routeRegex.Match(requestPath); if (!match.Success) { continue; } IEnumerable <string> parameters = routeContext.Parameters; foreach (string parameter in parameters) { string parameterValue = match.Groups[parameter].Value; context.HttpRequest.AddUrlParameter(parameter, parameterValue); } return(routeContext.RequestHandler.Handle(context)); } } catch (Exception exception) { return(new InternalServerErrorResponse(exception)); } return(new NotFoundResponse()); }
public void Unregister(Guid peerId, IRoutingContext context) { CopyOnModifyRoutingContextContainer routingContextContainer; if (routingContextByPeerId.TryGetValue(peerId, out routingContextContainer)) { routingContextContainer.Remove(context); } }
/// <summary> /// Creates a <see cref="IDispatchContext" /> based on the current context. /// </summary> public static IDispatchContext CreateDispatchContext(this StageConnector <IRoutingContext, IDispatchContext> stageConnector, IReadOnlyCollection <TransportOperation> transportOperations, IRoutingContext sourceContext) { Guard.AgainstNull(nameof(transportOperations), transportOperations); Guard.AgainstNull(nameof(sourceContext), sourceContext); return(new DispatchContext(transportOperations, sourceContext)); }
/// <summary> /// Creates a <see cref="IDispatchContext" /> based on the current context. /// </summary> public static IDispatchContext CreateDispatchContext(this StageConnector<IRoutingContext, IDispatchContext> stageConnector, IReadOnlyCollection<TransportOperation> transportOperations, IRoutingContext sourceContext) { Guard.AgainstNull(nameof(transportOperations), transportOperations); Guard.AgainstNull(nameof(sourceContext), sourceContext); return new DispatchContext(transportOperations, sourceContext); }