2 * ========================LICENSE_START=================================
5 * Copyright (C) 2023 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oran.pmlog;
23 import java.io.ByteArrayInputStream;
24 import java.util.zip.GZIPInputStream;
26 import lombok.ToString;
28 import org.apache.kafka.common.header.Header;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 public class DataFromKafkaTopic {
34 private final byte[] key;
35 private final byte[] value;
36 private String stringValue = null;
37 private static final Logger logger = LoggerFactory.getLogger(DataFromKafkaTopic.class);
39 public final Iterable<Header> headers;
41 private static byte[] noBytes = new byte[0];
43 public DataFromKafkaTopic(Iterable<Header> headers, byte[] key, byte[] value) {
44 this.key = key == null ? noBytes : key;
45 this.value = value == null ? noBytes : value;
46 this.headers = headers;
49 public String valueAsString() {
50 if (stringValue == null) {
52 stringValue = unzip(this.value);
54 stringValue = new String(this.value);
58 return this.stringValue;
61 private String unzip(byte[] bytes) {
62 try (final GZIPInputStream gzipInput = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
63 return new String(gzipInput.readAllBytes());
64 } catch (Exception e) {
65 logger.error("Could not unzip received info, reason: {}, typeId: {}", e.getMessage(),
66 getTypeIdFromHeaders());
71 public static final String ZIPPED_PROPERTY = "gzip";
72 public static final String TYPE_ID_PROPERTY = "type-id";
74 public boolean isZipped() {
75 if (headers == null) {
78 for (Header h : headers) {
79 if (h.key().equals(ZIPPED_PROPERTY)) {
86 public String getTypeIdFromHeaders() {
87 if (headers == null) {
90 for (Header h : headers) {
91 if (h.key().equals(TYPE_ID_PROPERTY)) {
92 return new String(h.value());