Search in sources :

Example 6 with HttpRequestHashModel

use of freemarker.ext.servlet.HttpRequestHashModel in project ofbiz-framework by apache.

the class ScreenRenderer method populateContextForRequest.

public static void populateContextForRequest(MapStack<String> context, ScreenRenderer screens, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) {
    HttpSession session = request.getSession();
    // attribute names to skip for session and application attributes; these are all handled as special cases, duplicating results and causing undesired messages
    Set<String> attrNamesToSkip = UtilMisc.toSet("delegator", "dispatcher", "security", "webSiteId", "org.apache.catalina.jsp_classpath");
    Map<String, Object> parameterMap = UtilHttp.getCombinedMap(request, attrNamesToSkip);
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    populateBasicContext(context, screens, parameterMap, (Delegator) request.getAttribute("delegator"), (LocalDispatcher) request.getAttribute("dispatcher"), (Security) request.getAttribute("security"), UtilHttp.getLocale(request), userLogin);
    context.put("autoUserLogin", session.getAttribute("autoUserLogin"));
    context.put("person", session.getAttribute("person"));
    context.put("partyGroup", session.getAttribute("partyGroup"));
    // some things also seem to require this, so here it is:
    request.setAttribute("userLogin", userLogin);
    // set up the user's time zone
    context.put("timeZone", UtilHttp.getTimeZone(request));
    // ========== setup values that are specific to OFBiz webapps
    VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
    if (visualTheme == null) {
        String defaultVisualThemeId = EntityUtilProperties.getPropertyValue("general", "VISUAL_THEME", (Delegator) request.getAttribute("delegator"));
        visualTheme = ThemeFactory.getVisualThemeFromId(defaultVisualThemeId);
    }
    context.put("visualTheme", visualTheme);
    context.put("modelTheme", visualTheme.getModelTheme());
    context.put("request", request);
    context.put("response", response);
    context.put("session", session);
    context.put("application", servletContext);
    context.put("webappName", session.getAttribute("_WEBAPP_NAME_"));
    if (servletContext != null) {
        String rootDir = (String) context.get("rootDir");
        String webSiteId = (String) context.get("webSiteId");
        String https = (String) context.get("https");
        if (UtilValidate.isEmpty(rootDir)) {
            rootDir = servletContext.getRealPath("/");
            context.put("rootDir", rootDir);
        }
        if (UtilValidate.isEmpty(webSiteId)) {
            webSiteId = WebSiteWorker.getWebSiteId(request);
            context.put("webSiteId", webSiteId);
        }
        if (UtilValidate.isEmpty(https)) {
            https = (String) servletContext.getAttribute("https");
            context.put("https", https);
        }
    }
    context.put("javaScriptEnabled", Boolean.valueOf(UtilHttp.isJavaScriptEnabled(request)));
    // these ones are FreeMarker specific and will only work in FTL templates, mainly here for backward compatibility
    context.put("sessionAttributes", new HttpSessionHashModel(session, FreeMarkerWorker.getDefaultOfbizWrapper()));
    context.put("requestAttributes", new HttpRequestHashModel(request, FreeMarkerWorker.getDefaultOfbizWrapper()));
    TaglibFactory JspTaglibs = new TaglibFactory(servletContext);
    context.put("JspTaglibs", JspTaglibs);
    context.put("requestParameters", UtilHttp.getParameterMap(request));
    ServletContextHashModel ftlServletContext = (ServletContextHashModel) request.getAttribute("ftlServletContext");
    context.put("Application", ftlServletContext);
    context.put("Request", context.get("requestAttributes"));
    // some information from/about the ControlServlet environment
    context.put("controlPath", request.getAttribute("_CONTROL_PATH_"));
    context.put("contextRoot", request.getAttribute("_CONTEXT_ROOT_"));
    context.put("serverRoot", request.getAttribute("_SERVER_ROOT_URL_"));
    context.put("checkLoginUrl", LoginWorker.makeLoginUrl(request));
    String externalLoginKey = null;
    boolean externalLoginKeyEnabled = "true".equals(EntityUtilProperties.getPropertyValue("security", "security.login.externalLoginKey.enabled", "true", (Delegator) request.getAttribute("delegator")));
    if (externalLoginKeyEnabled) {
        externalLoginKey = ExternalLoginKeysManager.getExternalLoginKey(request);
    }
    String externalKeyParam = externalLoginKey == null ? "" : "&amp;externalLoginKey=" + externalLoginKey;
    context.put("externalLoginKey", externalLoginKey);
    context.put("externalKeyParam", externalKeyParam);
    // setup message lists
    List<String> eventMessageList = UtilGenerics.toList(request.getAttribute("eventMessageList"));
    if (eventMessageList == null) {
        eventMessageList = new LinkedList<>();
    }
    List<String> errorMessageList = UtilGenerics.toList(request.getAttribute("errorMessageList"));
    if (errorMessageList == null) {
        errorMessageList = new LinkedList<>();
    }
    if (request.getAttribute("_EVENT_MESSAGE_") != null) {
        eventMessageList.add(UtilFormatOut.replaceString((String) request.getAttribute("_EVENT_MESSAGE_"), "\n", "<br/>"));
        request.removeAttribute("_EVENT_MESSAGE_");
    }
    List<String> msgList = UtilGenerics.toList(request.getAttribute("_EVENT_MESSAGE_LIST_"));
    if (msgList != null) {
        eventMessageList.addAll(msgList);
        request.removeAttribute("_EVENT_MESSAGE_LIST_");
    }
    if (request.getAttribute("_ERROR_MESSAGE_") != null) {
        errorMessageList.add(UtilFormatOut.replaceString((String) request.getAttribute("_ERROR_MESSAGE_"), "\n", "<br/>"));
        request.removeAttribute("_ERROR_MESSAGE_");
    }
    if (session.getAttribute("_ERROR_MESSAGE_") != null) {
        errorMessageList.add(UtilFormatOut.replaceString((String) session.getAttribute("_ERROR_MESSAGE_"), "\n", "<br/>"));
        session.removeAttribute("_ERROR_MESSAGE_");
    }
    msgList = UtilGenerics.toList(request.getAttribute("_ERROR_MESSAGE_LIST_"));
    if (msgList != null) {
        errorMessageList.addAll(msgList);
        request.removeAttribute("_ERROR_MESSAGE_LIST_");
    }
    context.put("eventMessageList", eventMessageList);
    context.put("errorMessageList", errorMessageList);
    if (request.getAttribute("serviceValidationException") != null) {
        context.put("serviceValidationException", request.getAttribute("serviceValidationException"));
        request.removeAttribute("serviceValidationException");
    }
    // if there was an error message, this is an error
    context.put("isError", errorMessageList.size() > 0 ? Boolean.TRUE : Boolean.FALSE);
    // if a parameter was passed saying this is an error, it is an error
    if ("true".equals(parameterMap.get("isError"))) {
        context.put("isError", Boolean.TRUE);
    }
    // to preserve these values, push the MapStack
    context.push();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) HttpSession(javax.servlet.http.HttpSession) TaglibFactory(freemarker.ext.jsp.TaglibFactory) HttpSessionHashModel(freemarker.ext.servlet.HttpSessionHashModel) Delegator(org.apache.ofbiz.entity.Delegator) ServletContextHashModel(freemarker.ext.servlet.ServletContextHashModel)

