All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.web.router.DefaultUriRouteMatch Maven / Gradle / Ivy

There is a newer version: 4.6.5
Show newest version
/*
 * Copyright 2017-2019 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.web.router;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.uri.UriMatchInfo;
import io.micronaut.http.uri.UriMatchVariable;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
 * Default implementation of the {@link RouteMatch} interface for matches to URIs.
 *
 * @param  The target type
 * @param  The return type
 * @author Graeme Rocher
 * @since 1.0
 */
@Internal
class DefaultUriRouteMatch extends AbstractRouteMatch implements UriRouteMatch {

    private final HttpMethod httpMethod;
    private final UriMatchInfo matchInfo;
    private final DefaultRouteBuilder.DefaultUriRoute uriRoute;
    private final Charset defaultCharset;

    /**
     * @param matchInfo The URI match info
     * @param uriRoute The URI route
     * @param defaultCharset The default charset
     * @param conversionService The conversion service
     */
    DefaultUriRouteMatch(UriMatchInfo matchInfo,
                         DefaultRouteBuilder.DefaultUriRoute uriRoute,
                         Charset defaultCharset, ConversionService conversionService
    ) {
        super(uriRoute, conversionService);
        this.uriRoute = uriRoute;
        this.matchInfo = matchInfo;
        this.httpMethod = uriRoute.httpMethod;
        this.defaultCharset = defaultCharset;
    }

    @Override
    public UriRouteMatch decorate(Function, R> executor) {
        Map variables = getVariableValues();
        List arguments = getRequiredArguments();
        RouteMatch thisRoute = this;
        return new DefaultUriRouteMatch(matchInfo, uriRoute, defaultCharset, conversionService) {
            @Override
            public List getRequiredArguments() {
                return Collections.unmodifiableList(arguments);
            }

            @Override
            public R execute(Map argumentValues) {
                return (R) executor.apply(thisRoute);
            }

            @Override
            public Map getVariableValues() {
                return variables;
            }
        };
    }

    @Override
    protected RouteMatch newFulfilled(Map newVariables, List requiredArguments) {
        return new DefaultUriRouteMatch(matchInfo, uriRoute, defaultCharset, conversionService) {

            @Override
            public List getRequiredArguments() {
                return Collections.unmodifiableList(requiredArguments);
            }

            @Override
            public Map getVariableValues() {
                return newVariables;
            }

            @Override
            public Optional> getRequiredInput(String name) {
                return super.getRequiredInput(name);
            }
        };
    }

    @Override
    public UriRouteMatch fulfill(Map argumentValues) {
        return (UriRouteMatch) super.fulfill(argumentValues);
    }

    @Override
    public String getUri() {
        return matchInfo.getUri();
    }

    @Override
    public Map getVariableValues() {
        Map variables = matchInfo.getVariableValues();
        Map decoded = new LinkedHashMap<>(variables.size());
        for (Map.Entry entry : variables.entrySet()) {
            String k = entry.getKey();
            Object v = entry.getValue();
            if (v instanceof CharSequence) {
                try {
                    v = URLDecoder.decode(v.toString(), defaultCharset.toString());
                } catch (UnsupportedEncodingException e) {
                    // ignore
                }
            }
            decoded.put(k, v);
        }
        return decoded;
    }

    @Override
    public List getVariables() {
        return matchInfo.getVariables();
    }

    @Override
    public UriRoute getRoute() {
        return (UriRoute) abstractRoute;
    }

    @Override
    public HttpMethod getHttpMethod() {
        return httpMethod;
    }

    @Override
    public String toString() {
        return httpMethod + " - " + matchInfo.getUri();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy