Search in sources :

Example 1 with UrlMappingInfo

use of grails.web.mapping.UrlMappingInfo in project grails-core by grails.

the class DefaultUrlMappingsHolder method allowedMethods.

@Override
public Set<HttpMethod> allowedMethods(String uri) {
    UrlMappingInfo[] urlMappingInfos = matchAll(uri, UrlMapping.ANY_HTTP_METHOD);
    Set<HttpMethod> methods = new HashSet<HttpMethod>();
    for (UrlMappingInfo urlMappingInfo : urlMappingInfos) {
        if (urlMappingInfo.getHttpMethod() == null || urlMappingInfo.getHttpMethod().equals(UrlMapping.ANY_HTTP_METHOD)) {
            methods.addAll(Arrays.asList(HttpMethod.values()));
            break;
        } else {
            HttpMethod method = HttpMethod.valueOf(urlMappingInfo.getHttpMethod().toUpperCase());
            methods.add(method);
        }
    }
    return Collections.unmodifiableSet(methods);
}
Also used : HttpMethod(org.springframework.http.HttpMethod) UrlMappingInfo(grails.web.mapping.UrlMappingInfo) HashSet(java.util.HashSet)

Example 2 with UrlMappingInfo

use of grails.web.mapping.UrlMappingInfo in project grails-core by grails.

the class DefaultUrlMappingsHolder method matchStatusCode.

public UrlMappingInfo matchStatusCode(int responseCode) {
    for (UrlMapping mapping : mappings) {
        if (mapping instanceof ResponseCodeUrlMapping) {
            ResponseCodeUrlMapping responseCodeUrlMapping = (ResponseCodeUrlMapping) mapping;
            if (responseCodeUrlMapping.getExceptionType() != null)
                continue;
            final UrlMappingInfo current = responseCodeUrlMapping.match(responseCode);
            if (current != null)
                return current;
        }
    }
    return null;
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) UrlMappingInfo(grails.web.mapping.UrlMappingInfo)

Example 3 with UrlMappingInfo

use of grails.web.mapping.UrlMappingInfo in project grails-core by grails.

the class DefaultUrlMappingsHolder method initialize.

public void initialize() {
    sortMappings();
    cachedMatches = new ConcurrentLinkedHashMap.Builder<String, UrlMappingInfo>().maximumWeightedCapacity(maxWeightedCacheCapacity).build();
    cachedListMatches = new ConcurrentLinkedHashMap.Builder<UriToUrlMappingKey, List<UrlMappingInfo>>().maximumWeightedCapacity(maxWeightedCacheCapacity).weigher(CustomListWeigher.INSTANCE).build();
    if (urlCreatorMaxWeightedCacheCapacity > 0) {
        urlCreatorCache = new UrlCreatorCache(urlCreatorMaxWeightedCacheCapacity);
    }
    mappings = urlMappings.toArray(new UrlMapping[urlMappings.size()]);
    for (UrlMapping mapping : mappings) {
        String mappingName = mapping.getMappingName();
        if (mappingName != null) {
            namedMappings.put(mappingName, mapping);
        }
        String controllerName = mapping.getControllerName() instanceof String ? mapping.getControllerName().toString() : null;
        String actionName = mapping.getActionName() instanceof String ? mapping.getActionName().toString() : null;
        String pluginName = mapping.getPluginName() instanceof String ? mapping.getPluginName().toString() : null;
        String httpMethod = mapping.getHttpMethod();
        String version = mapping.getVersion();
        String namespace = mapping.getNamespace() instanceof String ? mapping.getNamespace().toString() : null;
        Constrained[] params = mapping.getConstraints();
        Set<String> requiredParams = new HashSet<String>();
        int optionalIndex = -1;
        for (int j = 0; j < params.length; j++) {
            Constrained param = params[j];
            if (param instanceof ConstrainedProperty) {
                if (!param.isNullable()) {
                    requiredParams.add(((ConstrainedProperty) param).getPropertyName());
                } else {
                    optionalIndex = j;
                    break;
                }
            }
        }
        UrlMappingKey key = new UrlMappingKey(controllerName, actionName, namespace, pluginName, httpMethod, version, requiredParams);
        mappingsLookup.put(key, mapping);
        UrlMappingsListKey listKey = new UrlMappingsListKey(controllerName, actionName, namespace, pluginName, httpMethod, version);
        mappingsListLookup.put(listKey, key);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Reverse mapping: " + key + " -> " + mapping);
        }
        Set<String> requiredParamsAndOptionals = new HashSet<String>(requiredParams);
        if (optionalIndex > -1) {
            for (int j = optionalIndex; j < params.length; j++) {
                Constrained constrained = params[j];
                if (constrained instanceof ConstrainedProperty) {
                    ConstrainedProperty param = (ConstrainedProperty) constrained;
                    requiredParamsAndOptionals.add(param.getPropertyName());
                    key = new UrlMappingKey(controllerName, actionName, namespace, pluginName, httpMethod, version, new HashSet<>(requiredParamsAndOptionals));
                    mappingsLookup.put(key, mapping);
                    listKey = new UrlMappingsListKey(controllerName, actionName, namespace, pluginName, httpMethod, version);
                    mappingsListLookup.put(listKey, key);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Reverse mapping: " + key + " -> " + mapping);
                    }
                }
            }
        }
    }
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) ConcurrentLinkedHashMap(com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap) Constrained(grails.gorm.validation.Constrained) ConstrainedProperty(grails.gorm.validation.ConstrainedProperty) UrlMappingInfo(grails.web.mapping.UrlMappingInfo) HashSet(java.util.HashSet)

