Search in sources :

Example 1 with Route

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

the class ControllerResolver method resolveControllerClassAndMethod.

//A webapp could override this class entirely to feed back locations of Class's and methods based on the 
//Strings passed in from the RouteModules
public ResolvedMethod resolveControllerClassAndMethod(RouteMeta meta) {
    Route r = meta.getRoute();
    String controllerAndMethod = r.getControllerMethodString();
    int index = controllerAndMethod.lastIndexOf(".");
    String methodStr = controllerAndMethod.substring(index + 1);
    String controllerStr = controllerAndMethod.substring(0, index);
    if (countMatches(controllerAndMethod) > 1) {
        //If they do absolute class name like
        //org.webpieces.Controller.method, then just return it...
        ResolvedMethod method = new ResolvedMethod(controllerStr, methodStr);
        return method;
    }
    if (controllerStr.startsWith("/")) {
        //absolute reference using /org/webpieces/Controller.method, just replace / with .
        controllerStr = controllerStr.replace("/", ".");
        controllerStr = controllerStr.substring(1);
    } else if (controllerStr.contains("/")) {
        //relative reference is annoying but easier for users..(and more concise..
        controllerStr = ClassUtil.translate(meta.getPackageContext(), controllerStr);
    } else {
        //finally for Controllers in the same package as the router, it makes the 
        //Controller.method really concise...
        controllerStr = meta.getPackageContext() + "." + controllerStr;
    }
    ResolvedMethod method = new ResolvedMethod(controllerStr, methodStr);
    return method;
}
Also used : Route(org.webpieces.router.impl.Route)

Example 2 with Route

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

the class AbstractRouteBuilder method addContentRoute.

public void addContentRoute(HttpMethod method, String path, String controllerMethod) {
    UrlPath p = new UrlPath(routerInfo, path);
    Route route = new RouteImpl(method, p, controllerMethod, isHttpsOnlyRoutes);
    addRoute(route, null);
}
Also used : RouteImpl(org.webpieces.router.impl.RouteImpl) UrlPath(org.webpieces.router.impl.UrlPath) Route(org.webpieces.router.impl.Route) MultiRoute(org.webpieces.router.api.routing.MultiRoute)

Example 3 with Route

use of org.webpieces.router.impl.Route 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 4 with Route

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

the class AbstractDomainBuilder method setInternalErrorRoute.

@Override
public void setInternalErrorRoute(String controllerMethod) {
    Route route = new RouteImpl(controllerMethod, RouteType.INTERNAL_SERVER_ERROR);
    setInternalSvrErrorRoute(route);
}
Also used : RouteImpl(org.webpieces.router.impl.RouteImpl) Route(org.webpieces.router.impl.Route)

Example 5 with Route

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

the class AbstractRouteBuilder method addRoute.

@Override
public void addRoute(HttpMethod method, String path, String controllerMethod, RouteId routeId, boolean checkToken) {
    UrlPath p = new UrlPath(routerInfo, path);
    Route route = new RouteImpl(method, p, controllerMethod, routeId, isHttpsOnlyRoutes, checkToken);
    addRoute(route, routeId);
}
Also used : RouteImpl(org.webpieces.router.impl.RouteImpl) UrlPath(org.webpieces.router.impl.UrlPath) Route(org.webpieces.router.impl.Route) MultiRoute(org.webpieces.router.api.routing.MultiRoute)

Aggregations

Route (org.webpieces.router.impl.Route)6 RouteImpl (org.webpieces.router.impl.RouteImpl)4 MultiRoute (org.webpieces.router.api.routing.MultiRoute)2 UrlPath (org.webpieces.router.impl.UrlPath)2 Method (java.lang.reflect.Method)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1 RouterRequest (org.webpieces.ctx.api.RouterRequest)1 RedirectResponse (org.webpieces.router.api.dto.RedirectResponse)1 IllegalReturnValueException (org.webpieces.router.api.exceptions.IllegalReturnValueException)1 RouteMeta (org.webpieces.router.impl.RouteMeta)1