Search in sources :

Example 1 with GroovyPageScriptSource

use of org.grails.gsp.io.GroovyPageScriptSource in project grails-core by grails.

the class GroovyPagesTemplateRenderer method findAndCacheTemplate.

private Template findAndCacheTemplate(Object controller, TemplateVariableBinding pageScope, String templateName, String contextPath, String pluginName, final String uri) throws IOException {
    String templatePath = GrailsStringUtils.isNotEmpty(contextPath) ? GrailsResourceUtils.appendPiecesForUri(contextPath, templateName) : templateName;
    final GroovyPageScriptSource scriptSource;
    if (pluginName == null) {
        scriptSource = groovyPageLocator.findTemplateInBinding(controller, templatePath, pageScope);
    } else {
        scriptSource = groovyPageLocator.findTemplateInBinding(controller, pluginName, templatePath, pageScope);
    }
    String cacheKey;
    if (scriptSource == null) {
        cacheKey = contextPath + pluginName + uri;
    } else {
        if (pluginName != null) {
            cacheKey = contextPath + pluginName + scriptSource.getURI();
        } else {
            cacheKey = scriptSource.getURI();
        }
    }
    return CacheEntry.getValue(templateCache, cacheKey, reloadEnabled ? GroovyPageMetaInfo.LASTMODIFIED_CHECK_INTERVAL : -1, null, new Callable<CacheEntry<Template>>() {

        public CacheEntry<Template> call() {
            return new CacheEntry<Template>() {

                boolean allowCaching = cacheEnabled;

                boolean neverExpire = false;

                @Override
                protected boolean hasExpired(long timeout, Object cacheRequestObject) {
                    return neverExpire ? false : (allowCaching ? super.hasExpired(timeout, cacheRequestObject) : true);
                }

                @Override
                public boolean isInitialized() {
                    return allowCaching ? super.isInitialized() : false;
                }

                @Override
                public void setValue(Template val) {
                    if (allowCaching) {
                        super.setValue(val);
                    }
                }

                @Override
                protected Template updateValue(Template oldValue, Callable<Template> updater, Object cacheRequestObject) throws Exception {
                    Template t = null;
                    if (scriptSource != null) {
                        t = groovyPagesTemplateEngine.createTemplate(scriptSource);
                    }
                    if (t == null && scaffoldingTemplateGenerator != null) {
                        t = generateScaffoldedTemplate(GrailsWebRequest.lookup(), uri);
                        // always enable caching for generated
                        // scaffolded template
                        allowCaching = true;
                        // never expire scaffolded entry since scaffolding plugin flushes the whole cache on any change
                        neverExpire = true;
                    }
                    return t;
                }
            };
        }
    }, true, null);
}
Also used : GroovyPageScriptSource(org.grails.gsp.io.GroovyPageScriptSource) GroovyObject(groovy.lang.GroovyObject) CacheEntry(grails.util.CacheEntry) GrailsTagException(org.grails.taglib.GrailsTagException) IOException(java.io.IOException) Template(groovy.text.Template)

Example 2 with GroovyPageScriptSource

use of org.grails.gsp.io.GroovyPageScriptSource in project grails-core by grails.

the class GrailsConventionGroovyPageLocator method findView.

/**
     * <p>Finds a view for the given controller and view name. For example specifying a controller with a class name of HomeController and a view name of "index" will search for
     * /WEB-INF/grails-app/views/home/index.gsp in production and grails-app/views/home/index.gsp in development</p>
     *
     * <p>If the view is not found in the application then a scan is executed that searches through binary and source plugins looking for the first matching view name</p>
     *
     * @param controller The controller
     * @param viewName The view name
     * @return The GroovyPageScriptSource
     */
public GroovyPageScriptSource findView(Object controller, String viewName) {
    if (controller == null || viewName == null) {
        return null;
    }
    String controllerName = getNameForController(controller);
    String viewNameWithFormat = resolveViewFormat(viewName);
    GroovyPageScriptSource scriptSource = null;
    final String controllerClassName = GrailsNameUtils.getFullClassName(controller.getClass());
    Object controllerArtefact = grailsApplication != null ? grailsApplication.getArtefact(ControllerArtefactHandler.TYPE, controllerClassName) : null;
    if (controllerArtefact instanceof GrailsControllerClass) {
        GrailsControllerClass gcc = (GrailsControllerClass) controllerArtefact;
        String namespace = gcc.getNamespace();
        if (namespace != null) {
            scriptSource = findPage("/" + namespace + uriService.getViewURI(controllerName, viewNameWithFormat));
            if (scriptSource == null) {
                scriptSource = findPage("/" + namespace + uriService.getViewURI(controllerName, viewName));
            }
        }
    }
    if (scriptSource == null) {
        scriptSource = findPage(uriService.getViewURI(controllerName, viewNameWithFormat));
    }
    if (scriptSource == null) {
        scriptSource = findPage(uriService.getViewURI(controllerName, viewName));
    }
    if (scriptSource == null && pluginManager != null) {
        String pathToView = pluginManager.getPluginViewsPathForInstance(controller);
        if (pathToView != null) {
            scriptSource = findViewByPath(GrailsResourceUtils.appendPiecesForUri(pathToView, viewName));
        }
    }
    // if all else fails do a full search
    if (scriptSource == null) {
        scriptSource = findView(controllerName, viewName);
    }
    return scriptSource;
}
Also used : GrailsControllerClass(grails.core.GrailsControllerClass) GroovyPageScriptSource(org.grails.gsp.io.GroovyPageScriptSource)

