codeconsole commented on code in PR #15585: URL: https://github.com/apache/grails-core/pull/15585#discussion_r3139957873
########## grails-gsp/grails-sitemesh3/src/main/groovy/org/grails/plugins/sitemesh3/Sitemesh3LayoutTagLib.groovy: ########## @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.plugins.sitemesh3 + +import groovy.transform.CompileStatic + +import jakarta.servlet.http.HttpServletRequest + +import grails.artefact.TagLibrary +import grails.gsp.TagLib +import org.grails.buffer.FastStringWriter +import org.grails.buffer.GrailsPrintWriter +import org.grails.buffer.StreamCharBuffer +import org.grails.encoder.CodecLookup +import org.grails.encoder.Encoder +import org.grails.gsp.compiler.GrailsLayoutPreprocessor + +/** + * SiteMesh 3 counterpart of {@code GrailsLayoutTagLib}: populates a + * {@link Sitemesh3CapturedPage} at GSP render time so that SiteMesh 3 can + * decorate without parsing the HTML. + * + * <p>Registered under the {@code grailsLayout} namespace because the GSP + * compile-time preprocessor ({@link GrailsLayoutPreprocessor}) rewrites + * {@code <head>}, {@code <body>}, etc. to {@code <grailsLayout:capture*>} + * tags.</p> + */ +@CompileStatic +@TagLib +class Sitemesh3LayoutTagLib implements TagLibrary { + + static String namespace = 'grailsLayout' + + CodecLookup codecLookup + + def captureTagContent(GrailsPrintWriter writer, String tagname, Map attrs, Object body, boolean noEndTagForEmpty = false) { Review Comment: Done — added Grails-style javadoc with @attr annotations to all capture closures (captureHead, captureBody, captureTitle, wrapTitleTag, captureMeta, captureContent, parameter). ########## grails-gsp/grails-sitemesh3/src/main/groovy/org/grails/plugins/web/taglib/RenderSitemeshTagLib.groovy: ########## @@ -16,78 +16,105 @@ * specific language governing permissions and limitations * under the License. */ - package org.grails.plugins.web.taglib import java.nio.CharBuffer +import org.sitemesh.DecoratorSelector +import org.sitemesh.SiteMeshContext import org.sitemesh.content.Content +import org.sitemesh.content.ContentProcessor import org.sitemesh.content.ContentProperty -import org.sitemesh.webapp.SiteMeshFilter import org.sitemesh.webapp.WebAppContext import org.sitemesh.webapp.contentfilter.ResponseMetaData import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Qualifier -import org.springframework.boot.web.servlet.FilterRegistrationBean +import org.springframework.context.annotation.Lazy +import org.springframework.web.servlet.ViewResolver import grails.artefact.TagLibrary import grails.gsp.TagLib +import org.grails.plugins.sitemesh3.GrailsSiteMeshViewContext import org.grails.web.util.WebUtils /** - * The tags in this library are rendered by sitemesh itself instead of the grails tags so they should always be written - * as a 'sitemesh' namespace. + * Tags rendered by SiteMesh itself (not the Grails taglib pipeline) when a + * layout GSP is being processed. Kept in the {@code sitemesh} namespace. */ @TagLib class RenderSitemeshTagLib implements TagLibrary { - SiteMeshFilter siteMeshFilter - @Autowired - RenderSitemeshTagLib(@Qualifier('sitemesh') FilterRegistrationBean sitemesh) { - this.siteMeshFilter = (SiteMeshFilter) sitemesh.getFilter() - } + ContentProcessor contentProcessor + @Autowired + DecoratorSelector<SiteMeshContext> decoratorSelector + + // Break the circular dependency + // RenderSitemeshTagLib -> ViewResolver -> groovyPagesTemplateEngine -> + // gspTagLibraryLookup -> RenderSitemeshTagLib by deferring resolution. + // @Qualifier is required because the context has several ViewResolver + // beans (mvcViewResolver, beanNameViewResolver, groovyMarkupViewResolver, + // jspViewResolver, gspViewResolver) and autowiring by type is ambiguous. + @Autowired + @Lazy + @Qualifier('jspViewResolver') + ViewResolver viewResolver + + // Dispatches via GrailsSiteMeshViewContext so the layout is rendered + // through Spring's View API rather than RequestDispatcher.forward(). + // Using the default WebAppContext here would re-enter the servlet + // pipeline on every <g:applyLayout> call, and nesting (applyLayout + // inside applyLayout inside a Sitemesh3LayoutView render) would tear + // down the outer request scope before the outer render finished — + // causing "request is not active anymore" errors. Closure applyLayout = { Map attrs, body -> String savedAttribute = request.getAttribute(WebUtils.LAYOUT_ATTRIBUTE) - WebAppContext context = new WebAppContext('text/html', request, response, - servletContext, siteMeshFilter.contentProcessor, new ResponseMetaData(), false) - Content content = siteMeshFilter.contentProcessor.build(CharBuffer.wrap(body()), context) - if (attrs.name) { - request.setAttribute(WebUtils.LAYOUT_ATTRIBUTE, attrs.name) - } - String[] decoratorPaths = siteMeshFilter.decoratorSelector.selectDecoratorPaths(content, context) - for (String decoratorPath : decoratorPaths) { - content = context.decorate(decoratorPath, content) + GrailsSiteMeshViewContext context = new GrailsSiteMeshViewContext( + 'text/html', request, response, servletContext, + contentProcessor, new ResponseMetaData(), false, + viewResolver, request.getLocale()) + try { + Content content = contentProcessor.build(CharBuffer.wrap(body()), context) + if (attrs.name) { + request.setAttribute(WebUtils.LAYOUT_ATTRIBUTE, attrs.name) + } + String[] decoratorPaths = decoratorSelector.selectDecoratorPaths(content, context) + for (String decoratorPath : decoratorPaths) { + Content next = context.decorate(decoratorPath, content) + if (next == null) { + break + } + content = next + } + if (content != null) { + content.getData().writeValueTo(out) + } + } finally { + if (savedAttribute != null) { + request.setAttribute(WebUtils.LAYOUT_ATTRIBUTE, savedAttribute) + } else { + request.removeAttribute(WebUtils.LAYOUT_ATTRIBUTE) + } } - content.getData().writeValueTo(out) - request.setAttribute(WebUtils.LAYOUT_ATTRIBUTE, savedAttribute) } private ContentProperty getContentProperty(String name) { if (!name) { return null } - Content content = request.getAttribute(WebAppContext.CONTENT_KEY) + Content content = (Content) request.getAttribute(WebAppContext.CONTENT_KEY) + if (content == null) { + return null + } ContentProperty currentProperty = content.getExtractedProperties() for (String childPropertyName : name.split('\\.')) { currentProperty = currentProperty.getChild(childPropertyName) } currentProperty } - /** - * Used to retrieve a property of the decorated page.<br/> - * - * <g:pageProperty default="defaultValue" name="body.onload" /><br/> - * - * @emptyTag - * - * @attr REQUIRED name the property name - * @attr default the default value to use if the property is null - * @attr writeEntireProperty if true, writes the property in the form 'foo = "bar"', otherwise renders 'bar' - */ Closure pageProperty = { attrs -> Review Comment: Restored -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
