Search in sources :

Example 1 with StringValueResolverManager

use of act.app.data.StringValueResolverManager in project actframework by actframework.

the class Endpoint method exploreParamInfo.

private void exploreParamInfo(Method method) {
    Type[] paramTypes = method.getGenericParameterTypes();
    int paramCount = paramTypes.length;
    if (0 == paramCount) {
        return;
    }
    DependencyInjector injector = Act.injector();
    Annotation[][] allAnnos = method.getParameterAnnotations();
    Map<String, Object> sampleData = new HashMap<>();
    StringValueResolverManager resolver = Act.app().resolverManager();
    List<String> sampleQuery = new ArrayList<>();
    for (int i = 0; i < paramCount; ++i) {
        Type type = paramTypes[i];
        Annotation[] annos = allAnnos[i];
        ParamInfo info = paramInfo(type, annos, injector, null);
        if (null != info) {
            params.add(info);
            if (path.contains("{" + info.getName() + "}")) {
                // no sample data for URL path variable
                continue;
            }
            Object sample;
            if (null != info.defaultValue) {
                sample = resolver.resolve(info.defaultValue, info.beanSpec.rawType());
            } else {
                sample = generateSampleData(info.beanSpec, new HashSet<Type>(), new ArrayList<String>());
            }
            if (H.Method.GET == this.httpMethod) {
                String query = generateSampleQuery(info.beanSpec.withoutName(), info.bindName, new HashSet<Type>(), C.<String>newList());
                if (S.notBlank(query)) {
                    sampleQuery.add(query);
                }
            } else {
                sampleData.put(info.bindName, sample);
            }
        }
    }
    if (!sampleData.isEmpty()) {
        sampleJsonPost = JSON.toJSONString(sampleData, true);
    }
    if (!sampleQuery.isEmpty()) {
        this.sampleQuery = S.join("&", sampleQuery);
    }
}
Also used : DependencyInjector(act.inject.DependencyInjector) Annotation(java.lang.annotation.Annotation) StringValueResolverManager(act.app.data.StringValueResolverManager) ISObject(org.osgl.storage.ISObject)

Example 2 with StringValueResolverManager

use of act.app.data.StringValueResolverManager in project actframework by actframework.

the class App method initResolverManager.

private void initResolverManager() {
    resolverManager = new StringValueResolverManager(this);
    Osgl.propertyHandlerFactory = new ActPropertyHandlerFactory(this);
}
Also used : StringValueResolverManager(act.app.data.StringValueResolverManager) ActPropertyHandlerFactory(act.data.util.ActPropertyHandlerFactory)

Example 3 with StringValueResolverManager

use of act.app.data.StringValueResolverManager in project actframework by actframework.

the class JsonParamValueLoader method findDefValProvider.

private static Provider findDefValProvider(BeanSpec beanSpec, DependencyInjector<?> injector) {
    final Class c = beanSpec.rawType();
    final StringValueResolverManager resolver = App.instance().resolverManager();
    if (c.isPrimitive()) {
        return new Provider() {

            @Override
            public Object get() {
                return resolver.resolve(null, c);
            }
        };
    } else if (Collection.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c)) {
        return injector.getProvider(c);
    } else if (c.isArray()) {
        return new Provider() {

            @Override
            public Object get() {
                return Array.newInstance(c.getComponentType(), 0);
            }
        };
    } else {
        return NULL_PROVIDER;
    }
}
Also used : StringValueResolverManager(act.app.data.StringValueResolverManager) Provider(javax.inject.Provider)

Example 4 with StringValueResolverManager

use of act.app.data.StringValueResolverManager in project actframework by actframework.

the class ResourceLoader method _load.

