Search in sources :

Example 36 with TemplateModelException

use of freemarker.template.TemplateModelException 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 37 with TemplateModelException

use of freemarker.template.TemplateModelException in project ninja by ninjaframework.

the class TemplateEngineFreemarkerWebJarsAtMethod method exec.

public TemplateModel exec(List args) throws TemplateModelException {
    if (args.size() == 0) {
        throw new TemplateModelException("Error. You must specify a webjars asset URL.");
    }
    // Allow a boolean as second argument to force webjars usage
    if (useCDN && (args.size() == 1 || !((TemplateBooleanModel) args.get(1)).getAsBoolean())) {
        String filename = ((SimpleScalar) args.get(0)).getAsString();
        return new SimpleScalar(JSDELIVER_CDN + filename);
    } else {
        List argsWithControllerAndMethod = new ArrayList();
        argsWithControllerAndMethod.add(AssetsController.class.getName());
        argsWithControllerAndMethod.add("serveWebJars");
        argsWithControllerAndMethod.add("fileName");
        argsWithControllerAndMethod.addAll(args);
        return templateEngineFreemarkerReverseRouteHelper.computeReverseRoute(argsWithControllerAndMethod);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) AssetsController(ninja.AssetsController) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SimpleScalar(freemarker.template.SimpleScalar) TemplateBooleanModel(freemarker.template.TemplateBooleanModel)

Example 38 with TemplateModelException

use of freemarker.template.TemplateModelException in project ninja by ninjaframework.

the class TemplateEngineFreemarkerAuthenticityFormDirective method execute.

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    if (!params.isEmpty()) {
        throw new TemplateModelException("This directive doesn't allow parameters.");
    }
    if (loopVars.length != 0) {
        throw new TemplateModelException("This directive doesn't allow loop variables.");
    }
    Writer out = env.getOut();
    out.append("<input type=\"hidden\" value=\"" + context.getSession().getAuthenticityToken() + "\" name=\"" + NinjaConstant.AUTHENTICITY_TOKEN + "\" />");
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Writer(java.io.Writer)

Example 39 with TemplateModelException

use of freemarker.template.TemplateModelException in project leopard by tanhaichao.

the class AbstractTemplateMethod method exec.

@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException {
    // arguments.toArray();
    Object[] args = new Object[arguments.size()];
    int i = 0;
    for (Object obj : arguments) {
        if (obj != null) {
            if (obj instanceof SimpleDate) {
                args[i] = ((SimpleDate) obj).getAsDate();
            } else if (obj instanceof SimpleNumber) {
                args[i] = ((SimpleNumber) obj).getAsNumber();
            } else if (obj instanceof SimpleNumber) {
                args[i] = ((SimpleNumber) obj).getAsNumber();
            } else if (obj instanceof SimpleScalar) {
                args[i] = ((SimpleScalar) obj).toString();
            } else if (obj instanceof StringModel) {
                args[i] = ((StringModel) obj);
            } else {
                logger.error("obj:" + obj.getClass());
                args[i] = obj.toString();
            }
        }
        i++;
    }
    try {
        return this.execute(args);
    } catch (TemplateModelException e) {
        throw e;
    } catch (RuntimeException e) {
        logger.error("key:" + this.getKey() + " clazz:" + this.getClass().getName());
        throw e;
    } catch (Exception e) {
        throw new TemplateModelException(e.getMessage(), e);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleDate(freemarker.template.SimpleDate) SimpleNumber(freemarker.template.SimpleNumber) StringModel(freemarker.ext.beans.StringModel) SimpleScalar(freemarker.template.SimpleScalar) TemplateModelException(freemarker.template.TemplateModelException)

Example 40 with TemplateModelException

use of freemarker.template.TemplateModelException in project openj9 by eclipse.

the class Artifact method get.

public TemplateModel get(String arg0) throws TemplateModelException {
    try {
        if (arg0.equals("name")) {
            return new ArtifactName(artifact);
        }
        if (arg0.equals("data")) {
            return new ArtifactData(artifact);
        }
        if (arg0.equals("targetNameWithRelease")) {
            return new SimpleScalar(UMA.getUma().getPlatform().getTargetNameWithRelease(artifact));
        }
        if (arg0.equals("exportNames")) {
            return new ExportNames(artifact);
        }
        if (arg0.equals("bundledArtifacts")) {
            return new Artifacts(artifact.getAllArtifactsInThisBundleInHashtable());
        }
        TemplateModel platformExtension = com.ibm.uma.UMA.getUma().getPlatform().getDataModelExtension("uma.spec.flags." + artifact.getTargetName(), arg0);
        if (platformExtension != null)
            return platformExtension;
        TemplateModel configurationExtension = com.ibm.uma.UMA.getUma().getConfiguration().getDataModelExtension("uma.spec.flags." + artifact.getTargetName(), arg0);
        if (configurationExtension != null)
            return configurationExtension;
        return null;
    } catch (UMAException e) {
        throw new TemplateModelException(e);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) UMAException(com.ibm.uma.UMAException) SimpleScalar(freemarker.template.SimpleScalar)

Aggregations

TemplateModelException (freemarker.template.TemplateModelException)87 TemplateModel (freemarker.template.TemplateModel)24 IOException (java.io.IOException)21 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 SimpleScalar (freemarker.template.SimpleScalar)17 BeanModel (freemarker.ext.beans.BeanModel)14 Environment (freemarker.core.Environment)13 Writer (java.io.Writer)13 ArrayList (java.util.ArrayList)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Map (java.util.Map)10 List (java.util.List)9 SimpleNumber (freemarker.template.SimpleNumber)7 TemplateHashModel (freemarker.template.TemplateHashModel)7 Iterator (java.util.Iterator)6 freemarker.core._TemplateModelException (freemarker.core._TemplateModelException)5 TemplateHashModelEx (freemarker.template.TemplateHashModelEx)5 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)5 TemplateModelIterator (freemarker.template.TemplateModelIterator)5 UMAException (com.ibm.uma.UMAException)4