use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.
the class GrailsLayoutDecoratorMapperTests method testGetDecoratorHttpServletRequestPage.
/*
* Test method for 'GrailsLayoutDecoratorMapper.getDecorator(HttpServletRequest, Page)'
*/
public void testGetDecoratorHttpServletRequestPage() throws Exception {
GrailsWebRequest webRequest = buildMockRequest(null);
webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
appCtx.registerMockResource("/grails-app/views/layouts/test.gsp", "<html><body><g:layoutBody /></body></html>");
MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
request.setMethod("GET");
request.setRequestURI("orders/list");
ServletContext context = webRequest.getServletContext();
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>Test title</title><meta name=\"layout\" content=\"test\"></meta></head><body>here is the body</body></html>";
Page page = parser.parse(html.toCharArray());
Decorator d = m.getDecorator(request, page);
assertNotNull(d);
assertEquals("/layouts/test.gsp", d.getPage());
assertEquals("test", d.getName());
}
use of org.grails.web.servlet.mvc.GrailsWebRequest 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 org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.
the class GrailsLayoutDecoratorMapperTests method testDecoratedByLayoutPropertyInController.
public void testDecoratedByLayoutPropertyInController() throws Exception {
GrailsWebRequest webRequest = buildMockRequest(null);
webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
appCtx.registerMockResource("/grails-app/views/layouts/test.gsp", "<html><body><g:layoutBody /></body></html>");
appCtx.registerMockResource("/grails-app/views/layouts/mylayout.gsp", "<html><body><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 TestController {\n" + "def controllerName = 'test'\n" + "def actionUri = '/test/testAction'\n" + "static layout = 'mylayout'\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>Test 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/mylayout.gsp", d.getPage());
assertEquals("mylayout", d.getName());
}
use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.
the class GrailsLayoutDecoratorMapperTests method testDecoratedByControllerConvention.
public void testDecoratedByControllerConvention() throws Exception {
GrailsWebRequest webRequest = buildMockRequest(null);
webRequest.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
MockApplicationContext appCtx = (MockApplicationContext) webRequest.getApplicationContext();
appCtx.registerMockResource("/grails-app/views/layouts/test.gsp", "<html><body><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 TestController {\n" + "def controllerName = 'test'\n" + "def actionUri = '/test/testAction'\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>Test 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/test.gsp", d.getPage());
assertEquals("test", d.getName());
}
use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.
the class GSPResponseWriter method initialize.
void initialize(final StreamCharBuffer buffer, final ServletResponse response, BoundedCharsAsEncodedBytesCounter bytesCounter) {
DestinationFactory lazyTargetFactory = new DestinationFactory() {
public Writer activateDestination() throws IOException {
final GrailsWebRequest webRequest = GrailsWebRequest.lookup();
encoder = webRequest != null ? webRequest.lookupFilteringEncoder() : null;
if (encoder != null) {
return buffer.getWriterForEncoder(encoder, webRequest.getEncodingStateRegistry());
}
return buffer.getWriter();
}
};
updateDestination(lazyTargetFactory);
this.response = response;
this.bytesCounter = bytesCounter;
setBlockClose(true);
setBlockFlush(false);
}
Aggregations