Search in sources :

Example 11 with UrlMapping

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());
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) List(java.util.List) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) GroovyShell(groovy.lang.GroovyShell) UrlMappingInfo(grails.web.mapping.UrlMappingInfo)

Example 12 with UrlMapping

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;
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) GrailsControllerUrlMappings(org.grails.web.mapping.mvc.GrailsControllerUrlMappings) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) GrailsControllerClass(grails.core.GrailsControllerClass) Config(grails.config.Config) ArrayList(java.util.ArrayList) GrailsUrlMappingsClass(grails.core.GrailsUrlMappingsClass) ArtefactAdditionEvent(grails.core.events.ArtefactAdditionEvent) GrailsClass(grails.core.GrailsClass) UrlConverter(grails.web.UrlConverter) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with UrlMapping

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;
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) HashSet(java.util.HashSet) UrlCreator(grails.web.mapping.UrlCreator)

Example 14 with UrlMapping

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;
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) UrlMappingInfo(grails.web.mapping.UrlMappingInfo)

Aggregations

UrlMapping (grails.web.mapping.UrlMapping)14 UrlMappingInfo (grails.web.mapping.UrlMappingInfo)4 Binding (groovy.lang.Binding)3 Closure (groovy.lang.Closure)3 GroovyShell (groovy.lang.GroovyShell)3 Script (groovy.lang.Script)3 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Config (grails.config.Config)1 GrailsClass (grails.core.GrailsClass)1 GrailsControllerClass (grails.core.GrailsControllerClass)1 GrailsUrlMappingsClass (grails.core.GrailsUrlMappingsClass)1 ArtefactAdditionEvent (grails.core.events.ArtefactAdditionEvent)1 Constrained (grails.gorm.validation.Constrained)1 ConstrainedProperty (grails.gorm.validation.ConstrainedProperty)1 VersionComparator (grails.plugins.VersionComparator)1 UrlConverter (grails.web.UrlConverter)1 UrlCreator (grails.web.mapping.UrlCreator)1 UrlMappingEvaluator (grails.web.mapping.UrlMappingEvaluator)1