Search in sources :

Example 1 with StringValueResolver

use of org.osgl.util.StringValueResolver in project actframework by actframework.

the class StringValueResolverManager method collectionResolver.

public <T extends Collection> StringValueResolver<T> collectionResolver(final Class<T> targetType, final Class<?> elementType, final String separator) {
    final StringValueResolver elementResolver = resolver(elementType);
    return new StringValueResolver<T>(targetType) {

        @Override
        public T resolve(String value) {
            T col = app().getInstance(targetType);
            String[] parts = value.split(separator);
            for (String part : parts) {
                col.add(elementResolver.resolve(part));
            }
            return col;
        }
    };
}
Also used : StringValueResolver(org.osgl.util.StringValueResolver)

Example 2 with StringValueResolver

use of org.osgl.util.StringValueResolver in project actframework by actframework.

the class ActionContextParamLoader method findContextSpecificLoader.

@Override
protected ParamValueLoader findContextSpecificLoader(String bindName, BeanSpec spec) {
    HeaderVariable headerVariable = spec.getAnnotation(HeaderVariable.class);
    if (null != headerVariable) {
        return new HeaderValueLoader(headerVariable.value(), spec);
    }
    SessionVariable sessionVariable = spec.getAnnotation(SessionVariable.class);
    if (null != sessionVariable) {
        return new SessionValueLoader(sessionVariable.value(), spec);
    }
    if (spec.hasAnnotation(PartialPath.class)) {
        return new PartialPathLoader(bindName);
    }
    DefaultValue def = spec.getAnnotation(DefaultValue.class);
    Class<?> rawType = spec.rawType();
    ParamValueLoader loader = binder(spec, bindName);
    if (null == loader) {
        Resolve resolve = spec.getAnnotation(Resolve.class);
        if (null != resolve) {
            Class<? extends StringValueResolver>[] resolvers = resolve.value();
            for (Class<? extends StringValueResolver> resolverClass : resolvers) {
                StringValueResolver resolver = injector.get(resolverClass);
                if (rawType.isAssignableFrom(resolver.targetType())) {
                    loader = new StringValueResolverValueLoader(ParamKey.of(bindName), resolver, null, def, rawType);
                }
            }
        }
    }
    if (null == loader) {
        Annotation[] aa = spec.allAnnotations();
        for (Annotation a : aa) {
            Resolve resolve = AnnotationUtil.tagAnnotation(a, Resolve.class);
            if (null != resolve) {
                Class<? extends StringValueResolver>[] resolvers = resolve.value();
                for (Class<? extends StringValueResolver> resolverClass : resolvers) {
                    StringValueResolver resolver = injector.get(resolverClass);
                    resolver.attributes($.evaluate(a));
                    if (rawType.isAssignableFrom(resolver.targetType())) {
                        loader = new StringValueResolverValueLoader(ParamKey.of(bindName), resolver, null, def, rawType);
                        break;
                    }
                }
            }
        }
    }
    if (null == loader) {
        StringValueResolver resolver = null;
        Param param = spec.getAnnotation(Param.class);
        if (null != param) {
            Class<? extends StringValueResolver> resolverClass = param.resolverClass();
            if (Param.DEFAULT_RESOLVER.class != resolverClass) {
                resolver = injector.get(resolverClass);
            }
        }
        if (null == resolver) {
            resolver = resolverManager.resolver(rawType, spec);
        }
        loader = (null != resolver) ? new StringValueResolverValueLoader(ParamKey.of(bindName), resolver, param, def, rawType) : buildLoader(ParamKey.of(bindName), spec);
    }
    return loader;
}
Also used : HeaderVariable(act.inject.HeaderVariable) SessionVariable(act.inject.SessionVariable) Resolve(org.osgl.mvc.annotation.Resolve) Annotation(java.lang.annotation.Annotation) StringValueResolver(org.osgl.util.StringValueResolver) DefaultValue(act.inject.DefaultValue) Param(org.osgl.mvc.annotation.Param)

Example 3 with StringValueResolver

use of org.osgl.util.StringValueResolver in project actframework by actframework.

the class CliContextParamLoader method findIndirectAnnotatedResolver.

private StringValueResolver findIndirectAnnotatedResolver(BeanSpec spec) {
    Annotation[] aa = spec.allAnnotations();
    Class<?> rawType = spec.rawType();
    for (Annotation a : aa) {
        Resolve resolve = AnnotationUtil.tagAnnotation(a, Resolve.class);
        if (null != resolve) {
            Class<? extends StringValueResolver>[] resolvers = resolve.value();
            for (Class<? extends StringValueResolver> resolverClass : resolvers) {
                StringValueResolver resolver = injector.get(resolverClass);
                resolver.attributes($.evaluate(a));
                if (rawType.isAssignableFrom(resolver.targetType())) {
                    return resolver;
                }
            }
        }
    }
    return null;
}
Also used : StringValueResolver(org.osgl.util.StringValueResolver) Resolve(org.osgl.mvc.annotation.Resolve) Annotation(java.lang.annotation.Annotation)

Example 4 with StringValueResolver

use of org.osgl.util.StringValueResolver in project actframework by actframework.

the class WebSocketConnectionHandler method params.

private Object[] params(WebSocketContext context) {
    if (0 == paramCount) {
        return DUMP_PARAMS;
    }
    Object[] params = paramLoaderService.loadMethodParams(host, method, context);
    if (isSingleParam) {
        for (int i = 0; i < paramCount; ++i) {
            if (null == params[i]) {
                String singleVal = context.stringMessage();
                Class<?> paramType = paramTypes[i];
                StringValueResolver resolver = context.app().resolverManager().resolver(paramType);
                if (null != resolver) {
                    params[i] = resolver.apply(singleVal);
                } else {
                    E.unexpected("Cannot determine string value resolver for param type: %s", paramType);
                }
            }
        }
    }
    return params;
}
Also used : StringValueResolver(org.osgl.util.StringValueResolver)

Example 5 with StringValueResolver

use of org.osgl.util.StringValueResolver in project actframework by actframework.

the class StringValueResolverFinder method found.

@Override
protected void found(Class<? extends StringValueResolver> target, App app) {
    if (target == Param.DEFAULT_RESOLVER.class) {
        return;
    }
    if (target.isAnnotationPresent(NoAutoRegister.class)) {
        return;
    }
    StringValueResolver resolver = app.getInstance(target);
    app.resolverManager().register(resolver.targetType(), resolver);
}
Also used : StringValueResolver(org.osgl.util.StringValueResolver) Param(org.osgl.mvc.annotation.Param)

Aggregations

StringValueResolver (org.osgl.util.StringValueResolver)8 Resolve (org.osgl.mvc.annotation.Resolve)3 Annotation (java.lang.annotation.Annotation)2 Param (org.osgl.mvc.annotation.Param)2 Optional (act.cli.Optional)1 Required (act.cli.Required)1 DefaultValue (act.inject.DefaultValue)1 HeaderVariable (act.inject.HeaderVariable)1 SessionVariable (act.inject.SessionVariable)1