Permalink
Browse files

Namespaced controller support

  • Loading branch information...
jeffbrown committed Jul 27, 2012
1 parent 31e7396 commit 9da894e24ce76247b8f4090940ee0660e27a417c
Showing with 428 additions and 97 deletions.
  1. +34 −4 grails-core/src/main/groovy/org/codehaus/groovy/grails/commons/ControllerArtefactHandler.java
  2. +3 −2 ...ls-plugin-controllers/src/main/groovy/org/codehaus/groovy/grails/web/metaclass/ChainMethod.groovy
  3. +1 −1 ...plugin-gsp/src/main/groovy/org/codehaus/groovy/grails/plugins/web/taglib/ApplicationTagLib.groovy
  4. +2 −1 grails-plugin-url-mappings/src/main/groovy/grails/test/GrailsUrlMappingsTestCase.groovy
  5. +24 −6 ...plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/DefaultUrlCreator.java
  6. +25 −4 ...l-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/DefaultUrlMappingEvaluator.java
  7. +7 −1 ...in-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/DefaultUrlMappingInfo.java
  8. +43 −23 ...url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/DefaultUrlMappingsHolder.java
  9. +35 −10 ...s-plugin-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java
  10. +19 −3 ...n-url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/ResponseCodeUrlMapping.java
  11. +14 −1 ...url-mappings/src/main/groovy/org/codehaus/groovy/grails/web/mapping/filter/UrlMappingsFilter.java
  12. +16 −1 grails-test-suite-web/src/test/groovy/grails/test/GrailsUrlMappingsTestCaseTests.groovy
  13. +14 −14 ...test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/mapping/RegexUrlMappingTests.groovy
  14. +52 −0 ...t-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/taglib/LinkRenderingTagLibTests.groovy
  15. +7 −1 grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/AbstractUrlMapping.java
  16. +4 −3 grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/DefaultLinkGenerator.groovy
  17. +2 −0 grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/ForwardUrlMappingInfo.groovy
  18. +50 −0 grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/UrlCreator.java
  19. +44 −15 grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/UrlCreatorCache.java
  20. +2 −0 grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/UrlMapping.java
  21. +2 −0 grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/UrlMappingInfo.java
  22. +2 −2 grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/UrlMappingsHolder.java
  23. +1 −0 grails-web/src/main/groovy/org/codehaus/groovy/grails/web/servlet/GrailsApplicationAttributes.java
  24. +14 −2 ...ls-web/src/main/groovy/org/codehaus/groovy/grails/web/servlet/GrailsControllerHandlerMapping.java
  25. +8 −1 ...eb/src/main/groovy/org/codehaus/groovy/grails/web/servlet/mvc/AbstractGrailsControllerHelper.java
  26. +3 −2 ...ow/src/main/groovy/org/codehaus/groovy/grails/webflow/engine/builder/RuntimeRedirectAction.groovy
