Search in sources :

Example 6 with SiteItem

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;
}
Also used : HashMap(java.util.HashMap) RequestContext(org.craftercms.commons.http.RequestContext) SiteItem(org.craftercms.engine.model.SiteItem)

Example 7 with SiteItem

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);
}
Also used : TemplateException(freemarker.template.TemplateException) SimpleHash(freemarker.template.SimpleHash) TemplateModel(freemarker.template.TemplateModel) HashMap(java.util.HashMap) Map(java.util.Map) SiteItem(org.craftercms.engine.model.SiteItem) Writer(java.io.Writer) Template(freemarker.template.Template)

Example 8 with SiteItem

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));
}
Also used : Item(org.craftercms.core.service.Item) SiteItem(org.craftercms.engine.model.SiteItem) SiteItem(org.craftercms.engine.model.SiteItem) Test(org.junit.Test)

Example 9 with SiteItem

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));
}
Also used : Item(org.craftercms.core.service.Item) SiteItem(org.craftercms.engine.model.SiteItem) SiteItem(org.craftercms.engine.model.SiteItem) Test(org.junit.Test)

Example 10 with SiteItem

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;
    }
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) SiteItem(org.craftercms.engine.model.SiteItem)

Aggregations

SiteItem (org.craftercms.engine.model.SiteItem)10 Item (org.craftercms.core.service.Item)5 Test (org.junit.Test)3 TemplateException (freemarker.template.TemplateException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 NavItem (org.craftercms.engine.navigation.NavItem)2 SimpleHash (freemarker.template.SimpleHash)1 Template (freemarker.template.Template)1 TemplateModel (freemarker.template.TemplateModel)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 URI (java.net.URI)1 Map (java.util.Map)1 Predicate (org.apache.commons.collections4.Predicate)1 RequestContext (org.craftercms.commons.http.RequestContext)1 ItemProcessorPipeline (org.craftercms.core.processors.impl.ItemProcessorPipeline)1 SiteItemService (org.craftercms.engine.service.SiteItemService)1 SiteContext (org.craftercms.engine.service.context.SiteContext)1 Document (org.dom4j.Document)1