Skip to content

Commit

Permalink
Merge pull request #67 from conduktor/framiere/PXY-860
Browse files Browse the repository at this point in the history
PXY-860 Add metadata to Plugins
  • Loading branch information
framiere authored Jul 12, 2023
2 parents af0c639 + 84033e0 commit 7f1fe4b
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 5 deletions.
18 changes: 16 additions & 2 deletions interceptor-framework/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>conduktor-gateway</artifactId>
<groupId>io.conduktor</groupId>
Expand All @@ -13,5 +14,18 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.conduktor.gateway.interceptor;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MarkdownHeadersUtil {
private static final Pattern pattern = Pattern.compile("\\s*([^\\s]+):\\s*(.*+)", Pattern.MULTILINE);
public static final String HEADER_SEPARATOR = "---";

public Map<String, String> extractHeaders(String markdown) {
String headers = markdownHeaders(markdown);
Matcher matcher = pattern.matcher(headers);
Map<String, String> ret = new HashMap<>();
while (matcher.find()) {
ret.put(matcher.group(1), matcher.group(2));
}
return ret;
}

@NotNull
private String markdownHeaders(String v) {
int first = v.indexOf(HEADER_SEPARATOR);
if (first == -1) {
return "";
}
String newStr = v.substring(first + HEADER_SEPARATOR.length());
int second = newStr.indexOf(HEADER_SEPARATOR);
if (second == -1) {
return "";
}

return v.substring(first + HEADER_SEPARATOR.length(), second + first + HEADER_SEPARATOR.length());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@

package io.conduktor.gateway.interceptor;

import com.google.common.io.Resources;
import org.apache.kafka.common.requests.AbstractRequestResponse;

import java.util.*;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;

public interface Plugin {

Expand All @@ -36,4 +44,25 @@ default Map<Class<?>, List<Interceptor<? extends AbstractRequestResponse>>> getT
});
return result;
}

default String readme() {
try {
return resourceAsString("/META-INF/services/README.md");
} catch (Exception e) {
return "";
}
}

private String resourceAsString(String name) throws URISyntaxException, IOException {
try {
return Resources.toString(getClass().getClassLoader().getResource(name).toURI().toURL(), UTF_8);
} catch (Exception e) {
// support flat classloader
return Resources.toString(getClass().getResource(name).toURI().toURL(), UTF_8);
}
}

default Map<String, String> tags() {
return new MarkdownHeadersUtil().extractHeaders(readme());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.conduktor.gateway.interceptor;


import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class MarkdownHeadersUtilTest {
@Test
public void validHeader() {
assertThat(new MarkdownHeadersUtil().extractHeaders("""
---
version: 1.0.1-SNAPSHOT
title: Virtual SQL Topic
description: Don't reinvent the wheel to filter and project your messages, just use SQL!
parent: safeguard
---
# Interceptor"""))
.containsExactlyInAnyOrderEntriesOf(Map.of(
"version", "1.0.1-SNAPSHOT",
"title", "Virtual SQL Topic",
"description", "Don't reinvent the wheel to filter and project your messages, just use SQL!",
"parent", "safeguard"
));
}

@Test
public void emptyHeaders() {
assertThat(new MarkdownHeadersUtil().extractHeaders("""
---
---
# Interceptor"""))
.isEmpty();
}

@Test
public void emptyHeader() {
assertThat(new MarkdownHeadersUtil().extractHeaders("""
---
version:
---
# Interceptor"""))
.containsAllEntriesOf(Map.of(
"version", ""
));
}

@Test
public void noHeader() {
assertThat(new MarkdownHeadersUtil().extractHeaders("""
# Interceptor"""))
.isEmpty();
}

@Test
public void unclosedHeader() {
assertThat(new MarkdownHeadersUtil().extractHeaders("""
---
title: Virtual SQL Topic
# Interceptor"""))
.isEmpty();
}

}
14 changes: 13 additions & 1 deletion logger-interceptor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
<artifactId>interceptor-framework</artifactId>
<version>0.5.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -31,7 +44,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>

</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.conduktor.example.loggerinterceptor;


import com.google.common.collect.Lists;
import io.conduktor.gateway.interceptor.Interceptor;
import io.conduktor.gateway.interceptor.InterceptorConfigurationException;
import io.conduktor.gateway.interceptor.InterceptorProvider;
Expand Down Expand Up @@ -44,5 +45,4 @@ public List<InterceptorProvider<?>> getInterceptors(Map<String, Object> config)
new InterceptorProvider<>(AbstractResponse.class, new ResponseLoggerInterceptor())
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
version: 1.0.1-SNAPSHOT
title: Virtual SQL Topic
description: Don't reinvent the wheel to filter and project your messages, just use SQL!
parent: safeguard
---
# Interceptor

Welcome to Logger interceptor!
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.conduktor.example.loggerinterceptor;

import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class LoggerInterceptorPluginTest {
@Test
void hasReadme() {
assertThat(new LoggerInterceptorPlugin().readme())
.contains("Welcome to Logger interceptor!");
}

@Test
void hasTags() {
assertThat(new LoggerInterceptorPlugin().tags())
.containsExactlyInAnyOrderEntriesOf(Map.of(
"version", "1.0.1-SNAPSHOT",
"title", "Virtual SQL Topic",
"description", "Don't reinvent the wheel to filter and project your messages, just use SQL!",
"parent", "safeguard"
));
}
}

0 comments on commit 7f1fe4b

Please sign in to comment.