Search in sources :

Example 1 with Action

use of org.webpieces.router.api.actions.Action in project webpieces by deanhiller.

the class MetaLoader method preconditionCheck.

private void preconditionCheck(RouteMeta meta, Method controllerMethod) {
    if (meta.getRoute().isPostOnly()) {
        Class<?> clazz = controllerMethod.getReturnType();
        if (CompletableFuture.class.isAssignableFrom(clazz)) {
            Type genericReturnType = controllerMethod.getGenericReturnType();
            ParameterizedType t = (ParameterizedType) genericReturnType;
            Type type2 = t.getActualTypeArguments()[0];
            if (!(type2 instanceof Class))
                throw new IllegalArgumentException("Since this route=" + meta + " is for POST, the method MUST return a type 'Redirect' or 'CompletableFuture<Redirect>' for this method=" + controllerMethod);
            @SuppressWarnings("rawtypes") Class<?> type = (Class) type2;
            if (!Redirect.class.isAssignableFrom(type))
                throw new IllegalArgumentException("Since this route=" + meta + " is for POST, the method MUST return a type 'Redirect' or 'CompletableFuture<Redirect>' not 'CompletableFuture<" + type.getSimpleName() + ">'for this method=" + controllerMethod);
        } else if (!Redirect.class.isAssignableFrom(clazz))
            throw new IllegalArgumentException("Since this route=" + meta + " is for POST, the method MUST return a type 'Redirect' or 'CompletableFuture<Redirect>' not '" + clazz.getSimpleName() + "' for this method=" + controllerMethod);
    } else {
        Class<?> clazz = controllerMethod.getReturnType();
        if (CompletableFuture.class.isAssignableFrom(clazz)) {
            Type genericReturnType = controllerMethod.getGenericReturnType();
            ParameterizedType t = (ParameterizedType) genericReturnType;
            Type type2 = t.getActualTypeArguments()[0];
            if (!(type2 instanceof Class))
                throw new IllegalArgumentException("This route=" + meta + " has a method that MUST return a type 'Action' or 'CompletableFuture<Action>' for this method(and did not)=" + controllerMethod);
            @SuppressWarnings("rawtypes") Class<?> type = (Class) type2;
            if (!Action.class.isAssignableFrom(type))
                throw new IllegalArgumentException("This route=" + meta + " has a method that MUST return a type 'Action' or 'CompletableFuture<Action>' not 'CompletableFuture<" + type.getSimpleName() + ">'for this method=" + controllerMethod);
        } else if (!Action.class.isAssignableFrom(clazz))
            throw new IllegalArgumentException("This route=" + meta + " has a method that MUST return a type 'Action' or 'CompletableFuture<Action>' not '" + clazz.getSimpleName() + "' for this method=" + controllerMethod);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) RouteType(org.webpieces.router.api.dto.RouteType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Action(org.webpieces.router.api.actions.Action) Redirect(org.webpieces.router.api.actions.Redirect)

Example 2 with Action

use of org.webpieces.router.api.actions.Action in project webpieces by deanhiller.

the class ServiceProxy method invokeMethod.

@SuppressWarnings("unchecked")
private CompletableFuture<Action> invokeMethod(MethodMeta meta) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    parseBodyFromContentType(meta.getRoute(), meta.getCtx(), meta.getBodyContentBinder());
    Method m = meta.getMethod();
    Object obj = meta.getControllerInstance();
    //We chose to do this here so any filters ESPECIALLY API filters 
    //can catch and translate api errors and send customers a logical response
    //On top of that ORM plugins can have a transaction filter and then in this
    //createArgs can look up the bean before applying values since it is in
    //the transaction filter
    List<Object> argsResult = translator.createArgs(m, meta.getCtx(), meta.getBodyContentBinder());
    Object retVal = m.invoke(obj, argsResult.toArray());
    if (meta.getBodyContentBinder() != null)
        return unwrapResult(m, retVal, meta.getBodyContentBinder());
    if (retVal == null)
        throw new IllegalStateException("Your controller method returned null which is not allowed.  offending method=" + m);
    if (retVal instanceof CompletableFuture) {
        return (CompletableFuture<Action>) retVal;
    } else {
        Action action = (Action) retVal;
        return CompletableFuture.completedFuture(action);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Action(org.webpieces.router.api.actions.Action) Method(java.lang.reflect.Method)

Example 3 with Action

use of org.webpieces.router.api.actions.Action in project webpieces by deanhiller.

the class AbstractLoader method createServiceFromFilters.

@Override
public Service<MethodMeta, Action> createServiceFromFilters(RouteMeta meta, List<FilterInfo<?>> filterInfos) {
    Injector injector = meta.getInjector();
    List<RouteFilter<?>> filters = createFilters(injector, filterInfos);
    Service<MethodMeta, Action> svcWithFilters = loader.loadFilters(filters);
    return svcWithFilters;
}
Also used : MethodMeta(org.webpieces.router.api.dto.MethodMeta) Action(org.webpieces.router.api.actions.Action) Injector(com.google.inject.Injector) RouteFilter(org.webpieces.router.api.routing.RouteFilter)

Example 4 with Action

use of org.webpieces.router.api.actions.Action in project webpieces by deanhiller.

the class BeansController method pageParamAsync.

public CompletableFuture<Action> pageParamAsync() {
    CompletableFuture<Action> future = new CompletableFuture<>();
    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 : CompletableFuture(java.util.concurrent.CompletableFuture) Action(org.webpieces.router.api.actions.Action) RequestContext(org.webpieces.ctx.api.RequestContext)

Example 5 with Action

use of org.webpieces.router.api.actions.Action in project webpieces by deanhiller.

the class DevRoutingService method fetchNotFoundRoute.

public NotFoundInfo fetchNotFoundRoute(NotFoundException e, RouterRequest req) {
    //Production app's notFound route TBD and used in iframe later
    RouteMeta origMeta = routeLoader.fetchNotFoundRoute(req.domain);
    if (req.queryParams.containsKey("webpiecesShowPage")) {
        //This is actually a callback from the below code's iframe!!!
        if (origMeta.getControllerInstance() == null) {
            finder.loadControllerIntoMetaObject(origMeta, false);
            finder.loadFiltersIntoMeta(origMeta, origMeta.getFilters(), false);
        }
        Service<MethodMeta, Action> svc = origMeta.getService222();
        return new NotFoundInfo(origMeta, svc, req);
    }
    log.error("(Development only log message) Route not found!!! Either you(developer) typed the wrong url OR you have a bad route.  Either way,\n" + " something needs a'fixin.  req=" + req, e);
    RouteImpl r = new RouteImpl("/org/webpieces/devrouter/impl/NotFoundController.notFound", RouteType.NOT_FOUND);
    RouteModuleInfo info = new RouteModuleInfo("", null);
    RouteMeta meta = new RouteMeta(r, origMeta.getInjector(), info, config.getUrlEncoding());
    if (meta.getControllerInstance() == null) {
        finder.loadControllerIntoMetaObject(meta, false);
        meta.setService(serviceCreator.create());
    }
    String reason = "Your route was not found in routes table";
    if (e != null)
        reason = e.getMessage();
    RouterRequest newRequest = new RouterRequest();
    newRequest.putMultipart("webpiecesError", "Exception message=" + reason);
    newRequest.putMultipart("url", req.relativePath);
    return new NotFoundInfo(meta, meta.getService222(), newRequest);
}
Also used : MethodMeta(org.webpieces.router.api.dto.MethodMeta) NotFoundInfo(org.webpieces.router.impl.NotFoundInfo) Action(org.webpieces.router.api.actions.Action) RouteImpl(org.webpieces.router.impl.RouteImpl) RouteMeta(org.webpieces.router.impl.RouteMeta) RouteModuleInfo(org.webpieces.router.impl.model.RouteModuleInfo) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Aggregations

Action (org.webpieces.router.api.actions.Action)7 Method (java.lang.reflect.Method)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Redirect (org.webpieces.router.api.actions.Redirect)2 MethodMeta (org.webpieces.router.api.dto.MethodMeta)2 GET_ADD_USER_FORM (WEBPIECESxPACKAGE.base.crud.CrudUserRouteId.GET_ADD_USER_FORM)1 GET_EDIT_USER_FORM (WEBPIECESxPACKAGE.base.crud.CrudUserRouteId.GET_EDIT_USER_FORM)1 EducationEnum (WEBPIECESxPACKAGE.base.libs.EducationEnum)1 RoleEnum (WEBPIECESxPACKAGE.base.libs.RoleEnum)1 UserDbo (WEBPIECESxPACKAGE.base.libs.UserDbo)1 UserRole (WEBPIECESxPACKAGE.base.libs.UserRole)1 Injector (com.google.inject.Injector)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Singleton (javax.inject.Singleton)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 Current (org.webpieces.ctx.api.Current)1