Example 4 with UrlMappingInfo

use of grails.web.mapping.UrlMappingInfo in project grails-core by grails.

the class DefaultUrlMappingsHolder method matchAll.

public UrlMappingInfo[] matchAll(String uri, String httpMethod) {
    if (isExcluded(uri))
        return EMPTY_RESULTS;
    boolean anyHttpMethod = httpMethod != null && httpMethod.equalsIgnoreCase(UrlMapping.ANY_HTTP_METHOD);
    List<UrlMappingInfo> matchingUrls = new ArrayList<UrlMappingInfo>();
    UriToUrlMappingKey cacheKey = new UriToUrlMappingKey(uri, httpMethod, UrlMapping.ANY_VERSION);
    if (cachedListMatches.containsKey(cacheKey)) {
        matchingUrls = cachedListMatches.get(cacheKey);
    } else {
        for (UrlMapping mapping : mappings) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Attempting to match URI [" + uri + "] with pattern [" + mapping.getUrlData().getUrlPattern() + "]");
            }
            UrlMappingInfo current = mapping.match(uri);
            if (current != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Matched URI [" + uri + "] with pattern [" + mapping.getUrlData().getUrlPattern() + "], adding to posibilities");
                }
                String mappingHttpMethod = current.getHttpMethod();
                if (mappingHttpMethod == null || anyHttpMethod || mappingHttpMethod.equalsIgnoreCase(UrlMapping.ANY_HTTP_METHOD) || mappingHttpMethod.equalsIgnoreCase(httpMethod))
                    matchingUrls.add(current);
            }
        }
        cachedListMatches.put(cacheKey, matchingUrls);
    }
    return matchingUrls.toArray(new UrlMappingInfo[matchingUrls.size()]);
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) ArrayList(java.util.ArrayList) UrlMappingInfo(grails.web.mapping.UrlMappingInfo)

Example 5 with UrlMappingInfo

use of grails.web.mapping.UrlMappingInfo 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)

Aggregations

UrlMappingInfo (grails.web.mapping.UrlMappingInfo)6 UrlMapping (grails.web.mapping.UrlMapping)5 HashSet (java.util.HashSet)2 ConcurrentLinkedHashMap (com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap)1 Constrained (grails.gorm.validation.Constrained)1 ConstrainedProperty (grails.gorm.validation.ConstrainedProperty)1 Binding (groovy.lang.Binding)1 Closure (groovy.lang.Closure)1 GroovyShell (groovy.lang.GroovyShell)1 Script (groovy.lang.Script)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)1 HttpMethod (org.springframework.http.HttpMethod)1