Skip to content

WIP Flexible route matching usiing vert.x instead of JAX-RS #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
</parent>
<artifactId>kafka-hook</artifactId>
<dependencies>
<dependency><!-- library uses this for the cloudevent extension, but shouldn't annotate anything jaxrs -->
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
<version>${jaxrs.version}</version>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
23 changes: 23 additions & 0 deletions lib/src/main/java/se/yolean/kafka/hook/ProduceFailed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package se.yolean.kafka.hook;

import se.yolean.kafka.hook.types.v1.HookError;


/**
* Means we failed to fulfil the primary responsibility of kafka-hook.
*/
public class ProduceFailed extends Exception {

private static final long serialVersionUID = 1L;

private final HookError error;

public ProduceFailed(HookError error) {
this.error = error;
}

public HookError getError() {
return error;
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<!-- keep in sync with quarkus defaults -->
<jaxrs.version>2.0.1.Final</jaxrs.version>
<vertx.version>3.9.4</vertx.version>
<jackson.version>2.11.3</jackson.version>
<micrometer.version>1.5.6</micrometer.version>
<junit.version>5.7.0</junit.version>
Expand Down
4 changes: 2 additions & 2 deletions rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
<artifactId>quarkus-vertx-web</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
<artifactId>quarkus-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down

This file was deleted.

38 changes: 38 additions & 0 deletions rest/src/main/java/se/yolean/kafka/hook/rest/KafkaHookRoutes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package se.yolean.kafka.hook.rest;

import java.io.IOException;
import java.io.InputStream;

import javax.enterprise.context.ApplicationScoped;

import io.quarkus.vertx.web.Route;
import io.quarkus.vertx.web.Route.HandlerType;
import io.vertx.codegen.annotations.Nullable;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import se.yolean.kafka.hook.ProduceFailed;
import se.yolean.kafka.hook.types.v1.HookReceipt;

@ApplicationScoped
public class KafkaHookRoutes {

@Route(methods = HttpMethod.POST, regex = ".*/v1/?(.*)", type = Route.HandlerType.BLOCKING, produces = "application/json")
public HookReceipt produce(RoutingContext ctx) throws IOException {
String type = ctx.pathParam("param0");
MultiMap headers = ctx.request().headers();
@Nullable
Buffer payload = ctx.getBody();
return hook.produce(headers, uri, "", payload);
}

@Route(type = HandlerType.FAILURE, produces = "application/json")
void unsupported(ProduceFailed e, HttpServerResponse response) {
response.setStatusCode(500).end(e.getError());
}

// TODO pixy drop-in, stuff like: "/{topic: ^[^/]+$}/messages"

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.io.UnsupportedEncodingException;
import java.time.Duration;
Expand Down Expand Up @@ -160,6 +161,44 @@ public void testProduceString() throws UnsupportedEncodingException {
assertEquals("github.com/Yolean/kafka-hook/mytype", headers(record2).get("ce_type"));
}

@Test
public void testProduceAlternativeUrls() throws UnsupportedEncodingException {
given()
.contentType(ContentType.TEXT)
.accept(ContentType.JSON)
.body("test1".getBytes())
.when().post("/hook/v1/mytype/with/slashes")
.then()
.body(containsString("\"offset\":" + (startOffset)))
.statusCode(200);
given()
.contentType(ContentType.TEXT)
.accept(ContentType.JSON)
.body("test2".getBytes())
.when().post("/some-prefix/v1/hook")
.then()
.body(containsString("\"offset\":" + (startOffset + 1)))
.statusCode(200);
given()
.contentType(ContentType.TEXT)
.accept(ContentType.JSON)
.body("test3".getBytes())
.when().post("/some-prefix/v1/hook/sub/type/")
.then()
.body(containsString("\"offset\":" + (startOffset + 2)))
.statusCode(200);
waitBetweenPolls();
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
Iterator<ConsumerRecord<String, String>> it = records.iterator();
ConsumerRecord<String, String> record1 = it.next();
assertEquals("github.com/Yolean/kafka-hook/mytype/with/slashes", headers(record1).get("ce_type"));
ConsumerRecord<String, String> record2 = it.next();
assertEquals("github.com/Yolean/kafka-hook/", headers(record2).get("ce_type"));
ConsumerRecord<String, String> record3 = it.next();
assertNotEquals("github.com/Yolean/kafka-hook/sub/type/", headers(record3).get("ce_type"));
assertEquals("github.com/Yolean/kafka-hook/sub/type", headers(record3).get("ce_type"));
}

@Test
public void testCloudeventsDistributedTracingExtensionWithEnvoyHeaders() {
given()
Expand Down