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);
}
}
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);
}
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;
}
}
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);
}
}
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);
}
Aggregations