测试 subscribe catalog 请求/响应构造

This commit is contained in:
shikong 2023-09-28 11:54:02 +08:00
parent a87fedc28b
commit 3a00d027e4
5 changed files with 75 additions and 3 deletions

View File

@ -0,0 +1,33 @@
package cn.skcks.docking.gb28181.sip.manscdp.catalog.response;
import cn.skcks.docking.gb28181.constant.CmdType;
import com.fasterxml.jackson.annotation.JsonRootName;
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 = "Response")
@JsonRootName("Response")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class CatalogSubscribeResponseDTO {
@Builder.Default
private String cmdType = CmdType.CATALOG;
@JacksonXmlProperty(localName = "SN")
private String sn;
/**
* 目标设备的设备编码(必选)
*/
@JacksonXmlProperty(localName = "DeviceID")
private String deviceId;
@Builder.Default
private String result = "OK";
}

View File

@ -6,6 +6,7 @@ import cn.skcks.docking.gb28181.sip.generic.SipContentType;
import cn.skcks.docking.gb28181.sip.generic.SipRequestBuilder;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
import lombok.Data;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import javax.sip.address.Address;
@ -16,6 +17,8 @@ import javax.sip.message.Request;
@Data
@SuperBuilder
public class RequestBuilder {
@Setter
public static int DEFAULT_MAX_FORWARD = 70;
private String localIp;
private int localPort;
private String localId;
@ -37,7 +40,7 @@ public class RequestBuilder {
SipBuilder.createFromHeader(localAddress, SipUtil.generateFromTag()),
SipBuilder.createToHeader(targetAddress),
SipBuilder.createViaHeaders(getTargetIp(), getTargetPort(), getTransport(), SipUtil.generateViaTag()),
SipBuilder.createMaxForwardsHeader(70));
SipBuilder.createMaxForwardsHeader(DEFAULT_MAX_FORWARD));
}
public Request createRequest(String method, String callId, long cSeq, byte[] content) {
@ -53,7 +56,7 @@ public class RequestBuilder {
SipBuilder.createFromHeader(localAddress, SipUtil.generateFromTag()),
SipBuilder.createToHeader(targetAddress),
SipBuilder.createViaHeaders(getTargetIp(), getTargetPort(), getTransport(), SipUtil.generateViaTag()),
SipBuilder.createMaxForwardsHeader(70), SipContentType.XML, content);
SipBuilder.createMaxForwardsHeader(DEFAULT_MAX_FORWARD), SipContentType.XML, content);
}
public Request createRequest(String method, String callId, long cSeq, GB28181Description description) {
@ -69,6 +72,6 @@ public class RequestBuilder {
SipBuilder.createFromHeader(localAddress, SipUtil.generateFromTag()),
SipBuilder.createToHeader(targetAddress),
SipBuilder.createViaHeaders(getTargetIp(), getTargetPort(), getTransport(), SipUtil.generateViaTag()),
SipBuilder.createMaxForwardsHeader(70), SipContentType.SDP, description.toString());
SipBuilder.createMaxForwardsHeader(DEFAULT_MAX_FORWARD), SipContentType.SDP, description.toString());
}
}

View File

@ -19,4 +19,9 @@ public class SubscribeRequestBuilder extends RequestBuilder implements Subscribe
return SipBuilder.addHeaders(createRequest(METHOD,callId,cSeq,content),
SipBuilder.createEventHeader(event));
}
public Request createSubscribeRequest(String callId,long cSeq,String event,byte[] content, int expire){
return SipBuilder.addHeaders(createSubscribeRequest(callId,cSeq,event,content),
SipBuilder.createExpiresHeader(expire));
}
}

View File

@ -0,0 +1,20 @@
package cn.skcks.docking.gb28181.sip.method.subscribe.response;
import cn.skcks.docking.gb28181.sip.generic.SipResponseBuilder;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import javax.sip.message.Request;
import javax.sip.message.Response;
@Slf4j
@Data
@SuperBuilder
@ToString(callSuper = true)
public class SubscribeResponseBuilder {
public Response createSubscribeResponse(Request request, byte[] content) {
return SipResponseBuilder.createXmlResponse(Response.OK, request, content);
}
}

View File

@ -2,9 +2,11 @@ package cn.skcks.docking.gb28181.sip.process;
import cn.hutool.core.util.IdUtil;
import cn.skcks.docking.gb28181.sip.manscdp.catalog.query.CatalogQueryDTO;
import cn.skcks.docking.gb28181.sip.manscdp.catalog.response.CatalogSubscribeResponseDTO;
import cn.skcks.docking.gb28181.sip.method.register.request.RegisterRequestBuilder;
import cn.skcks.docking.gb28181.sip.method.register.response.RegisterResponseBuilder;
import cn.skcks.docking.gb28181.sip.method.subscribe.request.SubscribeRequestBuilder;
import cn.skcks.docking.gb28181.sip.method.subscribe.response.SubscribeResponseBuilder;
import cn.skcks.docking.gb28181.sip.utils.MANSCDPUtils;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
import gov.nist.javax.sip.message.SIPResponse;
@ -51,6 +53,15 @@ public class RequestTest {
Request subscribeRequest = subscribeRequestBuilder.createSubscribeRequest(callId,
1, catalogQueryDTO.getCmdType(), MANSCDPUtils.toByteXml(catalogQueryDTO));
log.info("\n{}",subscribeRequest);
catalogQueryDTO = MANSCDPUtils.parse(subscribeRequest.getRawContent(), CatalogQueryDTO.class);
CatalogSubscribeResponseDTO catalogSubscribeResponseDTO = CatalogSubscribeResponseDTO.builder()
.sn(catalogQueryDTO.getSn())
.deviceId(catalogQueryDTO.getDeviceId())
.build();
SubscribeResponseBuilder subscribeResponseBuilder = SubscribeResponseBuilder.builder().build();
Response subscribeResponse = subscribeResponseBuilder.createSubscribeResponse(subscribeRequest, MANSCDPUtils.toByteXml(catalogSubscribeResponseDTO));
log.info("\n{}",subscribeResponse);
}
@Test