use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.
the class GrailsLayoutDecoratorMapperTests method testDecoratedByActionConvention.
public void testDecoratedByActionConvention() 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/test2/testAction.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 Test2Controller {\n" + "def controllerName = 'test2'\n" + "def actionUri = '/test2/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/test2/testAction.gsp", d.getPage());
assertEquals("test2/testAction", d.getName());
}
use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.
the class GrailsLayoutDecoratorMapperTests method testDecoratedByApplicationConventionForViewsNotRenderedByAController.
public void testDecoratedByApplicationConventionForViewsNotRenderedByAController() 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/application.gsp", "<html><body><h1>Default Layout</h1><g:layoutBody /></body></html>");
MockHttpServletRequest request = (MockHttpServletRequest) webRequest.getCurrentRequest();
request.setMethod("GET");
request.setRequestURI("/");
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>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/application.gsp", d.getPage());
assertEquals("application", d.getName());
}
use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.
the class GrailsLayoutDecoratorMapperTests method testDecoratedByApplicationConvention.
public void testDecoratedByApplicationConvention() 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/application.gsp", "<html><body><h1>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/application.gsp", d.getPage());
assertEquals("application", d.getName());
}
use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.
the class DefaultUrlMappingEvaluatorTests method testNewMethod.
public void testNewMethod() throws Exception {
GroovyShell shell = new GroovyShell();
Binding binding = new Binding();
Script script = shell.parse("mappings = {\n" + " \"/$controller/$action?/$id?\" { \n" + " constraints {\n" + " id(matches:/\\d+/)\n" + " }\n" + " }\n" + "}\n");
script.setBinding(binding);
script.run();
Closure closure = (Closure) binding.getVariable("mappings");
List mappings = evaluator.evaluateMappings(closure);
assertEquals(1, mappings.size());
UrlMapping mapping = (UrlMapping) mappings.get(0);
assertNull(mapping.getActionName());
assertNull(mapping.getControllerName());
assertEquals("(*)", mapping.getUrlData().getTokens()[0]);
assertEquals("(*)?", mapping.getUrlData().getTokens()[1]);
assertEquals("(*)?", mapping.getUrlData().getTokens()[2]);
assertNotNull(mapping.getConstraints());
assertTrue(makeSureMatchesConstraintExistsOnId(mapping));
GrailsWebRequest r = GrailsWebMockUtil.bindMockWebRequest();
UrlMappingInfo info = mapping.match("/mycontroller");
info.configure(r);
assertEquals("mycontroller", info.getControllerName());
assertNull(mapping.match("/mycontroller").getActionName());
assertNull(mapping.match("/mycontroller").getId());
UrlMappingInfo info2 = mapping.match("/mycontroller/test");
info2.configure(r);
assertEquals("test", info2.getActionName());
assertNull(mapping.match("/mycontroller/test").getId());
assertEquals("234", mapping.match("/blog/test/234").getId());
}
use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.
the class UrlMappingUtils method forwardRequestForUrlMappingInfo.
/**
* Forwards a request for the given UrlMappingInfo object and model
*
* @param request The request
* @param response The response
* @param info The UrlMappingInfo object
* @param model The Model
* @param includeParams Whether to include any request parameters
* @return The URI forwarded too
*
* @throws javax.servlet.ServletException Thrown when an error occurs executing the forward
* @throws java.io.IOException Thrown when an error occurs executing the forward
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String forwardRequestForUrlMappingInfo(HttpServletRequest request, HttpServletResponse response, UrlMappingInfo info, Map model, boolean includeParams) throws ServletException, IOException {
String forwardUrl = buildDispatchUrlForMapping(info, includeParams);
org.springframework.web.util.WebUtils.exposeRequestAttributes(request, model);
//populateParamsForMapping(info);
RequestDispatcher dispatcher = request.getRequestDispatcher(forwardUrl);
// Clear the request attributes that affect view rendering. Otherwise
// whatever we forward to may render the wrong thing! Note that we
// don't care about the return value because we're delegating
// responsibility for rendering the response.
final GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
webRequest.removeAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, 0);
info.configure(webRequest);
webRequest.removeAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST);
webRequest.removeAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, WebRequest.SCOPE_REQUEST);
webRequest.removeAttribute("grailsWebRequestFilter" + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX, WebRequest.SCOPE_REQUEST);
dispatcher.forward(request, response);
return forwardUrl;
}
Aggregations