protected static Object _load(String resourcePath, BeanSpec spec, boolean ignoreResourceNotFound) {
    URL url = loadResource(resourcePath);
    if (null == url) {
        if (!ignoreResourceNotFound) {
            LOGGER.warn("resource not found: " + resourcePath);
        }
        return null;
    }
    boolean isJson = resourcePath.endsWith(".json");
    Class<?> rawType = spec.rawType();
    if (URL.class == rawType) {
        return url;
    } else if (String.class == rawType) {
        return IO.readContentAsString(url);
    } else if (byte[].class == rawType) {
        return readContent(url);
    } else if (List.class.equals(rawType)) {
        List<Type> typeParams = spec.typeParams();
        if (!typeParams.isEmpty()) {
            if (String.class == typeParams.get(0)) {
                return IO.readLines(url);
            } else if (isJson) {
                return JSON.parseObject(IO.readContentAsString(url), spec.type());
            }
        }
    } else if (Collection.class.isAssignableFrom(rawType)) {
        List<Type> typeParams = spec.typeParams();
        if (!typeParams.isEmpty()) {
            Collection col = (Collection) Act.getInstance(rawType);
            if (String.class == typeParams.get(0)) {
                col.addAll(IO.readLines(url));
                return col;
            } else {
                StringValueResolverManager resolverManager = Act.app().resolverManager();
                try {
                    Class componentType = spec.componentSpec().rawType();
                    List<String> stringList = IO.readLines(url);
                    for (String line : stringList) {
                        col.add(resolverManager.resolve(line, componentType));
                    }
                } catch (RuntimeException e) {
                    throw new UnexpectedException("return type not supported: " + spec);
                }
            }
        }
    } else if (ByteBuffer.class == rawType) {
        byte[] ba = readContent(url);
        ByteBuffer buffer = ByteBuffer.allocateDirect(ba.length);
        buffer.put(ba);
        buffer.flip();
        return buffer;
    } else if (Path.class.isAssignableFrom(rawType)) {
        try {
            return Paths.get(url.toURI());
        } catch (URISyntaxException exception) {
            throw E.unexpected(exception);
        }
    } else if (File.class.isAssignableFrom(rawType)) {
        return new File(url.getFile());
    } else if (ISObject.class.isAssignableFrom(rawType)) {
        return SObject.of(readContent(url));
    } else if (InputStream.class == rawType) {
        return IO.is(url);
    } else if (Reader.class == rawType) {
        return new InputStreamReader(IO.is(url));
    }
    if (isJson) {
        return JSON.parseObject(IO.readContentAsString(url), spec.type());
    }
    try {
        return Act.app().resolverManager().resolve(IO.readContentAsString(url), rawType);
    } catch (RuntimeException e) {
        throw new UnexpectedException("return type not supported: " + spec);
    }
}
Also used : UnexpectedException(org.osgl.exception.UnexpectedException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) URISyntaxException(java.net.URISyntaxException) ByteBuffer(java.nio.ByteBuffer) URL(java.net.URL) Type(java.lang.reflect.Type) StringValueResolverManager(act.app.data.StringValueResolverManager) Collection(java.util.Collection) List(java.util.List) File(java.io.File)

Example 5 with StringValueResolverManager

use of act.app.data.StringValueResolverManager in project actframework by actframework.

the class AutoConfigPluginTest method before.

@Before
public void before() throws Exception {
    setup();
    resolverManager = new StringValueResolverManager(mockApp);
    when(mockApp.resolverManager()).thenReturn(resolverManager);
    injector = new GenieInjector(mockApp);
    when(mockApp.injector()).thenReturn(injector);
    GenericPluginManager pluginManager = mock(GenericPluginManager.class);
    Field field = Act.class.getDeclaredField("pluginManager");
    field.setAccessible(true);
    field.set(null, pluginManager);
    when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".int")))).thenReturn(intVal);
    when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".long")))).thenReturn(longVal);
    when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".bool")))).thenReturn(boolVal);
    when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".str")))).thenReturn(stringVal);
    when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".method")))).thenReturn(enumVal);
    when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".list")))).thenReturn(intListVal);
}
Also used : Field(java.lang.reflect.Field) GenericPluginManager(act.plugin.GenericPluginManager) GenieInjector(act.inject.genie.GenieInjector) StringValueResolverManager(act.app.data.StringValueResolverManager) Before(org.junit.Before)

Aggregations

StringValueResolverManager (act.app.data.StringValueResolverManager)5 ActPropertyHandlerFactory (act.data.util.ActPropertyHandlerFactory)1 DependencyInjector (act.inject.DependencyInjector)1 GenieInjector (act.inject.genie.GenieInjector)1 GenericPluginManager (act.plugin.GenericPluginManager)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Type (java.lang.reflect.Type)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ByteBuffer (java.nio.ByteBuffer)1 Collection (java.util.Collection)1 List (java.util.List)1 Provider (javax.inject.Provider)1 Before (org.junit.Before)1 UnexpectedException (org.osgl.exception.UnexpectedException)1 ISObject (org.osgl.storage.ISObject)1