|
38 | 38 | import javax.servlet.http.HttpServletRequest; |
39 | 39 | import javax.servlet.http.HttpServletResponse; |
40 | 40 |
|
| 41 | +import grails.web.CamelCaseUrlConverter; |
41 | 42 | import grails.web.UrlConverter; |
42 | 43 | import groovy.lang.Binding; |
43 | 44 | import org.apache.commons.lang.StringUtils; |
|
54 | 55 | import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest; |
55 | 56 | import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException; |
56 | 57 | import org.codehaus.groovy.grails.web.sitemesh.GrailsLayoutDecoratorMapper; |
| 58 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
57 | 59 | import org.springframework.context.ApplicationContext; |
58 | 60 | import org.springframework.util.Assert; |
59 | 61 | import org.springframework.web.context.WebApplicationContext; |
@@ -325,12 +327,33 @@ public static String forwardRequestForUrlMappingInfo(HttpServletRequest request, |
325 | 327 | // responsibility for rendering the response. |
326 | 328 | final GrailsWebRequest webRequest = GrailsWebRequest.lookup(request); |
327 | 329 | webRequest.removeAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, 0); |
328 | | - webRequest.setActionName(info.getActionName()); |
329 | | - |
| 330 | + info.configure(webRequest); |
| 331 | + passControllerForForwardedRequest(webRequest, info); |
330 | 332 | dispatcher.forward(request, response); |
331 | 333 | return forwardUrl; |
332 | 334 | } |
333 | 335 |
|
| 336 | + protected static void passControllerForForwardedRequest(final GrailsWebRequest webRequest, |
| 337 | + UrlMappingInfo info) { |
| 338 | + webRequest.removeAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST); |
| 339 | + if (info.getViewName() == null && info.getURI() == null) { |
| 340 | + GrailsApplication grailsApplicationToUse = webRequest.getAttributes().getGrailsApplication(); |
| 341 | + if(grailsApplicationToUse != null) { |
| 342 | + WebUtils.passControllerForUrlMappingInfoInRequest(webRequest, info, locateUrlConverter(webRequest), grailsApplicationToUse); |
| 343 | + } |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + private static UrlConverter locateUrlConverter(final GrailsWebRequest webRequest) { |
| 348 | + UrlConverter urlConverter = null; |
| 349 | + try { |
| 350 | + urlConverter = webRequest.getAttributes().getApplicationContext().getBean("grailsUrlConverter", UrlConverter.class); |
| 351 | + } catch (NoSuchBeanDefinitionException e) { |
| 352 | + urlConverter = new CamelCaseUrlConverter(); |
| 353 | + } |
| 354 | + return urlConverter; |
| 355 | + } |
| 356 | + |
334 | 357 | /** |
335 | 358 | * Include whatever the given UrlMappingInfo maps to within the current response |
336 | 359 | * |
@@ -380,6 +403,7 @@ public static IncludedContent includeForUrlMappingInfo(HttpServletRequest reques |
380 | 403 | info.configure(webRequest); |
381 | 404 | webRequest.getParameterMap().putAll(info.getParameters()); |
382 | 405 | webRequest.removeAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, 0); |
| 406 | + passControllerForForwardedRequest(webRequest, info); |
383 | 407 | } |
384 | 408 | return includeForUrl(includeUrl, request, response, model); |
385 | 409 | } |
@@ -710,25 +734,29 @@ public static String getForwardURI(HttpServletRequest request) { |
710 | 734 | return result; |
711 | 735 | } |
712 | 736 |
|
| 737 | + @Deprecated |
713 | 738 | public static GrailsClass getConfiguredControllerForUrlMappingInfo(GrailsWebRequest webRequest, UrlMappingInfo info, UrlConverter urlConverterToUse, GrailsApplication grailsApplicationToUse) { |
714 | | - String viewName; |
715 | | - viewName = info.getViewName(); |
| 739 | + return passControllerForUrlMappingInfoInRequest(webRequest, info, urlConverterToUse, grailsApplicationToUse); |
| 740 | + } |
716 | 741 |
|
717 | | - GrailsClass controller = null; |
718 | | - if (viewName == null && info.getURI() == null) { |
| 742 | + public static GrailsClass passControllerForUrlMappingInfoInRequest(GrailsWebRequest webRequest, UrlMappingInfo info, UrlConverter urlConverterToUse, GrailsApplication grailsApplicationToUse) { |
| 743 | + if (info.getViewName() == null && info.getURI() == null) { |
719 | 744 | ControllerArtefactHandler.ControllerCacheKey featureId = getFeatureId(urlConverterToUse, info); |
720 | | - controller = grailsApplicationToUse.getArtefactForFeature(ControllerArtefactHandler.TYPE, featureId); |
| 745 | + GrailsClass controller = grailsApplicationToUse.getArtefactForFeature(ControllerArtefactHandler.TYPE, featureId); |
721 | 746 | if (controller != null) { |
722 | | - |
723 | 747 | webRequest.setAttribute(GrailsApplicationAttributes.CONTROLLER_NAME_ATTRIBUTE, controller.getLogicalPropertyName(), WebRequest.SCOPE_REQUEST); |
724 | 748 | webRequest.setAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS, controller, WebRequest.SCOPE_REQUEST); |
725 | 749 | webRequest.setAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, Boolean.TRUE, WebRequest.SCOPE_REQUEST); |
| 750 | + } else { |
| 751 | + webRequest.removeAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST); |
726 | 752 | } |
727 | | - |
| 753 | + return controller; |
| 754 | + } else { |
| 755 | + webRequest.removeAttribute(GrailsApplicationAttributes.GRAILS_CONTROLLER_CLASS_AVAILABLE, WebRequest.SCOPE_REQUEST); |
| 756 | + return null; |
728 | 757 | } |
729 | | - return controller; |
730 | 758 | } |
731 | | - |
| 759 | + |
732 | 760 | public static ControllerArtefactHandler.ControllerCacheKey getFeatureId(UrlConverter urlConverter, UrlMappingInfo info) { |
733 | 761 | final String action = info.getActionName() == null ? "" : info.getActionName(); |
734 | 762 | final String controllerName = info.getControllerName(); |
|
0 commit comments