Search in sources :

Example 1 with BaseMethod

use of com.publiccms.common.base.BaseMethod in project PublicCMS-preview by sanluan.

the class MethodController method method.

/**
 * 接口指令统一分发
 *
 * @param name
 * @param appToken
 * @param request
 * @param response
 * @return result
 */
@RequestMapping("method/{name}")
public Object method(@PathVariable String name, String appToken, HttpServletRequest request, HttpServletResponse response) {
    BaseMethod method = methodMap.get(name);
    if (null != method) {
        try {
            if (method.needAppToken()) {
                SysAppToken token = appTokenService.getEntity(appToken);
                if (null == token) {
                    return NEED_APP_TOKEN_MAP;
                }
                SysApp app = appService.getEntity(token.getAppId());
                if (null == app) {
                    return NEED_APP_TOKEN_MAP;
                }
            }
            Map<String, Object> map = new HashMap<>();
            String[] paramters = request.getParameterValues("paramters");
            if (CommonUtils.notEmpty(paramters) && paramters.length >= method.minParamtersNumber()) {
                List<TemplateModel> list = new ArrayList<>();
                for (String paramter : paramters) {
                    list.add(getObjectWrapper().wrap(paramter));
                }
                map.put("result", method.exec(list));
                return map;
            } else if (CommonUtils.empty(paramters) && 0 == method.minParamtersNumber()) {
                map.put("result", method.exec(null));
                return map;
            } else {
                map.put(ERROR, "paramtersError");
                return map;
            }
        } catch (TemplateModelException e) {
            log.error(e.getMessage(), e);
            Map<String, String> map = new HashMap<>();
            map.put(ERROR, ApiController.EXCEPTION);
            return map;
        }
    } else {
        return ApiController.NOT_FOUND_MAP;
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TemplateModel(freemarker.template.TemplateModel) SysApp(com.publiccms.entities.sys.SysApp) BaseMethod(com.publiccms.common.base.BaseMethod) SysAppToken(com.publiccms.entities.sys.SysAppToken) HashMap(java.util.HashMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with BaseMethod

use of com.publiccms.common.base.BaseMethod in project PublicCMS-preview by sanluan.

the class DirectiveComponent method init.

@Autowired
private void init(List<AbstractTemplateDirective> templateDirectiveList, List<AbstractTaskDirective> taskDirectiveList, List<BaseMethod> methodList) {
    for (AbstractTemplateDirective directive : templateDirectiveList) {
        if (null == directive.getName()) {
            directive.setName(getDirectiveName(directive.getClass().getSimpleName()));
        }
        templateDirectiveMap.put(directive.getName(), directive);
    }
    log.info(new StringBuilder().append(templateDirectiveMap.size()).append(" template directives created:").append(templateDirectiveMap.keySet()).toString());
    for (AbstractTaskDirective directive : taskDirectiveList) {
        if (null == directive.getName()) {
            directive.setName(getDirectiveName(directive.getClass().getSimpleName()));
        }
        taskDirectiveMap.put(directive.getName(), directive);
    }
    log.info(new StringBuilder().append(taskDirectiveMap.size()).append(" task directives created:").append(taskDirectiveMap.keySet()).toString());
    for (BaseMethod method : methodList) {
        if (null == method.getName()) {
            method.setName(StringUtils.uncapitalize(method.getClass().getSimpleName().replaceAll(methodRemoveRegex, BLANK)));
        }
        methodMap.put(method.getName(), method);
    }
    log.info(new StringBuilder().append(methodMap.size()).append(" methods created:").append(methodMap.keySet()).toString());
}
Also used : BaseMethod(com.publiccms.common.base.BaseMethod) AbstractTemplateDirective(com.publiccms.common.base.AbstractTemplateDirective) AbstractTaskDirective(com.publiccms.common.base.AbstractTaskDirective) Autowired(org.springframework.beans.factory.annotation.Autowired)

Aggregations

BaseMethod (com.publiccms.common.base.BaseMethod)2 AbstractTaskDirective (com.publiccms.common.base.AbstractTaskDirective)1 AbstractTemplateDirective (com.publiccms.common.base.AbstractTemplateDirective)1 SysApp (com.publiccms.entities.sys.SysApp)1 SysAppToken (com.publiccms.entities.sys.SysAppToken)1 TemplateModel (freemarker.template.TemplateModel)1 TemplateModelException (freemarker.template.TemplateModelException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1