Search in sources :

Example 6 with GrailsWebRequest

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

the class UrlMappingUtils method includeForUrlMappingInfo.

/**
     * Include whatever the given UrlMappingInfo maps to within the current response
     *
     * @param request The request
     * @param response The response
     * @param info The UrlMappingInfo
     * @param model The model
     *
     * @return The included content
     */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static IncludedContent includeForUrlMappingInfo(HttpServletRequest request, HttpServletResponse response, UrlMappingInfo info, Map model) {
    String includeUrl = buildDispatchUrlForMapping(info, true);
    final GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
    String currentController = null;
    String currentAction = null;
    String currentId = null;
    ModelAndView currentMv = null;
    Binding currentPageBinding = null;
    Map currentParams = null;
    Object currentLayoutAttribute = null;
    Object currentRenderingView = null;
    if (webRequest != null) {
        currentPageBinding = (Binding) webRequest.getAttribute(GrailsApplicationAttributes.PAGE_SCOPE, 0);
        webRequest.removeAttribute(GrailsApplicationAttributes.PAGE_SCOPE, 0);
        currentLayoutAttribute = webRequest.getAttribute(WebUtils.LAYOUT_ATTRIBUTE, 0);
        if (currentLayoutAttribute != null) {
            webRequest.removeAttribute(WebUtils.LAYOUT_ATTRIBUTE, 0);
        }
        currentRenderingView = webRequest.getAttribute(WebUtils.RENDERING_VIEW, 0);
        if (currentRenderingView != null) {
            webRequest.removeAttribute(WebUtils.RENDERING_VIEW, 0);
        }
        currentController = webRequest.getControllerName();
        currentAction = webRequest.getActionName();
        currentId = webRequest.getId();
        currentParams = new HashMap();
        currentParams.putAll(webRequest.getParameterMap());
        currentMv = (ModelAndView) webRequest.getAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, 0);
    }
    try {
        if (webRequest != null) {
            webRequest.getParameterMap().clear();
            info.configure(webRequest);
            webRequest.getParameterMap().putAll(info.getParameters());
            webRequest.removeAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, 0);
        }
        return includeForUrl(includeUrl, request, response, model);
    } finally {
        if (webRequest != null) {
            webRequest.setAttribute(GrailsApplicationAttributes.PAGE_SCOPE, currentPageBinding, 0);
            if (currentLayoutAttribute != null) {
                webRequest.setAttribute(WebUtils.LAYOUT_ATTRIBUTE, currentLayoutAttribute, 0);
            }
            if (currentRenderingView != null) {
                webRequest.setAttribute(WebUtils.RENDERING_VIEW, currentRenderingView, 0);
            }
            webRequest.getParameterMap().clear();
            webRequest.getParameterMap().putAll(currentParams);
            webRequest.setId(currentId);
            webRequest.setControllerName(currentController);
            webRequest.setActionName(currentAction);
            if (currentMv != null) {
                webRequest.setAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, currentMv, 0);
            }
        }
    }
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with GrailsWebRequest

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

the class GrailsPageResponseWrapper method sendError.

@Override
public void sendError(int sc) throws IOException {
    clearBuffer();
    GrailsWebRequest webRequest = GrailsWebRequest.lookup();
    try {
        super.sendError(sc);
    } finally {
        if (webRequest != null)
            WebUtils.storeGrailsWebRequest(webRequest);
    }
}
Also used : GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 8 with GrailsWebRequest

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

the class GrailsPageResponseWrapper method sendError.

@Override
public void sendError(int sc, String msg) throws IOException {
    clearBuffer();
    GrailsWebRequest webRequest = GrailsWebRequest.lookup();
    try {
        super.sendError(sc, msg);
    } finally {
        if (webRequest != null)
            WebUtils.storeGrailsWebRequest(webRequest);
    }
}
Also used : GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 9 with GrailsWebRequest

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

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

the class WebUtils method resolveView.

/**
 * Resolves a view for the given view name and controller name
 * @param request The request
 * @param viewName The view name
 * @param controllerName The controller name
 * @param viewResolver The resolver
 * @return A View or null
 * @throws Exception Thrown if an error occurs
 */
public static View resolveView(HttpServletRequest request, String viewName, String controllerName, ViewResolver viewResolver) throws Exception {
    GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
    Locale locale = webRequest != null ? webRequest.getLocale() : Locale.getDefault();
    return viewResolver.resolveViewName(addViewPrefix(viewName, controllerName), locale);
}
Also used : Locale(java.util.Locale) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

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