Search in sources :

Example 16 with SiteContext

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

the class CircularQueueLogAppender method append.

@Override
@SuppressWarnings("unchecked")
protected void append(final LoggingEvent event) {
    final SiteContext ctx = SiteContext.getCurrent();
    if (ctx != null) {
        final String siteName = ctx.getSiteName();
        if (StringUtils.isNoneBlank(siteName)) {
            Map<String, Object> mappy = new HashMap<>();
            mappy.put("site", siteName);
            mappy.put("level", event.getLevel().toString());
            mappy.put("message", event.getRenderedMessage());
            mappy.put("thread", event.getThreadName());
            mappy.put("exception", subAppend(event));
            mappy.put("timestamp", dateFormat.format(new Date(event.getTimeStamp())));
            mappy.put("timestampm", event.getTimeStamp());
            buffer.add(mappy);
        }
    }
}
Also used : HashMap(java.util.HashMap) SiteContext(org.craftercms.engine.service.context.SiteContext) Date(java.util.Date)

Example 17 with SiteContext

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

the class RejectIndexFilesItemFilterTest method setUpCurrentConfig.

private void setUpCurrentConfig() {
    XMLConfiguration config = mock(XMLConfiguration.class);
    when(config.getString(INDEX_FILE_NAME_CONFIG_KEY, DEFAULT_INDEX_FILE_NAME)).thenReturn(DEFAULT_INDEX_FILE_NAME);
    when(config.getBoolean(TARGETING_ENABLED_CONFIG_KEY, false)).thenReturn(true);
    SiteContext siteContext = mock(SiteContext.class);
    when(siteContext.getSiteName()).thenReturn("test");
    when(siteContext.getConfig()).thenReturn(config);
    SiteContext.setCurrent(siteContext);
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) SiteContext(org.craftercms.engine.service.context.SiteContext)

Example 18 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);
    XMLConfiguration config = ConfigUtils.readXmlConfiguration(new ClassPathResource("config/site-config.xml"), ',');
    config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
    when(siteContext.getSiteName()).thenReturn("default");
    when(siteContext.getContext()).thenReturn(mock(Context.class));
    when(siteContext.getStoreService()).thenReturn(storeService);
    when(siteContext.getConfig()).thenReturn(config);
    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.configuration2.XMLConfiguration) DefaultListDelimiterHandler(org.apache.commons.configuration2.convert.DefaultListDelimiterHandler) SiteContext(org.craftercms.engine.service.context.SiteContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 19 with SiteContext

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

the class ConfigurationScriptJobResolver method getJobsUnderFolder.

protected List<JobContext> getJobsUnderFolder(SiteContext siteContext, HierarchicalConfiguration jobFolderConfig) {
    List<JobContext> jobContexts = null;
    String folderPath = jobFolderConfig.getString(PATH_KEY);
    String cronExpression = jobFolderConfig.getString(CRON_EXPRESSION_KEY);
    ContentStoreService storeService = siteContext.getStoreService();
    Context context = siteContext.getContext();
    if (StringUtils.isNotEmpty(folderPath) && StringUtils.isNotEmpty(cronExpression)) {
        List<String> scriptPaths = ContentStoreUtils.findChildrenUrl(storeService, context, folderPath);
        if (CollectionUtils.isNotEmpty(scriptPaths)) {
            for (String scriptPath : scriptPaths) {
                if (scriptPath.endsWith(scriptSuffix)) {
                    if (jobContexts == null) {
                        jobContexts = new ArrayList<>();
                    }
                    jobContexts.add(SchedulingUtils.createJobContext(siteContext, scriptPath, cronExpression, servletContext));
                }
            }
        }
    }
    return jobContexts;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) JobContext(org.craftercms.engine.util.quartz.JobContext) ServletContext(javax.servlet.ServletContext) ContentStoreService(org.craftercms.core.service.ContentStoreService) JobContext(org.craftercms.engine.util.quartz.JobContext)

Example 20 with SiteContext

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

the class ConfigAwareUrlAccessRestrictionCheckingProcessor method getUrlRestrictions.

@Override
@SuppressWarnings("unchecked")
protected Map<String, Expression> getUrlRestrictions() {
    Callback<Map<String, Expression>> callback = new Callback<Map<String, Expression>>() {

        @Override
        public Map<String, Expression> execute() {
            HierarchicalConfiguration config = ConfigUtils.getCurrentConfig();
            Map<String, Expression> customRestrictions = null;
            if (config != null) {
                List<HierarchicalConfiguration> restrictionsConfig = config.configurationsAt(URL_RESTRICTION_KEY);
                if (CollectionUtils.isNotEmpty(restrictionsConfig)) {
                    customRestrictions = new LinkedHashMap<>(restrictionsConfig.size());
                    ExpressionParser parser = new SpelExpressionParser();
                    for (HierarchicalConfiguration restrictionConfig : restrictionsConfig) {
                        String url = restrictionConfig.getString(URL_RESTRICTION_URL_KEY);
                        String expression = restrictionConfig.getString(URL_RESTRICTION_EXPRESSION_KEY);
                        if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(expression)) {
                            try {
                                customRestrictions.put(url, parser.parseExpression(expression));
                            } catch (ParseException e) {
                                throw new ConfigurationException(expression + " is not a valid SpEL expression", e);
                            }
                        }
                    }
                }
            }
            if (customRestrictions != null) {
                return customRestrictions;
            } else {
                return urlRestrictions;
            }
        }
    };
    SiteContext siteContext = SiteContext.getCurrent();
    if (siteContext != null) {
        return cacheTemplate.getObject(siteContext.getContext(), callback, URL_RESTRICTIONS_CACHE_KEY);
    } else {
        return urlRestrictions;
    }
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Callback(org.craftercms.commons.lang.Callback) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ConfigurationException(org.craftercms.engine.exception.ConfigurationException) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ParseException(org.springframework.expression.ParseException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

SiteContext (org.craftercms.engine.service.context.SiteContext)39 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)8 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)8 Context (org.craftercms.core.service.Context)7 HashMap (java.util.HashMap)4 ServletContext (javax.servlet.ServletContext)4 XMLConfiguration (org.apache.commons.configuration2.XMLConfiguration)4 RequestContext (org.craftercms.commons.http.RequestContext)4 IOException (java.io.IOException)3 Callback (org.craftercms.commons.lang.Callback)3 Content (org.craftercms.core.service.Content)3 ContentStoreService (org.craftercms.core.service.ContentStoreService)3 Script (org.craftercms.engine.scripting.Script)3 TemplateException (freemarker.template.TemplateException)2 ArrayList (java.util.ArrayList)2 Configuration (org.apache.commons.configuration2.Configuration)2 DefaultListDelimiterHandler (org.apache.commons.configuration2.convert.DefaultListDelimiterHandler)2 CrafterException (org.craftercms.core.exception.CrafterException)2 ConfigurationException (org.craftercms.engine.exception.ConfigurationException)2 JobContext (org.craftercms.engine.util.quartz.JobContext)2