Search in sources :

Example 1 with CrafterException

use of org.craftercms.core.exception.CrafterException in project engine by craftercms.

the class SiteItemScriptResolverImpl method getScriptUrls.

@Override
public List<String> getScriptUrls(SiteItem item) {
    List<String> scriptUrls = null;
    SiteContext siteContext = SiteContext.getCurrent();
    if (siteContext != null) {
        String contentType = item.getItem().queryDescriptorValue(contentTypeXPathQuery);
        if (StringUtils.isNotEmpty(contentType)) {
            String scriptUrl = getScriptUrlForContentType(contentType);
            if (StringUtils.isNotEmpty(scriptUrl)) {
                try {
                    // Check that the script exists. If not, ignore.
                    if (storeService.exists(siteContext.getContext(), scriptUrl)) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Script for content type '" + contentType + "' found at " + scriptUrl);
                        }
                        scriptUrls = new ArrayList<>();
                        scriptUrls.add(scriptUrl);
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug("No script for content type '" + contentType + "' found at " + scriptUrl);
                    }
                } catch (CrafterException e) {
                    logger.error("Error retrieving script for content type '" + contentType + "' at " + scriptUrl, e);
                }
            }
        }
        if (CollectionUtils.isNotEmpty(scriptUrls)) {
            List<String> additionalUrls = item.getItem().queryDescriptorValues(scriptsXPathQuery);
            if (scriptUrls == null) {
                scriptUrls = new ArrayList<>();
            }
            if (CollectionUtils.isNotEmpty(additionalUrls)) {
                scriptUrls.addAll(additionalUrls);
            }
        } else {
            scriptUrls = item.getItem().queryDescriptorValues(scriptsXPathQuery);
        }
    }
    return scriptUrls;
}
Also used : CrafterException(org.craftercms.core.exception.CrafterException) SiteContext(org.craftercms.engine.service.context.SiteContext)

Example 2 with CrafterException

use of org.craftercms.core.exception.CrafterException in project engine by craftercms.

the class PageRenderController method getControllerScript.

protected Script getControllerScript(SiteContext siteContext, HttpServletRequest request, String pageUrl) {
    ScriptFactory scriptFactory = siteContext.getScriptFactory();
    if (scriptFactory == null) {
        throw new IllegalStateException("No script factory associated to current site context '" + siteContext.getSiteName() + "'");
    }
    String scriptUrl = getScriptUrl(siteContext, scriptFactory, request, pageUrl);
    try {
        // Check controller script exists
        if (storeService.exists(siteContext.getContext(), scriptUrl)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Controller script found for page " + pageUrl + " at " + scriptUrl);
            }
            return scriptFactory.getScript(scriptUrl);
        } else if (logger.isDebugEnabled()) {
            logger.debug("No controller script for page " + pageUrl + " at " + scriptUrl);
        }
    } catch (CrafterException e) {
        logger.error("Error while trying to retrieve controller script at " + scriptUrl, e);
    }
    return null;
}
Also used : CrafterException(org.craftercms.core.exception.CrafterException) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Example 3 with CrafterException

use of org.craftercms.core.exception.CrafterException in project engine by craftercms.

the class SiteItemScriptResolverImplTest method createContentStoreService.

private ContentStoreService createContentStoreService() {
    ContentStoreService storeService = mock(ContentStoreService.class);
    when(storeService.exists(any(Context.class), eq("/scripts/pages/mypage1.groovy"))).thenReturn(true);
    when(storeService.exists(any(Context.class), eq("/scripts/pages/mypage2.groovy"))).thenReturn(false);
    when(storeService.exists(any(Context.class), eq("/scripts/pages/mypage3.groovy"))).thenThrow(new CrafterException());
    return storeService;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) RequestContext(org.craftercms.commons.http.RequestContext) Context(org.craftercms.core.service.Context) CrafterException(org.craftercms.core.exception.CrafterException) ContentStoreService(org.craftercms.core.service.ContentStoreService)

Example 4 with CrafterException

use of org.craftercms.core.exception.CrafterException in project engine by craftercms.

the class SiteContextResolvingFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    SiteContext siteContext = contextResolver.getContext((HttpServletRequest) request);
    if (siteContext == null) {
        throw new CrafterException("No site context was resolved for the current request");
    }
    SiteContext.setCurrent(siteContext);
    try {
        chain.doFilter(request, response);
    } finally {
        SiteContext.clear();
    }
}
Also used : CrafterException(org.craftercms.core.exception.CrafterException) SiteContext(org.craftercms.engine.service.context.SiteContext)

Example 5 with CrafterException

use of org.craftercms.core.exception.CrafterException in project engine by craftercms.

the class ReloadableMappingsSiteResolver method loadMappings.

protected void loadMappings() throws CrafterException {
    Properties tmpMappings = new Properties();
    try {
        tmpMappings.load(mappingsFile.getInputStream());
    } catch (IOException e) {
        throw new CrafterException("Unable to load domain name to site name mappings from " + mappingsFile, e);
    }
    Properties newMappings = new Properties();
    for (Map.Entry<Object, Object> entry : tmpMappings.entrySet()) {
        String trimmedKey = entry.getKey().toString().trim();
        String trimmedVal = entry.getValue().toString().trim();
        newMappings.setProperty(trimmedKey, trimmedVal);
    }
    logger.info("Domain name to site name mappings loaded from " + mappingsFile);
    mappings = newMappings;
}
Also used : CrafterException(org.craftercms.core.exception.CrafterException) IOException(java.io.IOException) Properties(java.util.Properties) Map(java.util.Map)

Aggregations

CrafterException (org.craftercms.core.exception.CrafterException)5 SiteContext (org.craftercms.engine.service.context.SiteContext)3 IOException (java.io.IOException)1 Map (java.util.Map)1 Properties (java.util.Properties)1 RequestContext (org.craftercms.commons.http.RequestContext)1 ContentStoreService (org.craftercms.core.service.ContentStoreService)1 Context (org.craftercms.core.service.Context)1 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)1