Skip to content

Commit

Permalink
Refactored OpenAPI v2 endpoint handling (#972)
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Patierno <[email protected]>
  • Loading branch information
ppatierno authored Feb 21, 2025
1 parent c48dff0 commit ba48354
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions src/main/java/io/strimzi/kafka/bridge/http/HttpBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,35 +576,41 @@ private void ready(RoutingContext routingContext) {
}

private void openapi(RoutingContext routingContext) {
FileSystem fileSystem = vertx.fileSystem();
fileSystem.readFile("openapi.json")
.onComplete(readFile -> {
if (readFile.succeeded()) {
String xForwardedPath = routingContext.request().getHeader("x-forwarded-path");
String xForwardedPrefix = routingContext.request().getHeader("x-forwarded-prefix");
if (xForwardedPath == null && xForwardedPrefix == null) {
HttpUtils.sendFile(routingContext, HttpResponseStatus.OK.code(), BridgeContentType.JSON, "openapi.json");
} else {
String path = "/";
if (xForwardedPrefix != null) {
path = xForwardedPrefix;
}
if (xForwardedPath != null) {
path = xForwardedPath;
if (routingContext.request().path().contains("/openapi/v2")) {
HttpBridgeError error = new HttpBridgeError(HttpResponseStatus.GONE.code(), "OpenAPI v2 Swagger not supported");
HttpUtils.sendResponse(routingContext, HttpResponseStatus.GONE.code(),
BridgeContentType.KAFKA_JSON, JsonUtils.jsonToBytes(error.toJson()));
} else {
FileSystem fileSystem = vertx.fileSystem();
fileSystem.readFile("openapi.json")
.onComplete(readFile -> {
if (readFile.succeeded()) {
String xForwardedPath = routingContext.request().getHeader("x-forwarded-path");
String xForwardedPrefix = routingContext.request().getHeader("x-forwarded-prefix");
if (xForwardedPath == null && xForwardedPrefix == null) {
HttpUtils.sendFile(routingContext, HttpResponseStatus.OK.code(), BridgeContentType.JSON, "openapi.json");
} else {
String path = "/";
if (xForwardedPrefix != null) {
path = xForwardedPrefix;
}
if (xForwardedPath != null) {
path = xForwardedPath;
}
ObjectNode json = (ObjectNode) JsonUtils.bytesToJson(readFile.result().getBytes());
json.put("basePath", path);
HttpUtils.sendResponse(routingContext, HttpResponseStatus.OK.code(), BridgeContentType.JSON, JsonUtils.jsonToBytes(json));
}
ObjectNode json = (ObjectNode) JsonUtils.bytesToJson(readFile.result().getBytes());
json.put("basePath", path);
HttpUtils.sendResponse(routingContext, HttpResponseStatus.OK.code(), BridgeContentType.JSON, JsonUtils.jsonToBytes(json));
} else {
LOGGER.error("Failed to read OpenAPI JSON file", readFile.cause());
HttpBridgeError error = new HttpBridgeError(
HttpResponseStatus.INTERNAL_SERVER_ERROR.code(),
readFile.cause().getMessage());
HttpUtils.sendResponse(routingContext, HttpResponseStatus.INTERNAL_SERVER_ERROR.code(),
BridgeContentType.JSON, JsonUtils.jsonToBytes(error.toJson()));
}
} else {
LOGGER.error("Failed to read OpenAPI JSON file", readFile.cause());
HttpBridgeError error = new HttpBridgeError(
HttpResponseStatus.INTERNAL_SERVER_ERROR.code(),
readFile.cause().getMessage());
HttpUtils.sendResponse(routingContext, HttpResponseStatus.INTERNAL_SERVER_ERROR.code(),
BridgeContentType.JSON, JsonUtils.jsonToBytes(error.toJson()));
}
});
});
}
}

private void metrics(RoutingContext routingContext) {
Expand Down Expand Up @@ -878,13 +884,11 @@ public void process(RoutingContext routingContext) {
}
};

final static HttpOpenApiOperation OPENAPIV2 = new HttpOpenApiOperation(HttpOpenApiOperations.OPENAPIV2) {
final HttpOpenApiOperation OPENAPIV2 = new HttpOpenApiOperation(HttpOpenApiOperations.OPENAPIV2) {

@Override
public void process(RoutingContext routingContext) {
HttpBridgeError error = new HttpBridgeError(HttpResponseStatus.GONE.code(), "OpenAPI v2 Swagger not supported");
HttpUtils.sendResponse(routingContext, HttpResponseStatus.GONE.code(),
BridgeContentType.KAFKA_JSON, JsonUtils.jsonToBytes(error.toJson()));
openapi(routingContext);
}
};

Expand Down

0 comments on commit ba48354

Please sign in to comment.