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