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;
}
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);
}
}
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);
}
}
}
}
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);
}
}
}
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);
}
Aggregations