use of org.osgl.mvc.result.BadRequest in project actframework by actframework.
the class ReflectedHandlerInvoker method ensureJsonDTOGenerated.
private void ensureJsonDTOGenerated(ActionContext context) {
if (0 == fieldsAndParamsCount || !context.jsonEncoded()) {
return;
}
Class<? extends JsonDTO> dtoClass = jsonDTOClassManager.get(controllerClass, method);
if (null == dtoClass) {
// there are neither fields nor params
return;
}
try {
JsonDTO dto = JSON.parseObject(patchedJsonBody(context), dtoClass);
cacheJsonDTO(context, dto);
} catch (JSONException e) {
if (e.getCause() != null) {
warn(e.getCause(), "error parsing JSON data");
} else {
warn(e, "error parsing JSON data");
}
throw new BadRequest(e.getCause());
}
}
use of org.osgl.mvc.result.BadRequest in project actframework by actframework.
the class WebSocketConnectionHandler method ensureJsonDTOGenerated.
private void ensureJsonDTOGenerated(WebSocketContext context) {
if (0 == fieldsAndParamsCount || !context.isJson()) {
return;
}
Class<? extends JsonDTO> dtoClass = jsonDTOClassManager.get(handlerClass, method);
if (null == dtoClass) {
// there are neither fields nor params
return;
}
try {
JsonDTO dto = JSON.parseObject(patchedJsonBody(context), dtoClass);
context.attribute(JsonDTO.CTX_ATTR_KEY, dto);
} catch (JSONException e) {
if (e.getCause() != null) {
logger.warn(e.getCause(), "error parsing JSON data");
} else {
logger.warn(e, "error parsing JSON data");
}
throw new BadRequest(e.getCause());
}
}
use of org.osgl.mvc.result.BadRequest in project actframework by actframework.
the class MapLoader method load.
@Override
public Object load(Object bean, ActContext<?> context, boolean noDefaultValue) {
ParamTree tree = ParamValueLoaderService.ensureParamTree(context);
ParamTreeNode node = tree.node(key);
if (null == node) {
return noDefaultValue ? null : injector.get(mapClass);
}
Map map = null == bean ? injector.get(mapClass) : (Map) bean;
if (node.isList()) {
if (Integer.class != keyClass) {
throw new BadRequest("cannot load list into map with key type: %s", this.keyClass);
}
List<ParamTreeNode> list = node.list();
for (int i = 0; i < list.size(); ++i) {
ParamTreeNode elementNode = list.get(i);
if (!elementNode.isLeaf()) {
throw new BadRequest("cannot parse param: expect leaf node, found: \n%s", node.debug());
}
if (null == valueResolver) {
throw E.unexpected("Component type not resolvable: %s", valType);
}
if (null != elementNode.value()) {
map.put(i, valueResolver.resolve(elementNode.value()));
}
}
} else if (node.isMap()) {
Set<String> childrenKeys = node.mapKeys();
Class valClass = BeanSpec.rawTypeOf(valType);
for (String s : childrenKeys) {
ParamTreeNode child = node.child(s);
Object key = s;
if (String.class != keyClass) {
key = keyResolver.resolve(s);
}
if (child.isLeaf()) {
if (null == valueResolver) {
throw E.unexpected("Component type not resolvable: %s", valType);
}
String sval = child.value();
if (null == sval) {
continue;
}
if (valClass != String.class) {
Object value = valueResolver.resolve(sval);
if (!valClass.isInstance(value)) {
throw new BadRequest("Cannot load parameter, expected type: %s, found: %s", valClass, value.getClass());
}
map.put(key, value);
} else {
map.put(key, sval);
}
} else {
ParamValueLoader childLoader = childLoader(child.key());
Object value = childLoader.load(null, context, false);
if (null != value) {
if (!valClass.isInstance(value)) {
throw new BadRequest("Cannot load parameter, expected type: %s, found: %s", valClass, value.getClass());
}
map.put(key, value);
}
}
}
} else {
// try evaluate the Map instance from string value
// to support Matrix style URL path variable
String value = node.value();
if (S.notBlank(value)) {
// ";" has higher priority in common separators
String[] pairs = value.split(value.contains(";") ? ";" : S.COMMON_SEP);
for (String pair : pairs) {
if (!pair.contains("=")) {
throw new BadRequest("Cannot load parameter, expected map, found:%s", node.value());
}
String sKey = S.beforeFirst(pair, "=");
String sVal = S.afterFirst(pair, "=");
Object oKey = keyResolver.resolve(sKey);
Object oVal = valueResolver.resolve(sVal);
map.put(oKey, oVal);
}
}
}
return map;
}
Aggregations