Search in sources :

Example 31 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project freemarker by apache.

the class IncludePage method execute.

public void execute(final Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    // Determine the path
    final TemplateModel path = (TemplateModel) params.get("path");
    if (path == null) {
        throw new _MiscTemplateException(env, "Missing required parameter \"path\"");
    }
    if (!(path instanceof TemplateScalarModel)) {
        throw new _MiscTemplateException(env, "Expected a scalar model. \"path\" is instead ", new _DelayedFTLTypeDescription(path));
    }
    final String strPath = ((TemplateScalarModel) path).getAsString();
    if (strPath == null) {
        throw new _MiscTemplateException(env, "String value of \"path\" parameter is null");
    }
    // See whether we need to use a custom response (if we're inside a TTM
    // or TDM or macro nested body, we'll need to as then the current
    // FM environment writer is not identical to HTTP servlet response
    // writer.
    final Writer envOut = env.getOut();
    final HttpServletResponse wrappedResponse;
    if (envOut == response.getWriter()) {
        // Don't bother wrapping if environment's writer is same as
        // response writer
        wrappedResponse = response;
    } else {
        final PrintWriter printWriter = (envOut instanceof PrintWriter) ? (PrintWriter) envOut : new PrintWriter(envOut);
        // Otherwise, create a response wrapper that will pass the
        // env writer, potentially first wrapping it in a print
        // writer when it ain't one already.
        wrappedResponse = new HttpServletResponseWrapper(response) {

            @Override
            public PrintWriter getWriter() {
                return printWriter;
            }
        };
    }
    // Determine inherit_params value
    final boolean inheritParams;
    final TemplateModel inheritParamsModel = (TemplateModel) params.get("inherit_params");
    if (inheritParamsModel == null) {
        // defaults to true when not specified
        inheritParams = true;
    } else {
        if (!(inheritParamsModel instanceof TemplateBooleanModel)) {
            throw new _MiscTemplateException(env, "\"inherit_params\" should be a boolean but it's a(n) ", inheritParamsModel.getClass().getName(), " instead");
        }
        inheritParams = ((TemplateBooleanModel) inheritParamsModel).getAsBoolean();
    }
    // Get explicit params, if any
    final TemplateModel paramsModel = (TemplateModel) params.get("params");
    // Determine whether we need to wrap the request
    final HttpServletRequest wrappedRequest;
    if (paramsModel == null && inheritParams) {
        // Inherit original request params & no params explicitly
        // specified, so use the original request
        wrappedRequest = request;
    } else {
        // In any other case, use a custom request wrapper
        final Map paramsMap;
        if (paramsModel != null) {
            // Convert params to a Map
            final Object unwrapped = DeepUnwrap.unwrap(paramsModel);
            if (!(unwrapped instanceof Map)) {
                throw new _MiscTemplateException(env, "Expected \"params\" to unwrap into a java.util.Map. It unwrapped into ", unwrapped.getClass().getName(), " instead.");
            }
            paramsMap = (Map) unwrapped;
        } else {
            paramsMap = Collections.EMPTY_MAP;
        }
        wrappedRequest = new CustomParamsRequest(request, paramsMap, inheritParams);
    }
    // Finally, do the include
    try {
        request.getRequestDispatcher(strPath).include(wrappedRequest, wrappedResponse);
    } catch (ServletException e) {
        throw new _MiscTemplateException(e, env);
    }
}
Also used : freemarker.core._MiscTemplateException(freemarker.core._MiscTemplateException) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) freemarker.core._DelayedFTLTypeDescription(freemarker.core._DelayedFTLTypeDescription) TemplateModel(freemarker.template.TemplateModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TemplateScalarModel(freemarker.template.TemplateScalarModel) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) TemplateBooleanModel(freemarker.template.TemplateBooleanModel) PrintWriter(java.io.PrintWriter)

Example 32 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project ofbiz-framework by apache.

the class OfbizCurrencyTransform method getArg.

private static String getArg(Map args, String key) {
    String result = "";
    Object o = args.get(key);
    if (o != null) {
        if (Debug.verboseOn())
            Debug.logVerbose("Arg Object : " + o.getClass().getName(), module);
        if (o instanceof TemplateScalarModel) {
            TemplateScalarModel s = (TemplateScalarModel) o;
            try {
                result = s.getAsString();
            } catch (TemplateModelException e) {
                Debug.logError(e, "Template Exception", module);
            }
        } else {
            result = o.toString();
        }
    }
    return result;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Example 33 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project ofbiz-framework by apache.

the class SeoTransform method getWriter.

public Writer getWriter(final Writer out, Map args) {
    final StringBuffer buf = new StringBuffer();
    final boolean fullPath = checkArg(args, "fullPath", false);
    final boolean secure = checkArg(args, "secure", false);
    final boolean encode = checkArg(args, "encode", true);
    return new Writer(out) {

        public void write(char[] cbuf, int off, int len) {
            buf.append(cbuf, off, len);
        }

        public void flush() throws IOException {
            out.flush();
        }

        public void close() throws IOException {
            try {
                Environment env = Environment.getCurrentEnvironment();
                BeanModel req = (BeanModel) env.getVariable("request");
                BeanModel res = (BeanModel) env.getVariable("response");
                Object prefix = env.getVariable("urlPrefix");
                if (req != null) {
                    HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
                    ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
                    HttpServletResponse response = null;
                    if (res != null) {
                        response = (HttpServletResponse) res.getWrappedObject();
                    }
                    HttpSession session = request.getSession();
                    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
                    // anonymous shoppers are not logged in
                    if (userLogin != null && "anonymous".equals(userLogin.getString("userLoginId"))) {
                        userLogin = null;
                    }
                    RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
                    out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure, encode), userLogin == null));
                } else if (prefix != null) {
                    if (prefix instanceof TemplateScalarModel) {
                        TemplateScalarModel s = (TemplateScalarModel) prefix;
                        String prefixString = s.getAsString();
                        String bufString = buf.toString();
                        boolean prefixSlash = prefixString.endsWith("/");
                        boolean bufSlash = bufString.startsWith("/");
                        if (prefixSlash && bufSlash) {
                            bufString = bufString.substring(1);
                        } else if (!prefixSlash && !bufSlash) {
                            bufString = "/" + bufString;
                        }
                        out.write(prefixString + bufString);
                    }
                } else {
                    out.write(buf.toString());
                }
            } catch (IOException | TemplateModelException e) {
                throw new IOException(e.getMessage());
            }
        }
    };
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) Environment(freemarker.core.Environment) ServletContext(javax.servlet.ServletContext) TemplateScalarModel(freemarker.template.TemplateScalarModel) Writer(java.io.Writer)

