MANSCDPUtils.Builder
支持 自定义 字符串编码 默认为 GB28181 默认 GB2312 编码
This commit is contained in:
parent
63367ab05f
commit
d9805afe88
@ -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";
|
||||||
|
}
|
@ -1,21 +1,23 @@
|
|||||||
package cn.skcks.docking.gb28181.sip.utils;
|
package cn.skcks.docking.gb28181.sip.utils;
|
||||||
|
|
||||||
import cn.skcks.docking.gb28181.constant.GB28181Constant;
|
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.json.JsonReadFeature;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
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 com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.Objects;
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class MANSCDPUtils {
|
public class MANSCDPUtils {
|
||||||
private static final XmlMapper mapper = new XmlMapper();
|
private static final XmlMapper mapper = new XmlMapper();
|
||||||
@ -33,36 +35,55 @@ public class MANSCDPUtils {
|
|||||||
mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.UpperCamelCaseStrategy());
|
mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.UpperCamelCaseStrategy());
|
||||||
// 添加 xml 头部声明
|
// 添加 xml 头部声明
|
||||||
mapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, false);
|
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
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Data
|
@Data
|
||||||
public static class XMLWrapper<T> {
|
public static class XMLBuilder<T> {
|
||||||
@Builder.Default
|
@Builder.Default
|
||||||
@JacksonXmlProperty(isAttribute = true, localName = "encoding")
|
@JsonIgnore
|
||||||
private String encoding = GB28181Constant.CHARSET;
|
private String encoding = GB28181Constant.CHARSET;
|
||||||
|
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
||||||
private T query;
|
private T query;
|
||||||
|
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
||||||
private T response;
|
private T response;
|
||||||
|
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
||||||
private T notify;
|
private T notify;
|
||||||
|
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
||||||
private T control;
|
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) {
|
public static String toXml(Object obj, String inputCharset,String outputCharset) {
|
||||||
try {
|
try {
|
||||||
return new String(Objects.requireNonNull(toByteXml(obj, inputCharset)), Charset.forName(outputCharset));
|
return new String(toByteXml(obj, inputCharset), Charset.forName(outputCharset));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
@ -74,7 +95,7 @@ public class MANSCDPUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return new String(toByteXml(obj, GB28181Constant.CHARSET), Charset.forName(charset));
|
return new String(toByteXml(obj, charset), Charset.forName(charset));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
@ -104,7 +125,7 @@ public class MANSCDPUtils {
|
|||||||
|
|
||||||
public static byte[] toByteXml(Object obj, Charset charset) {
|
public static byte[] toByteXml(Object obj, Charset charset) {
|
||||||
try {
|
try {
|
||||||
return mapper.writeValueAsString(obj).getBytes(charset);
|
return writeXmlWithEncoding(obj, charset.name());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
@ -116,7 +137,7 @@ public class MANSCDPUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return mapper.writeValueAsString(obj).getBytes(Charset.forName(charset));
|
return toByteXml(obj, Charset.forName(charset));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
@ -159,8 +180,17 @@ public class MANSCDPUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T parse(String xml, TypeReference<T> 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 {
|
try {
|
||||||
return mapper.readValue(xml, clazz);
|
return mapper.readValue(xml.getBytes(charset), clazz);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
@ -9,7 +9,6 @@ import cn.skcks.docking.gb28181.sip.response.SipResponseBuilder;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import javax.sip.ListeningPoint;
|
|
||||||
import javax.sip.address.Address;
|
import javax.sip.address.Address;
|
||||||
import javax.sip.address.SipURI;
|
import javax.sip.address.SipURI;
|
||||||
import javax.sip.header.*;
|
import javax.sip.header.*;
|
||||||
|
@ -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.CatalogItemDTO;
|
||||||
import cn.skcks.docking.gb28181.sip.manscdp.catalog.CatalogQueryDTO;
|
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.catalog.CatalogResponseDTO;
|
||||||
|
import cn.skcks.docking.gb28181.sip.manscdp.keepalive.KeepaliveNotifyDTO;
|
||||||
import cn.skcks.docking.gb28181.sip.utils.MANSCDPUtils;
|
import cn.skcks.docking.gb28181.sip.utils.MANSCDPUtils;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -16,10 +17,25 @@ public class MANSCDPTest {
|
|||||||
public static final String deviceId = "44050100001110000010";
|
public static final String deviceId = "44050100001110000010";
|
||||||
public static final String channelId = "44050100001310000010";
|
public static final String channelId = "44050100001310000010";
|
||||||
public static final String deviceName = "模拟设备名称";
|
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
|
@Test
|
||||||
public void catalog(){
|
public void catalog(){
|
||||||
String sn = String.valueOf(1);
|
|
||||||
// 创建 目录查询 请求
|
// 创建 目录查询 请求
|
||||||
CatalogQueryDTO catalogQueryDTO = CatalogQueryDTO.builder()
|
CatalogQueryDTO catalogQueryDTO = CatalogQueryDTO.builder()
|
||||||
.deviceId(deviceId)
|
.deviceId(deviceId)
|
||||||
@ -27,19 +43,14 @@ public class MANSCDPTest {
|
|||||||
.build();
|
.build();
|
||||||
log.info("\n{}", MANSCDPUtils.toXml(catalogQueryDTO));
|
log.info("\n{}", MANSCDPUtils.toXml(catalogQueryDTO));
|
||||||
|
|
||||||
MANSCDPUtils.XMLWrapper<CatalogQueryDTO> query = MANSCDPUtils.XMLWrapper.<CatalogQueryDTO>builder()
|
MANSCDPUtils.XMLBuilder<CatalogQueryDTO> query = MANSCDPUtils.XMLBuilder.<CatalogQueryDTO>builder()
|
||||||
.query(catalogQueryDTO)
|
.query(catalogQueryDTO)
|
||||||
.build();
|
.build();
|
||||||
String serialize = MANSCDPUtils.toXml(query);
|
String serialize = MANSCDPUtils.toQuery(query);
|
||||||
log.info("\n{}", serialize);
|
log.info("\n{}", serialize);
|
||||||
|
|
||||||
MANSCDPUtils.XMLWrapper<CatalogQueryDTO> deserializeCatalogQueryDTO = MANSCDPUtils.parse(serialize, new TypeReference<>(){});
|
CatalogQueryDTO queryDTO = MANSCDPUtils.parse(serialize, CatalogQueryDTO.class);
|
||||||
log.info("\n{}",deserializeCatalogQueryDTO);
|
log.info("\n{}",queryDTO);
|
||||||
if (deserializeCatalogQueryDTO == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
CatalogQueryDTO queryDTO = deserializeCatalogQueryDTO.getQuery();
|
|
||||||
log.info("{}",queryDTO);
|
|
||||||
|
|
||||||
// 创建 目录 查询响应
|
// 创建 目录 查询响应
|
||||||
CatalogItemDTO catalogItemDTO = CatalogItemDTO.builder()
|
CatalogItemDTO catalogItemDTO = CatalogItemDTO.builder()
|
||||||
@ -53,16 +64,22 @@ public class MANSCDPTest {
|
|||||||
.deviceList(new CatalogDeviceListDTO(1,Collections.singletonList(catalogItemDTO)))
|
.deviceList(new CatalogDeviceListDTO(1,Collections.singletonList(catalogItemDTO)))
|
||||||
.sumNum(0L)
|
.sumNum(0L)
|
||||||
.build();
|
.build();
|
||||||
MANSCDPUtils.XMLWrapper<CatalogResponseDTO> response = MANSCDPUtils.XMLWrapper.<CatalogResponseDTO>builder()
|
// 默认 GB2312 编码
|
||||||
|
MANSCDPUtils.XMLBuilder<CatalogResponseDTO> response = MANSCDPUtils.XMLBuilder.<CatalogResponseDTO>builder()
|
||||||
.response(catalogResponseDTO)
|
.response(catalogResponseDTO)
|
||||||
.build();
|
.build();
|
||||||
serialize = MANSCDPUtils.toXml(response);
|
serialize = MANSCDPUtils.toResponse(response);
|
||||||
log.info("\n{}", serialize);
|
log.info("\n{}", serialize);
|
||||||
MANSCDPUtils.XMLWrapper<CatalogResponseDTO> deserializeCatalogResponseDTO = MANSCDPUtils.parse(serialize, new TypeReference<>(){});
|
CatalogResponseDTO responseDTO = MANSCDPUtils.parse(serialize, CatalogResponseDTO.class);
|
||||||
if (deserializeCatalogResponseDTO == null) {
|
log.info("{}",responseDTO);
|
||||||
return;
|
// UTF8 编码
|
||||||
}
|
response = MANSCDPUtils.XMLBuilder.<CatalogResponseDTO>builder()
|
||||||
CatalogResponseDTO responseDTO = deserializeCatalogResponseDTO.getResponse();
|
.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);
|
log.info("{}",responseDTO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user