aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/http/SessionHandler.java
blob: d63dc714b9defc19a9225e7ba581ce0ee6cd792f (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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;

import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.jdisc.application.BindingMatch;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.TimeoutBudget;

import java.time.Clock;
import java.time.Duration;

/**
 * Super class for session handlers, that takes care of checking valid
 * methods for a request. Session handlers should subclass this method and
 * implement the handleMETHOD methods that it supports.
 *
 * @author hmusum
 */
public class SessionHandler extends HttpHandler {

    protected final ApplicationRepository applicationRepository;

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

    /**
     * Set to make sure that timeout for the handler is higher than any timeouts used inside the handler (e.g. zookeeper barrier timeout)
     * Setting this too low will lead to a response with status code 504 and empty response body.
     */
    @Override
    public Duration getTimeout() {
        return Duration.ofSeconds(applicationRepository.configserverConfig().zookeeper().barrierTimeout()).plus(Duration.ofSeconds(30));
    }

    /**
     * Gets the raw session id from request (v2). Input request must have a valid path.
     *
     * @param request a request
     * @return a session id
     */
    public static String getRawSessionIdV2(HttpRequest request) {
        final String path = request.getUri().toString();
        BindingMatch<?> bm = Utils.getBindingMatch(request, "http://*/application/v2/tenant/*/session/*/*");
        if (bm.groupCount() < 4) {
            // This would mean the subtype of this doesn't have the correct binding
            throw new IllegalArgumentException("Can not get session id from request '" + path + "'");
        }
        return bm.group(3);
    }

    /**
     * Gets session id (as a number) from request (v2). Input request must have a valid path.
     *
     * @param request a request
     * @return a session id
     */
    public static Long getSessionIdV2(HttpRequest request) {
        try {
            return Long.parseLong(getRawSessionIdV2(request));
        } catch (NumberFormatException e) {
            throw createSessionException(request);
        }
    }

    private static BadRequestException createSessionException(HttpRequest request) {
        return new BadRequestException("Session id in request is not a number, request was '" +
                request.getUri().toString() + "'");
    }

    public static TimeoutBudget getTimeoutBudget(HttpRequest request, Duration defaultTimeout) {
        return getTimeoutBudget(getRequestTimeout(request, defaultTimeout));
    }

    public static TimeoutBudget getTimeoutBudget(Duration requestTimeout) {
        return new TimeoutBudget(Clock.systemUTC(), requestTimeout);
    }

    /**
     * True if this request should ignore activation failure because the session was made from an active session that is not active now
     * @param request a {@link com.yahoo.container.jdisc.HttpRequest}
     * @return true if ignore failure
     */
    protected static boolean shouldIgnoreSessionStaleFailure(HttpRequest request) {
        return request.getBooleanProperty("force");
    }

}