cn.leancloud.EngineHookHandlerInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloud-function-sdk Show documentation
Show all versions of cloud-function-sdk Show documentation
leancloud leanengine cloud function library
package cn.leancloud;
import com.alibaba.fastjson.JSON;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.AVUtils;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class EngineHookHandlerInfo extends EngineHandlerInfo {
public EngineHookHandlerInfo(String endpoint, Method handlerMethod,
List params, Class returnType, String hookKey, String hookClass) {
super(endpoint, handlerMethod, params, returnType, hookKey, hookClass);
}
@Override
public Object parseParams(String requestBody) {
Map hookParams = JSON.parseObject(requestBody, Map.class);
Map currentUser = (Map) hookParams.get("user");
if (currentUser != null) {
AVUser user = new AVUser();
AVUtils.copyPropertiesFromMapToAVObject(currentUser, user);
AVUser.changeCurrentUser(user, true);
}
AVObject param = null;
EngineFunctionParamInfo paramInfo = methodParameterList.get(0);
if (AVUser.class.isAssignableFrom(paramInfo.type)) {
param = new AVUser();
} else {
param = AVUtils.newAVObjectByClassName(hookClass);
}
EngineRequestContext.parseMetaData((Map) hookParams.get(paramInfo.getName()));
AVUtils.copyPropertiesFromMapToAVObject(
(Map) hookParams.get(paramInfo.getName()), param);
return param;
}
@Override
public Object wrapperResponse(Object result, String requestBody, boolean rpcCall) {
Map hookParams = new HashMap();
if (result != null) {
Map objectMapping =
(Map) AVUtils.getParsedObject(result, true, true, false, true, true);
objectMapping.remove("__type");
objectMapping.remove("className");
hookParams.putAll(objectMapping);
}
return hookParams;
}
}