Search in sources :

Example 1 with RedirectResponse

use of org.webpieces.router.api.dto.RedirectResponse in project webpieces by deanhiller.

the class ResponseProcessor method createRawRedirect.

public CompletableFuture<Void> createRawRedirect(RawRedirect controllerResponse) {
    String url = controllerResponse.getUrl();
    if (url.startsWith("http")) {
        return wrapFunctionInContext(() -> responseCb.sendRedirect(new RedirectResponse(url)));
    }
    RouterRequest request = ctx.getRequest();
    RedirectResponse redirectResponse = new RedirectResponse(false, request.isHttps, request.domain, request.port, url);
    return wrapFunctionInContext(() -> responseCb.sendRedirect(redirectResponse));
}
Also used : RedirectResponse(org.webpieces.router.api.dto.RedirectResponse) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 2 with RedirectResponse

use of org.webpieces.router.api.dto.RedirectResponse 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 3 with RedirectResponse

use of org.webpieces.router.api.dto.RedirectResponse in project webpieces by deanhiller.

the class ProxyResponse method sendRedirectAndClearCookie.

public void sendRedirectAndClearCookie(RouterRequest req, String badCookieName) {
    RedirectResponse httpResponse = new RedirectResponse(false, req.isHttps, req.domain, req.port, req.relativePath);
    Http2Response response = createRedirect(httpResponse);
    responseCreator.addDeleteCookie(response, badCookieName);
    log.info("sending REDIRECT(due to bad cookie) response responseSender=" + stream);
    stream.sendResponse(response);
    channelCloser.closeIfNeeded(request, stream);
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response) RedirectResponse(org.webpieces.router.api.dto.RedirectResponse)

Aggregations

RedirectResponse (org.webpieces.router.api.dto.RedirectResponse)3 RouterRequest (org.webpieces.ctx.api.RouterRequest)2 Http2Response (com.webpieces.hpack.api.dto.Http2Response)1 Method (java.lang.reflect.Method)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1 IllegalReturnValueException (org.webpieces.router.api.exceptions.IllegalReturnValueException)1 Route (org.webpieces.router.impl.Route)1 RouteMeta (org.webpieces.router.impl.RouteMeta)1