use of grails.web.mapping.UrlMapping in project grails-core by grails.
the class DefaultUrlMappingEvaluatorTests method testNewMethod.
public void testNewMethod() throws Exception {
GroovyShell shell = new GroovyShell();
Binding binding = new Binding();
Script script = shell.parse("mappings = {\n" + " \"/$controller/$action?/$id?\" { \n" + " constraints {\n" + " id(matches:/\\d+/)\n" + " }\n" + " }\n" + "}\n");
script.setBinding(binding);
script.run();
Closure closure = (Closure) binding.getVariable("mappings");
List mappings = evaluator.evaluateMappings(closure);
assertEquals(1, mappings.size());
UrlMapping mapping = (UrlMapping) mappings.get(0);
assertNull(mapping.getActionName());
assertNull(mapping.getControllerName());
assertEquals("(*)", mapping.getUrlData().getTokens()[0]);
assertEquals("(*)?", mapping.getUrlData().getTokens()[1]);
assertEquals("(*)?", mapping.getUrlData().getTokens()[2]);
assertNotNull(mapping.getConstraints());
assertTrue(makeSureMatchesConstraintExistsOnId(mapping));
GrailsWebRequest r = GrailsWebMockUtil.bindMockWebRequest();
UrlMappingInfo info = mapping.match("/mycontroller");
info.configure(r);
assertEquals("mycontroller", info.getControllerName());
assertNull(mapping.match("/mycontroller").getActionName());
assertNull(mapping.match("/mycontroller").getId());
UrlMappingInfo info2 = mapping.match("/mycontroller/test");
info2.configure(r);
assertEquals("test", info2.getActionName());
assertNull(mapping.match("/mycontroller/test").getId());
assertEquals("234", mapping.match("/blog/test/234").getId());
}
use of grails.web.mapping.UrlMapping in project grails-core by grails.
the class UrlMappingsHolderFactoryBean method afterPropertiesSet.
public void afterPropertiesSet() throws Exception {
Assert.state(applicationContext != null, "Property [applicationContext] must be set!");
Assert.state(grailsApplication != null, "Property [grailsApplication] must be set!");
List urlMappings = new ArrayList();
List excludePatterns = new ArrayList();
GrailsClass[] mappings = grailsApplication.getArtefacts(UrlMappingsArtefactHandler.TYPE);
final DefaultUrlMappingEvaluator mappingEvaluator = new DefaultUrlMappingEvaluator(applicationContext);
mappingEvaluator.setPluginManager(pluginManager);
if (mappings.length == 0) {
urlMappings.addAll(mappingEvaluator.evaluateMappings(DefaultUrlMappings.getMappings()));
} else {
for (int i = 0; i < mappings.length; i++) {
GrailsClass mapping = mappings[i];
GrailsUrlMappingsClass mappingClass = (GrailsUrlMappingsClass) mapping;
List<UrlMapping> grailsClassMappings;
if (Script.class.isAssignableFrom(mappingClass.getClazz())) {
grailsClassMappings = mappingEvaluator.evaluateMappings(mappingClass.getClazz());
} else {
grailsClassMappings = mappingEvaluator.evaluateMappings(mappingClass.getMappingsClosure());
}
if (!StringUtils.isEmpty(mapping.getPluginName())) {
for (int j = 0; j < grailsClassMappings.size(); j++) {
grailsClassMappings.get(j).setPluginIndex(i);
}
}
urlMappings.addAll(grailsClassMappings);
if (mappingClass.getExcludePatterns() != null) {
excludePatterns.addAll(mappingClass.getExcludePatterns());
}
}
}
DefaultUrlMappingsHolder defaultUrlMappingsHolder = new DefaultUrlMappingsHolder(urlMappings, excludePatterns, true);
Config config = grailsApplication.getConfig();
Integer cacheSize = config.getProperty(URL_MAPPING_CACHE_MAX_SIZE, Integer.class, null);
if (cacheSize != null) {
defaultUrlMappingsHolder.setMaxWeightedCacheCapacity(cacheSize);
}
Integer urlCreatorCacheSize = config.getProperty(URL_CREATOR_CACHE_MAX_SIZE, Integer.class, null);
if (urlCreatorCacheSize != null) {
defaultUrlMappingsHolder.setUrlCreatorMaxWeightedCacheCapacity(urlCreatorCacheSize);
}
// call initialize() after settings are in place
defaultUrlMappingsHolder.initialize();
UrlConverter urlConverter = applicationContext.containsBean(UrlConverter.BEAN_NAME) ? applicationContext.getBean(UrlConverter.BEAN_NAME, UrlConverter.class) : null;
final GrailsControllerUrlMappings grailsControllerUrlMappings = new GrailsControllerUrlMappings(grailsApplication, defaultUrlMappingsHolder, urlConverter);
((ConfigurableApplicationContext) applicationContext).addApplicationListener(new ApplicationListener<ArtefactAdditionEvent>() {
@Override
public void onApplicationEvent(ArtefactAdditionEvent event) {
GrailsClass artefact = event.getArtefact();
if (artefact instanceof GrailsControllerClass) {
grailsControllerUrlMappings.registerController((GrailsControllerClass) artefact);
}
}
});
urlMappingsHolder = grailsControllerUrlMappings;
}
use of grails.web.mapping.UrlMapping in project grails-core by grails.
the class DefaultUrlMappingsHolder method resolveUrlCreator.
@SuppressWarnings("unchecked")
private UrlCreator resolveUrlCreator(final String controller, final String action, final String namespace, final String pluginName, String httpMethod, String version, Map params, boolean useDefault) {
UrlMapping mapping = null;
if (httpMethod == null) {
httpMethod = UrlMapping.ANY_HTTP_METHOD;
}
mapping = namedMappings.get(params.remove("mappingName"));
if (mapping == null) {
mapping = lookupMapping(controller, action, namespace, pluginName, httpMethod, version, params);
if (mapping == null) {
lookupMapping(controller, action, namespace, pluginName, UrlMapping.ANY_HTTP_METHOD, version, params);
}
}
if (mapping == null || (mapping instanceof ResponseCodeUrlMapping)) {
UrlMappingKey lookupKey = new UrlMappingKey(controller, action, namespace, pluginName, httpMethod, version, Collections.<String>emptySet());
mapping = mappingsLookup.get(lookupKey);
if (mapping == null) {
lookupKey.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKey);
}
}
if (mapping == null || (mapping instanceof ResponseCodeUrlMapping)) {
Set<String> lookupParams = new HashSet<String>(DEFAULT_ACTION_PARAMS);
Set<String> paramKeys = new HashSet<String>(params.keySet());
paramKeys.removeAll(lookupParams);
lookupParams.addAll(paramKeys);
UrlMappingKey lookupKey = new UrlMappingKey(controller, null, namespace, pluginName, httpMethod, version, lookupParams);
mapping = mappingsLookup.get(lookupKey);
if (mapping == null) {
lookupKey.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKey);
}
if (mapping == null) {
lookupParams.removeAll(paramKeys);
UrlMappingKey lookupKeyModifiedParams = new UrlMappingKey(controller, null, namespace, pluginName, httpMethod, version, lookupParams);
mapping = mappingsLookup.get(lookupKeyModifiedParams);
if (mapping == null) {
lookupKeyModifiedParams.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKeyModifiedParams);
}
}
}
if (mapping == null || (mapping instanceof ResponseCodeUrlMapping)) {
Set<String> lookupParams = new HashSet<String>(DEFAULT_CONTROLLER_ONLY_PARAMS);
Set<String> paramKeys = new HashSet<String>(params.keySet());
paramKeys.removeAll(lookupParams);
lookupParams.addAll(paramKeys);
UrlMappingKey lookupKey = new UrlMappingKey(null, action, namespace, pluginName, httpMethod, version, lookupParams);
mapping = mappingsLookup.get(lookupKey);
if (mapping == null) {
lookupKey.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKey);
}
if (mapping == null) {
lookupParams.removeAll(paramKeys);
UrlMappingKey lookupKeyModifiedParams = new UrlMappingKey(null, action, namespace, pluginName, httpMethod, version, lookupParams);
mapping = mappingsLookup.get(lookupKeyModifiedParams);
if (mapping == null) {
lookupKeyModifiedParams.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKeyModifiedParams);
}
}
}
if (mapping == null || (mapping instanceof ResponseCodeUrlMapping)) {
Set<String> lookupParams = new HashSet<String>(DEFAULT_CONTROLLER_PARAMS);
Set<String> paramKeys = new HashSet<String>(params.keySet());
paramKeys.removeAll(lookupParams);
lookupParams.addAll(paramKeys);
UrlMappingKey lookupKey = new UrlMappingKey(null, null, namespace, pluginName, httpMethod, version, lookupParams);
mapping = mappingsLookup.get(lookupKey);
if (mapping == null) {
lookupKey.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKey);
}
if (mapping == null) {
lookupParams.removeAll(paramKeys);
UrlMappingKey lookupKeyModifiedParams = new UrlMappingKey(null, null, namespace, pluginName, httpMethod, version, lookupParams);
mapping = mappingsLookup.get(lookupKeyModifiedParams);
if (mapping == null) {
lookupKeyModifiedParams.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKeyModifiedParams);
}
}
}
if (mapping == null || (mapping instanceof ResponseCodeUrlMapping)) {
Set<String> lookupParams = new HashSet<>(DEFAULT_NAMESPACE_PARAMS);
Set<String> paramKeys = new HashSet<String>(params.keySet());
paramKeys.removeAll(lookupParams);
lookupParams.addAll(paramKeys);
UrlMappingKey lookupKey = new UrlMappingKey(null, null, null, pluginName, httpMethod, version, lookupParams);
mapping = mappingsLookup.get(lookupKey);
if (mapping == null) {
lookupKey.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKey);
}
if (mapping == null) {
lookupParams.removeAll(paramKeys);
UrlMappingKey lookupKeyModifiedParams = new UrlMappingKey(null, null, null, pluginName, httpMethod, version, lookupParams);
mapping = mappingsLookup.get(lookupKeyModifiedParams);
if (mapping == null) {
lookupKeyModifiedParams.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKeyModifiedParams);
}
}
}
if (mapping == null || (mapping instanceof ResponseCodeUrlMapping)) {
Set<String> lookupParams = new HashSet<String>();
UrlMappingKey lookupKey = new UrlMappingKey(controller, null, namespace, pluginName, httpMethod, version, lookupParams);
mapping = mappingsLookup.get(lookupKey);
if (mapping == null) {
lookupKey.httpMethod = UrlMapping.ANY_HTTP_METHOD;
mapping = mappingsLookup.get(lookupKey);
}
}
UrlCreator creator = null;
if (mapping == null || (mapping instanceof ResponseCodeUrlMapping)) {
if (useDefault) {
creator = new DefaultUrlCreator(controller, action);
}
} else {
creator = mapping;
}
return creator;
}
use of grails.web.mapping.UrlMapping in project grails-core by grails.
the class DefaultUrlMappingsHolder method matchStatusCode.
public UrlMappingInfo matchStatusCode(int responseCode, Throwable e) {
for (UrlMapping mapping : mappings) {
if (mapping instanceof ResponseCodeUrlMapping) {
ResponseCodeUrlMapping responseCodeUrlMapping = (ResponseCodeUrlMapping) mapping;
final UrlMappingInfo current = responseCodeUrlMapping.match(responseCode);
if (current != null) {
if (responseCodeUrlMapping.getExceptionType() != null && responseCodeUrlMapping.getExceptionType().isInstance(e)) {
return current;
}
}
}
}
return null;
}
Aggregations