Search in sources :

Example 26 with GrailsWebRequest

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());
}
Also used : 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) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 27 with GrailsWebRequest

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());
}
Also used : 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) Decorator(com.opensymphony.module.sitemesh.Decorator) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 28 with GrailsWebRequest

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());
}
Also used : 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) ServletContext(javax.servlet.ServletContext) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 29 with GrailsWebRequest

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());
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) List(java.util.List) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) GroovyShell(groovy.lang.GroovyShell) UrlMappingInfo(grails.web.mapping.UrlMappingInfo)

Example 30 with GrailsWebRequest

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;
}
Also used : GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)32 Decorator (com.opensymphony.module.sitemesh.Decorator)8 Page (com.opensymphony.module.sitemesh.Page)7 HTMLPageParser (com.opensymphony.module.sitemesh.parser.HTMLPageParser)7 Config (grails.config.Config)7 GroovyObject (groovy.lang.GroovyObject)7 ServletContext (javax.servlet.ServletContext)7 PropertySourcesConfig (org.grails.config.PropertySourcesConfig)7 MockApplicationContext (org.grails.support.MockApplicationContext)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 MockServletConfig (org.springframework.mock.web.MockServletConfig)7 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 Binding (groovy.lang.Binding)3 RequestDispatcher (javax.servlet.RequestDispatcher)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 GroovyPageScriptSource (org.grails.gsp.io.GroovyPageScriptSource)2 ControllerExecutionException (org.grails.web.servlet.mvc.exceptions.ControllerExecutionException)2