Skip to content

Commit 56e9ff7

Browse files
authored
Add "user friendly" bytes deserializer (#194)
* Add "user friendly" bytes deserializer * code cleanup
1 parent b054a99 commit 56e9ff7

File tree

7 files changed

+117
-3
lines changed

7 files changed

+117
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
33
and this project adheres to [Semantic Versioning](http://semver.org/).
44

55
## 2.5.0 (UNRELEASED)
6+
#### New Features
7+
- [PR-194](https://github.com/SourceLabOrg/kafka-webview/pull/194) Adds a new built-in deserializer for byte[] that decodes the bytes into HEX values.
8+
69
#### Internal Dependency Updates
710
- Upgrade from SpringBoot 2.0.8 to 2.1.8.
811
- org.apache.commons:commons-compress updated from 1.18 to 1.19.

kafka-webview-ui/src/main/java/org/sourcelab/kafka/webview/ui/configuration/DataLoaderConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.kafka.common.serialization.LongDeserializer;
3333
import org.apache.kafka.common.serialization.ShortDeserializer;
3434
import org.apache.kafka.common.serialization.StringDeserializer;
35+
import org.sourcelab.kafka.webview.ui.manager.kafka.deserializer.BytesToHexDeserializer;
3536
import org.sourcelab.kafka.webview.ui.manager.user.UserManager;
3637
import org.sourcelab.kafka.webview.ui.model.MessageFormat;
3738
import org.sourcelab.kafka.webview.ui.model.UserRole;
@@ -102,6 +103,7 @@ private void createDefaultMessageFormats() {
102103
defaultFormats.put("Integer", IntegerDeserializer.class.getName());
103104
defaultFormats.put("Long", LongDeserializer.class.getName());
104105
defaultFormats.put("String", StringDeserializer.class.getName());
106+
defaultFormats.put("Bytes (Hex Encoded)", BytesToHexDeserializer.class.getName());
105107

106108
// Create if needed.
107109
for (final Map.Entry<String, String> entry : defaultFormats.entrySet()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2017, 2018, 2019 SourceLab.org (https://github.com/SourceLabOrg/kafka-webview/)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package org.sourcelab.kafka.webview.ui.manager.kafka.deserializer;
26+
27+
import org.apache.kafka.common.serialization.Deserializer;
28+
import org.apache.tomcat.util.buf.HexUtils;
29+
30+
import java.util.Map;
31+
32+
/**
33+
* Deserializes bytes into Hex strings. Intended to allow for a more user friendly display of bytes.
34+
*/
35+
public class BytesToHexDeserializer implements Deserializer<String> {
36+
37+
@Override
38+
public void configure(final Map<String, ?> configs, final boolean isKey) {
39+
}
40+
41+
@Override
42+
public String deserialize(final String topic, final byte[] data) {
43+
// Convert bytes into Hex.
44+
return HexUtils.toHexString(data);
45+
}
46+
47+
@Override
48+
public void close() {
49+
}
50+
}

kafka-webview-ui/src/main/java/org/sourcelab/kafka/webview/ui/manager/plugin/PluginFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525
package org.sourcelab.kafka.webview.ui.manager.plugin;
2626

27-
import java.io.IOException;
2827
import org.sourcelab.kafka.webview.ui.manager.plugin.exception.LoaderException;
2928
import org.sourcelab.kafka.webview.ui.manager.plugin.exception.UnableToFindClassException;
3029
import org.sourcelab.kafka.webview.ui.manager.plugin.exception.WrongImplementationException;
3130

31+
import java.io.IOException;
3232
import java.lang.reflect.InvocationTargetException;
3333
import java.net.MalformedURLException;
3434
import java.net.URL;

kafka-webview-ui/src/main/java/org/sourcelab/kafka/webview/ui/manager/plugin/UploadManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525
package org.sourcelab.kafka.webview.ui.manager.plugin;
2626

27-
import java.io.BufferedInputStream;
2827
import org.slf4j.Logger;
2928
import org.slf4j.LoggerFactory;
3029
import org.springframework.web.multipart.MultipartFile;
3130

31+
import java.io.BufferedInputStream;
3232
import java.io.File;
3333
import java.io.IOException;
3434
import java.nio.file.Files;

kafka-webview-ui/src/test/java/org/sourcelab/kafka/webview/ui/manager/kafka/WebKafkaConsumerFactoryTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.junit.Test;
3737
import org.junit.runner.RunWith;
3838
import org.sourcelab.kafka.webview.ui.manager.encryption.SecretManager;
39-
import org.sourcelab.kafka.webview.ui.manager.kafka.config.FilterDefinition;
4039
import org.sourcelab.kafka.webview.ui.manager.kafka.dto.KafkaResult;
4140
import org.sourcelab.kafka.webview.ui.manager.kafka.dto.KafkaResults;
4241
import org.sourcelab.kafka.webview.ui.manager.plugin.PluginFactory;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2017, 2018, 2019 SourceLab.org (https://github.com/SourceLabOrg/kafka-webview/)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package org.sourcelab.kafka.webview.ui.manager.kafka.deserializer;
26+
27+
import junitparams.JUnitParamsRunner;
28+
import junitparams.Parameters;
29+
import org.apache.kafka.common.serialization.Deserializer;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
33+
import static org.junit.Assert.assertEquals;
34+
35+
@RunWith(JUnitParamsRunner.class)
36+
public class BytesToHexDeserializerTest {
37+
38+
private final Deserializer<String> deserializer = new BytesToHexDeserializer();
39+
40+
/**
41+
* Validate the deserializer works as expected.
42+
*/
43+
@Test
44+
@Parameters(method = "provideBytes")
45+
public void doTest(final byte[] bytes, final String expectedHexStr) {
46+
final String results = deserializer.deserialize("not-relevant", bytes);
47+
assertEquals("Strings should match", expectedHexStr, results);
48+
}
49+
50+
public Object[] provideBytes() {
51+
return new Object[]{
52+
new Object[]{ "blahblahlbha".getBytes(), "626c6168626c61686c626861" },
53+
new Object[]{ "blahblahlbhah".getBytes(), "626c6168626c61686c62686168" },
54+
new Object[]{ "key1572998885260".getBytes(), "6b657931353732393938383835323630" },
55+
new Object[]{ "key1572998885261".getBytes(), "6b657931353732393938383835323631" },
56+
new Object[]{ "key1572998885255".getBytes(), "6b657931353732393938383835323535" },
57+
new Object[]{ "key1572998885258".getBytes(), "6b657931353732393938383835323538" },
58+
};
59+
}
60+
}

0 commit comments

Comments
 (0)