Search in sources :

Example 31 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class ConfigurationScriptJobResolverTest method setUpSiteContext.

private void setUpSiteContext(SiteContext siteContext, ContentStoreService storeService) throws Exception {
    XMLConfiguration config = new XMLConfiguration("config/site.xml");
    when(siteContext.getSiteName()).thenReturn("default");
    when(siteContext.getContext()).thenReturn(mock(Context.class));
    when(siteContext.getStoreService()).thenReturn(storeService);
    when(siteContext.getConfig()).thenReturn(config);
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) JobContext(org.craftercms.engine.util.quartz.JobContext) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration)

Example 32 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class CandidateTargetedUrlsResolverImplTest method setUpCurrentSiteContext.

private void setUpCurrentSiteContext() {
    HierarchicalConfiguration config = mock(HierarchicalConfiguration.class);
    when(config.getStringArray(ROOT_FOLDERS_CONFIG_KEY)).thenReturn(ROOT_FOLDERS);
    SiteContext context = new SiteContext();
    context.setSiteName(SITE_NAME);
    context.setConfig(config);
    SiteContext.setCurrent(context);
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration)

Example 33 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class TargetedContentDescriptorMergeStrategyTest method setUpCurrentSiteContext.

private void setUpCurrentSiteContext() {
    HierarchicalConfiguration config = mock(HierarchicalConfiguration.class);
    when(config.getStringArray(ROOT_FOLDERS_CONFIG_KEY)).thenReturn(ROOT_FOLDERS);
    when(config.getStringArray(AVAILABLE_TARGET_IDS_CONFIG_KEY)).thenReturn(AVAILABLE_TARGET_IDS);
    when(config.getString(FALLBACK_ID_CONFIG_KEY)).thenReturn(FALLBACK_TARGET_ID);
    SiteContext siteContext = new SiteContext();
    siteContext.setSiteName(SITE_NAME);
    siteContext.setConfig(config);
    SiteContext.setCurrent(siteContext);
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration)

Example 34 with SiteContext

use of org.craftercms.engine.service.context.SiteContext 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)

Example 35 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class RestScriptsController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    SiteContext siteContext = SiteContext.getCurrent();
    ScriptFactory scriptFactory = siteContext.getScriptFactory();
    if (scriptFactory == null) {
        throw new IllegalStateException("No script factory associate to current site context '" + siteContext.getSiteName() + "'");
    }
    String serviceUrl = getServiceUrl(request);
    String scriptUrl = getScriptUrl(scriptFactory, siteContext, request, serviceUrl);
    Map<String, Object> scriptVariables = createScriptVariables(request, response);
    scriptUrl = parseScriptUrlForVariables(siteContext, scriptUrl, scriptVariables);
    Object responseBody = executeScript(scriptFactory, scriptVariables, request, response, scriptUrl);
    if (response.isCommitted()) {
        // If response has been already committed by the script, just return null
        logger.debug("Response already committed by script " + scriptUrl);
        return null;
    }
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject(responseBodyModelAttributeName, responseBody);
    return modelAndView;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) ModelAndView(org.springframework.web.servlet.ModelAndView) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Aggregations

SiteContext (org.craftercms.engine.service.context.SiteContext)35 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)8 Context (org.craftercms.core.service.Context)7 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)6 HashMap (java.util.HashMap)4 ServletContext (javax.servlet.ServletContext)4 XMLConfiguration (org.apache.commons.configuration.XMLConfiguration)4 RequestContext (org.craftercms.commons.http.RequestContext)4 Callback (org.craftercms.commons.lang.Callback)3 ContentStoreService (org.craftercms.core.service.ContentStoreService)3 Script (org.craftercms.engine.scripting.Script)3 TemplateException (freemarker.template.TemplateException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Configuration (org.apache.commons.configuration.Configuration)2 CrafterException (org.craftercms.core.exception.CrafterException)2 Content (org.craftercms.core.service.Content)2 ConfigurationException (org.craftercms.engine.exception.ConfigurationException)2 JobContext (org.craftercms.engine.util.quartz.JobContext)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2