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