Search in sources :

Example 1 with Config

use of grails.config.Config in project grails-core by grails.

the class GrailsLayoutDecoratorMapperTests method testOverridingDefaultTemplateViaConfig.

public void testOverridingDefaultTemplateViaConfig() throws Exception {
    ConfigObject config = new ConfigSlurper().parse("grails.sitemesh.default.layout='otherApplication'");
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MapPropertySource("grails", config));
    GrailsWebRequest webRequest = buildMockRequest(new PropertySourcesConfig(propertySources));
    webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
    MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
    appCtx.registerMockResource("/grails-app/views/layouts/application.gsp", "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");
    appCtx.registerMockResource("/grails-app/views/layouts/otherApplication.gsp", "<html><body><h1>Other Default Layout</h1><g:layoutBody /></body></html>");
    MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
    request.setMethod("GET");
    request.setRequestURI("orders/list");
    ServletContext context = webRequest.getServletContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    // create mock controller
    GroovyObject controller = (GroovyObject) gcl.parseClass("class FooController {\n" + "def controllerName = 'foo'\n" + "def actionUri = '/foo/fooAction'\n" + "}").newInstance();
    request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller);
    GrailsLayoutDecoratorMapper m = new GrailsLayoutDecoratorMapper();
    com.opensymphony.module.sitemesh.Config c = new com.opensymphony.module.sitemesh.Config(new MockServletConfig(context));
    m.init(c, null, null);
    HTMLPageParser parser = new HTMLPageParser();
    String html = "<html><head><title>Foo title</title></head><body>here is the body</body></html>";
    Page page = parser.parse(html.toCharArray());
    Decorator d = m.getDecorator(request, page);
    assertNotNull(d);
    assertEquals("/layouts/otherApplication.gsp", d.getPage());
    assertEquals("otherApplication", d.getName());
}
Also used : PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Config(grails.config.Config) MockServletConfig(org.springframework.mock.web.MockServletConfig) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) MockServletConfig(org.springframework.mock.web.MockServletConfig) Page(com.opensymphony.module.sitemesh.Page) HTMLPageParser(com.opensymphony.module.sitemesh.parser.HTMLPageParser) MockApplicationContext(org.grails.support.MockApplicationContext) GroovyObject(groovy.lang.GroovyObject) GroovyClassLoader(groovy.lang.GroovyClassLoader) Decorator(com.opensymphony.module.sitemesh.Decorator) MapPropertySource(org.springframework.core.env.MapPropertySource) ServletContext(javax.servlet.ServletContext) MutablePropertySources(org.springframework.core.env.MutablePropertySources) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) ConfigObject(groovy.util.ConfigObject) ConfigSlurper(groovy.util.ConfigSlurper)

Example 2 with Config

use of grails.config.Config in project grails-core by grails.

the class GrailsExceptionResolver method getRequestLogMessage.

protected String getRequestLogMessage(String exceptionName, HttpServletRequest request, String message) {
    StringBuilder sb = new StringBuilder();
    sb.append(exceptionName).append(" occurred when processing request: ").append("[").append(request.getMethod().toUpperCase()).append("] ");
    if (request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) != null) {
        sb.append(request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE));
    } else {
        sb.append(request.getRequestURI());
    }
    Config config = grailsApplication != null ? grailsApplication.getConfig() : null;
    final boolean shouldLogRequestParameters = config != null ? config.getProperty(Settings.SETTING_LOG_REQUEST_PARAMETERS, Boolean.class, Environment.getCurrent() == Environment.DEVELOPMENT) : false;
    if (shouldLogRequestParameters) {
        Enumeration<String> params = request.getParameterNames();
        if (params.hasMoreElements()) {
            String param;
            String[] values;
            int i;
            sb.append(" - parameters:");
            @SuppressWarnings("unchecked") List<String> blackList = (config.getProperty(Settings.SETTING_EXCEPTION_RESOLVER_PARAM_EXCLUDES, List.class, Collections.emptyList()));
            if (blackList == null) {
                blackList = Collections.emptyList();
            }
            while (params.hasMoreElements()) {
                param = params.nextElement();
                values = request.getParameterValues(param);
                if (values != null) {
                    for (i = 0; i < values.length; i++) {
                        sb.append(LINE_SEPARATOR).append(param).append(": ");
                        if (blackList.contains(param)) {
                            sb.append("***");
                        } else {
                            sb.append(values[i]);
                        }
                    }
                }
            }
        }
    }
    sb.append(LINE_SEPARATOR);
    if (message != null) {
        sb.append(message).append(". ");
    }
    sb.append("Stacktrace follows:");
    return sb.toString();
}
Also used : Config(grails.config.Config) List(java.util.List)

Example 3 with Config

use of grails.config.Config in project grails-core by grails.

the class GroovyPagesTemplateEngine method setApplicationContext.

