|
| 1 | +package com.redhat.openshift.knative.showcase.events; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import io.cloudevents.CloudEvent; |
| 7 | +import io.cloudevents.core.builder.CloudEventBuilder; |
| 8 | +import io.cloudevents.http.restful.ws.StructuredEncoding; |
| 9 | +import io.cloudevents.jackson.JsonFormat; |
| 10 | +import io.quarkus.runtime.StartupEvent; |
| 11 | +import io.smallrye.mutiny.Multi; |
| 12 | +import org.eclipse.microprofile.openapi.annotations.Operation; |
| 13 | +import org.jboss.resteasy.reactive.RestStreamElementType; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import javax.enterprise.context.ApplicationScoped; |
| 18 | +import javax.enterprise.event.Observes; |
| 19 | +import javax.ws.rs.Consumes; |
| 20 | +import javax.ws.rs.GET; |
| 21 | +import javax.ws.rs.POST; |
| 22 | +import javax.ws.rs.Path; |
| 23 | +import javax.ws.rs.Produces; |
| 24 | +import javax.ws.rs.core.MediaType; |
| 25 | +import java.net.URI; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.UUID; |
| 29 | +import java.util.random.RandomGenerator; |
| 30 | + |
| 31 | +@Path("events") |
| 32 | +@ApplicationScoped |
| 33 | +class Endpoint { |
| 34 | + |
| 35 | + private static final Logger LOGGER = LoggerFactory.getLogger(Endpoint.class); |
| 36 | + private final List<CloudEvent> events = new ArrayList<>(); |
| 37 | + |
| 38 | + void init(@Observes StartupEvent ignored, ObjectMapper om) |
| 39 | + throws JsonProcessingException { |
| 40 | + var rg = RandomGenerator.getDefault(); |
| 41 | + for (int i = 0; i < 3; i++) { |
| 42 | + var s = Score.random(rg); |
| 43 | + events.add(CloudEventBuilder.v1() |
| 44 | + .withId(String.valueOf(i)) |
| 45 | + .withSource(URI.create("//localhost/dev")) |
| 46 | + .withType(Endpoint.class.getName()) |
| 47 | + .withData(MediaType.APPLICATION_JSON, om.writeValueAsBytes(s)) |
| 48 | + .build()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @GET |
| 53 | + @Operation(summary = "Retrieves all registered events as a JSON stream") |
| 54 | + @RestStreamElementType(MediaType.APPLICATION_JSON) |
| 55 | + @StructuredEncoding(JsonFormat.CONTENT_TYPE) |
| 56 | + public Multi<CloudEvent> events() { |
| 57 | + return Multi.createFrom().iterable(events); |
| 58 | + } |
| 59 | + |
| 60 | + @GET |
| 61 | + @Path("last") |
| 62 | + @Produces(MediaType.APPLICATION_JSON) |
| 63 | + @StructuredEncoding(JsonFormat.CONTENT_TYPE) |
| 64 | + public CloudEvent last() { |
| 65 | + return events.get(events.size() - 1); |
| 66 | + } |
| 67 | + |
| 68 | + @POST |
| 69 | + @Consumes(MediaType.APPLICATION_JSON) |
| 70 | + @Operation(summary = "Receives a CloudEvent and stores it") |
| 71 | + public void receive(CloudEvent event) { |
| 72 | + events.add(event); |
| 73 | + LOGGER.info("Received event: {}", event); |
| 74 | + } |
| 75 | + |
| 76 | + private static final class Score { |
| 77 | + @JsonProperty |
| 78 | + Play play; |
| 79 | + @JsonProperty |
| 80 | + int score; |
| 81 | + |
| 82 | + static Score random(RandomGenerator rg) { |
| 83 | + var s = new Score(); |
| 84 | + s.score = rg.nextInt(1_000); |
| 85 | + s.play = new Play(); |
| 86 | + s.play.id = new UUID(rg.nextLong(), rg.nextLong()).toString(); |
| 87 | + s.play.game = rg.nextInt(300); |
| 88 | + return s; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static class Play { |
| 93 | + @JsonProperty |
| 94 | + String id; |
| 95 | + @JsonProperty |
| 96 | + Integer game; |
| 97 | + } |
| 98 | +} |
0 commit comments