Search in sources :

Example 1 with EntityLookup

use of org.webpieces.router.api.extensions.EntityLookup in project webpieces by deanhiller.

the class ParamToObjectTranslatorImpl method translate.

private XFuture<Object> translate(RouterRequest req, Method method, ParamNode valuesToUse, Meta fieldMeta, Validation validator) {
    Class<?> fieldClass = fieldMeta.getFieldClass();
    ObjectStringConverter<?> converter = objectTranslator.getConverter(fieldClass);
    if (converter != null) {
        Object convert = convert(req, method, valuesToUse, fieldMeta, converter, validator);
        return XFuture.completedFuture(convert);
    } else if (fieldClass.isArray()) {
        throw new UnsupportedOperationException("not done yet...let me know and I will do it=" + fieldMeta);
    } else if (fieldClass.isEnum()) {
        throw new UnsupportedOperationException("You need to install a " + ObjectStringConverter.class.getSimpleName() + " for this enum " + fieldClass + " meta class=" + fieldMeta.getFieldClass() + ". This\n" + "can be done like so in one of your guice modules....\n" + "\t\tMultibinder<ObjectStringConverter> conversionBinder = Multibinder.newSetBinder(binder, ObjectStringConverter.class);\n" + "\t\tconversionBinder.addBinding().to({YOUR_ENUM}.WebConverter.class);");
    } else if (List.class.isAssignableFrom(fieldClass)) {
        if (valuesToUse == null)
            return XFuture.completedFuture(new ArrayList<>());
        else if (valuesToUse instanceof ArrayNode) {
            List<ParamNode> paramNodes = ((ArrayNode) valuesToUse).getList();
            return createList(req, method, fieldMeta, validator, paramNodes);
        } else if (valuesToUse instanceof ValueNode) {
            List<ParamNode> paramNodes = new ArrayList<>();
            paramNodes.add(valuesToUse);
            return createList(req, method, fieldMeta, validator, paramNodes);
        }
        throw new IllegalArgumentException("Found List on field or param=" + fieldMeta + " but did not find ArrayNode type");
    } else if (valuesToUse instanceof ArrayNode) {
        throw new IllegalArgumentException("Incoming array need a type List but instead found type=" + fieldClass + " on field=" + fieldMeta);
    } else if (valuesToUse instanceof ValueNode) {
        ValueNode v = (ValueNode) valuesToUse;
        String fullName = v.getFullName();
        throw new IllegalArgumentException("Could not convert incoming value=" + v.getValue() + " of key name=" + fullName + " field=" + fieldMeta);
    } else if (valuesToUse == null) {
        // validate if null is ok or not
        fieldMeta.validateNullValue();
        return XFuture.completedFuture(null);
    } else if (!(valuesToUse instanceof ParamTreeNode)) {
        throw new IllegalStateException("Bug, must be missing a case. v=" + valuesToUse + " type to field=" + fieldMeta);
    }
    ParamTreeNode tree = (ParamTreeNode) valuesToUse;
    EntityLookup pluginLookup = fetchPluginLoader(fieldClass);
    XFuture<Object> future = null;
    if (pluginLookup != null) {
        future = pluginLookup.find(fieldMeta, tree, c -> createBean(c));
        if (future == null)
            throw new IllegalStateException("plugin=" + pluginLookup.getClass() + " failed to create bean.  This is a plugin bug");
    } else {
        Object newBean = createBean(fieldClass);
        future = XFuture.completedFuture(newBean);
    }
    return future.thenCompose(bean -> {
        return fillBeanIn(bean, tree, req, method, validator);
    });
}
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) EntityLookup(org.webpieces.router.api.extensions.EntityLookup) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with EntityLookup

use of org.webpieces.router.api.extensions.EntityLookup 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)

Aggregations

Set (java.util.Set)2 BodyContentBinder (org.webpieces.router.api.extensions.BodyContentBinder)2 EntityLookup (org.webpieces.router.api.extensions.EntityLookup)2 ObjectStringConverter (org.webpieces.router.api.extensions.ObjectStringConverter)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)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 RouterRequest (org.webpieces.ctx.api.RouterRequest)1