4 changed files with 112 additions and 7 deletions
@ -0,0 +1,108 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed 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 org.thingsboard.server.common.data; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
||||
|
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.thingsboard.server.common.data.id.UUIDBased; |
||||
|
import org.thingsboard.server.common.data.validation.NoXss; |
||||
|
|
||||
|
import java.io.ByteArrayInputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.util.Arrays; |
||||
|
import java.util.Objects; |
||||
|
import java.util.function.Consumer; |
||||
|
import java.util.function.Supplier; |
||||
|
|
||||
|
/** |
||||
|
* Created by ashvayka on 19.02.18. |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public abstract class BaseDataWithAdditionalInfo<I extends UUIDBased> extends BaseData<I> implements HasAdditionalInfo { |
||||
|
|
||||
|
public static final ObjectMapper mapper = new ObjectMapper(); |
||||
|
@NoXss |
||||
|
private transient JsonNode additionalInfo; |
||||
|
@JsonIgnore |
||||
|
private byte[] additionalInfoBytes; |
||||
|
|
||||
|
public BaseDataWithAdditionalInfo() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public BaseDataWithAdditionalInfo(I id) { |
||||
|
super(id); |
||||
|
} |
||||
|
|
||||
|
public BaseDataWithAdditionalInfo(BaseDataWithAdditionalInfo<I> baseData) { |
||||
|
super(baseData); |
||||
|
setAdditionalInfo(baseData.getAdditionalInfo()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public JsonNode getAdditionalInfo() { |
||||
|
return getJson(() -> additionalInfo, () -> additionalInfoBytes); |
||||
|
} |
||||
|
|
||||
|
public void setAdditionalInfo(JsonNode addInfo) { |
||||
|
setJson(addInfo, json -> this.additionalInfo = json, bytes -> this.additionalInfoBytes = bytes); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||
|
if (!super.equals(o)) return false; |
||||
|
BaseDataWithAdditionalInfo<?> that = (BaseDataWithAdditionalInfo<?>) o; |
||||
|
return Arrays.equals(additionalInfoBytes, that.additionalInfoBytes); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
return Objects.hash(super.hashCode(), additionalInfoBytes); |
||||
|
} |
||||
|
|
||||
|
public static JsonNode getJson(Supplier<JsonNode> jsonData, Supplier<byte[]> binaryData) { |
||||
|
JsonNode json = jsonData.get(); |
||||
|
if (json != null) { |
||||
|
return json; |
||||
|
} else { |
||||
|
byte[] data = binaryData.get(); |
||||
|
if (data != null) { |
||||
|
try { |
||||
|
return mapper.readTree(new ByteArrayInputStream(data)); |
||||
|
} catch (IOException e) { |
||||
|
log.warn("Can't deserialize json data: ", e); |
||||
|
return null; |
||||
|
} |
||||
|
} else { |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static void setJson(JsonNode json, Consumer<JsonNode> jsonConsumer, Consumer<byte[]> bytesConsumer) { |
||||
|
jsonConsumer.accept(json); |
||||
|
try { |
||||
|
bytesConsumer.accept(mapper.writeValueAsBytes(json)); |
||||
|
} catch (JsonProcessingException e) { |
||||
|
log.warn("Can't serialize json data: ", e); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue