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