MANSCDPUtils.Builder

支持 自定义 字符串编码
默认为 GB28181 默认 GB2312 编码
This commit is contained in:
shikong 2023-09-25 00:07:20 +08:00
parent 63367ab05f
commit d9805afe88
4 changed files with 111 additions and 37 deletions

View File

@ -0,0 +1,28 @@
package cn.skcks.docking.gb28181.sip.manscdp.keepalive;
import cn.skcks.docking.gb28181.constant.CmdType;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@JacksonXmlRootElement(localName = "Notify")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class KeepaliveNotifyDTO {
@Builder.Default
private String cmdType = CmdType.KEEPALIVE;
@JacksonXmlProperty(localName = "SN")
private String sn;
@JacksonXmlProperty(localName = "DeviceID")
private String deviceId;
@Builder.Default
private String status = "OK";
}

View File

@ -1,21 +1,23 @@
package cn.skcks.docking.gb28181.sip.utils;
import cn.skcks.docking.gb28181.constant.GB28181Constant;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Objects;
import java.text.MessageFormat;
@Slf4j
@SuppressWarnings("unused")
public class MANSCDPUtils {
private static final XmlMapper mapper = new XmlMapper();
@ -33,36 +35,55 @@ public class MANSCDPUtils {
mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.UpperCamelCaseStrategy());
// 添加 xml 头部声明
mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, false);
mapper.configure(ToXmlGenerator.Feature.UNWRAP_ROOT_OBJECT_NODE, true);
// mapper.configure(ToXmlGenerator.Feature.UNWRAP_ROOT_OBJECT_NODE, false);
}
@JacksonXmlRootElement(localName = "xml")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public static class XMLWrapper<T> {
public static class XMLBuilder<T> {
@Builder.Default
@JacksonXmlProperty(isAttribute = true, localName = "encoding")
@JsonIgnore
private String encoding = GB28181Constant.CHARSET;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T query;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T response;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T notify;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T control;
}
@SneakyThrows
public static byte[] writeXmlWithEncoding(Object obj, String charset){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, charset);
outputStreamWriter.write(MessageFormat.format("<?xml version=\"1.0\" encoding=\"{0}\" ?>\n", charset));
mapper.writer().writeValue(outputStreamWriter,obj);
return byteArrayOutputStream.toByteArray();
}
public static <T> String toQuery(XMLBuilder<T> wrapper){
return MANSCDPUtils.toXml(wrapper.getQuery(), wrapper.encoding);
}
public static <T> String toResponse(XMLBuilder<T> wrapper){
return MANSCDPUtils.toXml(wrapper.getResponse(), wrapper.encoding);
}
public static <T> String toNotify(XMLBuilder<T> wrapper){
return MANSCDPUtils.toXml(wrapper.getNotify(), wrapper.encoding);
}
public static <T> String toControl(XMLBuilder<T> wrapper){
return MANSCDPUtils.toXml(wrapper.getControl(), wrapper.encoding);
}
public static String toXml(Object obj, String inputCharset,String outputCharset) {
try {
return new String(Objects.requireNonNull(toByteXml(obj, inputCharset)), Charset.forName(outputCharset));
return new String(toByteXml(obj, inputCharset), Charset.forName(outputCharset));
} catch (Exception e) {
e.printStackTrace();
return null;
@ -74,7 +95,7 @@ public class MANSCDPUtils {
return null;
}
try {
return new String(toByteXml(obj, GB28181Constant.CHARSET), Charset.forName(charset));
return new String(toByteXml(obj, charset), Charset.forName(charset));
} catch (Exception e) {
e.printStackTrace();
return null;
@ -104,7 +125,7 @@ public class MANSCDPUtils {
public static byte[] toByteXml(Object obj, Charset charset) {
try {
return mapper.writeValueAsString(obj).getBytes(charset);
return writeXmlWithEncoding(obj, charset.name());
} catch (Exception e) {
e.printStackTrace();
return null;
@ -116,7 +137,7 @@ public class MANSCDPUtils {
return null;
}
try {
return mapper.writeValueAsString(obj).getBytes(Charset.forName(charset));
return toByteXml(obj, Charset.forName(charset));
} catch (Exception e) {
e.printStackTrace();
return null;
@ -155,12 +176,21 @@ public class MANSCDPUtils {
}
public static <T> T parse(byte[] xml, TypeReference<T> clazz, Charset charset) {
return parse(new String(xml, charset), clazz);
return parse(new String(xml,charset), clazz);
}
public static <T> T parse(String xml, TypeReference<T> clazz) {
return parse(xml, clazz, GB28181Constant.CHARSET);
}
@SneakyThrows
public static <T> T parse(String xml, TypeReference<T> clazz, String charset) {
return parse(xml, clazz, Charset.forName(charset));
}
public static <T> T parse(String xml, TypeReference<T> clazz, Charset charset) {
try {
return mapper.readValue(xml, clazz);
return mapper.readValue(xml.getBytes(charset), clazz);
} catch (Exception e) {
e.printStackTrace();
return null;

View File

@ -9,7 +9,6 @@ import cn.skcks.docking.gb28181.sip.response.SipResponseBuilder;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import javax.sip.ListeningPoint;
import javax.sip.address.Address;
import javax.sip.address.SipURI;
import javax.sip.header.*;

View File

@ -4,11 +4,12 @@ import cn.skcks.docking.gb28181.sip.manscdp.catalog.CatalogDeviceListDTO;
import cn.skcks.docking.gb28181.sip.manscdp.catalog.CatalogItemDTO;
import cn.skcks.docking.gb28181.sip.manscdp.catalog.CatalogQueryDTO;
import cn.skcks.docking.gb28181.sip.manscdp.catalog.CatalogResponseDTO;
import cn.skcks.docking.gb28181.sip.manscdp.keepalive.KeepaliveNotifyDTO;
import cn.skcks.docking.gb28181.sip.utils.MANSCDPUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
@Slf4j
@ -16,10 +17,25 @@ public class MANSCDPTest {
public static final String deviceId = "44050100001110000010";
public static final String channelId = "44050100001310000010";
public static final String deviceName = "模拟设备名称";
public static String sn = String.valueOf(1);
@Test
public void keepalive(){
KeepaliveNotifyDTO keepaliveNotifyDTO = KeepaliveNotifyDTO.builder()
.deviceId(deviceId)
.sn(sn)
.build();
MANSCDPUtils.XMLBuilder<KeepaliveNotifyDTO> xmlBuilder = MANSCDPUtils.XMLBuilder.<KeepaliveNotifyDTO>builder()
.notify(keepaliveNotifyDTO)
.build();
log.info("\n{}",MANSCDPUtils.toXml(xmlBuilder));
}
@Test
public void catalog(){
String sn = String.valueOf(1);
// 创建 目录查询 请求
CatalogQueryDTO catalogQueryDTO = CatalogQueryDTO.builder()
.deviceId(deviceId)
@ -27,19 +43,14 @@ public class MANSCDPTest {
.build();
log.info("\n{}", MANSCDPUtils.toXml(catalogQueryDTO));
MANSCDPUtils.XMLWrapper<CatalogQueryDTO> query = MANSCDPUtils.XMLWrapper.<CatalogQueryDTO>builder()
MANSCDPUtils.XMLBuilder<CatalogQueryDTO> query = MANSCDPUtils.XMLBuilder.<CatalogQueryDTO>builder()
.query(catalogQueryDTO)
.build();
String serialize = MANSCDPUtils.toXml(query);
String serialize = MANSCDPUtils.toQuery(query);
log.info("\n{}", serialize);
MANSCDPUtils.XMLWrapper<CatalogQueryDTO> deserializeCatalogQueryDTO = MANSCDPUtils.parse(serialize, new TypeReference<>(){});
log.info("\n{}",deserializeCatalogQueryDTO);
if (deserializeCatalogQueryDTO == null) {
return;
}
CatalogQueryDTO queryDTO = deserializeCatalogQueryDTO.getQuery();
log.info("{}",queryDTO);
CatalogQueryDTO queryDTO = MANSCDPUtils.parse(serialize, CatalogQueryDTO.class);
log.info("\n{}",queryDTO);
// 创建 目录 查询响应
CatalogItemDTO catalogItemDTO = CatalogItemDTO.builder()
@ -53,16 +64,22 @@ public class MANSCDPTest {
.deviceList(new CatalogDeviceListDTO(1,Collections.singletonList(catalogItemDTO)))
.sumNum(0L)
.build();
MANSCDPUtils.XMLWrapper<CatalogResponseDTO> response = MANSCDPUtils.XMLWrapper.<CatalogResponseDTO>builder()
// 默认 GB2312 编码
MANSCDPUtils.XMLBuilder<CatalogResponseDTO> response = MANSCDPUtils.XMLBuilder.<CatalogResponseDTO>builder()
.response(catalogResponseDTO)
.build();
serialize = MANSCDPUtils.toXml(response);
serialize = MANSCDPUtils.toResponse(response);
log.info("\n{}", serialize);
MANSCDPUtils.XMLWrapper<CatalogResponseDTO> deserializeCatalogResponseDTO = MANSCDPUtils.parse(serialize, new TypeReference<>(){});
if (deserializeCatalogResponseDTO == null) {
return;
}
CatalogResponseDTO responseDTO = deserializeCatalogResponseDTO.getResponse();
CatalogResponseDTO responseDTO = MANSCDPUtils.parse(serialize, CatalogResponseDTO.class);
log.info("{}",responseDTO);
// UTF8 编码
response = MANSCDPUtils.XMLBuilder.<CatalogResponseDTO>builder()
.encoding(StandardCharsets.UTF_8.name())
.response(catalogResponseDTO)
.build();
serialize = MANSCDPUtils.toResponse(response);
log.info("\n{}", serialize);
responseDTO = MANSCDPUtils.parse(serialize, CatalogResponseDTO.class);
log.info("{}",responseDTO);
}
}