Search in sources :

Example 6 with RouteMeta

use of org.webpieces.router.impl.RouteMeta in project webpieces by deanhiller.

the class DevRoutingService method incomingRequestImpl.

@Override
public CompletableFuture<Void> incomingRequestImpl(RequestContext ctx, ResponseStreamer responseCb) {
    //In DevRouter, check if we need to reload the text file as it points to a new RouterModules.java implementation file
    boolean reloaded = reloadIfTextFileChanged();
    if (!reloaded)
        reloadIfClassFilesChanged();
    MatchResult result = fetchRoute(ctx);
    CompletableFuture<Void> future;
    try {
        RouteMeta meta = result.getMeta();
        if (meta.getRoute().getRouteType() == RouteType.STATIC) {
            //RESET the encodings to known so we don't try to go the compressed cache which doesn't
            //exist in dev server since we want the latest files always
            ctx.getRequest().encodings = new ArrayList<>();
        } else if (meta.getControllerInstance() == null) {
            finder.loadControllerIntoMetaObject(meta, false);
            finder.loadFiltersIntoMeta(meta, meta.getFilters(), false);
        }
        future = routeLoader.invokeRoute(result, ctx, responseCb, new DevErrorRoutes(ctx.getRequest()));
    } catch (Throwable e) {
        future = new CompletableFuture<Void>();
        future.completeExceptionally(e);
    }
    return future.exceptionally(t -> {
        throw new HaveRouteException(result, t);
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RouteMeta(org.webpieces.router.impl.RouteMeta) HaveRouteException(org.webpieces.router.impl.loader.HaveRouteException) MatchResult(org.webpieces.router.impl.model.MatchResult)

Example 7 with RouteMeta

use of org.webpieces.router.impl.RouteMeta in project webpieces by deanhiller.

the class ResponseProcessor method createRedirect.

private CompletableFuture<Void> createRedirect(RouteId id, Map<String, Object> args, boolean isAjaxRedirect) {
    if (responseSent)
        throw new IllegalStateException("You already sent a response.  do not call Actions.redirect or Actions.render more than once");
    responseSent = true;
    RouterRequest request = ctx.getRequest();
    Method method = matchedMeta.getMethod();
    RouteMeta nextRequestMeta = reverseRoutes.get(id);
    if (nextRequestMeta == null)
        throw new IllegalReturnValueException("Route=" + id + " returned from method='" + method + "' was not added in the RouterModules");
    else if (!nextRequestMeta.getRoute().matchesMethod(HttpMethod.GET))
        throw new IllegalReturnValueException("method='" + method + "' is trying to redirect to routeid=" + id + " but that route is not a GET method route and must be");
    Route route = nextRequestMeta.getRoute();
    Map<String, String> keysToValues = reverseTranslator.formMap(method, route.getPathParamNames(), args);
    Set<String> keySet = keysToValues.keySet();
    List<String> argNames = route.getPathParamNames();
    if (keySet.size() != argNames.size()) {
        throw new IllegalReturnValueException("Method='" + method + "' returns a Redirect action with wrong number of arguments.  args=" + keySet.size() + " when it should be size=" + argNames.size());
    }
    String path = route.getFullPath();
    for (String name : argNames) {
        String value = keysToValues.get(name);
        if (value == null)
            throw new IllegalArgumentException("Method='" + method + "' returns a Redirect that is missing argument key=" + name + " to form the url on the redirect");
        path = path.replace("{" + name + "}", value);
    }
    RedirectResponse redirectResponse = new RedirectResponse(isAjaxRedirect, request.isHttps, request.domain, request.port, path);
    return wrapFunctionInContext(() -> responseCb.sendRedirect(redirectResponse));
}
Also used : IllegalReturnValueException(org.webpieces.router.api.exceptions.IllegalReturnValueException) RouteMeta(org.webpieces.router.impl.RouteMeta) RedirectResponse(org.webpieces.router.api.dto.RedirectResponse) Method(java.lang.reflect.Method) HttpMethod(org.webpieces.ctx.api.HttpMethod) Route(org.webpieces.router.impl.Route) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 8 with RouteMeta

use of org.webpieces.router.impl.RouteMeta in project webpieces by deanhiller.

the class AbstractRouteBuilder method addRoute.

public void addRoute(Route r, RouteId routeId) {
    log.info("scope:'" + routerInfo + "' adding route=" + r.getMethod() + " " + r.getFullPath() + " method=" + r.getControllerMethodString());
    RouteMeta meta = new RouteMeta(r, holder.getInjector(), currentPackage.get(), holder.getUrlEncoding());
    holder.getFinder().loadControllerIntoMetaObject(meta, true);
    routes.addRoute(meta);
    if (routeId != null)
        holder.getReverseRoutes().addRoute(routeId, meta);
    else {
        holder.getReverseRoutes().addContentRoute(meta);
    }
}
Also used : RouteMeta(org.webpieces.router.impl.RouteMeta)

Example 9 with RouteMeta

use of org.webpieces.router.impl.RouteMeta in project webpieces by deanhiller.

the class R1RouterBuilder method applyFilters.

public void applyFilters(WebAppMeta rm) {
    ReverseRoutes reverseRoutes = holder.getReverseRoutes();
    Collection<RouteMeta> metas = reverseRoutes.getAllRouteMetas();
    for (RouteMeta meta : metas) {
        String path = meta.getRoute().getFullPath();
        List<FilterInfo<?>> filters = findMatchingFilters(path, meta.getRoute().isHttpsRoute());
        meta.setFilters(filters);
    }
    List<L2DomainRoutes> allDomains = allRouting.getAllDomains();
    for (L2DomainRoutes domainRoutes : allDomains) {
        applyFilters(domainRoutes, rm);
    }
}
Also used : RouteMeta(org.webpieces.router.impl.RouteMeta) ReverseRoutes(org.webpieces.router.impl.ReverseRoutes) FilterInfo(org.webpieces.router.impl.FilterInfo)

Example 10 with RouteMeta

use of org.webpieces.router.impl.RouteMeta in project webpieces by deanhiller.

the class AbstractDomainBuilder method setNotFoundRoute.

private void setNotFoundRoute(Route r) {
    if (!"".equals(this.routerInfo.getPath()))
        throw new UnsupportedOperationException("setNotFoundRoute can only be called on the root Router, not a scoped router");
    log.info("scope:'" + routerInfo + "' adding PAGE_NOT_FOUND route=" + r.getFullPath() + " method=" + r.getControllerMethodString());
    RouteMeta meta = new RouteMeta(r, holder.getInjector(), currentPackage.get(), holder.getUrlEncoding());
    holder.getFinder().loadControllerIntoMetaObject(meta, true);
    domainRoutes.setPageNotFoundRoute(meta);
}
Also used : RouteMeta(org.webpieces.router.impl.RouteMeta)

Aggregations

RouteMeta (org.webpieces.router.impl.RouteMeta)11 RouterRequest (org.webpieces.ctx.api.RouterRequest)2 Method (java.lang.reflect.Method)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1 Action (org.webpieces.router.api.actions.Action)1 MethodMeta (org.webpieces.router.api.dto.MethodMeta)1 RedirectResponse (org.webpieces.router.api.dto.RedirectResponse)1 IllegalReturnValueException (org.webpieces.router.api.exceptions.IllegalReturnValueException)1 FilterInfo (org.webpieces.router.impl.FilterInfo)1 NotFoundInfo (org.webpieces.router.impl.NotFoundInfo)1 ReverseRoutes (org.webpieces.router.impl.ReverseRoutes)1 Route (org.webpieces.router.impl.Route)1 RouteImpl (org.webpieces.router.impl.RouteImpl)1 StaticRoute (org.webpieces.router.impl.StaticRoute)1 UrlPath (org.webpieces.router.impl.UrlPath)1 HaveRouteException (org.webpieces.router.impl.loader.HaveRouteException)1 MatchResult (org.webpieces.router.impl.model.MatchResult)1 RouteModuleInfo (org.webpieces.router.impl.model.RouteModuleInfo)1