Example 3 with GroovyPageScriptSource

use of org.grails.gsp.io.GroovyPageScriptSource in project grails-core by grails.

the class GrailsConventionGroovyPageLocator method findTemplate.

/**
     * Finds a template for the given controller name and template name
     *
     * @param controller The controller n
     * @param templateName The view name
     * @return The GroovyPageScriptSource
     */
public GroovyPageScriptSource findTemplate(Object controller, String templateName) {
    String controllerName = getNameForController(controller);
    GroovyPageScriptSource scriptSource = null;
    final String templateURI = uriService.getTemplateURI(controllerName, templateName);
    final String fullClassName = GrailsNameUtils.getFullClassName(controller.getClass());
    Object controllerArtefact = grailsApplication != null ? grailsApplication.getArtefact(ControllerArtefactHandler.TYPE, fullClassName) : null;
    if (controllerArtefact instanceof GrailsControllerClass) {
        GrailsControllerClass gcc = (GrailsControllerClass) controllerArtefact;
        String namespace = gcc.getNamespace();
        if (namespace != null) {
            scriptSource = findPage("/" + namespace + templateURI);
        }
    }
    if (scriptSource == null) {
        scriptSource = findPage(templateURI);
    }
    return scriptSource;
}
Also used : GrailsControllerClass(grails.core.GrailsControllerClass) GroovyPageScriptSource(org.grails.gsp.io.GroovyPageScriptSource)

Example 4 with GroovyPageScriptSource

use of org.grails.gsp.io.GroovyPageScriptSource in project grails-core by grails.

the class GrailsConventionGroovyPageLocator method findTemplateInBinding.

/**
     * Finds a template for the given given template name, looking up the controller from the request as necessary
     *
     * @param pluginName The plugin
     * @param templateName The template name
     * @param binding The binding
     * @return The GroovyPageScriptSource
     */
public GroovyPageScriptSource findTemplateInBinding(Object controller, String pluginName, String templateName, TemplateVariableBinding binding) {
    if (controller == null) {
        GrailsWebRequest webRequest = GrailsWebRequest.lookup();
        if (webRequest == null) {
            return findPageInBinding(pluginName, uriService.getAbsoluteTemplateURI(templateName, true), binding);
        }
        return findPageInBinding(pluginName, uriService.getTemplateURI(webRequest.getControllerName(), templateName), binding);
    }
    final GrailsControllerClass controllerClass = (GrailsControllerClass) grailsApplication.getArtefact(ControllerArtefactHandler.TYPE, GrailsNameUtils.getFullClassName(controller.getClass()));
    String templateURI;
    final String ns = controllerClass.getNamespace();
    GroovyPageScriptSource scriptSource = null;
    final String controllerName = controllerClass.getLogicalPropertyName();
    if (ns != null) {
        templateURI = '/' + ns + uriService.getTemplateURI(controllerName, templateName);
        scriptSource = findPageInBinding(pluginName, templateURI, binding);
    }
    if (scriptSource == null) {
        templateURI = uriService.getTemplateURI(controllerName, templateName);
        scriptSource = findPageInBinding(pluginName, templateURI, binding);
    }
    return scriptSource;
}
Also used : GrailsControllerClass(grails.core.GrailsControllerClass) GroovyPageScriptSource(org.grails.gsp.io.GroovyPageScriptSource) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 5 with GroovyPageScriptSource

use of org.grails.gsp.io.GroovyPageScriptSource in project grails-core by grails.

the class GroovyPagesServlet method doService.

@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request.setAttribute(GrailsApplicationAttributes.REQUEST_SCOPE_ID, grailsAttributes);
    request.setAttribute(GroovyPagesServlet.SERVLET_INSTANCE, this);
    String pageName = (String) request.getAttribute(GrailsApplicationAttributes.GSP_TO_RENDER);
    if (GrailsStringUtils.isBlank(pageName)) {
        pageName = getCurrentRequestUri(request);
    }
    boolean isNotInclude = !WebUtils.isIncludeRequest(request);
    if (isNotInclude && isSecurePath(pageName)) {
        sendNotFound(response, pageName);
    } else {
        GroovyPageScriptSource scriptSource = groovyPagesTemplateEngine.findScriptSource(pageName);
        if (scriptSource == null) {
            scriptSource = findPageInBinaryPlugins(pageName);
        }
        if (scriptSource == null || (isNotInclude && !scriptSource.isPublic())) {
            sendNotFound(response, pageName);
            return;
        }
        renderPageWithEngine(groovyPagesTemplateEngine, request, response, scriptSource);
    }
}
Also used : GroovyPageScriptSource(org.grails.gsp.io.GroovyPageScriptSource)

Aggregations

GroovyPageScriptSource (org.grails.gsp.io.GroovyPageScriptSource)6 GrailsControllerClass (grails.core.GrailsControllerClass)3 GroovyObject (groovy.lang.GroovyObject)2 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)2 CacheEntry (grails.util.CacheEntry)1 Template (groovy.text.Template)1 IOException (java.io.IOException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 GrailsTagException (org.grails.taglib.GrailsTagException)1