Search in sources :

Example 6 with Script

use of org.craftercms.engine.scripting.Script in project engine by craftercms.

the class ScriptFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    List<FilterMapping> filterMappings = getFilterMappings();
    if (CollectionUtils.isNotEmpty(filterMappings)) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String requestUri = HttpUtils.getRequestUriWithoutContextPath(httpRequest);
        List<Script> scripts = new ArrayList<>();
        for (FilterMapping mapping : filterMappings) {
            if (!excludeRequest(requestUri, mapping.excludes) && includeRequest(requestUri, mapping.includes)) {
                scripts.add(mapping.script);
            }
        }
        if (CollectionUtils.isNotEmpty(scripts)) {
            chain = new ScriptFilterChainImpl(scripts.iterator(), chain, servletContext);
        }
    }
    chain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Script(org.craftercms.engine.scripting.Script) ArrayList(java.util.ArrayList)

Example 7 with Script

use of org.craftercms.engine.scripting.Script in project engine by craftercms.

the class PageRenderController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String pageUrl;
    SiteContext siteContext = SiteContext.getCurrent();
    if (siteContext != null) {
        if (siteContext.isFallback()) {
            logger.warn("Rendering fallback page [" + fallbackPageUrl + "]");
            pageUrl = fallbackPageUrl;
        } else {
            pageUrl = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
            if (StringUtils.isEmpty(pageUrl)) {
                throw new IllegalStateException("Required request attribute '" + HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE + "' is not set");
            }
            Script controllerScript = getControllerScript(siteContext, request, pageUrl);
            if (controllerScript != null) {
                Map<String, Object> model = new HashMap<>();
                Map<String, Object> variables = createScriptVariables(request, response, model);
                String viewName = executeScript(controllerScript, variables);
                if (StringUtils.isNotEmpty(viewName)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Rendering view " + viewName + " returned by script " + controllerScript);
                    }
                    return new ModelAndView(viewName, model);
                } else {
                    return null;
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Rendering page [" + pageUrl + "]");
            }
        }
    } else {
        throw new IllegalStateException("No current site context found");
    }
    return new ModelAndView(pageUrl);
}
Also used : Script(org.craftercms.engine.scripting.Script) HashMap(java.util.HashMap) SiteContext(org.craftercms.engine.service.context.SiteContext) ModelAndView(org.springframework.web.servlet.ModelAndView)

Aggregations

Script (org.craftercms.engine.scripting.Script)7 HashMap (java.util.HashMap)3 SiteContext (org.craftercms.engine.service.context.SiteContext)3 TemplateException (freemarker.template.TemplateException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)2 TemplateModelException (freemarker.template.TemplateModelException)1 ServletException (javax.servlet.ServletException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CachingAwareObject (org.craftercms.core.util.cache.CachingAwareObject)1 ScriptException (org.craftercms.engine.exception.ScriptException)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1