Search in sources :

Example 1 with BodyContentBinder

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;
}
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 2 with BodyContentBinder

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);
}
Also used : BodyContentBinder(org.webpieces.router.api.extensions.BodyContentBinder) Set(java.util.Set) TemplateApi(org.webpieces.router.api.TemplateApi) EntityLookup(org.webpieces.router.api.extensions.EntityLookup) HtmlTagCreator(org.webpieces.ctx.api.extension.HtmlTagCreator) ObjectStringConverter(org.webpieces.router.api.extensions.ObjectStringConverter)

Example 3 with BodyContentBinder

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());
    }
}
Also used : BodyContentBinder(org.webpieces.router.api.extensions.BodyContentBinder)

Example 4 with BodyContentBinder

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();
}
Also used : BodyContentBinder(org.webpieces.router.api.extensions.BodyContentBinder) ArrayList(java.util.ArrayList) Parameter(java.lang.reflect.Parameter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Annotation(java.lang.annotation.Annotation)

Example 5 with BodyContentBinder

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);
}
Also used : BodyContentBinder(org.webpieces.router.api.extensions.BodyContentBinder)

Aggregations

BodyContentBinder (org.webpieces.router.api.extensions.BodyContentBinder)5 Annotation (java.lang.annotation.Annotation)2 Parameter (java.lang.reflect.Parameter)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 EntityLookup (org.webpieces.router.api.extensions.EntityLookup)2 ObjectStringConverter (org.webpieces.router.api.extensions.ObjectStringConverter)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Inject (javax.inject.Inject)1 Singleton (javax.inject.Singleton)1 HttpMethod (org.webpieces.ctx.api.HttpMethod)1 RequestContext (org.webpieces.ctx.api.RequestContext)1