All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kafka.entity.changelog.topic.TopicHttpService Maven / Gradle / Ivy

The newest version!
package kafka.entity.changelog.topic;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.util.RawValue;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.MediaType;
import com.linecorp.armeria.server.annotation.Get;
import com.linecorp.armeria.server.annotation.Param;
import java.util.List;
import kafka.entity.changelog.schema.Topic;

public class TopicHttpService {
  static final ObjectMapper JACKSON = new ObjectMapper();

  final TopicChangelog changelog;

  public TopicHttpService(TopicChangelog changelog) {
    this.changelog = changelog;
  }

  @Get("/topics")
  public AggregatedHttpResponse getTopics() {
    ArrayNode arrayNode = JACKSON.createArrayNode();
    List topics = changelog.listTopics();
    if (topics == null) return AggregatedHttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
    topics.forEach(arrayNode::add);
    return AggregatedHttpResponse.of(HttpStatus.OK, MediaType.JSON, arrayNode.toString());
  }

  @Get("/topics/:topic_name")
  public AggregatedHttpResponse getTopicEvents(@Param("topic_name") String topicName) {
    List events = changelog.topicEvents(topicName);
    if (events == null) return AggregatedHttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
    if (events.isEmpty()) return AggregatedHttpResponse.of(HttpStatus.NOT_FOUND);
    ArrayNode arrayNode = JACKSON.createArrayNode();
    events.forEach(event -> {
      try {
        arrayNode.addRawValue(new RawValue(JsonFormat.printer().print(event)));
      } catch (InvalidProtocolBufferException e) {
        e.printStackTrace();
      }
    });
    return AggregatedHttpResponse.of(HttpStatus.OK, MediaType.JSON, arrayNode.toString());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy