Search in sources :

Example 21 with RouterRequest

use of org.webpieces.ctx.api.RouterRequest in project webpieces by deanhiller.

the class TestSimpleRoutes method createHttpRequest.

private RouterRequest createHttpRequest(HttpMethod method, String path) {
    RouterRequest r = new RouterRequest();
    r.method = method;
    r.relativePath = path;
    return r;
}
Also used : RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 22 with RouterRequest

use of org.webpieces.ctx.api.RouterRequest 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 23 with RouterRequest

use of org.webpieces.ctx.api.RouterRequest in project webpieces by deanhiller.

the class LoginFilter method filter.

@Override
public CompletableFuture<Action> filter(MethodMeta meta, Service<MethodMeta, Action> next) {
    Session session = Current.session();
    if (session.containsKey(token)) {
        Current.addModifyResponse(resp -> addCacheHeaders(resp));
        return next.invoke(meta);
    }
    RouterRequest request = Current.request();
    if (request.isAjaxRequest) {
        if (request.referrer != null) {
            Current.flash().put("url", request.referrer);
            Current.flash().keep();
        }
        return CompletableFuture.completedFuture(Actions.ajaxRedirect(loginRoute));
    } else if (request.method == HttpMethod.GET) {
        //store url requested in flash so after logging in, we can redirect the user
        //back to the original page
        Current.flash().put("url", request.relativePath);
        Current.flash().keep();
    } else if (request.method == HttpMethod.POST) {
        //adding a validation error avoids the posting of the form so they post AFTER logging in
        if (request.referrer != null)
            Current.flash().put("url", request.referrer);
        else
            Current.flash().put("url", request.relativePath);
        Current.flash().keep();
    }
    //redirect to login page..
    return CompletableFuture.completedFuture(Actions.redirect(loginRoute));
}
Also used : Session(org.webpieces.ctx.api.Session) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Aggregations

RouterRequest (org.webpieces.ctx.api.RouterRequest)23 Test (org.junit.Test)8 RequestContext (org.webpieces.ctx.api.RequestContext)8 MockResponseStream (org.webpieces.router.api.mocks.MockResponseStream)8 RouterService (org.webpieces.router.api.RouterService)5 RedirectResponse (org.webpieces.router.api.dto.RedirectResponse)5 FlashImpl (org.webpieces.router.impl.ctx.FlashImpl)5 SessionImpl (org.webpieces.router.impl.ctx.SessionImpl)5 ValidationImpl (org.webpieces.router.impl.ctx.ValidationImpl)5 HttpMethod (org.webpieces.ctx.api.HttpMethod)4 Method (java.lang.reflect.Method)3 Annotation (java.lang.annotation.Annotation)2 Parameter (java.lang.reflect.Parameter)2 ArrayList (java.util.ArrayList)2 RenderResponse (org.webpieces.router.api.dto.RenderResponse)2 ErrorCommonTest (org.webpieces.router.api.error.ErrorCommonTest)2 IllegalReturnValueException (org.webpieces.router.api.exceptions.IllegalReturnValueException)2 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)1 Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1