diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/manscdp/keepalive/KeepaliveNotifyDTO.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/manscdp/keepalive/KeepaliveNotifyDTO.java new file mode 100644 index 0000000..943ba26 --- /dev/null +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/manscdp/keepalive/KeepaliveNotifyDTO.java @@ -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"; +} diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/utils/MANSCDPUtils.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/utils/MANSCDPUtils.java index d66acea..159cdce 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/utils/MANSCDPUtils.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/utils/MANSCDPUtils.java @@ -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 { + public static class XMLBuilder { @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("\n", charset)); + mapper.writer().writeValue(outputStreamWriter,obj); + return byteArrayOutputStream.toByteArray(); + } + + public static String toQuery(XMLBuilder wrapper){ + return MANSCDPUtils.toXml(wrapper.getQuery(), wrapper.encoding); + } + + public static String toResponse(XMLBuilder wrapper){ + return MANSCDPUtils.toXml(wrapper.getResponse(), wrapper.encoding); + } + + public static String toNotify(XMLBuilder wrapper){ + return MANSCDPUtils.toXml(wrapper.getNotify(), wrapper.encoding); + } + + public static String toControl(XMLBuilder 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 parse(byte[] xml, TypeReference clazz, Charset charset) { - return parse(new String(xml, charset), clazz); + return parse(new String(xml,charset), clazz); } public static T parse(String xml, TypeReference clazz) { + return parse(xml, clazz, GB28181Constant.CHARSET); + } + + @SneakyThrows + public static T parse(String xml, TypeReference clazz, String charset) { + return parse(xml, clazz, Charset.forName(charset)); + } + + public static T parse(String xml, TypeReference clazz, Charset charset) { try { - return mapper.readValue(xml, clazz); + return mapper.readValue(xml.getBytes(charset), clazz); } catch (Exception e) { e.printStackTrace(); return null; diff --git a/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/SipTest.java b/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/SipTest.java index c7a3331..4c3fbeb 100644 --- a/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/SipTest.java +++ b/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/SipTest.java @@ -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.*; diff --git a/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/manscdp/MANSCDPTest.java b/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/manscdp/MANSCDPTest.java index 09f7214..679f934 100644 --- a/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/manscdp/MANSCDPTest.java +++ b/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/manscdp/MANSCDPTest.java @@ -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 xmlBuilder = MANSCDPUtils.XMLBuilder.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 query = MANSCDPUtils.XMLWrapper.builder() + MANSCDPUtils.XMLBuilder query = MANSCDPUtils.XMLBuilder.builder() .query(catalogQueryDTO) .build(); - String serialize = MANSCDPUtils.toXml(query); + String serialize = MANSCDPUtils.toQuery(query); log.info("\n{}", serialize); - MANSCDPUtils.XMLWrapper 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 response = MANSCDPUtils.XMLWrapper.builder() + // 默认 GB2312 编码 + MANSCDPUtils.XMLBuilder response = MANSCDPUtils.XMLBuilder.builder() .response(catalogResponseDTO) .build(); - serialize = MANSCDPUtils.toXml(response); + serialize = MANSCDPUtils.toResponse(response); log.info("\n{}", serialize); - MANSCDPUtils.XMLWrapper 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.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); } }