|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Dynamia Soluciones IT S.A.S - NIT 900302344-1 |
| 3 | + * Colombia / South America |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package tools.dynamia.zk.websocket; |
| 19 | + |
| 20 | +import org.springframework.context.annotation.Bean; |
| 21 | +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; |
| 22 | +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; |
| 23 | +import org.springframework.web.socket.server.support.DefaultHandshakeHandler; |
| 24 | +import tools.dynamia.commons.logger.LoggingService; |
| 25 | +import tools.dynamia.commons.logger.SLF4JLoggingService; |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * Spring WebSocket configurer for ZK global commands. |
| 30 | + * |
| 31 | + * <p>This class implements {@link WebSocketConfigurer} and registers a WebSocket handler |
| 32 | + * for ZK commands at a configurable endpoint. It exposes two beans: |
| 33 | + * <ul> |
| 34 | + * <li>{@link WebSocketGlobalCommandHandler}</li> |
| 35 | + * <li>{@link DefaultHandshakeHandler}</li> |
| 36 | + * </ul> |
| 37 | + * |
| 38 | + * Usage: |
| 39 | + * <ul> |
| 40 | + * <li>Extend this class in your own {@code @Configuration} to customize endpoint, |
| 41 | + * allowed origins or handshake behavior.</li> |
| 42 | + * <li>Or import it from another {@code @Configuration} class using |
| 43 | + * {@code @Import(ZKWebSocketConfigurer.class)} to reuse the default configuration.</li> |
| 44 | + * </ul> |
| 45 | + * |
| 46 | + * Important: When using this class as a Spring configuration (either by extending it |
| 47 | + * or importing it), the hosting configuration must enable WebSocket support by being |
| 48 | + * annotated with {@code @EnableWebSocket}. Without {@code @EnableWebSocket} Spring |
| 49 | + * will not process WebSocket configuration. Examples: |
| 50 | + * |
| 51 | + * <pre>{@code |
| 52 | + * @Configuration |
| 53 | + * @EnableWebSocket |
| 54 | + * public class MyWsConfig extends ZKWebSocketConfigurer { |
| 55 | + * public MyWsConfig() { |
| 56 | + * setEndpoint("/my-ws"); |
| 57 | + * setAllowedOrigins(new String[]{"https://example.com"}); |
| 58 | + * } |
| 59 | + * } |
| 60 | + * |
| 61 | + * @Configuration |
| 62 | + * @EnableWebSocket |
| 63 | + * @Import(ZKWebSocketConfigurer.class) |
| 64 | + * public class AppConfig { } |
| 65 | + * }</pre> |
| 66 | + * |
| 67 | + * This class is intentionally lightweight and provides sensible defaults. Override |
| 68 | + * or extend behaviour when a project needs different endpoint paths, CORS rules |
| 69 | + * or a custom handshake handler. |
| 70 | + */ |
| 71 | +public class ZKWebSocketConfigurer implements WebSocketConfigurer { |
| 72 | + |
| 73 | + private String endpoint = "/ws-commands"; |
| 74 | + private String[] allowedOrigins = new String[]{"*"}; |
| 75 | + private String[] allowedOriginPatterns = null; |
| 76 | + |
| 77 | + private final LoggingService logger = new SLF4JLoggingService(ZKWebSocketConfigurer.class); |
| 78 | + |
| 79 | + /** |
| 80 | + * Creates a new ZKWebSocketConfigurer with default configuration values. |
| 81 | + * A startup message is logged when an instance is created. |
| 82 | + */ |
| 83 | + public ZKWebSocketConfigurer() { |
| 84 | + logger.info("Starting " + getClass()); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Register the WebSocket handlers into the provided {@link WebSocketHandlerRegistry}. |
| 89 | + * This method configures the handler endpoint, allowed origins / origin patterns |
| 90 | + * and the handshake handler. |
| 91 | + * |
| 92 | + * @param registry the WebSocketHandlerRegistry used to register handlers |
| 93 | + */ |
| 94 | + @Override |
| 95 | + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { |
| 96 | + logger.info("Registering WS Handler for ZK Commands"); |
| 97 | + var reg = registry.addHandler(globalCommandHandler(), endpoint != null ? endpoint : "/ws-commands"); |
| 98 | + if (allowedOrigins != null) { |
| 99 | + reg.setAllowedOrigins(allowedOrigins); |
| 100 | + } |
| 101 | + if (allowedOriginPatterns != null) { |
| 102 | + reg.setAllowedOriginPatterns(allowedOriginPatterns); |
| 103 | + } |
| 104 | + reg.setHandshakeHandler(handshakeHandler()); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Exposes the {@link WebSocketGlobalCommandHandler} as a Spring bean. |
| 109 | + * Override this method if you need to provide a custom handler implementation. |
| 110 | + * |
| 111 | + * @return a new instance of {@link WebSocketGlobalCommandHandler} |
| 112 | + */ |
| 113 | + @Bean |
| 114 | + public WebSocketGlobalCommandHandler globalCommandHandler() { |
| 115 | + return new WebSocketGlobalCommandHandler(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Exposes the {@link DefaultHandshakeHandler} as a Spring bean. |
| 120 | + * Override this method to provide a custom handshake handler when needed. |
| 121 | + * |
| 122 | + * @return a new instance of {@link DefaultHandshakeHandler} |
| 123 | + */ |
| 124 | + @Bean |
| 125 | + public DefaultHandshakeHandler handshakeHandler() { |
| 126 | + return new DefaultHandshakeHandler(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Returns the configured WebSocket endpoint path. Default is {@code "/ws-commands"}. |
| 131 | + * |
| 132 | + * @return the endpoint path where the WebSocket handler is registered |
| 133 | + */ |
| 134 | + public String getEndpoint() { |
| 135 | + return endpoint; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Sets the WebSocket endpoint path where the handler will be registered. |
| 140 | + * |
| 141 | + * @param endpoint the endpoint path to set (e.g. {@code "/ws-commands"}) |
| 142 | + */ |
| 143 | + public void setEndpoint(String endpoint) { |
| 144 | + this.endpoint = endpoint; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns the allowed origins used for CORS. By default this is {@code new String[]{"*"}}. |
| 149 | + * |
| 150 | + * @return the allowed origins array or {@code null} if not set |
| 151 | + */ |
| 152 | + public String[] getAllowedOrigins() { |
| 153 | + return allowedOrigins; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Sets the allowed origins used for CORS. |
| 158 | + * |
| 159 | + * @param allowedOrigins an array of allowed origin strings |
| 160 | + */ |
| 161 | + public void setAllowedOrigins(String... allowedOrigins) { |
| 162 | + this.allowedOrigins = allowedOrigins; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Returns the allowed origin patterns used for CORS. If set, these patterns |
| 167 | + * will be applied instead of {@link #allowedOrigins}. |
| 168 | + * |
| 169 | + * @return the allowed origin patterns array or {@code null} if not set |
| 170 | + */ |
| 171 | + public String[] getAllowedOriginPatterns() { |
| 172 | + return allowedOriginPatterns; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Sets the allowed origin patterns used for CORS. |
| 177 | + * |
| 178 | + * @param allowedOriginPatterns an array of allowed origin pattern strings |
| 179 | + */ |
| 180 | + public void setAllowedOriginPatterns(String... allowedOriginPatterns) { |
| 181 | + this.allowedOriginPatterns = allowedOriginPatterns; |
| 182 | + } |
| 183 | +} |
0 commit comments