Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public List<DiscoveredAPI> discoverAPI() {
String cpId = link.getControlPlaneId();
String serviceId = link.getId();
svc = apiGatewayClient.getService(cpId, serviceId);
String apiType = KongAPIUtil.determineAPIType(svc);
if (svc != null && svc.getHost() != null && svc.getProtocol() != null &&
svc.getPort() != null) {
String endpoint = KongAPIUtil.buildEndpointUrl(
Expand All @@ -188,7 +189,16 @@ public List<DiscoveredAPI> discoverAPI() {
svc.getPort(),
svc.getPath()
);
api.setType(apiType);
api.setEndpointConfig(KongAPIUtil.buildEndpointConfigJson(endpoint, endpoint, false));
if (oas != null) {
if ("WS".equals(apiType)) {
api.setAsyncApiDefinition(oas);
api.setTransports("ws,wss");
} else if ("HTTP".equals(apiType)) {
api.setTransports("http,https");
}
}
}
}

Expand Down Expand Up @@ -288,13 +298,22 @@ public List<DiscoveredAPI> discoverAPI() {
api.setInitiatedFromGateway(true);
api.setGatewayVendor(KongConstants.DEFAULT_GATEWAY_VENDOR);
api.setGatewayType(environment.getGatewayType());
api.setType(KongAPIUtil.determineAPIType(svc));

String ApiType = KongAPIUtil.determineAPIType(svc);
String vhost = environment.getVhosts() != null && !environment.getVhosts().isEmpty() ?
environment.getVhosts().get(0).getHost() :
KongConstants.DEFAULT_VHOST;

String apiDefinition = KongAPIUtil.buildOasFromRoutes(svc, routes, vhost);
api.setSwaggerDefinition(apiDefinition);
if ("WS".equalsIgnoreCase(ApiType)) {
api.setTransports("ws,wss");
api.setAsyncApiDefinition(
KongAPIUtil.buildOasFromRoutesForAsync(svc, routes, vhost)
);
} else {
api.setTransports("http,https");
api.setSwaggerDefinition(
KongAPIUtil.buildOasFromRoutes(svc, routes, vhost));
}
String endpoint = KongAPIUtil.buildEndpointUrl(svc.getProtocol(), svc.getHost(), svc.getPort(),
svc.getPath());
api.setEndpointConfig(KongAPIUtil.buildEndpointConfigJson(endpoint, endpoint, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ public static CORSConfiguration kongCorsToWso2Cors(KongPlugin plugin) {
return cors;
}

public static String determineAPIType(KongService service) {
// 1. Check Service Protocol (The most common indicator)
if (service != null && service.getProtocol() != null) {
String proto = service.getProtocol().toLowerCase();
if ("ws".equals(proto) || "wss".equals(proto)) {
return "WS";
} else if ("http".equals(proto) || "https".equals(proto)) {
return "HTTP";
}
}
return null;
}

public static boolean getBoolean(JsonObject obj, String key, boolean def) {
if (obj == null || !obj.has(key) || obj.get(key).isJsonNull()) {
return def;
Expand Down Expand Up @@ -376,6 +389,74 @@ public static String buildOasFromRoutes(KongService svc, List<KongRoute> routes,
return root.toString();
}

public static String buildOasFromRoutesForAsync(KongService svc, List<KongRoute> routes, String vhost) {
JsonObject root = new JsonObject();
root.addProperty("asyncapi", "2.0.0");

JsonObject info = new JsonObject();
info.addProperty("title", svc.getName() != null ? svc.getName() : "kong-service");
info.addProperty("version", "v1");
root.add("info", info);

JsonObject servers = new JsonObject();
JsonObject production = new JsonObject();
production.addProperty("url", "wss://" + vhost);
production.addProperty("protocol", "wss");
servers.add("production", production);
root.add("servers", servers);

JsonObject channels = new JsonObject();
for (KongRoute r : routes) {
List<String> routePaths = (r.getPaths() != null && !r.getPaths().isEmpty()) ?
r.getPaths() : Collections.singletonList("/*");

for (String kongPath : routePaths) {
String oasPath = toOasPath(kongPath);
if (channels.has(oasPath)) {
continue;
}
JsonObject channelItem = new JsonObject();
JsonObject pub = new JsonObject();
pub.addProperty("operationId", safeOpId(r.getName(), "pub", oasPath));
channelItem.add("publish", pub);
JsonObject sub = new JsonObject();
sub.addProperty("operationId", safeOpId(r.getName(), "sub", oasPath));
channelItem.add("subscribe", sub);

// Parameters
Matcher m = Pattern.compile("\\{([A-Za-z_][A-Za-z0-9_-]*)\\}").matcher(oasPath);
JsonObject parameters = new JsonObject();
while (m.find()) {
String pName = m.group(1);
JsonObject pObj = new JsonObject();
JsonObject schema = new JsonObject();
schema.addProperty("type", "string");
pObj.add("schema", schema);
parameters.add(pName, pObj);
}
if (parameters.size() > 0) {
channelItem.add("parameters", parameters);
}
channels.add(oasPath, channelItem);
}
}

if (channels.entrySet().isEmpty()) {
JsonObject channelItem = new JsonObject();
JsonObject pub = new JsonObject();
pub.addProperty("operationId", "pub_default");
channelItem.add("publish", pub);
JsonObject sub = new JsonObject();
sub.addProperty("operationId", "sub_default");
channelItem.add("subscribe", sub);
channels.add("/*", channelItem);
}

root.add("channels", channels);
root.add("components", new JsonObject());
return root.toString();
}

/**
* Convert Kong route path value to an OAS path template. Handles:
* - plain prefixes like "/get" (returned as-is)
Expand Down