use of org.webpieces.ctx.api.RequestContext in project webpieces by deanhiller.
the class ReverseRoutes method convertToUrl.
public String convertToUrl(String routeId, Map<String, String> args, boolean isValidating) {
RouteMeta routeMeta = get(routeId);
Route route = routeMeta.getRoute();
String urlPath = route.getFullPath();
List<String> pathParamNames = route.getPathParamNames();
for (String param : pathParamNames) {
String val = args.get(param);
if (val == null) {
String strArgs = "";
for (Entry<String, String> entry : args.entrySet()) {
boolean equals = entry.getKey().equals(param);
strArgs = " ARG:'" + entry.getKey() + "'='" + entry.getValue() + "' equals=" + equals + "\n";
}
throw new RouteNotFoundException("missing argument. param=" + param + " is required" + " to exist(and cannot be null as well). route=" + routeId + " args=" + strArgs);
}
String encodedVal = urlEncode(val);
urlPath = urlPath.replace("{" + param + "}", encodedVal);
}
if (isValidating)
return urlPath;
RequestContext ctx = Current.getContext();
RouterRequest request = ctx.getRequest();
if (!route.isHttpsRoute() || request.isHttps)
return urlPath;
//we are rendering an http page with a link to https so need to do special magic
String domain = request.domain;
if (ports == null)
ports = portConfigCallback.fetchPortConfig();
int httpsPort = ports.getHttpsPort();
return "https://" + domain + ":" + httpsPort + urlPath;
}
use of org.webpieces.ctx.api.RequestContext in project webpieces by deanhiller.
the class BeansController method pageParamAsync.
public XFuture<Action> pageParamAsync() {
XFuture<Action> future = new XFuture<>();
RequestContext ctx = Current.getContext();
executor.execute(new Runnable() {
@Override
public void run() {
ctx.getFlash().put("testkey", "testflashvalue");
future.complete(Actions.renderThis("user", "Dean Hiller"));
}
});
return future;
}
use of org.webpieces.ctx.api.RequestContext in project webpieces by deanhiller.
the class AbstractLoginController method postLogin.
public Redirect postLogin(String username, String password) {
boolean authenticated = isValidLogin(username, password);
if (!authenticated || Current.validation().hasErrors()) {
return Actions.redirectFlashAllSecure(getRenderLoginRoute(), Current.getContext(), "password");
}
// officially makes them logged in by putting the token in the session
Current.session().put(getLoginSessionKey(), username);
RequestContext ctx = Current.getContext();
String url = Current.flash().get("url");
if (url != null) {
Set<String> mySet = new HashSet<>(Arrays.asList(secureFields));
ctx.moveFormParamsToFlash(mySet);
ctx.getFlash().keep(true);
ctx.getValidation().keep(true);
// page the user was trying to access before logging in
return Actions.redirectToUrl(url);
}
ctx.getFlash().keep(false);
ctx.getValidation().keep(false);
// base page after login screen
return Actions.redirect(getRenderAfterLoginHome());
}
use of org.webpieces.ctx.api.RequestContext in project webpieces by deanhiller.
the class ParamToObjectTranslatorImpl method createArgsImpl.
protected XFuture<List<Object>> createArgsImpl(Method method, RequestContext ctx, BodyContentBinder binder) {
RouterRequest req = ctx.getRequest();
Parameter[] paramMetas = method.getParameters();
Annotation[][] paramAnnotations = method.getParameterAnnotations();
ParamTreeNode paramTree = new ParamTreeNode();
// For multipart AND for query params such as ?var=xxx&var=yyy&var2=xxx AND for url path params /mypath/{var1}/account/{id}
// query params first
Map<String, String> queryParams = translate(req.queryParams);
treeCreator.createTree(paramTree, queryParams, FromEnum.QUERY_PARAM);
// next multi-part params
Map<String, String> multiPartParams = translate(req.multiPartFields);
treeCreator.createTree(paramTree, multiPartParams, FromEnum.FORM_MULTIPART);
// lastly path params
treeCreator.createTree(paramTree, ctx.getPathParams(), FromEnum.URL_PATH);
List<Object> results = new ArrayList<>();
XFuture<List<Object>> future = XFuture.completedFuture(results);
for (int i = 0; i < paramMetas.length; i++) {
Parameter paramMeta = paramMetas[i];
Annotation[] annotations = paramAnnotations[i];
ParamMeta fieldMeta = new ParamMeta(method, paramMeta, annotations);
String name = fieldMeta.getName();
ParamNode paramNode = paramTree.get(name);
XFuture<Object> beanFuture;
if (binder != null && isManagedBy(binder, fieldMeta)) {
Object bean = binder.unmarshal(ctx, fieldMeta, req.body.createByteArray());
beanFuture = XFuture.completedFuture(bean);
} else {
beanFuture = translate(req, method, paramNode, fieldMeta, ctx.getValidation());
}
future = future.thenCompose(list -> {
return beanFuture.thenApply(bean -> {
list.add(bean);
return list;
});
});
}
return future;
}
use of org.webpieces.ctx.api.RequestContext in project webpieces by deanhiller.
the class AbstractRouteInvoker method invokeSvc.
private XFuture<Void> invokeSvc(InvokeInfo invokeInfo, Endpoint dynamicInfo, RouteData data, Processor processor) {
LoadedController loadedController = invokeInfo.getLoadedController();
ProxyStreamHandle handle = invokeInfo.getHandler();
RequestContext requestCtx = invokeInfo.getRequestCtx();
MethodMeta methodMeta = new MethodMeta(loadedController, requestCtx, invokeInfo.getRouteType(), data);
String i18nBundleName = "";
return serviceInvoker.invokeSvc(methodMeta, i18nBundleName, dynamicInfo, processor, handle);
}
Aggregations