Skip to content

Commit

Permalink
PrettyPrint event for Quarkus
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed May 12, 2023
1 parent d65bcb0 commit 772bd73
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 13 deletions.
6 changes: 3 additions & 3 deletions expressjs/src/routes/events/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const { HTTP } = require('cloudevents')
const openapi = require('../../lib/openapi')
const EventStore = require('./store')
const devdata = require('./devdata')
const Printer = require('./printer')
const PrettyPrinter = require('./printer')
const { log } = require('../../lib/logging')

const store = new EventStore()
const printer = new Printer()
const printer = new PrettyPrinter()

/**
* @typedef {import('express').Express} Express
Expand Down Expand Up @@ -71,7 +71,7 @@ function recv(req, res) {
*/
function recvEvent(ce) {
const out = printer.print(ce)
log.info('Received:\n%s', out)
log.info('Received event:\n%s', out)
ce.validate()
store.add(ce)
}
Expand Down
8 changes: 4 additions & 4 deletions expressjs/src/routes/events/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @typedef {import('cloudevents').CloudEvent} CloudEvent
*/

class Printer {
class PrettyPrinter {

/**
* Prints a CloudEvent in human-readable format.
Expand All @@ -19,7 +19,7 @@ class Printer {
}
let buf = '☁️ cloudevents.Event\n'
buf += `Validation: ${valid}\n`
const attr = attribites(ce)
const attr = attributes(ce)
buf += printAttr(attr)
buf += printExts(ce, attr)
buf += printData(ce)
Expand All @@ -33,7 +33,7 @@ class Printer {
* @returns {Object} - the CloudEvent attributes
* @private
*/
function attribites(ce) {
function attributes(ce) {
const attr = {
specversion: ce.specversion,
type: ce.type,
Expand Down Expand Up @@ -149,4 +149,4 @@ function indent(str, numOfIndents, spacesPerIndent) {
: txt
}

module.exports = Printer
module.exports = PrettyPrinter
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.redhat.openshift.knative.showcase.events;

import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cloudevents.CloudEvent;
import io.cloudevents.jackson.JsonFormat;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.io.IOException;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@ApplicationScoped
class Presenter {

private static final int INDENT_SIZE = 2;
private final ObjectMapper mapper;

@Inject
Presenter(ObjectMapper mapper) {
this.mapper = mapper;
}

byte[] asJson(CloudEvent ce) {
var serializer = new JsonFormat();
return serializer.serialize(ce);
}

CharSequence asHumanReadable(CloudEvent ce) {
try {
var buf = new StringBuilder("☁️ cloudevents.Event\n");
buf.append("Validation: valid\n");
buf.append(printAttr(attributes(ce)));
buf.append(printExts(ce));
buf.append(printData(ce));
return buf;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private Map<String, Serializable> attributes(CloudEvent ce) {
return ce.getAttributeNames()
.stream()
.map(name -> Map.entry(name, (Serializable) Objects.requireNonNull(ce.getAttribute(name))))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private CharSequence printAttr(Map<String, Serializable> attr) {
var buf = new StringBuilder();
if (!attr.isEmpty()) {
buf.append("Context Attributes,\n");
attr.forEach((key, value) ->
buf.append(String.format(" %s: %s%n", key, value)));
}
return buf;
}

private CharSequence printExts(CloudEvent ce) {
var buf = new StringBuilder();
var exts = extensions(ce);
if (!exts.isEmpty()) {
buf.append("Extensions,\n");
exts.forEach((key, value) ->
buf.append(String.format(" %s: %s%n", key, value)));
}
return buf;
}

private Map<String, Serializable> extensions(CloudEvent ce) {
return ce.getExtensionNames()
.stream()
.map(name -> Map.entry(name, (Serializable) Objects.requireNonNull(ce.getExtension(name))))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private CharSequence printData(CloudEvent ce) throws IOException {
var buf = new StringBuilder();
var data = ce.getData();
if (data != null) {
buf.append("Data,\n");
var contentType = ce.getDataContentType();
assert contentType != null;

if ("application/json".equals(contentType)) {
var json = mapper.readValue(data.toBytes(), Object.class);
var writer = mapper.writer(new DefaultPrettyPrinter());
var repr = writer.writeValueAsString(json)
.indent(INDENT_SIZE)
.replace("\" : ", "\": ");
buf.append(repr).append("\n");
return buf;
}
var repr = data.toString();
var types = List.of("text", "xml", "html", "csv", "json", "yaml");
if (types.stream().anyMatch(contentType::contains)) {
repr = new String(data.toBytes(), StandardCharsets.UTF_8);
}
buf.append(repr.indent(INDENT_SIZE)).append("\n");
return buf;
}
return buf;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.redhat.openshift.knative.showcase.events;

import io.cloudevents.CloudEvent;
import io.cloudevents.jackson.JsonFormat;
import io.smallrye.mutiny.Multi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Path;

@Path("")
Expand All @@ -15,17 +15,24 @@ class Rest implements Endpoint {

private static final Logger LOGGER = LoggerFactory.getLogger(Rest.class);
private final EventStore events = new EventStore();
private final Presenter presenter;

@Inject
Rest(Presenter presenter) {
this.presenter = presenter;
}

@Override
public Multi<byte[]> events() {
return events.stream()
.map(Rest::workaroundQuarkus31587);
.map(this::workaroundQuarkus31587);
}

@Override
public void receive(CloudEvent event) {
var he = presenter.asHumanReadable(event);
LOGGER.debug("Received event:\n{}", he);
events.add(event);
LOGGER.debug("Received event: {}", event);
}

@Override
Expand All @@ -40,8 +47,7 @@ public void receiveOnIndex(CloudEvent event) {
*
* TODO: Remove this method once the above issues is fixed.
*/
private static byte[] workaroundQuarkus31587(CloudEvent event) {
var serializer = new JsonFormat();
return serializer.serialize(event);
private byte[] workaroundQuarkus31587(CloudEvent event) {
return presenter.asJson(event);
}
}

0 comments on commit 772bd73

Please sign in to comment.