Example 34 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project ofbiz-framework by apache.

the class SetRequestAttributeMethod method exec.

/*
     * @see freemarker.template.TemplateMethodModel#exec(java.util.List)
     */
public Object exec(List args) throws TemplateModelException {
    if (args == null || args.size() != 2)
        throw new TemplateModelException("Invalid number of arguements");
    if (!(args.get(0) instanceof TemplateScalarModel))
        throw new TemplateModelException("First argument not an instance of TemplateScalarModel");
    if (!(args.get(1) instanceof BeanModel) && !(args.get(1) instanceof TemplateNumberModel) && !(args.get(1) instanceof TemplateScalarModel))
        throw new TemplateModelException("Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel");
    Environment env = Environment.getCurrentEnvironment();
    BeanModel req = (BeanModel) env.getVariable("request");
    HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
    String name = ((TemplateScalarModel) args.get(0)).getAsString();
    Object value = null;
    if (args.get(1) instanceof TemplateScalarModel)
        value = ((TemplateScalarModel) args.get(1)).getAsString();
    if (args.get(1) instanceof TemplateNumberModel)
        value = ((TemplateNumberModel) args.get(1)).getAsNumber();
    if (args.get(1) instanceof BeanModel)
        value = ((BeanModel) args.get(1)).getWrappedObject();
    request.setAttribute(name, value);
    return new SimpleScalar("");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) TemplateNumberModel(freemarker.template.TemplateNumberModel) Environment(freemarker.core.Environment) TemplateScalarModel(freemarker.template.TemplateScalarModel) SimpleScalar(freemarker.template.SimpleScalar)

Example 35 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project ofbiz-framework by apache.

the class SetContextFieldTransform method exec.

/*
     * @see freemarker.template.TemplateMethodModel#exec(java.util.List)
     */
@SuppressWarnings("unchecked")
public Object exec(List args) throws TemplateModelException {
    if (args == null || args.size() != 2)
        throw new TemplateModelException("Invalid number of arguements");
    if (!(args.get(0) instanceof TemplateScalarModel))
        throw new TemplateModelException("First argument not an instance of TemplateScalarModel");
    if (!(args.get(1) instanceof BeanModel) && !(args.get(1) instanceof TemplateNumberModel) && !(args.get(1) instanceof TemplateScalarModel))
        throw new TemplateModelException("Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel");
    Environment env = Environment.getCurrentEnvironment();
    BeanModel req = (BeanModel) env.getVariable("context");
    Map context = (Map) req.getWrappedObject();
    String name = ((TemplateScalarModel) args.get(0)).getAsString();
    Object value = null;
    if (args.get(1) instanceof TemplateScalarModel)
        value = ((TemplateScalarModel) args.get(1)).getAsString();
    if (args.get(1) instanceof TemplateNumberModel)
        value = ((TemplateNumberModel) args.get(1)).getAsNumber();
    if (args.get(1) instanceof BeanModel)
        value = ((BeanModel) args.get(1)).getWrappedObject();
    context.put(name, value);
    return new SimpleScalar("");
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) TemplateNumberModel(freemarker.template.TemplateNumberModel) Environment(freemarker.core.Environment) TemplateScalarModel(freemarker.template.TemplateScalarModel) Map(java.util.Map) SimpleScalar(freemarker.template.SimpleScalar)

Aggregations

TemplateScalarModel (freemarker.template.TemplateScalarModel)41 TemplateModel (freemarker.template.TemplateModel)18 TemplateModelException (freemarker.template.TemplateModelException)18 TemplateHashModel (freemarker.template.TemplateHashModel)9 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)9 IOException (java.io.IOException)9 TemplateNumberModel (freemarker.template.TemplateNumberModel)8 Environment (freemarker.core.Environment)7 BeanModel (freemarker.ext.beans.BeanModel)7 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)7 SimpleScalar (freemarker.template.SimpleScalar)6 Test (org.junit.Test)6 SimpleSequence (freemarker.template.SimpleSequence)5 Template (freemarker.template.Template)5 TemplateNodeModel (freemarker.template.TemplateNodeModel)5 Writer (java.io.Writer)5 Map (java.util.Map)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 TemplateBooleanModel (freemarker.template.TemplateBooleanModel)4 TemplateException (freemarker.template.TemplateException)3