use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class ExecuteControllerDirective method createScriptVariables.
protected Map<String, Object> createScriptVariables(Environment env) throws TemplateException {
Map<String, Object> variables = new HashMap<String, Object>();
RequestContext context = RequestContext.getCurrent();
SiteItem contentModel = getContentModel(env);
Object templateModel = getTemplateModel(env);
if (context != null) {
GroovyScriptUtils.addSiteItemScriptVariables(variables, context.getRequest(), context.getResponse(), servletContext, contentModel, templateModel);
} else {
throw new IllegalStateException("No current request context found");
}
return variables;
}
use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class RenderComponentDirective method execute.
@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
TemplateModel componentParam = (TemplateModel) params.get(COMPONENT_PARAM_NAME);
TemplateModel componentPathParam = (TemplateModel) params.get(COMPONENT_PATH_PARAM_NAME);
TemplateModel additionalModelParam = (TemplateModel) params.get(ADDITIONAL_MODEL_PARAM_NAME);
Map<String, Object> additionalModel = null;
SiteItem component;
if (componentParam == null && componentPathParam == null) {
throw new TemplateException("No '" + COMPONENT_PARAM_NAME + "' or '" + COMPONENT_PATH_PARAM_NAME + "' param specified", env);
} else if (componentParam != null) {
component = getComponentFromNode(componentParam, env);
} else {
component = getComponentFromPath(componentPathParam, env);
}
if (additionalModelParam != null) {
additionalModel = unwrap(ADDITIONAL_MODEL_PARAM_NAME, additionalModelParam, Map.class, env);
}
Map<String, Object> templateModel = executeScripts(component, additionalModel, env);
SimpleHash model = getFullModel(component, templateModel, additionalModel);
Template template = getTemplate(component, env);
Writer output = env.getOut();
processComponentTemplate(template, model, output, env);
}
use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class SiteItemScriptResolverImplTest method testGetScriptUrlsExisting.
@Test
public void testGetScriptUrlsExisting() throws Exception {
Item item = mock(Item.class);
when(item.queryDescriptorValue("*/content-type")).thenReturn("/page/mypage1");
when(item.queryDescriptorValues("*/scripts/item/key")).thenReturn(Arrays.asList("/scripts/pages/script1.groovy"));
SiteItem siteItem = mock(SiteItem.class);
when(siteItem.getItem()).thenReturn(item);
List<String> scriptUrls = scriptResolver.getScriptUrls(siteItem);
assertNotNull(scriptUrls);
assertEquals(2, scriptUrls.size());
assertEquals("/scripts/pages/mypage1.groovy", scriptUrls.get(0));
assertEquals("/scripts/pages/script1.groovy", scriptUrls.get(1));
}
use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class SiteItemScriptResolverImplTest method testGetScriptUrlError.
@Test
public void testGetScriptUrlError() throws Exception {
Item item = mock(Item.class);
when(item.queryDescriptorValue("*/content-type")).thenReturn("/page/mypage3");
when(item.queryDescriptorValues("*/scripts/item/key")).thenReturn(Arrays.asList("/scripts/pages/script1.groovy"));
SiteItem siteItem = mock(SiteItem.class);
when(siteItem.getItem()).thenReturn(item);
List<String> scriptUrls = scriptResolver.getScriptUrls(siteItem);
assertNotNull(scriptUrls);
assertEquals(1, scriptUrls.size());
assertEquals("/scripts/pages/script1.groovy", scriptUrls.get(0));
}
use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class CrafterPageViewResolver method getCachedLocalizedView.
protected View getCachedLocalizedView(final String url, final Locale locale) {
final SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
return cacheTemplate.getObject(siteContext.getContext(), cachingOptions, () -> {
SiteItem page = getPage(url);
if (page != null) {
String redirectUrl = page.getItem().queryDescriptorValue(redirectUrlXPathQuery);
String contentType = page.getItem().queryDescriptorValue(contentTypeXPathQuery);
String forceHttps = page.getItem().queryDescriptorValue(forceHttpsXPathQuery);
if (StringUtils.isNotEmpty(contentType) && StringUtils.equalsIgnoreCase(redirectContentType, contentType) && StringUtils.isNotEmpty(redirectUrl)) {
if (logger.isDebugEnabled()) {
logger.debug("Redirecting page @ " + url + " to URL " + redirectUrl);
}
return getRedirectView(redirectUrl, true);
} else if (StringUtils.isNotEmpty(forceHttps) && Boolean.parseBoolean(forceHttps)) {
if (logger.isDebugEnabled()) {
logger.debug("Forcing HTTPS on page @ " + url);
}
return getCurrentPageHttpsRedirectView();
} else {
UserAgentAwareCrafterPageView view = new UserAgentAwareCrafterPageView();
view.setServletContext(getServletContext());
view.setPage(page);
view.setLocale(locale);
view.setSiteItemService(siteItemService);
view.setPageViewNameXPathQuery(pageViewNameXPathQuery);
view.setMimeTypeXPathQuery(mimeTypeXPathQuery);
view.setDelegatedViewResolver(delegatedViewResolver);
view.setUserAgentTemplateDetector(userAgentTemplateDetector);
loadScripts(siteContext.getScriptFactory(), page, view);
view.addDependencyKey(page.getItem().getKey());
return applyLifecycleMethods(page.getStoreUrl(), view);
}
} else {
// Return null to continue with the ViewResolverChain
return null;
}
}, url, locale, PAGE_CONST_KEY_ELEM);
} else {
// Return null to continue with the ViewResolverChain
return null;
}
}
Aggregations