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