aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/controller/WellKnownApiHandler.java
blob: 63f600aaa5066e6142ae36bcbaf0d634cbb5a500 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.controller;

import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.ThreadedHttpRequestHandler;
import com.yahoo.restapi.ErrorResponse;
import com.yahoo.restapi.Path;
import com.yahoo.restapi.StringResponse;
import com.yahoo.vespa.hosted.controller.config.WellKnownFolderConfig;
import com.yahoo.yolean.Exceptions;


/**
 * Responsible for serving contents from the RFC 8615 well-known directory
 * @author olaa
 */
public class WellKnownApiHandler extends ThreadedHttpRequestHandler {

    private final String securityTxt;

    public WellKnownApiHandler(Context context, WellKnownFolderConfig wellKnownFolderConfig) {
        super(context);
        this.securityTxt = wellKnownFolderConfig.securityTxt();
    }

    @Override
    public HttpResponse handle(HttpRequest request) {
        return switch (request.getMethod()) {
            case GET -> get(request);
            default -> ErrorResponse.methodNotAllowed("Method '" + request.getMethod() + "' is not supported");
        };
    }

    private HttpResponse get(HttpRequest request) {
        try {
            Path path = new Path(request.getUri());
            if (path.matches("/.well-known/security.txt")) return securityTxt();
            return ErrorResponse.notFoundError("Nothing at " + path);
        }
        catch (IllegalArgumentException e) {
            return ErrorResponse.badRequest(Exceptions.toMessageString(e));
        }
    }

    private HttpResponse securityTxt() {
        return new StringResponse(securityTxt);
    }

}