Search in sources :

Example 6 with UrlMapping

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

the class DefaultUrlMappingsHolder method sortMappings.

@SuppressWarnings("unchecked")
private void sortMappings() {
    List<ResponseCodeUrlMapping> responseCodeUrlMappings = new ArrayList<ResponseCodeUrlMapping>();
    Iterator<UrlMapping> iter = urlMappings.iterator();
    while (iter.hasNext()) {
        UrlMapping mapping = iter.next();
        if (mapping instanceof ResponseCodeUrlMapping) {
            responseCodeUrlMappings.add((ResponseCodeUrlMapping) mapping);
            iter.remove();
        }
    }
    if (initCounter.getAndIncrement() == 0) {
        Collections.reverse(responseCodeUrlMappings);
    }
    Collections.sort(urlMappings);
    Collections.reverse(urlMappings);
    urlMappings.addAll(0, responseCodeUrlMappings);
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) ArrayList(java.util.ArrayList)

Example 7 with UrlMapping

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

the class DefaultUrlMappingsHolder method toString.

@Override
public String toString() {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    pw.println("URL Mappings");
    pw.println("------------");
    for (UrlMapping mapping : mappings) {
        pw.println(mapping);
    }
    pw.flush();
    return sw.toString();
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 8 with UrlMapping

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

the class DefaultUrlMappingsHolder method initialize.

public void initialize() {
    sortMappings();
    cachedMatches = Caffeine.newBuilder().maximumSize(maxWeightedCacheCapacity).build();
    cachedListMatches = Caffeine.newBuilder().maximumWeight(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) Constrained(grails.gorm.validation.Constrained) ConstrainedProperty(grails.gorm.validation.ConstrainedProperty) HashSet(java.util.HashSet)

Example 9 with UrlMapping

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

the class DefaultUrlMappingsHolder method lookupMapping.

/**
 * Performs a match uses reverse mappings to looks up a mapping from the
 * controller, action and params. This is refactored to use a list of mappings
 * identified by only controller and action and then matches the mapping to select
 * the mapping that best matches the params (most possible matches).
 *
 * @param controller The controller name
 * @param action The action name
 * @param httpMethod The HTTP method
 * @param version
 * @param params The params  @return A UrlMapping instance or null
 */
@SuppressWarnings("unchecked")
protected UrlMapping lookupMapping(String controller, String action, String namespace, String pluginName, String httpMethod, String version, Map params) {
    final UrlMappingsListKey lookupKey = new UrlMappingsListKey(controller, action, namespace, pluginName, httpMethod, version);
    Collection mappingKeysSet = mappingsListLookup.get(lookupKey);
    final String actionName = lookupKey.action;
    boolean secondAttempt = false;
    final boolean isIndexAction = GrailsControllerClass.INDEX_ACTION.equals(actionName);
    if (null == mappingKeysSet) {
        lookupKey.httpMethod = UrlMapping.ANY_HTTP_METHOD;
        mappingKeysSet = mappingsListLookup.get(lookupKey);
    }
    if (null == mappingKeysSet && actionName != null) {
        lookupKey.action = null;
        mappingKeysSet = mappingsListLookup.get(lookupKey);
        secondAttempt = true;
    }
    if (null == mappingKeysSet)
        return null;
    Set<String> lookupParams = new HashSet<String>(params.keySet());
    if (secondAttempt) {
        lookupParams.removeAll(DEFAULT_ACTION_PARAMS);
        lookupParams.addAll(DEFAULT_ACTION_PARAMS);
    }
    UrlMappingKey[] mappingKeys = (UrlMappingKey[]) mappingKeysSet.toArray(new UrlMappingKey[mappingKeysSet.size()]);
    for (int i = mappingKeys.length; i > 0; i--) {
        UrlMappingKey mappingKey = mappingKeys[i - 1];
        if (lookupParams.containsAll(mappingKey.paramNames)) {
            final UrlMapping mapping = mappingsLookup.get(mappingKey);
            if (canInferAction(actionName, secondAttempt, isIndexAction, mapping)) {
                return mapping;
            }
            if (!secondAttempt) {
                return mapping;
            }
        }
    }
    return null;
}
Also used : UrlMapping(grails.web.mapping.UrlMapping) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 10 with UrlMapping

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

the class DefaultUrlMappingEvaluatorTests method testResourceMappingsWithVersionAndNamespace.

public void testResourceMappingsWithVersionAndNamespace() throws Exception {
    GroovyShell shell = new GroovyShell();
    Binding binding = new Binding();
    // Resource Entry: "/api/foo"(resources:"foo", version:'1.0', namespace:'v1')
    Script script = shell.parse("mappings = {\n" + "\"/api/foo\"(resources: 'foo', version: '1.0', namespace: 'v1')\n" + "}");
    script.setBinding(binding);
    script.run();
    Closure closure = (Closure) binding.getVariable("mappings");
    List<UrlMapping> mappings = evaluator.evaluateMappings(closure);
    assertTrue(mappings.size() > 0);
    //Check that version and namespace are correct for each mapping
    for (UrlMapping mapping : mappings) {
        assertEquals("1.0", mapping.getVersion());
        assertEquals("v1", mapping.getNamespace());
    }
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) GroovyShell(groovy.lang.GroovyShell)

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