/**
     * Sets the ResourceLoader from the ApplicationContext
     *
     * @param applicationContext The ApplicationContext
     * @throws BeansException Thrown when an error occurs with the ApplicationContext
     */
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    if (applicationContext.containsBean(GrailsApplication.APPLICATION_ID)) {
        this.grailsApplication = applicationContext.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class);
        Config config = grailsApplication.getConfig();
        this.gspEncoding = config.getProperty(GroovyPageParser.CONFIG_PROPERTY_GSP_ENCODING, System.getProperty("file.encoding", GroovyPageParser.DEFAULT_ENCODING));
    }
}
Also used : Config(grails.config.Config) GrailsApplication(grails.core.GrailsApplication)

Example 4 with Config

use of grails.config.Config in project grails-core by grails.

the class ConvertersConfigurationInitializer method initJSONConfiguration.

private void initJSONConfiguration() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Initializing default JSON Converters Configuration...");
    }
    List<ObjectMarshaller<JSON>> marshallers = new ArrayList<ObjectMarshaller<JSON>>();
    marshallers.addAll(getPreviouslyConfiguredMarshallers(JSON.class));
    marshallers.add(new org.grails.web.converters.marshaller.json.ArrayMarshaller());
    marshallers.add(new org.grails.web.converters.marshaller.json.ByteArrayMarshaller());
    marshallers.add(new org.grails.web.converters.marshaller.json.CollectionMarshaller());
    marshallers.add(new org.grails.web.converters.marshaller.json.MapMarshaller());
    marshallers.add(new org.grails.web.converters.marshaller.json.EnumMarshaller());
    marshallers.add(new org.grails.web.converters.marshaller.ProxyUnwrappingMarshaller<JSON>());
    Config grailsConfig = getGrailsConfig();
    if ("javascript".equals(grailsConfig.getProperty(SETTING_CONVERTERS_JSON_DATE, String.class, "default", Arrays.asList("javascript", "default")))) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Using Javascript JSON Date Marshaller.");
        }
        marshallers.add(new org.grails.web.converters.marshaller.json.JavascriptDateMarshaller());
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Using default JSON Date Marshaller");
        }
        marshallers.add(new org.grails.web.converters.marshaller.json.DateMarshaller());
    }
    marshallers.add(new org.grails.web.converters.marshaller.json.ToStringBeanMarshaller());
    boolean includeDomainVersion = includeDomainVersionProperty(grailsConfig, "json");
    boolean includeDomainClassName = includeDomainClassProperty(grailsConfig, "json");
    ProxyHandler proxyHandler = getProxyHandler();
    if (grailsConfig.getProperty(SETTING_CONVERTERS_JSON_DEFAULT_DEEP, Boolean.class, false)) {
        LOG.debug("Using DeepDomainClassMarshaller as default.");
        marshallers.add(new org.grails.web.converters.marshaller.json.DeepDomainClassMarshaller(includeDomainVersion, includeDomainClassName, proxyHandler, grailsApplication));
    } else {
        marshallers.add(new org.grails.web.converters.marshaller.json.DomainClassMarshaller(includeDomainVersion, includeDomainClassName, proxyHandler, grailsApplication));
    }
    marshallers.add(new org.grails.web.converters.marshaller.json.GroovyBeanMarshaller());
    marshallers.add(new org.grails.web.converters.marshaller.json.GenericJavaBeanMarshaller());
    DefaultConverterConfiguration<JSON> cfg = new DefaultConverterConfiguration<JSON>(marshallers, proxyHandler);
    cfg.setEncoding(grailsConfig.getProperty(SETTING_CONVERTERS_ENCODING, "UTF-8"));
    String defaultCirRefBehaviour = grailsConfig.getProperty(SETTING_CONVERTERS_CIRCULAR_REFERENCE_BEHAVIOUR, "DEFAULT");
    cfg.setCircularReferenceBehaviour(Converter.CircularReferenceBehaviour.valueOf(grailsConfig.getProperty(SETTING_CONVERTERS_JSON_CIRCULAR_REFERENCE_BEHAVIOUR, String.class, defaultCirRefBehaviour, Converter.CircularReferenceBehaviour.allowedValues())));
    Boolean defaultPrettyPrint = grailsConfig.getProperty(SETTING_CONVERTERS_PRETTY_PRINT, Boolean.class, false);
    Boolean prettyPrint = grailsConfig.getProperty(SETTING_CONVERTERS_JSON_PRETTY_PRINT, Boolean.class, defaultPrettyPrint);
    cfg.setPrettyPrint(prettyPrint);
    cfg.setCacheObjectMarshallerByClass(grailsConfig.getProperty(SETTING_CONVERTERS_JSON_CACHE_OBJECTS, Boolean.class, true));
    registerObjectMarshallersFromApplicationContext(cfg, JSON.class);
    ConvertersConfigurationHolder.setDefaultConfiguration(JSON.class, new ChainedConverterConfiguration<JSON>(cfg, proxyHandler));
}
Also used : Config(grails.config.Config) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) ArrayList(java.util.ArrayList) JSON(grails.converters.JSON) DefaultProxyHandler(grails.core.support.proxy.DefaultProxyHandler) ProxyHandler(grails.core.support.proxy.ProxyHandler) ObjectMarshaller(org.grails.web.converters.marshaller.ObjectMarshaller)

