|
| 1 | +package info.unterrainer.websocketclient; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.concurrent.CompletableFuture; |
| 8 | +import java.util.function.Consumer; |
| 9 | + |
| 10 | +import org.glassfish.tyrus.client.ClientManager; |
| 11 | + |
| 12 | +import info.unterrainer.oauthtokenmanager.LocalOauthTokens; |
| 13 | +import info.unterrainer.oauthtokenmanager.OauthTokenManager; |
| 14 | +import jakarta.websocket.ClientEndpointConfig; |
| 15 | +import jakarta.websocket.ClientEndpointConfig.Builder; |
| 16 | +import jakarta.websocket.CloseReason; |
| 17 | +import jakarta.websocket.Session; |
| 18 | +import lombok.Data; |
| 19 | +import lombok.EqualsAndHashCode; |
| 20 | +import lombok.RequiredArgsConstructor; |
| 21 | +import lombok.experimental.SuperBuilder; |
| 22 | +import lombok.extern.slf4j.Slf4j; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +@Data |
| 26 | +@RequiredArgsConstructor |
| 27 | +@SuperBuilder(toBuilder = true) |
| 28 | +@EqualsAndHashCode() |
| 29 | +public class WebsocketConnection implements AutoCloseable { |
| 30 | + |
| 31 | + private final String host; |
| 32 | + final Consumer<Session> onOpenHandler; |
| 33 | + final Consumer<String> onMessageHandler; |
| 34 | + final Consumer<Session> onCloseHandler; |
| 35 | + final Consumer<Throwable> onErrorHandler; |
| 36 | + final String keycloakHost; |
| 37 | + final String keycloakClient; |
| 38 | + final String keycloakClientSecret; |
| 39 | + final String keycloakUser; |
| 40 | + final String keycloakPassword; |
| 41 | + |
| 42 | + private WebsocketEndpoints endpoints; |
| 43 | + |
| 44 | + final CompletableFuture<Session> sessionReady = new CompletableFuture<>(); |
| 45 | + |
| 46 | + public Session awaitOpen() { |
| 47 | + try { |
| 48 | + return sessionReady.get(); |
| 49 | + } catch (Exception e) { |
| 50 | + throw new IllegalStateException("WebSocket did not open in time.", e); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public void send(String message) { |
| 55 | + Session s = awaitOpen(); |
| 56 | + try { |
| 57 | + s.getBasicRemote().sendText(message); |
| 58 | + } catch (Exception e) { |
| 59 | + log.error("Error sending message: ", e); |
| 60 | + throw new SendingMessageWebsocketException(String.format("Failed to send message [%s].", message), e); |
| 61 | + } |
| 62 | + log.debug("Sent message: " + message); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void close() { |
| 67 | + Session s = awaitOpen(); |
| 68 | + try { |
| 69 | + s.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Normal closure")); |
| 70 | + } catch (IOException e) { |
| 71 | + log.error("Error closing session.", e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public void establish() { |
| 76 | + String accessToken = null; |
| 77 | + ClientManager container = ClientManager.createClient(); |
| 78 | + endpoints = new WebsocketEndpoints(this); |
| 79 | + Builder c = ClientEndpointConfig.Builder.create(); |
| 80 | + |
| 81 | + if (keycloakHost != null) { |
| 82 | + OauthTokenManager tokenManager = new OauthTokenManager(keycloakHost, keycloakClient); |
| 83 | + LocalOauthTokens tokens = tokenManager.getTokensFromCredentials(keycloakClient, keycloakUser, |
| 84 | + keycloakPassword); |
| 85 | + accessToken = tokens.getAccessToken(); |
| 86 | + String at = "Bearer " + accessToken; |
| 87 | + |
| 88 | + c.configurator(new ClientEndpointConfig.Configurator() { |
| 89 | + @Override |
| 90 | + public void beforeRequest(Map<String, List<String>> headers) { |
| 91 | + headers.put("Authorization", List.of(at)); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + ClientEndpointConfig config = c.build(); |
| 96 | + |
| 97 | + try { |
| 98 | + container.connectToServer(endpoints, config, URI.create(host)); |
| 99 | + } catch (Exception e) { |
| 100 | + log.error("Error connecting to WebSocket server: ", e); |
| 101 | + return; |
| 102 | + } |
| 103 | + log.info("WebSocket client connected to: {}", host); |
| 104 | + } |
| 105 | +} |
0 commit comments