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());
}
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();
}
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));
}
}
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));
}
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;
}
Aggregations