Search in sources :

Example 1 with BodyContentBinder

use of org.webpieces.router.api.BodyContentBinder in project webpieces by deanhiller.

the class MetaLoader method loadInstIntoMeta.

public void loadInstIntoMeta(RouteMeta meta, Object controllerInst, String methodStr) {
    Method[] methods = controllerInst.getClass().getMethods();
    List<Method> matches = new ArrayList<>();
    for (Method m : methods) {
        if (m.getName().equals(methodStr))
            matches.add(m);
    }
    String controllerStr = controllerInst.getClass().getSimpleName();
    if (matches.size() == 0)
        throw new IllegalArgumentException("Invalid Route.  Cannot find 'public' method='" + methodStr + "' on class=" + controllerStr);
    else if (matches.size() > 1)
        throw new UnsupportedOperationException("You have more than one 'public' method named=" + methodStr + " on class=" + controllerStr + "  This is not yet supported until we support method parameters(let us know you hit this and we will immediately implement)");
    Method controllerMethod = matches.get(0);
    Parameter[] parameters = controllerMethod.getParameters();
    List<String> paramNames = new ArrayList<>();
    for (Parameter p : parameters) {
        String value;
        String name = p.getName();
        if (matchesBadName(name)) {
            Param annotation = p.getAnnotation(Param.class);
            if (annotation == null)
                throw new IllegalArgumentException("Method='" + controllerMethod + "' has to have every argument annotated with @Param(paramName) since\n" + "you are not compiling with -parameters to enable the param names to be built into the *.class files.  Most likely, you " + "changed the build.gradle we generated or switched to a different build system and did not enable this compiler option");
            value = annotation.value();
        } else {
            //use the param name in the method...
            value = name;
        }
        paramNames.add(value);
    }
    if (meta.getRoute().getRouteType() == RouteType.HTML) {
        preconditionCheck(meta, controllerMethod);
    } else if (meta.getRoute().getRouteType() == RouteType.CONTENT) {
        BodyContentBinder binder = contentPreconditionCheck(meta, controllerMethod, parameters);
        meta.setContentBinder(binder);
    }
    meta.setMethodParamNames(paramNames);
    meta.setControllerInstance(controllerInst);
    meta.setMethod(controllerMethod);
}
Also used : BodyContentBinder(org.webpieces.router.api.BodyContentBinder) ArrayList(java.util.ArrayList) Param(org.webpieces.router.api.routing.Param) Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method)

Example 2 with BodyContentBinder

use of org.webpieces.router.api.BodyContentBinder in project webpieces by deanhiller.

the class MetaLoader method contentPreconditionCheck.

private BodyContentBinder contentPreconditionCheck(RouteMeta 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.getRoute().getFullPath() + " 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.BodyContentBinder) ArrayList(java.util.ArrayList) Parameter(java.lang.reflect.Parameter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Annotation(java.lang.annotation.Annotation)

Example 3 with BodyContentBinder

use of org.webpieces.router.api.BodyContentBinder in project webpieces by deanhiller.

the class JacksonModule method configure.

@Override
protected void configure() {
    Multibinder<BodyContentBinder> uriBinder = Multibinder.newSetBinder(binder(), BodyContentBinder.class);
    uriBinder.addBinding().to(JacksonLookup.class);
    bind(ObjectMapper.class).toInstance(new ObjectMapper());
}
Also used : BodyContentBinder(org.webpieces.router.api.BodyContentBinder) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

BodyContentBinder (org.webpieces.router.api.BodyContentBinder)3 Parameter (java.lang.reflect.Parameter)2 ArrayList (java.util.ArrayList)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 Param (org.webpieces.router.api.routing.Param)1