aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/request/HttpConfigRequests.java
blob: 88b97932066f68e4f31e78792697f77784ef5e87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http.v2.request;

import java.net.URI;

import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.jdisc.application.BindingMatch;
import com.yahoo.jdisc.application.UriPattern;
import com.yahoo.jdisc.application.UriPattern.Match;
import com.yahoo.vespa.config.server.RequestHandler;
import com.yahoo.vespa.config.server.tenant.Tenant;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.vespa.config.server.http.NotFoundException;

/**
 * Helpers for v2 config REST API
 *
 * @author vegardh
 */
public class HttpConfigRequests {

    static final String RECURSIVE_QUERY_PROPERTY = "recursive";
    
    /**
     * Produces the binding match for the request. If it's not available on the jDisc request, create one for
     * testing using the long and short app id URL patterns given.
     * @param req an {@link com.yahoo.container.jdisc.HttpRequest}
     * @param patterns A list of patterns that should be matched if no match on binding.
     * @return match
     */
    public static BindingMatch<?> getBindingMatch(HttpRequest req, String ... patterns) {
        com.yahoo.jdisc.http.HttpRequest jDiscRequest = req.getJDiscRequest();
        if (jDiscRequest==null) throw new IllegalArgumentException("No JDisc request for: " + req.getUri());
        BindingMatch<?> jdBm = jDiscRequest.getBindingMatch();
        if (jdBm!=null) return jdBm;

        // If not, use provided patterns
        for (String pattern : patterns) {
            UriPattern fullAppIdPattern = new UriPattern(pattern);
            URI uri = req.getUri();
            Match match = fullAppIdPattern.match(uri);
            if (match!=null) return new BindingMatch<>(match, new Object(), fullAppIdPattern);
        }
        throw new IllegalArgumentException("Illegal url for config request: " + req.getUri());
    }


    public static RequestHandler getRequestHandler(TenantRepository tenantRepository, TenantRequest request) {
        Tenant tenant = tenantRepository.getTenant(request.getApplicationId().tenant());
        if (tenant==null) throw new NotFoundException("No such tenant: "+request.getApplicationId().tenant());
        return tenant.getRequestHandler();
    }
    
}