use of org.grails.web.util.IncludeResponseWrapper 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);
}
}
Aggregations