Example 7 with HttpRequestHashModel

use of freemarker.ext.servlet.HttpRequestHashModel in project ofbiz-framework by apache.

the class FreeMarkerViewHandler method prepOfbizRoot.

public static void prepOfbizRoot(Map<String, Object> root, HttpServletRequest request, HttpServletResponse response) {
    ServletContext servletContext = (ServletContext) request.getAttribute("servletContext");
    HttpSession session = request.getSession();
    // add in the OFBiz objects
    root.put("delegator", request.getAttribute("delegator"));
    root.put("dispatcher", request.getAttribute("dispatcher"));
    root.put("security", request.getAttribute("security"));
    root.put("userLogin", session.getAttribute("userLogin"));
    // add the response object (for transforms) to the context as a BeanModel
    root.put("response", response);
    // add the application object (for transforms) to the context as a BeanModel
    root.put("application", servletContext);
    // add the servlet context -- this has been deprecated, and now requires servlet, do we really need it?
    // root.put("applicationAttributes", new ServletContextHashModel(servletContext, FreeMarkerWorker.getDefaultOfbizWrapper()));
    // add the session object (for transforms) to the context as a BeanModel
    root.put("session", session);
    // add the session
    root.put("sessionAttributes", new HttpSessionHashModel(session, FreeMarkerWorker.getDefaultOfbizWrapper()));
    // add the request object (for transforms) to the context as a BeanModel
    root.put("request", request);
    // add the request
    root.put("requestAttributes", new HttpRequestHashModel(request, FreeMarkerWorker.getDefaultOfbizWrapper()));
    // add the request parameters -- this now uses a Map from UtilHttp
    Map<String, Object> requestParameters = UtilHttp.getParameterMap(request);
    root.put("requestParameters", requestParameters);
    // add the TabLibFactory
    TaglibFactory JspTaglibs = new TaglibFactory(servletContext);
    root.put("JspTaglibs", JspTaglibs);
}
Also used : HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) HttpSessionHashModel(freemarker.ext.servlet.HttpSessionHashModel) HttpSession(javax.servlet.http.HttpSession) ServletContext(javax.servlet.ServletContext) TaglibFactory(freemarker.ext.jsp.TaglibFactory)

Example 8 with HttpRequestHashModel

use of freemarker.ext.servlet.HttpRequestHashModel in project wombat by PLOS.

the class AppLinkDirective method getValue.

@Override
protected String getValue(Environment env, Map params) throws TemplateModelException {
    Object pathObj = params.get("path");
    if (!(pathObj instanceof TemplateScalarModel)) {
        throw new RuntimeException("path parameter required");
    }
    String path = ((TemplateScalarModel) pathObj).getAsString();
    HttpServletRequest request = ((HttpRequestHashModel) env.getDataModel().get("Request")).getRequest();
    return request.getContextPath() + "/" + path;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Aggregations

HttpRequestHashModel (freemarker.ext.servlet.HttpRequestHashModel)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 TaglibFactory (freemarker.ext.jsp.TaglibFactory)4 HttpSessionHashModel (freemarker.ext.servlet.HttpSessionHashModel)3 ServletContextHashModel (freemarker.ext.servlet.ServletContextHashModel)3 HttpSession (javax.servlet.http.HttpSession)3 TemplateModel (freemarker.template.TemplateModel)2 Map (java.util.Map)2 ServletContext (javax.servlet.ServletContext)2 AllHttpScopesHashModel (freemarker.ext.servlet.AllHttpScopesHashModel)1 HttpRequestParametersHashModel (freemarker.ext.servlet.HttpRequestParametersHashModel)1 TemplateModelException (freemarker.template.TemplateModelException)1 TemplateScalarModel (freemarker.template.TemplateScalarModel)1 LinkedHashMap (java.util.LinkedHashMap)1 GenericServlet (javax.servlet.GenericServlet)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Site (org.ambraproject.wombat.config.site.Site)1 SitePageContext (org.ambraproject.wombat.freemarker.SitePageContext)1 Delegator (org.apache.ofbiz.entity.Delegator)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1