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);
}
}
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;
}
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());
}
}
};
}
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("");
}
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("");
}
Aggregations