Search in sources :

Example 6 with Decorator

use of com.opensymphony.module.sitemesh.Decorator in project grails-core by grails.

the class GrailsLayoutDecoratorMapper method getNamedDecorator.

@Override
public Decorator getNamedDecorator(HttpServletRequest request, String name) {
    if (groovyPageLayoutFinder == null) {
        return super.getNamedDecorator(request, name);
    }
    Decorator layout = groovyPageLayoutFinder.getNamedDecorator(request, name);
    if (layout != null) {
        return layout;
    }
    layout = parent != null ? super.getNamedDecorator(request, name) : null;
    if (layout == null || layout.getPage() == null) {
        layout = new GrailsNoDecorator();
    }
    return layout;
}
Also used : Decorator(com.opensymphony.module.sitemesh.Decorator)

Example 7 with Decorator

use of com.opensymphony.module.sitemesh.Decorator in project grails-core by grails.

the class GroovyPageLayoutFinder method getNamedDecorator.

public Decorator getNamedDecorator(HttpServletRequest request, String name, boolean viewMustExist) {
    if (GrailsStringUtils.isBlank(name) || NONE_LAYOUT.equals(name)) {
        return null;
    }
    if (cacheEnabled) {
        DecoratorCacheValue cacheValue = decoratorCache.get(name);
        if (cacheValue != null && (!gspReloadEnabled || !cacheValue.isExpired())) {
            return cacheValue.getDecorator();
        }
    }
    View view;
    try {
        view = viewResolver.resolveViewName(GrailsResourceUtils.cleanPath(GrailsResourceUtils.appendPiecesForUri(LAYOUTS_PATH, name)), request.getLocale());
        // it's only possible to check that GroovyPageView exists
        if (viewMustExist && !(view instanceof AbstractGrailsView)) {
            view = null;
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to resolve view", e);
    }
    Decorator d = null;
    if (view != null) {
        d = createDecorator(name, view);
    }
    if (cacheEnabled) {
        decoratorCache.put(name, new DecoratorCacheValue(d));
    }
    return d;
}
Also used : Decorator(com.opensymphony.module.sitemesh.Decorator) AbstractGrailsView(org.grails.web.servlet.view.AbstractGrailsView) AbstractGrailsView(org.grails.web.servlet.view.AbstractGrailsView) View(org.springframework.web.servlet.View)

Example 8 with Decorator

use of com.opensymphony.module.sitemesh.Decorator in project grails-core by grails.

the class GroovyPageLayoutFinder method resolveDecorator.

private Decorator resolveDecorator(HttpServletRequest request, GroovyObject controller, String controllerName, String actionUri) {
    Decorator d = null;
    Object layoutProperty = GrailsClassUtils.getStaticPropertyValue(controller.getClass(), "layout");
    if (layoutProperty instanceof CharSequence) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("layout property found in controller, looking for template named " + layoutProperty);
        }
        d = getNamedDecorator(request, layoutProperty.toString());
    } else {
        if (!GrailsStringUtils.isBlank(actionUri)) {
            d = getNamedDecorator(request, actionUri.substring(1), true);
        }
        if (d == null && !GrailsStringUtils.isBlank(controllerName)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Action layout not found, trying controller");
            }
            d = getNamedDecorator(request, controllerName, true);
        }
        if (d == null) {
            d = getApplicationDefaultDecorator(request);
        }
    }
    return d;
}
Also used : Decorator(com.opensymphony.module.sitemesh.Decorator) GroovyObject(groovy.lang.GroovyObject)

Example 9 with Decorator

use of com.opensymphony.module.sitemesh.Decorator in project grails-core by grails.

the class GroovyPageLayoutFinder method findLayout.

public Decorator findLayout(HttpServletRequest request, Page page) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Evaluating layout for request: " + request.getRequestURI());
    }
    final Object layoutAttribute = request.getAttribute(LAYOUT_ATTRIBUTE);
    if (request.getAttribute(RENDERING_VIEW_ATTRIBUTE) != null || layoutAttribute != null) {
        String layoutName = layoutAttribute == null ? null : layoutAttribute.toString();
        if (layoutName == null) {
            layoutName = page.getProperty("meta.layout");
        }
        Decorator d = null;
        if (GrailsStringUtils.isBlank(layoutName)) {
            GroovyObject controller = (GroovyObject) request.getAttribute(GrailsApplicationAttributes.CONTROLLER);
            if (controller != null) {
                GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
                String controllerName = webRequest.getControllerName();
                if (controllerName == null) {
                    controllerName = GrailsNameUtils.getLogicalPropertyName(controller.getClass().getName(), ControllerArtefactHandler.TYPE);
                }
                String actionUri = webRequest.getAttributes().getControllerActionUri(request);
                if (controllerName != null && actionUri != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Found controller in request, locating layout for controller [" + controllerName + "] and action [" + actionUri + "]");
                    }
                    LayoutCacheKey cacheKey = null;
                    boolean cachedIsNull = false;
                    if (cacheEnabled) {
                        cacheKey = new LayoutCacheKey(controllerName, actionUri);
                        DecoratorCacheValue cacheValue = layoutDecoratorCache.get(cacheKey);
                        if (cacheValue != null && (!gspReloadEnabled || !cacheValue.isExpired())) {
                            d = cacheValue.getDecorator();
                            if (d == null) {
                                cachedIsNull = true;
                            }
                        }
                    }
                    if (d == null && !cachedIsNull) {
                        d = resolveDecorator(request, controller, controllerName, actionUri);
                        if (cacheEnabled) {
                            if (LOG.isDebugEnabled() && d != null) {
                                LOG.debug("Caching resolved layout {} for controller {} and action {}", d.getPage(), controllerName, actionUri);
                            }
                            layoutDecoratorCache.put(cacheKey, new DecoratorCacheValue(d));
                        }
                    }
                }
            } else {
                d = getApplicationDefaultDecorator(request);
            }
        } else {
            d = getNamedDecorator(request, layoutName);
        }
        if (d != null) {
            return d;
        }
    }
    return null;
}
Also used : Decorator(com.opensymphony.module.sitemesh.Decorator) GroovyObject(groovy.lang.GroovyObject) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) GroovyObject(groovy.lang.GroovyObject)

Example 10 with Decorator

use of com.opensymphony.module.sitemesh.Decorator 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)

Aggregations

Decorator (com.opensymphony.module.sitemesh.Decorator)12 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)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 ConfigObject (groovy.util.ConfigObject)1 ConfigSlurper (groovy.util.ConfigSlurper)1 AbstractGrailsView (org.grails.web.servlet.view.AbstractGrailsView)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 MutablePropertySources (org.springframework.core.env.MutablePropertySources)1 View (org.springframework.web.servlet.View)1