Skip to content

Commit a490600

Browse files
authored
[ISSUE-114] Properly deserialize avro to json (#125)
* [ISSUE-114] Properly deserialize avro to json * [ISSUE-114] update version, changelog * add missing header * Use updated commons-compress library
1 parent 97ce140 commit a490600

File tree

8 files changed

+133
-8
lines changed

8 files changed

+133
-8
lines changed

CHANGELOG.md

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

5+
## 2.1.2 (UNRELEASED)
6+
7+
#### Bug fixes
8+
- [ISSUE-114](https://github.com/SourceLabOrg/kafka-webview/issues/114) Better out of the box support for Avro decoding / Confluent Schema Registry.
9+
510
## 2.1.1 (01/08/2019)
611
#### New Features
712
- Added ability to Copy previously created views.

dev-cluster/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<artifactId>kafka-webview</artifactId>
77
<groupId>org.sourcelab</groupId>
8-
<version>2.1.1</version>
8+
<version>2.1.2</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>dev-cluster</artifactId>
13-
<version>2.1.1</version>
13+
<version>2.1.2</version>
1414

1515
<!-- Require Maven 3.3.9 -->
1616
<prerequisites>

kafka-webview-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.sourcelab</groupId>
77
<artifactId>kafka-webview</artifactId>
8-
<version>2.1.1</version>
8+
<version>2.1.2</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111
<artifactId>kafka-webview-plugin</artifactId>

kafka-webview-ui/pom.xml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<parent>
66
<artifactId>kafka-webview</artifactId>
77
<groupId>org.sourcelab</groupId>
8-
<version>2.1.1</version>
8+
<version>2.1.2</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111
<artifactId>kafka-webview-ui</artifactId>
12-
<version>2.1.1</version>
12+
<version>2.1.2</version>
1313

1414
<!-- Module Description and Ownership -->
1515
<name>Kafka WebView UI</name>
@@ -30,9 +30,12 @@
3030
<properties>
3131
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3232
<java.version>1.8</java.version>
33+
34+
<avro.version>1.8.2</avro.version>
3335
<bootstrap.version>4.0.0-beta</bootstrap.version>
34-
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
3536
<kafka.version>2.0.0</kafka.version>
37+
<protobuf.version>3.6.1</protobuf.version>
38+
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
3639
</properties>
3740

3841
<dependencies>
@@ -175,7 +178,27 @@
175178
<dependency>
176179
<groupId>com.google.protobuf</groupId>
177180
<artifactId>protobuf-java</artifactId>
178-
<version>3.6.1</version>
181+
<version>${protobuf.version}</version>
182+
</dependency>
183+
184+
<!-- Jackson support for avro-->
185+
<dependency>
186+
<groupId>org.apache.avro</groupId>
187+
<artifactId>avro</artifactId>
188+
<version>${avro.version}</version>
189+
<exclusions>
190+
<!-- exclude vulnerable dependency -->
191+
<exclusion>
192+
<groupId>org.apache.commons</groupId>
193+
<artifactId>commons-compress</artifactId>
194+
</exclusion>
195+
</exclusions>
196+
</dependency>
197+
<!-- Explicitly include updated version -->
198+
<dependency>
199+
<groupId>org.apache.commons</groupId>
200+
<artifactId>commons-compress</artifactId>
201+
<version>1.18</version>
179202
</dependency>
180203

181204
<!-- For tests -->

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class AppProperties {
5252
@Value("${app.requireSsl:false}")
5353
private boolean requireSsl = false;
5454

55+
@Value("${app.avro.includeSchema:true}")
56+
private boolean avroIncludeSchema = true;
57+
5558
/**
5659
* Flag read from configuration file to determine if the app should
5760
* enforce User authentication.
@@ -97,6 +100,10 @@ public LdapAppProperties getLdapProperties() {
97100
return ldapProperties;
98101
}
99102

103+
public boolean isAvroIncludeSchema() {
104+
return avroIncludeSchema;
105+
}
106+
100107
@Override
101108
public String toString() {
102109
return "AppProperties{"

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
import com.fasterxml.jackson.databind.ObjectMapper;
2828
import com.hubspot.jackson.datatype.protobuf.ProtobufModule;
29+
import org.apache.avro.generic.GenericData;
30+
import org.sourcelab.kafka.webview.ui.manager.jackson.SimpleAvroDataSerializer;
31+
import org.springframework.beans.factory.annotation.Autowired;
2932
import org.springframework.context.annotation.Configuration;
3033
import org.springframework.http.converter.HttpMessageConverter;
3134
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@@ -42,6 +45,9 @@
4245
@Configuration
4346
@EnableWebMvc
4447
public class WebConfig implements WebMvcConfigurer {
48+
@Autowired
49+
private AppProperties appProperties;
50+
4551
@Override
4652
public void addResourceHandlers(ResourceHandlerRegistry registry) {
4753
// css resource
@@ -76,6 +82,7 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
7682
final ObjectMapper mapper = Jackson2ObjectMapperBuilder
7783
.json()
7884
.modulesToInstall(new ProtobufModule())
85+
.serializerByType(GenericData.Record.class, new SimpleAvroDataSerializer(appProperties.isAvroIncludeSchema()))
7986
.build();
8087
converters.add(new MappingJackson2HttpMessageConverter(mapper));
8188
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.jackson;
26+
27+
import com.fasterxml.jackson.core.JsonGenerator;
28+
import com.fasterxml.jackson.databind.JsonSerializer;
29+
import com.fasterxml.jackson.databind.SerializerProvider;
30+
import org.apache.avro.generic.GenericData;
31+
32+
import java.io.IOException;
33+
34+
/**
35+
* Simple Avro to JSON serializer for Jackson.
36+
*/
37+
public class SimpleAvroDataSerializer extends JsonSerializer<GenericData.Record> {
38+
private final boolean includeSchema;
39+
40+
/**
41+
* No arg constructor. Default to including the schema.
42+
*/
43+
public SimpleAvroDataSerializer() {
44+
this(true);
45+
}
46+
47+
/**
48+
* Constructor.
49+
* @param includeSchema Should the schema be included.
50+
*/
51+
public SimpleAvroDataSerializer(final boolean includeSchema) {
52+
this.includeSchema = includeSchema;
53+
}
54+
55+
@Override
56+
public void serialize(final GenericData.Record value, final JsonGenerator gen, final SerializerProvider provider) throws IOException {
57+
if (includeSchema) {
58+
writeIncludingSchema(value, gen);
59+
} else {
60+
writeValueOnly(value, gen);
61+
}
62+
}
63+
64+
private void writeIncludingSchema(final GenericData.Record value, final JsonGenerator gen) throws IOException {
65+
// Start new object.
66+
gen.writeStartObject();
67+
68+
// Write value
69+
gen.writeFieldName("value");
70+
gen.writeRawValue(value.toString());
71+
72+
// Write schema
73+
gen.writeFieldName("schema");
74+
gen.writeRawValue(value.getSchema().toString());
75+
76+
// End object
77+
gen.writeEndObject();
78+
}
79+
80+
private void writeValueOnly(final GenericData.Record value, final JsonGenerator gen) throws IOException {
81+
gen.writeRawValue(value.toString());
82+
}
83+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>org.sourcelab</groupId>
88
<artifactId>kafka-webview</artifactId>
99
<packaging>pom</packaging>
10-
<version>2.1.1</version>
10+
<version>2.1.2</version>
1111

1212
<!-- Define submodules -->
1313
<modules>

0 commit comments

Comments
 (0)