Search in sources :

Example 1 with RequestContext

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;
}
Also used : RequestContext(org.webpieces.ctx.api.RequestContext) RouteNotFoundException(org.webpieces.router.api.exceptions.RouteNotFoundException) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 2 with RequestContext

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;
}
Also used : Action(org.webpieces.router.api.controller.actions.Action) XFuture(org.webpieces.util.futures.XFuture) RequestContext(org.webpieces.ctx.api.RequestContext)

Example 3 with RequestContext

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());
}
Also used : RequestContext(org.webpieces.ctx.api.RequestContext) HashSet(java.util.HashSet)

Example 4 with RequestContext

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;
}
Also used : SneakyThrow(org.webpieces.util.exceptions.SneakyThrow) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) NotFoundException(org.webpieces.http.exception.NotFoundException) RouterRequest(org.webpieces.ctx.api.RouterRequest) EntityLookup(org.webpieces.router.api.extensions.EntityLookup) RequestContext(org.webpieces.ctx.api.RequestContext) Meta(org.webpieces.router.api.extensions.Meta) Parameter(java.lang.reflect.Parameter) IllegalArgException(org.webpieces.router.api.exceptions.IllegalArgException) Map(java.util.Map) DataMismatchException(org.webpieces.router.api.exceptions.DataMismatchException) Method(java.lang.reflect.Method) BadRequestException(org.webpieces.http.exception.BadRequestException) Validation(org.webpieces.ctx.api.Validation) ParamMeta(org.webpieces.router.api.extensions.ParamMeta) Set(java.util.Set) Field(java.lang.reflect.Field) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) HttpMethod(org.webpieces.ctx.api.HttpMethod) ParameterizedType(java.lang.reflect.ParameterizedType) XFuture(org.webpieces.util.futures.XFuture) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) ObjectStringConverter(org.webpieces.router.api.extensions.ObjectStringConverter) BodyContentBinder(org.webpieces.router.api.extensions.BodyContentBinder) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) ParamMeta(org.webpieces.router.api.extensions.ParamMeta) Parameter(java.lang.reflect.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Example 5 with RequestContext

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);
}
Also used : MethodMeta(org.webpieces.router.api.routes.MethodMeta) LoadedController(org.webpieces.router.impl.loader.LoadedController) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) RequestContext(org.webpieces.ctx.api.RequestContext)

Aggregations

RequestContext (org.webpieces.ctx.api.RequestContext)25 RouterRequest (org.webpieces.ctx.api.RouterRequest)8 LoadedController (org.webpieces.router.impl.loader.LoadedController)7 XFuture (org.webpieces.util.futures.XFuture)6 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)5 List (java.util.List)5 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)5 Method (java.lang.reflect.Method)4 Map (java.util.Map)4 Function (java.util.function.Function)4 NotFoundException (org.webpieces.http.exception.NotFoundException)4 ArrayList (java.util.ArrayList)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 HttpMethod (org.webpieces.ctx.api.HttpMethod)3 RenderResponse (org.webpieces.router.impl.dto.RenderResponse)3 View (org.webpieces.router.impl.dto.View)3 RouterStreamRef (org.webpieces.router.impl.routeinvoker.RouterStreamRef)3 FutureHelper (org.webpieces.util.futures.FutureHelper)3 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)2