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;
27 import lombok.ToString;
29 import org.apache.kafka.common.header.Header;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 public class DataFromKafkaTopic {
36 private final byte[] key;
38 private final byte[] value;
40 private String stringValue = null;
41 private static final Logger logger = LoggerFactory.getLogger(DataFromKafkaTopic.class);
43 public final Iterable<Header> headers;
45 private static byte[] noBytes = new byte[0];
47 public DataFromKafkaTopic(Iterable<Header> headers, byte[] key, byte[] value) {
48 this.key = key == null ? noBytes : key;
49 this.value = value == null ? noBytes : value;
50 this.headers = headers;
53 public String valueAsString() {
54 if (stringValue == null) {
56 stringValue = unzip(this.value);
58 stringValue = new String(this.value);
62 return this.stringValue;
65 private String unzip(byte[] bytes) {
66 try (final GZIPInputStream gzipInput = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
67 return new String(gzipInput.readAllBytes());
68 } catch (Exception e) {
69 logger.error("Could not unzip received info, reason: {}, typeId: {}", e.getMessage(),
70 getTypeIdFromHeaders());
75 public static final String ZIPPED_PROPERTY = "gzip";
76 public static final String TYPE_ID_PROPERTY = "type-id";
78 public boolean isZipped() {
79 if (headers == null) {
82 for (Header h : headers) {
83 if (h.key().equals(ZIPPED_PROPERTY)) {
90 public String getTypeIdFromHeaders() {
91 if (headers == null) {
94 for (Header h : headers) {
95 if (h.key().equals(TYPE_ID_PROPERTY)) {
96 return new String(h.value());