Search in sources :

Example 21 with GrailsWebRequest

use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.

the class GrailsConventionGroovyPageLocator method lookupRequestFormat.

protected String lookupRequestFormat() {
    if (mimeTypeResolver != null) {
        MimeType mimeType = mimeTypeResolver.resolveResponseMimeType();
        if (mimeType != null) {
            return mimeType.getExtension();
        }
    } else {
        GrailsWebRequest webRequest = GrailsWebRequest.lookup();
        if (webRequest != null) {
            HttpServletRequest request = webRequest.getCurrentRequest();
            Object format = request.getAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT);
            return format == null ? null : format.toString();
        }
    }
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) MimeType(grails.web.mime.MimeType)

Example 22 with GrailsWebRequest

use of org.grails.web.servlet.mvc.GrailsWebRequest 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 23 with GrailsWebRequest

use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.

the class GroovyPagesServlet method createResponseWriter.

/**
     * Creates a response writer for the given response object
     *
     * @param response The HttpServletResponse
     * @return The created java.io.Writer
     */
protected GSPResponseWriter createResponseWriter(HttpServletResponse response) {
    GSPResponseWriter out = GSPResponseWriter.getInstance(response);
    GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.currentRequestAttributes();
    webRequest.setOut(out);
    return out;
}
Also used : GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 24 with GrailsWebRequest

use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.

the class GroovyPageViewResolver method resolveCurrentControllerKeyPrefixes.

/**
     * @return prefix for cache key that contains current controller's context (currently plugin and namespace)
     */
protected String resolveCurrentControllerKeyPrefixes() {
    String pluginContextPath;
    String namespace;
    GrailsWebRequest webRequest = GrailsWebRequest.lookup();
    if (webRequest != null) {
        namespace = webRequest.getControllerNamespace();
        pluginContextPath = (webRequest.getAttributes() != null && webRequest.getCurrentRequest() != null) ? webRequest.getAttributes().getPluginContextPath(webRequest.getCurrentRequest()) : null;
        return (pluginContextPath != null ? pluginContextPath : "-") + "," + (namespace != null ? namespace : "-");
    } else {
        return null;
    }
}
Also used : GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 25 with GrailsWebRequest

use of org.grails.web.servlet.mvc.GrailsWebRequest in project grails-core by grails.

the class GroovyPageViewResolver method createGrailsView.

protected View createGrailsView(String viewName) throws Exception {
    // try GSP if res is null
    GroovyObject controller = null;
    GrailsWebRequest webRequest = GrailsWebRequest.lookup();
    if (webRequest != null) {
        HttpServletRequest request = webRequest.getCurrentRequest();
        controller = webRequest.getAttributes().getController(request);
    }
    GroovyPageScriptSource scriptSource;
    if (controller == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Locating GSP view for path {}", viewName);
        }
        scriptSource = groovyPageLocator.findViewByPath(viewName);
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Locating GSP view for controller {} and path {}", controller, viewName);
        }
        scriptSource = groovyPageLocator.findView(controller, viewName);
    }
    if (scriptSource != null) {
        return createGroovyPageView(scriptSource.getURI(), scriptSource);
    }
    return createFallbackView(viewName);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GroovyPageScriptSource(org.grails.gsp.io.GroovyPageScriptSource) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) GroovyObject(groovy.lang.GroovyObject)

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