Search in sources :

Example 16 with GrailsWebRequest

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

the class UrlMappingUtils method forwardRequestForUrlMappingInfo.

/**
 * Forwards a request for the given UrlMappingInfo object and model
 *
 * @param request The request
 * @param response The response
 * @param info The UrlMappingInfo object
 * @param model The Model
 * @param includeParams Whether to include any request parameters
 * @return The URI forwarded too
 *
 * @throws javax.servlet.ServletException Thrown when an error occurs executing the forward
 * @throws java.io.IOException Thrown when an error occurs executing the forward
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String forwardRequestForUrlMappingInfo(HttpServletRequest request, HttpServletResponse response, UrlMappingInfo info, Map<String, Object> model, boolean includeParams) throws ServletException, IOException {
    String forwardUrl = buildDispatchUrlForMapping(info, includeParams);
    for (Map.Entry<String, Object> entry : model.entrySet()) {
        request.setAttribute(entry.getKey(), entry.getValue());
    }
    // populateParamsForMapping(info);
    RequestDispatcher dispatcher = request.getRequestDispatcher(forwardUrl);
    // Clear the request attributes that affect view rendering. Otherwise
    // whatever we forward to may render the wrong thing! Note that we
    // don't care about the return value because we're delegating
    // responsibility for rendering the response.
    final GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
    webRequest.removeAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, 0);
    info.configure(webRequest);
    webRequest.removeAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST);
    webRequest.removeAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, WebRequest.SCOPE_REQUEST);
    webRequest.removeAttribute("grailsWebRequestFilter" + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX, WebRequest.SCOPE_REQUEST);
    dispatcher.forward(request, response);
    return forwardUrl;
}
Also used : GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) HashMap(java.util.HashMap) Map(java.util.Map) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 17 with GrailsWebRequest

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

the class UrlMappingUtils method includeForUrl.

/**
 * Includes the given URL returning the resulting content as a String
 *
 * @param includeUrl The URL to include
 * @param request The request
 * @param response The response
 * @param model The model
 * @return The content
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static IncludedContent includeForUrl(String includeUrl, HttpServletRequest request, HttpServletResponse response, Map model) {
    RequestDispatcher dispatcher = request.getRequestDispatcher(includeUrl);
    HttpServletResponse wrapped = WrappedResponseHolder.getWrappedResponse();
    response = wrapped != null ? wrapped : response;
    WebUtils.exposeIncludeRequestAttributes(request);
    Map toRestore = WebUtils.exposeRequestAttributesAndReturnOldValues(request, model);
    final GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
    final boolean hasPreviousWebRequest = webRequest != null;
    final Object previousControllerClass = hasPreviousWebRequest ? webRequest.getAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST) : null;
    final Object previousMatchedRequest = hasPreviousWebRequest ? webRequest.getAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, WebRequest.SCOPE_REQUEST) : null;
    try {
        if (hasPreviousWebRequest) {
            webRequest.removeAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST);
            webRequest.removeAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, WebRequest.SCOPE_REQUEST);
            webRequest.removeAttribute("grailsWebRequestFilter" + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX, WebRequest.SCOPE_REQUEST);
        }
        final IncludeResponseWrapper responseWrapper = new IncludeResponseWrapper(response);
        try {
            WrappedResponseHolder.setWrappedResponse(responseWrapper);
            WebUtils.clearGrailsWebRequest();
            dispatcher.include(request, responseWrapper);
            if (responseWrapper.getRedirectURL() != null) {
                return new IncludedContent(responseWrapper.getRedirectURL());
            }
            return new IncludedContent(responseWrapper.getContentType(), responseWrapper.getContent());
        } finally {
            if (hasPreviousWebRequest) {
                WebUtils.storeGrailsWebRequest(webRequest);
                if (webRequest.isActive()) {
                    webRequest.setAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, previousControllerClass, WebRequest.SCOPE_REQUEST);
                    webRequest.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, previousMatchedRequest, WebRequest.SCOPE_REQUEST);
                }
            }
            WrappedResponseHolder.setWrappedResponse(wrapped);
        }
    } catch (Exception e) {
        throw new ControllerExecutionException("Unable to execute include: " + e.getMessage(), e);
    } finally {
        WebUtils.cleanupIncludeRequestAttributes(request, toRestore);
    }
}
Also used : IncludeResponseWrapper(org.grails.web.util.IncludeResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) ControllerExecutionException(org.grails.web.servlet.mvc.exceptions.ControllerExecutionException) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) IncludedContent(org.grails.web.util.IncludedContent) HashMap(java.util.HashMap) Map(java.util.Map) RequestDispatcher(javax.servlet.RequestDispatcher) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ControllerExecutionException(org.grails.web.servlet.mvc.exceptions.ControllerExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 18 with GrailsWebRequest

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

the class UrlMappingUtils method includeForUrlMappingInfoHelper.

private static IncludedContent includeForUrlMappingInfoHelper(String includeUrl, HttpServletRequest request, HttpServletResponse response, UrlMappingInfo info, Map model) {
    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) {
            if (webRequest.isActive()) {
                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);
                }
            } else {
                RequestContextHolder.setRequestAttributes(null);
            }
        }
    }
}
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 19 with GrailsWebRequest

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

the class DefaultUrlCreator method createURLInternal.

private String createURLInternal(String controller, String action, Map<String, String> parameterValues, boolean includeContextPath) {
    GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.getRequestAttributes();
    if (parameterValues == null)
        parameterValues = new HashMap<String, String>();
    boolean blankController = GrailsStringUtils.isBlank(controller);
    boolean blankAction = GrailsStringUtils.isBlank(action);
    if (!blankController) {
        parameterValues.put(GrailsControllerClass.CONTROLLER, controller);
    }
    if (!blankAction) {
        parameterValues.put(GrailsControllerClass.ACTION, action);
    }
    try {
        return createURLWithWebRequest(parameterValues, webRequest, includeContextPath);
    } finally {
        if (!blankController) {
            parameterValues.remove(GrailsControllerClass.CONTROLLER);
        }
        if (!blankAction) {
            parameterValues.remove(GrailsControllerClass.ACTION);
        }
    }
}
Also used : HashMap(java.util.HashMap) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest)

Example 20 with GrailsWebRequest

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

the class DefaultUrlMappingInfo method getActionName.

public String getActionName() {
    GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.getRequestAttributes();
    String name = webRequest == null ? null : checkDispatchAction(webRequest.getCurrentRequest());
    if (name == null) {
        name = evaluateNameForValue(actionName, webRequest);
    }
    return urlConverter.toUrlElement(name);
}
Also used : 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