Example 5 with Config

use of grails.config.Config in project grails-core by grails.

the class UrlMappingsHolderFactoryBean method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    Assert.state(applicationContext != null, "Property [applicationContext] must be set!");
    Assert.state(grailsApplication != null, "Property [grailsApplication] must be set!");
    List urlMappings = new ArrayList();
    List excludePatterns = new ArrayList();
    GrailsClass[] mappings = grailsApplication.getArtefacts(UrlMappingsArtefactHandler.TYPE);
    final DefaultUrlMappingEvaluator mappingEvaluator = new DefaultUrlMappingEvaluator(applicationContext);
    mappingEvaluator.setPluginManager(pluginManager);
    if (mappings.length == 0) {
        urlMappings.addAll(mappingEvaluator.evaluateMappings(DefaultUrlMappings.getMappings()));
    } else {
        for (GrailsClass mapping : mappings) {
            GrailsUrlMappingsClass mappingClass = (GrailsUrlMappingsClass) mapping;
            List grailsClassMappings;
            if (Script.class.isAssignableFrom(mappingClass.getClazz())) {
                grailsClassMappings = mappingEvaluator.evaluateMappings(mappingClass.getClazz());
            } else {
                grailsClassMappings = mappingEvaluator.evaluateMappings(mappingClass.getMappingsClosure());
            }
            urlMappings.addAll(grailsClassMappings);
            if (mappingClass.getExcludePatterns() != null) {
                excludePatterns.addAll(mappingClass.getExcludePatterns());
            }
        }
    }
    DefaultUrlMappingsHolder defaultUrlMappingsHolder = new DefaultUrlMappingsHolder(urlMappings, excludePatterns, true);
    Config config = grailsApplication.getConfig();
    Integer cacheSize = config.getProperty(URL_MAPPING_CACHE_MAX_SIZE, Integer.class, null);
    if (cacheSize != null) {
        defaultUrlMappingsHolder.setMaxWeightedCacheCapacity(cacheSize);
    }
    Integer urlCreatorCacheSize = config.getProperty(URL_CREATOR_CACHE_MAX_SIZE, Integer.class, null);
    if (urlCreatorCacheSize != null) {
        defaultUrlMappingsHolder.setUrlCreatorMaxWeightedCacheCapacity(urlCreatorCacheSize);
    }
    // call initialize() after settings are in place
    defaultUrlMappingsHolder.initialize();
    UrlConverter urlConverter = applicationContext.containsBean(UrlConverter.BEAN_NAME) ? applicationContext.getBean(UrlConverter.BEAN_NAME, UrlConverter.class) : null;
    final GrailsControllerUrlMappings grailsControllerUrlMappings = new GrailsControllerUrlMappings(grailsApplication, defaultUrlMappingsHolder, urlConverter);
    ((ConfigurableApplicationContext) applicationContext).addApplicationListener(new ApplicationListener<ArtefactAdditionEvent>() {

        @Override
        public void onApplicationEvent(ArtefactAdditionEvent event) {
            GrailsClass artefact = event.getArtefact();
            if (artefact instanceof GrailsControllerClass) {
                grailsControllerUrlMappings.registerController((GrailsControllerClass) artefact);
            }
        }
    });
    urlMappingsHolder = grailsControllerUrlMappings;
}
Also used : GrailsControllerUrlMappings(org.grails.web.mapping.mvc.GrailsControllerUrlMappings) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) GrailsControllerClass(grails.core.GrailsControllerClass) Config(grails.config.Config) ArrayList(java.util.ArrayList) GrailsUrlMappingsClass(grails.core.GrailsUrlMappingsClass) ArtefactAdditionEvent(grails.core.events.ArtefactAdditionEvent) GrailsClass(grails.core.GrailsClass) UrlConverter(grails.web.UrlConverter) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Config (grails.config.Config)9 ArrayList (java.util.ArrayList)3 PropertySourcesConfig (org.grails.config.PropertySourcesConfig)3 DefaultProxyHandler (grails.core.support.proxy.DefaultProxyHandler)2 ProxyHandler (grails.core.support.proxy.ProxyHandler)2 IOException (java.io.IOException)2 List (java.util.List)2 ObjectMarshaller (org.grails.web.converters.marshaller.ObjectMarshaller)2 Decorator (com.opensymphony.module.sitemesh.Decorator)1 Page (com.opensymphony.module.sitemesh.Page)1 HTMLPageParser (com.opensymphony.module.sitemesh.parser.HTMLPageParser)1 JSON (grails.converters.JSON)1 XML (grails.converters.XML)1 GrailsApplication (grails.core.GrailsApplication)1 GrailsClass (grails.core.GrailsClass)1 GrailsControllerClass (grails.core.GrailsControllerClass)1 GrailsUrlMappingsClass (grails.core.GrailsUrlMappingsClass)1 ArtefactAdditionEvent (grails.core.events.ArtefactAdditionEvent)1 UrlConverter (grails.web.UrlConverter)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1