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;
}
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));
}
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));
}
Aggregations