/
AbsSessionHandler.java
44 lines (36 loc) · 1.29 KB
/
AbsSessionHandler.java
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
package xyz.gianlu.librespot.api.handlers;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import io.undertow.util.StatusCodes;
import org.jetbrains.annotations.NotNull;
import xyz.gianlu.librespot.api.SessionWrapper;
import xyz.gianlu.librespot.core.Session;
/**
* @author Gianlu
*/
public abstract class AbsSessionHandler implements HttpHandler {
private final SessionWrapper wrapper;
public AbsSessionHandler(@NotNull SessionWrapper wrapper) {
this.wrapper = wrapper;
}
@Override
public final void handleRequest(HttpServerExchange exchange) throws Exception {
Session s = wrapper.get();
if (s == null) {
exchange.setStatusCode(StatusCodes.NO_CONTENT);
return;
}
if (s.reconnecting()) {
exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
exchange.getResponseHeaders().add(Headers.RETRY_AFTER, 10);
return;
}
if (!s.valid()) {
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
return;
}
handleRequest(exchange, s);
}
protected abstract void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Session session) throws Exception;
}