-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support extract-headers kafka message transform (#1177)
- Loading branch information
Showing
13 changed files
with
300 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
.../main/java/io/aklivity/zilla/runtime/binding/kafka/config/KafkaTopicTransformsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2021-2023 Aklivity Inc. | ||
* | ||
* Aklivity licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.aklivity.zilla.runtime.binding.kafka.config; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public class KafkaTopicTransformsConfig | ||
{ | ||
public final List<KafkaTopicHeaderType> extractHeaders; | ||
|
||
public static KafkaTopicTransformsConfigBuilder<KafkaTopicTransformsConfig> builder() | ||
{ | ||
return new KafkaTopicTransformsConfigBuilder<>(KafkaTopicTransformsConfig.class::cast); | ||
} | ||
|
||
public static <T> KafkaTopicTransformsConfigBuilder<T> builder( | ||
Function<KafkaTopicTransformsConfig, T> mapper) | ||
{ | ||
return new KafkaTopicTransformsConfigBuilder<>(mapper); | ||
} | ||
|
||
KafkaTopicTransformsConfig( | ||
List<KafkaTopicHeaderType> extractHeaders) | ||
{ | ||
this.extractHeaders = extractHeaders; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...ava/io/aklivity/zilla/runtime/binding/kafka/config/KafkaTopicTransformsConfigBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2021-2023 Aklivity Inc. | ||
* | ||
* Aklivity licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.aklivity.zilla.runtime.binding.kafka.config; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import io.aklivity.zilla.runtime.engine.config.ConfigBuilder; | ||
|
||
public final class KafkaTopicTransformsConfigBuilder<T> extends ConfigBuilder<T, KafkaTopicTransformsConfigBuilder<T>> | ||
{ | ||
private static final String PATH = "^\\$\\{message\\.value\\.([A-Za-z_][A-Za-z0-9_]*)\\}$"; | ||
private static final Pattern PATH_PATTERN = Pattern.compile(PATH); | ||
private static final String INTERNAL_VALUE = "$.%s"; | ||
private static final String INTERNAL_PATH = "^\\$\\..*$"; | ||
private static final Pattern INTERNAL_PATH_PATTERN = Pattern.compile(INTERNAL_PATH); | ||
|
||
private final Matcher matcher; | ||
private final Matcher internalMatcher; | ||
private final Function<KafkaTopicTransformsConfig, T> mapper; | ||
private List<KafkaTopicHeaderType> extractHeaders; | ||
|
||
KafkaTopicTransformsConfigBuilder( | ||
Function<KafkaTopicTransformsConfig, T> mapper) | ||
{ | ||
this.mapper = mapper; | ||
this.extractHeaders = new ArrayList<>(); | ||
this.matcher = PATH_PATTERN.matcher(""); | ||
this.internalMatcher = INTERNAL_PATH_PATTERN.matcher(""); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
protected Class<KafkaTopicTransformsConfigBuilder<T>> thisType() | ||
{ | ||
return (Class<KafkaTopicTransformsConfigBuilder<T>>) getClass(); | ||
} | ||
|
||
public KafkaTopicTransformsConfigBuilder<T> extractHeaders( | ||
List<KafkaTopicHeaderType> extractHeaders) | ||
{ | ||
if (extractHeaders != null) | ||
{ | ||
extractHeaders.forEach(this::extractHeader); | ||
} | ||
return this; | ||
} | ||
|
||
public KafkaTopicTransformsConfigBuilder<T> extractHeader( | ||
KafkaTopicHeaderType header) | ||
{ | ||
return extractHeader(header.name, header.path); | ||
} | ||
|
||
public KafkaTopicTransformsConfigBuilder<T> extractHeader( | ||
String name, | ||
String path) | ||
{ | ||
if (this.extractHeaders == null) | ||
{ | ||
this.extractHeaders = new ArrayList<>(); | ||
} | ||
if (matcher.reset(path).matches()) | ||
{ | ||
this.extractHeaders.add(new KafkaTopicHeaderType(name, | ||
String.format(INTERNAL_VALUE, matcher.group(1)))); | ||
} | ||
else if (internalMatcher.reset(path).matches()) | ||
{ | ||
this.extractHeaders.add(new KafkaTopicHeaderType(name, path)); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public T build() | ||
{ | ||
return mapper.apply(new KafkaTopicTransformsConfig(extractHeaders)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.