Search in sources :

Example 11 with SiteContext

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

the class RestScriptsControllerTest method createSiteContext.

private SiteContext createSiteContext(ContentStoreService storeService) {
    SiteContext siteContext = mock(SiteContext.class);
    ContentStoreResourceConnector resourceConnector = new ContentStoreResourceConnector(siteContext);
    ScriptFactory scriptFactory = new GroovyScriptFactory(resourceConnector, Collections.<String, Object>emptyMap());
    when(siteContext.getSiteName()).thenReturn("test");
    when(siteContext.getContext()).thenReturn(mock(Context.class));
    when(siteContext.getRestScriptsPath()).thenReturn("/scripts");
    when(siteContext.getStoreService()).thenReturn(storeService);
    when(siteContext.getScriptFactory()).thenReturn(scriptFactory);
    return siteContext;
}
Also used : RequestContext(org.craftercms.commons.http.RequestContext) SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) ServletContext(javax.servlet.ServletContext) ContentStoreResourceConnector(org.craftercms.engine.util.groovy.ContentStoreResourceConnector) SiteContext(org.craftercms.engine.service.context.SiteContext) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Example 12 with SiteContext

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

the class StaticAssetsRequestHandler method handleRequest.

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    checkAndPrepare(request, response, true);
    SiteContext siteContext = SiteContext.getCurrent();
    String path = getPath(request, siteContext);
    if (siteContext == null) {
        throw new IllegalStateException("No current site context found");
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Trying to get content for static asset at [context=" + siteContext + ", path='" + path + "']");
    }
    Content content = getContent(siteContext, path);
    if (content == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("No static asset found at [context=" + siteContext + ", path='" + path + "'] - returning 404");
        }
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    MediaType mediaType = getMediaType(path);
    if (mediaType != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Determined media type '" + mediaType + "' for static asset at [context=" + siteContext + ", path='" + path + "']");
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("No media type found for static asset at [context=" + siteContext + ", path='" + path + "'] - not sending a content-type header");
        }
    }
    if ((new ServletWebRequest(request, response)).checkNotModified(content.getLastModified())) {
        if (logger.isDebugEnabled()) {
            logger.debug("Static asset not modified - returning 304");
        }
        return;
    }
    setHeaders(response, content, mediaType);
    if (disableCaching) {
        if (logger.isDebugEnabled()) {
            logger.debug("Caching disabled on client");
        }
        HttpUtils.disableCaching(response);
    }
    if (METHOD_HEAD.equals(request.getMethod())) {
        logger.trace("HEAD request - skipping content");
        return;
    }
    writeContent(response, content);
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Content(org.craftercms.core.service.Content) MediaType(org.springframework.http.MediaType) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 13 with SiteContext

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

the class ScriptFilterTest method createSiteContext.

private SiteContext createSiteContext(ContentStoreService storeService) throws Exception {
    SiteContext siteContext = mock(SiteContext.class);
    ScriptFactory scriptFactory = createScriptFactory(siteContext);
    when(siteContext.getSiteName()).thenReturn("default");
    when(siteContext.getContext()).thenReturn(mock(Context.class));
    when(siteContext.getStoreService()).thenReturn(storeService);
    when(siteContext.getConfig()).thenReturn(new XMLConfiguration("config/site.xml"));
    when(siteContext.getScriptFactory()).thenReturn(scriptFactory);
    return siteContext;
}
Also used : RequestContext(org.craftercms.commons.http.RequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) ServletContext(javax.servlet.ServletContext) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) SiteContext(org.craftercms.engine.service.context.SiteContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Example 14 with SiteContext

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

the class SiteItemScriptResolverImplTest method createSiteContext.

private SiteContext createSiteContext() {
    SiteContext siteContext = mock(SiteContext.class);
    when(siteContext.getSiteName()).thenReturn("test");
    when(siteContext.getContext()).thenReturn(mock(Context.class));
    return siteContext;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) RequestContext(org.craftercms.commons.http.RequestContext) Context(org.craftercms.core.service.Context) SiteContext(org.craftercms.engine.service.context.SiteContext)

Example 15 with SiteContext

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

the class ToTargetedUrlTransformerTest method setUpCurrentSiteContext.

private void setUpCurrentSiteContext() {
    HierarchicalConfiguration config = mock(HierarchicalConfiguration.class);
    when(config.getBoolean(TARGETING_ENABLED_CONFIG_KEY, false)).thenReturn(true);
    when(config.getStringArray(ROOT_FOLDERS_CONFIG_KEY)).thenReturn(ROOT_FOLDERS);
    when(config.getStringArray(EXCLUDE_PATTERNS_CONFIG_KEY)).thenReturn(new String[] { "/site/website/index\\.xml" });
    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)

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