use of org.webpieces.router.api.extensions.BodyContentBinder 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.router.api.extensions.BodyContentBinder in project webpieces by deanhiller.
the class PluginSetup method wireInPluginPoints.
/**
* This is where we wire in all plugin points EXCEPT the Startup one
* we can't inject them :(
*/
@SuppressWarnings("rawtypes")
public void wireInPluginPoints(Injector appInjector) {
Key<Set<EntityLookup>> key = Key.get(new TypeLiteral<Set<EntityLookup>>() {
});
Set<EntityLookup> lookupHooks = appInjector.getInstance(key);
translator.install(lookupHooks);
Key<Set<ObjectStringConverter>> key3 = Key.get(new TypeLiteral<Set<ObjectStringConverter>>() {
});
Set<ObjectStringConverter> converters = appInjector.getInstance(key3);
translation.install(converters);
Key<Set<BodyContentBinder>> key2 = Key.get(new TypeLiteral<Set<BodyContentBinder>>() {
});
Set<BodyContentBinder> bodyBinders = appInjector.getInstance(key2);
bodyContentChecker.install(bodyBinders);
Key<Set<HtmlTagCreator>> key4 = Key.get(new TypeLiteral<Set<HtmlTagCreator>>() {
});
Set<HtmlTagCreator> htmlTagCreators = appInjector.getInstance(key4);
// Guice circular dependency we could not work around quite yet. figure out later maybe
TemplateApi api = templateApi.get();
api.installCustomTags(htmlTagCreators);
}
use of org.webpieces.router.api.extensions.BodyContentBinder in project webpieces by deanhiller.
the class BodyContentBinderChecker method install.
public void install(Set<BodyContentBinder> bodyBinders) {
this.bodyBinderPlugins = bodyBinders;
this.allBinderAnnotations = new ArrayList<>();
for (BodyContentBinder binder : bodyBinders) {
allBinderAnnotations.add("@" + binder.getAnnotation().getSimpleName());
}
}
use of org.webpieces.router.api.extensions.BodyContentBinder in project webpieces by deanhiller.
the class BodyContentBinderChecker method contentPreconditionCheck.
public BodyContentBinder contentPreconditionCheck(Injector injector, Object meta, Method controllerMethod, Parameter[] parameters) {
List<String> binderMatches = new ArrayList<>();
AtomicReference<BodyContentBinder> lastMatch = new AtomicReference<BodyContentBinder>(null);
for (BodyContentBinder binder : bodyBinderPlugins) {
for (Parameter p : parameters) {
Annotation[] annotations = p.getAnnotations();
Class<?> entityClass = p.getType();
recordParameterMatches(lastMatch, binderMatches, binder, annotations, entityClass);
}
Annotation[] annotations = controllerMethod.getAnnotations();
recordParameterMatches(lastMatch, binderMatches, binder, annotations, null);
}
Class<?> returnType = controllerMethod.getReturnType();
if (Action.class.isAssignableFrom(returnType))
throw new IllegalArgumentException("The method for content route=" + meta + " is returning Action and this is not allowed. method=" + controllerMethod);
if (binderMatches.size() == 0)
throw new IllegalArgumentException("there was not a single method parameter annotated with a Plugin" + " annotation on method=" + controllerMethod + ". looking at your\n" + "plugins, these are the annotations available=" + allBinderAnnotations + "\n" + "You either need one parameter with one of the annotations OR\n" + "you need to annotata the method(if it is read only and no request is supplied)");
else if (binderMatches.size() > 1)
throw new IllegalArgumentException("there is more than one parameter with a Plugin" + " annotation on method=" + controllerMethod + ". These\n" + "are the ones we found(please delete one)=" + binderMatches + "\n" + "Also make sure one parameter OR the method has the annotation, not both");
return lastMatch.get();
}
use of org.webpieces.router.api.extensions.BodyContentBinder in project webpieces by deanhiller.
the class ControllerLoader method loadContentController.
public BinderAndLoader loadContentController(Injector injector, RouteInfo routeInfo) {
MethodMetaAndController mAndC = loadGenericController(injector, routeInfo);
BodyContentBinder binder = null;
LoadedController loadedController = mAndC.getLoadedController();
binder = binderChecker.contentPreconditionCheck(injector, routeInfo, loadedController.getControllerMethod(), loadedController.getParameters());
return new BinderAndLoader(mAndC, binder);
}
Aggregations