Skip to content

Commit

Permalink
Merge pull request quarkusio#39484 from mkouba/issue-39464
Browse files Browse the repository at this point in the history
WebSockets Next: add basic Dev UI
  • Loading branch information
mkouba authored Mar 20, 2024
2 parents ef0d181 + 30d21d8 commit 8d109e0
Show file tree
Hide file tree
Showing 10 changed files with 871 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import io.quarkus.builder.item.MultiBuildItem;

/**
* A generated {@link io.quarkus.websockets.next.runtime.WebSocketEndpoint}.
* A generated representation of a {@link io.quarkus.websockets.next.runtime.WebSocketEndpoint}.
*/
final class GeneratedEndpointBuildItem extends MultiBuildItem {
public final class GeneratedEndpointBuildItem extends MultiBuildItem {

final String className;
final String path;
public final String endpointClassName;
public final String generatedClassName;
public final String path;

GeneratedEndpointBuildItem(String className, String path) {
this.className = className;
GeneratedEndpointBuildItem(String endpointClassName, String generatedClassName, String path) {
this.endpointClassName = endpointClassName;
this.generatedClassName = generatedClassName;
this.path = path;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ public String apply(String name) {
// and delegates callback invocations to the endpoint bean
String generatedName = generateEndpoint(endpoint, classOutput);
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(generatedName).constructors().build());
generatedEndpoints.produce(new GeneratedEndpointBuildItem(generatedName, endpoint.path));
generatedEndpoints.produce(new GeneratedEndpointBuildItem(endpoint.bean.getImplClazz().name().toString(),
generatedName, endpoint.path));
}
}

Expand All @@ -190,7 +191,7 @@ public void registerRoutes(WebSocketServerRecorder recorder, HttpRootPathBuildIt
RouteBuildItem.Builder builder = RouteBuildItem.builder()
.route(httpRootPath.relativePath(endpoint.path))
.handlerType(HandlerType.NORMAL)
.handler(recorder.createEndpointHandler(endpoint.className));
.handler(recorder.createEndpointHandler(endpoint.generatedClassName));
routes.produce(builder.build());
}
}
Expand Down Expand Up @@ -252,7 +253,7 @@ static String getPath(String path) {
m.appendReplacement(sb, ":" + match.subSequence(1, match.length() - 1));
}
m.appendTail(sb);
return sb.toString();
return path.startsWith("/") ? sb.toString() : "/" + sb.toString();
}

private String callbackToString(MethodInfo callback) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.quarkus.websockets.next.deployment.devui;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.devui.spi.JsonRPCProvidersBuildItem;
import io.quarkus.devui.spi.page.CardPageBuildItem;
import io.quarkus.devui.spi.page.Page;
import io.quarkus.websockets.next.deployment.GeneratedEndpointBuildItem;
import io.quarkus.websockets.next.deployment.WebSocketEndpointBuildItem;
import io.quarkus.websockets.next.runtime.devui.WebSocketNextJsonRPCService;

public class WebSocketServerDevUIProcessor {

@BuildStep(onlyIf = IsDevelopment.class)
public void pages(List<WebSocketEndpointBuildItem> endpoints, List<GeneratedEndpointBuildItem> generatedEndpoints,
BuildProducer<CardPageBuildItem> cardPages) {

CardPageBuildItem pageBuildItem = new CardPageBuildItem();

pageBuildItem.addBuildTimeData("endpoints", createEndpointsJson(endpoints, generatedEndpoints));

pageBuildItem.addPage(Page.webComponentPageBuilder()
.title("Endpoints")
.icon("font-awesome-solid:plug")
.componentLink("qwc-wsn-endpoints.js")
.staticLabel(String.valueOf(endpoints.size())));

cardPages.produce(pageBuildItem);
}

@BuildStep(onlyIf = IsDevelopment.class)
JsonRPCProvidersBuildItem rpcProvider() {
return new JsonRPCProvidersBuildItem(WebSocketNextJsonRPCService.class);
}

private List<Map<String, Object>> createEndpointsJson(List<WebSocketEndpointBuildItem> endpoints,
List<GeneratedEndpointBuildItem> generatedEndpoints) {
List<Map<String, Object>> json = new ArrayList<>();
for (WebSocketEndpointBuildItem endpoint : endpoints.stream().sorted(Comparator.comparing(e -> e.path))
.collect(Collectors.toList())) {
Map<String, Object> endpointJson = new HashMap<>();
String clazz = endpoint.bean.getImplClazz().name().toString();
endpointJson.put("clazz", clazz);
endpointJson.put("generatedClazz",
generatedEndpoints.stream().filter(ge -> ge.endpointClassName.equals(clazz)).findFirst()
.orElseThrow().generatedClassName);
endpointJson.put("path", getOriginalPath(endpoint.path));
endpointJson.put("executionMode", endpoint.executionMode.toString());
List<Map<String, Object>> callbacks = new ArrayList<>();
addCallback(endpoint.onOpen, callbacks);
addCallback(endpoint.onBinaryMessage, callbacks);
addCallback(endpoint.onTextMessage, callbacks);
addCallback(endpoint.onPongMessage, callbacks);
addCallback(endpoint.onClose, callbacks);
endpointJson.put("callbacks", callbacks);
json.add(endpointJson);
}
return json;
}

private void addCallback(WebSocketEndpointBuildItem.Callback callback, List<Map<String, Object>> callbacks) {
if (callback != null) {
callbacks.add(Map.of("annotation", callback.annotation.toString(), "method", callback.method.toString()));
}
}

private static final Pattern PATH_PARAM_PATTERN = Pattern.compile(":[a-zA-Z0-9_]+");

static String getOriginalPath(String path) {
StringBuilder sb = new StringBuilder();
Matcher m = PATH_PARAM_PATTERN.matcher(path);
while (m.find()) {
// Replace :foo with {foo}
String match = m.group();
m.appendReplacement(sb, "{" + match.subSequence(1, match.length()) + "}");
}
m.appendTail(sb);
return sb.toString();
}
}
Loading

0 comments on commit 8d109e0

Please sign in to comment.