@@ -15,8 +15,13 @@
*/
package org.codehaus.groovy.grails.commons;
+import java.util.Map;
+
import grails.util.Environment;
+import grails.util.GrailsNameUtils;
+import org.codehaus.groovy.grails.plugins.GrailsPlugin;
+import org.codehaus.groovy.grails.plugins.GrailsPluginManager;
import org.codehaus.groovy.grails.plugins.support.aware.GrailsApplicationAware;
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
@@ -68,21 +73,46 @@ public String getPluginName() {
}
@Override
- public GrailsClass getArtefactForFeature(Object feature) {
+ public GrailsClass getArtefactForFeature(Object featureId) {
if (artefactInfo == null) {
return null;
}
+
+ String uri = null;
+ String pluginName = null;
+
+ if(featureId instanceof Map) {
+ Map featureIdMap = (Map)featureId;
+ uri = (String)featureIdMap.get("uri");
+ pluginName = (String)featureIdMap.get("pluginName");
+ } else {
+ uri = featureId.toString();
+ }
- String uri = feature.toString();
GrailsClass controllerClass = uriToControllerClassCache.get(uri);
if (controllerClass == null) {
final GrailsClass[] controllerClasses = artefactInfo.getGrailsClasses();
// iterate in reverse in order to pick up application classes first
for (int i = (controllerClasses.length-1); i >= 0; i--) {
GrailsClass c = controllerClasses[i];
if (((GrailsControllerClass) c).mapsToURI(uri)) {
- controllerClass = c;
- break;
+ boolean foundController = false;
+ if(pluginName != null) {
+ Object bean = grailsApplication.getMainContext().getBean("pluginManager");
+ if(bean instanceof GrailsPluginManager) {
+ GrailsPluginManager gpm = (GrailsPluginManager) bean;
+ GrailsPlugin pluginForClass = gpm.getPluginForClass(c.getClazz());
+ if(pluginForClass != null && pluginName.equals(pluginForClass.getName())) {
+ foundController = true;
+ }
+ }
+ } else {
+ foundController = true;
+ }
+ if(foundController) {
+ controllerClass = c;
+ break;
+ }
}
}
if (controllerClass == null) {
@@ -35,6 +35,7 @@ class ChainMethod {
def controller = args.controller ?: GrailsNameUtils.getLogicalPropertyName(
target.class.name, ControllerArtefactHandler.TYPE)
def action = args.action
+ def plugin = args.remove('plugin')
def id = args.id
def params = args.params ?: [:]
def model = args.model ?: [:]
@@ -70,10 +71,10 @@ class ChainMethod {
// the reverse URL mapping.
if (id) params.id = id
- UrlCreator creator = mappings.getReverseMapping(controller, action, params)
+ UrlCreator creator = mappings.getReverseMapping(controller, action, plugin, params)
def response = webRequest.getCurrentResponse()
- def url = response.encodeRedirectURL(creator.createURL(controller,action, params, 'utf-8'))
+ def url = response.encodeRedirectURL(creator.createURL(controller, action, plugin, params, 'utf-8'))
response.sendRedirect url
}
}
@@ -219,7 +219,7 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr
if (elementId) {
writer << " id=\"${elementId}\""
}
-
+ attrs.remove('plugin')
def remainingKeys = attrs.keySet() - LinkGenerator.LINK_ATTRIBUTES
for (key in remainingKeys) {
writer << " " << key << "=\"" << attrs[key]?.encodeAsHTML() << "\""
@@ -206,13 +206,14 @@ class GrailsUrlMappingsTestCase extends GroovyTestCase {
void assertReverseUrlMapping(assertions, url, paramAssertions) {
def controller = assertions.controller
def action = assertions.action
+ def plugin = assertions.plugin
def params = [:]
if (paramAssertions) {
paramAssertions.delegate = params
paramAssertions.resolveStrategy = Closure.DELEGATE_ONLY
paramAssertions.call()
}
- def urlCreator = mappingsHolder.getReverseMapping(controller, action, params)
+ def urlCreator = mappingsHolder.getReverseMapping(controller, action, plugin, params)
assertNotNull("could not create reverse mapping of '$url' for {controller = $controller, action = $action, params = $params}", urlCreator)
def createdUrl = urlCreator.createRelativeURL(controller, action, params, "UTF-8")
assertEquals("reverse mapping assertion for {controller = $controller, action = $action, params = $params}", url, createdUrl)
@@ -102,12 +102,16 @@ private String createURLWithWebRequest(Map parameterValues, GrailsWebRequest web
return actualUriBuf.toString();
}
- @SuppressWarnings("unchecked")
public String createURL(String controller, String action, Map parameterValues, String encoding) {
- return createURLInternal(controller, action, parameterValues, true);
+ return createURL(controller, action, null, parameterValues, encoding);
+ }
+
+ @SuppressWarnings("unchecked")
+ public String createURL(String controller, String action, String pluginName, Map parameterValues, String encoding) {
+ return createURLInternal(controller, action, pluginName, parameterValues, true);
}
- private String createURLInternal(String controller, String action,
+ private String createURLInternal(String controller, String action, String pluginName,
Map<String, String> parameterValues, boolean includeContextPath) {
GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.getRequestAttributes();
@@ -137,17 +141,31 @@ private String createURLInternal(String controller, String action,
@SuppressWarnings("unchecked")
public String createRelativeURL(String controller, String action, Map parameterValues, String encoding) {
- return createURLInternal(controller, action, parameterValues, false);
+ return createRelativeURL(controller, action, null, parameterValues, encoding);
+ }
+
+ @SuppressWarnings("unchecked")
+ public String createRelativeURL(String controller, String action, String pluginName, Map parameterValues, String encoding) {
+ return createURLInternal(controller, action, pluginName, parameterValues, false);
}
@SuppressWarnings("unchecked")
public String createRelativeURL(String controller, String action, Map parameterValues, String encoding, String fragment) {
- final String url = createURLInternal(controller, action, parameterValues, false);
+ return createRelativeURL(controller, action, null, parameterValues, encoding, fragment);
+ }
+
+ @SuppressWarnings("unchecked")
+ public String createRelativeURL(String controller, String action, String pluginName, Map parameterValues, String encoding, String fragment) {
+ final String url = createURLInternal(controller, action, pluginName, parameterValues, false);
return createUrlWithFragment(encoding, fragment, url);
}
public String createURL(String controller, String action, Map parameterValues, String encoding, String fragment) {
- String url = createURL(controller, action, parameterValues, encoding);
+ return createURL(controller, action, null, parameterValues, encoding, fragment);
+ }
+
+ public String createURL(String controller, String action, String pluginName, Map parameterValues, String encoding, String fragment) {
+ String url = createURL(controller, action, pluginName, parameterValues, encoding);
return createUrlWithFragment(encoding, fragment, url);
}
@@ -224,6 +224,7 @@ public Object call(Object... args) {
private Map<String, Object> parameterValues = new HashMap<String, Object>();
private Binding binding;
private Object actionName = null;
+ private Object pluginName = null;
private Object controllerName = null;
private Object viewName = null;
private ServletContext sc;
@@ -292,6 +293,14 @@ public void setController(Object controller) {
public Object getController() {
return controllerName;
}
+
+ public void setPlugin(Object plugin) {
+ pluginName = plugin;
+ }
+
+ public Object getPlugin() {
+ return pluginName;
+ }
public Object getView() {
return viewName;
@@ -352,6 +361,8 @@ private Object _invoke(String methodName, Object arg, Object delegate) {
@SuppressWarnings("hiding")
Object actionName;
@SuppressWarnings("hiding")
+ Object pluginName;
+ @SuppressWarnings("hiding")
Object viewName;
@SuppressWarnings("hiding")
Object uri;
@@ -361,10 +372,12 @@ private Object _invoke(String methodName, Object arg, Object delegate) {
actionName = binding.getVariables().get(GrailsControllerClass.ACTION);
viewName = binding.getVariables().get(GrailsControllerClass.VIEW);
uri = binding.getVariables().get("uri");
+ pluginName = binding.getVariables().get("plugin");
}
else {
controllerName = this.controllerName;
actionName = this.actionName;
+ pluginName = this.pluginName;
viewName = this.viewName;
uri = this.uri;
}
@@ -380,7 +393,7 @@ private Object _invoke(String methodName, Object arg, Object delegate) {
}
}
else {
- urlMapping = createURLMapping(urlData, isResponseCode, controllerName, actionName, viewName, constraints);
+ urlMapping = createURLMapping(urlData, isResponseCode, controllerName, actionName, pluginName, viewName, constraints);
}
if (binding != null) {
@@ -416,6 +429,7 @@ private Object _invoke(String methodName, Object arg, Object delegate) {
controllerName = null;
actionName = null;
viewName = null;
+ pluginName = null;
}
previousConstraints.clear();
urlDefiningMode = true;
@@ -506,6 +520,8 @@ private UrlMapping getURLMappingForNamedArgs(Map namedArguments,
controllerName = getControllerName(namedArguments, bindingVariables);
actionName = getActionName(namedArguments, bindingVariables);
}
+ @SuppressWarnings("hiding")
+ Object pluginName = getPluginName(namedArguments, bindingVariables);
@SuppressWarnings("hiding")
Object viewName = getViewName(namedArguments, bindingVariables);
@@ -528,7 +544,7 @@ private UrlMapping getURLMappingForNamedArgs(Map namedArguments,
}
}
else {
- urlMapping = createURLMapping(urlData, isResponseCode, controllerName, actionName, viewName, constraints);
+ urlMapping = createURLMapping(urlData, isResponseCode, controllerName, actionName, pluginName, viewName, constraints);
}
Object exceptionArg = getException(namedArguments, bindingVariables);
@@ -588,6 +604,10 @@ private Object getControllerName(Map namedArguments, Map bindingVariables) {
return getVariableFromNamedArgsOrBinding(namedArguments, bindingVariables,GrailsControllerClass.CONTROLLER, controllerName);
}
+ private Object getPluginName(Map namedArguments, Map bindingVariables) {
+ return getVariableFromNamedArgsOrBinding(namedArguments, bindingVariables, "plugin", pluginName);
+ }
+
private Object getViewName(Map namedArguments, Map bindingVariables) {
return getVariableFromNamedArgsOrBinding(namedArguments, bindingVariables,GrailsControllerClass.VIEW, viewName);
}
@@ -603,13 +623,14 @@ private Object getException(Map namedArguments, Map bindingVariables) {
private UrlMapping createURLMapping(UrlMappingData urlData, boolean isResponseCode,
@SuppressWarnings("hiding") Object controllerName,
@SuppressWarnings("hiding") Object actionName,
+ @SuppressWarnings("hiding") Object pluginName,
@SuppressWarnings("hiding") Object viewName, ConstrainedProperty[] constraints) {
if (!isResponseCode) {
- return new RegexUrlMapping(urlData, controllerName, actionName, viewName,
+ return new RegexUrlMapping(urlData, controllerName, actionName, pluginName, viewName,
constraints, sc);
}
- return new ResponseCodeUrlMapping(urlData, controllerName, actionName, viewName,
+ return new ResponseCodeUrlMapping(urlData, controllerName, actionName, pluginName, viewName,
null, sc);
}
}
@@ -48,6 +48,7 @@
private Object controllerName;
private Object actionName;
+ private Object pluginName;
private Object id;
private static final String ID_PARAM = "id";
private UrlMappingData urlData;
@@ -70,13 +71,14 @@ private DefaultUrlMappingInfo(Map params, UrlMappingData urlData, ServletContext
}
@SuppressWarnings("rawtypes")
- public DefaultUrlMappingInfo(Object controllerName, Object actionName, Object viewName, Map params,
+ public DefaultUrlMappingInfo(Object controllerName, Object actionName, Object pluginName, Object viewName, Map params,
UrlMappingData urlData, ServletContext servletContext) {
this(params, urlData, servletContext);
Assert.isTrue(controllerName != null || viewName != null, "URL mapping must either provide a controller or view name to map to!");
Assert.notNull(params, "Argument [params] cannot be null");
this.controllerName = controllerName;
this.actionName = actionName;
+ this.pluginName = pluginName;
if (actionName == null) {
this.viewName = viewName;
}
@@ -112,6 +114,10 @@ public boolean isParsingRequest() {
public void setParsingRequest(boolean parsingRequest) {
this.parsingRequest = parsingRequest;
}
+
+ public String getPluginName() {
+ return pluginName != null ? pluginName.toString() : null;
+ }
public String getControllerName() {
String name = evaluateNameForValue(controllerName);
Oops, something went wrong.

0 comments on commit 9da894e

Please sign in to comment.