aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/HostHandler.java
blob: 327bed8a0055607582a81ee93e9eba1d9daa8572 (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
55
56
57
58
// 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;

import com.yahoo.component.annotation.Inject;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Zone;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.application.BindingMatch;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.http.HttpErrorResponse;
import com.yahoo.vespa.config.server.http.HttpHandler;
import com.yahoo.vespa.config.server.http.JSONResponse;
import com.yahoo.vespa.config.server.http.v2.request.HttpConfigRequests;

/**
 * Handler for getting tenant and application for a given hostname.
 *
 * @author hmusum
 */
public class HostHandler extends HttpHandler {
    private final ApplicationRepository applicationRepository;

    @Inject
    public HostHandler(HttpHandler.Context ctx, ApplicationRepository applicationRepository) {
        super(ctx);
        this.applicationRepository = applicationRepository;
    }

    @Override
    public HttpResponse handleGET(HttpRequest request) {
        String hostname = getBindingMatch(request).group(2);
        ApplicationId applicationId = applicationRepository.getApplicationIdForHostname(hostname);
        return (applicationId == null)
                ? createError(hostname)
                : new HostResponse(Response.Status.OK, applicationId, applicationRepository.zone());
    }

    private HttpErrorResponse createError(String hostname) {
        return HttpErrorResponse.notFoundError("Could not find any application using host '" + hostname + "'");
    }

    private static BindingMatch<?> getBindingMatch(HttpRequest request) {
        return HttpConfigRequests.getBindingMatch(request, "http://*/application/v2/host/*");
    }

    private static class HostResponse extends JSONResponse {
        public HostResponse(int status, ApplicationId applicationId, Zone zone) {
            super(status);
            object.setString("tenant", applicationId.tenant().value());
            object.setString("application", applicationId.application().value());
            object.setString("environment", zone.environment().value());
            object.setString("region", zone.region().value());
            object.setString("instance", applicationId.instance().value());
        }
    }
}