Search in sources :

Example 1 with IncludedContent

use of org.grails.web.util.IncludedContent 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 Object previousControllerClass = webRequest.getAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST);
    final Object previousMatchedRequest = webRequest.getAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, WebRequest.SCOPE_REQUEST);
    try {
        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);
            dispatcher.include(request, responseWrapper);
            if (responseWrapper.getRedirectURL() != null) {
                return new IncludedContent(responseWrapper.getRedirectURL());
            }
            return new IncludedContent(responseWrapper.getContentType(), responseWrapper.getContent());
        } finally {
            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) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletException (javax.servlet.ServletException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)1 ControllerExecutionException (org.grails.web.servlet.mvc.exceptions.ControllerExecutionException)1 IncludeResponseWrapper (org.grails.web.util.IncludeResponseWrapper)1 IncludedContent (org.grails.web.util.IncludedContent)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1