Skip to content

Commit

Permalink
Kafka ui RPC creates own ObjectMapper (deserialization)
Browse files Browse the repository at this point in the history
Fixes quarkusio#29976

(cherry picked from commit b2ac6b2)
  • Loading branch information
ozangunalp authored and gsmet committed Jan 4, 2023
1 parent b6eeaa0 commit 81edd04
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public void handlePost(RoutingContext event) {
endResponse(event, BAD_REQUEST, "Request body is null");
return;
}
var body = event.body().asJsonObject();
var webUtils = kafkaWebUiUtils();
var body = webUtils.fromJson(event.body().buffer());
if (body == null) {
endResponse(event, BAD_REQUEST, "Request JSON body is null");
return;
Expand All @@ -34,7 +35,6 @@ public void handlePost(RoutingContext event) {
var message = "OK";
var error = "";

var webUtils = kafkaWebUiUtils();
var adminClient = kafkaAdminClient();

boolean res = false;
Expand All @@ -50,7 +50,7 @@ public void handlePost(RoutingContext event) {
res = true;
break;
case "createTopic":
var topicCreateRq = event.body().asPojo(KafkaCreateTopicRequest.class);
var topicCreateRq = webUtils.fromJson(event.body().buffer(), KafkaCreateTopicRequest.class);
res = adminClient.createTopic(topicCreateRq);
message = webUtils.toJson(webUtils.getTopics());
break;
Expand All @@ -64,17 +64,17 @@ public void handlePost(RoutingContext event) {
res = true;
break;
case "topicMessages":
var msgRequest = event.body().asPojo(KafkaMessagesRequest.class);
var msgRequest = webUtils.fromJson(event.body().buffer(), KafkaMessagesRequest.class);
message = webUtils.toJson(webUtils.getMessages(msgRequest));
res = true;
break;
case "getOffset":
var request = event.body().asPojo(KafkaOffsetRequest.class);
var request = webUtils.fromJson(event.body().buffer(), KafkaOffsetRequest.class);
message = webUtils.toJson(webUtils.getOffset(request));
res = true;
break;
case "createMessage":
var rq = event.body().asPojo(KafkaMessageCreateRequest.class);
var rq = webUtils.fromJson(event.body().buffer(), KafkaMessageCreateRequest.class);
webUtils.createMessage(rq);
message = "{}";
res = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static io.quarkus.kafka.client.runtime.ui.util.ConsumerFactory.createConsumer;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -21,19 +23,23 @@
import org.apache.kafka.common.Node;
import org.apache.kafka.common.TopicPartition;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

import io.netty.buffer.ByteBufInputStream;
import io.quarkus.kafka.client.runtime.KafkaAdminClient;
import io.quarkus.kafka.client.runtime.ui.model.Order;
import io.quarkus.kafka.client.runtime.ui.model.request.KafkaMessageCreateRequest;
import io.quarkus.kafka.client.runtime.ui.model.request.KafkaMessagesRequest;
import io.quarkus.kafka.client.runtime.ui.model.request.KafkaOffsetRequest;
import io.quarkus.kafka.client.runtime.ui.model.response.*;
import io.smallrye.common.annotation.Identifier;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;

@Singleton
public class KafkaUiUtils {
Expand Down Expand Up @@ -229,9 +235,22 @@ public String toJson(Object o) {
try {
res = objectMapper.writeValueAsString(o);
} catch (JsonProcessingException ex) {
//FIXME:
res = "";
}
return res;
}

public JsonObject fromJson(Buffer buffer) {
return new JsonObject(fromJson(buffer, Map.class));
}

public <T> T fromJson(Buffer buffer, Class<T> type) {
try {
JsonParser parser = objectMapper.createParser((InputStream) new ByteBufInputStream(buffer.getByteBuf()));
return objectMapper.readValue(parser, type);
} catch (IOException e) {
return null;
}
}

}

0 comments on commit 81edd04

Please sign in to comment.