From a3d6573bbbc11e70365c4269899d460aa5e20530 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Sat, 23 Sep 2023 23:06:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E5=B0=81=E8=A3=85=20SipReque?= =?UTF-8?q?stBuilder=20=E4=B8=8E=20SipResponseBuilder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gb28181/constant/GB28181Constant.java | 7 +++ .../gb28181/sip/generic/SipBuilder.java | 34 ++++++++++++- .../gb28181/sip/generic/SipContentType.java | 9 ++++ .../sip/request/SipRequestBuilder.java | 32 +++++++++++- .../sip/response/SipResponseBuilder.java | 33 +++++++++++- .../cn/skcks/docking/gb28181/sip/SipTest.java | 50 +++++++++++++++++-- 6 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 gb28181-sip/src/main/java/cn/skcks/docking/gb28181/constant/GB28181Constant.java create mode 100644 gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipContentType.java diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/constant/GB28181Constant.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/constant/GB28181Constant.java new file mode 100644 index 0000000..66729be --- /dev/null +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/constant/GB28181Constant.java @@ -0,0 +1,7 @@ +package cn.skcks.docking.gb28181.constant; + +public class GB28181Constant { + public static final String TIME_ZONE = "Asia/Shanghai"; + public static final String CHARSET = "GB2312"; + public static final String GEO_COORD_SYS = "WGS84"; +} diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipBuilder.java index 02d049a..f7eb92a 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipBuilder.java @@ -1,8 +1,11 @@ package cn.skcks.docking.gb28181.sip.generic; +import cn.skcks.docking.gb28181.constant.GB28181Constant; import cn.skcks.docking.gb28181.sip.header.XGBVerHeader; import cn.skcks.docking.gb28181.sip.header.impl.XGBVerHeaderImpl; +import gov.nist.javax.sip.message.MessageFactoryImpl; import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.sip.SipFactory; @@ -11,18 +14,32 @@ import javax.sip.address.AddressFactory; import javax.sip.address.SipURI; import javax.sip.header.*; import javax.sip.message.MessageFactory; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collections; import java.util.List; +@Slf4j public class SipBuilder { + public static String DEFAULT_CHARSET = GB28181Constant.CHARSET; public static SipFactory getSipFactory(){ return SipFactory.getInstance(); } @SneakyThrows public static MessageFactory getMessageFactory(){ - return getSipFactory().createMessageFactory(); + MessageFactoryImpl messageFactory = (MessageFactoryImpl)getSipFactory().createMessageFactory(); + messageFactory.setDefaultContentEncodingCharset(DEFAULT_CHARSET); + log.debug("将使用 {} 编码 sip 消息", DEFAULT_CHARSET); + return messageFactory; + } + + @SneakyThrows + public static MessageFactory getMessageFactory(String charset){ + MessageFactoryImpl messageFactory = (MessageFactoryImpl)getSipFactory().createMessageFactory(); + messageFactory.setDefaultContentEncodingCharset(charset); + log.debug("将使用 {} 编码 sip 消息", charset); + return messageFactory; } @SneakyThrows @@ -55,11 +72,21 @@ public class SipBuilder { return getHeaderFactory().createToHeader(toAddress, toTag); } + @SneakyThrows + public static ToHeader createToHeader(Address toAddress) { + return createToHeader(toAddress, null); + } + @SneakyThrows public static FromHeader createFromHeader(Address fromAddress, String fromTag) { return getHeaderFactory().createFromHeader(fromAddress, fromTag); } + @SneakyThrows + public static FromHeader createFromHeader(Address fromAddress) { + return createFromHeader(fromAddress, null); + } + @SneakyThrows public static CSeqHeader createCSeqHeader(long cSeq, String method){ return getHeaderFactory().createCSeqHeader(cSeq, method); @@ -108,6 +135,11 @@ public class SipBuilder { return getHeaderFactory().createUserAgentHeader(product); } + @SneakyThrows + public static CallIdHeader createCallIdHeader(String callId){ + return getHeaderFactory().createCallIdHeader(callId); + } + @SneakyThrows public static XGBVerHeader createXGBVerHeader(int m,int n){ return new XGBVerHeaderImpl(m,n); diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipContentType.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipContentType.java new file mode 100644 index 0000000..a20cc5b --- /dev/null +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipContentType.java @@ -0,0 +1,9 @@ +package cn.skcks.docking.gb28181.sip.generic; + +import javax.sip.header.ContentTypeHeader; + +public class SipContentType { + public static final ContentTypeHeader XML = SipBuilder.createContentTypeHeader("Application", "MANSCDP+xml"); + public static final ContentTypeHeader SDP = SipBuilder.createContentTypeHeader("Application", "SDP"); + public static final ContentTypeHeader RTSP = SipBuilder.createContentTypeHeader("Application", "MANSRTSP"); +} diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/request/SipRequestBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/request/SipRequestBuilder.java index 2caf882..7dea79f 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/request/SipRequestBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/request/SipRequestBuilder.java @@ -1,7 +1,37 @@ package cn.skcks.docking.gb28181.sip.request; -import javax.sip.SipFactory; +import cn.skcks.docking.gb28181.sip.generic.SipBuilder; +import cn.skcks.docking.gb28181.sip.generic.SipContentType; +import lombok.SneakyThrows; + +import javax.sip.address.SipURI; +import javax.sip.header.*; +import javax.sip.message.Request; +import java.util.List; public class SipRequestBuilder { + @SneakyThrows + public static Request createRequest(SipURI requestURI, String method, CallIdHeader callId, CSeqHeader cSeq, + FromHeader from, ToHeader to, List via, MaxForwardsHeader maxForwards){ + return SipBuilder.getMessageFactory().createRequest(requestURI, method,callId,cSeq,from,to,via,maxForwards); + } + @SneakyThrows + public static Request createRequest(SipURI requestURI, String method, CallIdHeader callId, CSeqHeader cSeq, + FromHeader from, ToHeader to, List via, MaxForwardsHeader maxForwards, + ContentTypeHeader contentType, T content) { + return SipBuilder.getMessageFactory().createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards, contentType, content); + } + + @SneakyThrows + public static Request createXmlRequest(SipURI requestURI, String method, CallIdHeader callId, CSeqHeader cSeq, + FromHeader from, ToHeader to, List via, MaxForwardsHeader maxForwards, T content) { + return SipBuilder.getMessageFactory().createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards, SipContentType.XML, content); + } + + @SneakyThrows + public static Request createXmlRequest(SipURI requestURI, String method, CallIdHeader callId, CSeqHeader cSeq, + FromHeader from, ToHeader to, List via, MaxForwardsHeader maxForwards, T content, String charset) { + return SipBuilder.getMessageFactory(charset).createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards, SipContentType.XML, content); + } } diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/response/SipResponseBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/response/SipResponseBuilder.java index 83d8ec1..59564a5 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/response/SipResponseBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/response/SipResponseBuilder.java @@ -1,7 +1,38 @@ package cn.skcks.docking.gb28181.sip.response; -import javax.sip.SipFactory; +import cn.skcks.docking.gb28181.sip.generic.SipBuilder; +import cn.skcks.docking.gb28181.sip.generic.SipContentType; +import lombok.SneakyThrows; + +import javax.sip.header.*; +import javax.sip.message.Request; +import javax.sip.message.Response; +import java.util.List; public class SipResponseBuilder { + @SneakyThrows + public static Response createResponse(int statusCode, Request request){ + return SipBuilder.getMessageFactory().createResponse(statusCode,request); + } + @SneakyThrows + public static Response createResponse(int statusCode, CallIdHeader callId, CSeqHeader cSeq, + FromHeader from, ToHeader to, List via, MaxForwardsHeader maxForwards){ + return SipBuilder.getMessageFactory().createResponse(statusCode,callId,cSeq,from,to,via,maxForwards); + } + + @SneakyThrows + public static Response createResponse(int statusCode, Request request, ContentTypeHeader contentType, T content){ + return SipBuilder.getMessageFactory().createResponse(statusCode,request,contentType,content); + } + + @SneakyThrows + public static Response createXmlResponse(int statusCode, Request request, T content){ + return SipBuilder.getMessageFactory().createResponse(statusCode,request, SipContentType.XML,content); + } + + @SneakyThrows + public static Response createXmlResponse(int statusCode, Request request, T content, String charset) { + return SipBuilder.getMessageFactory(charset).createResponse(statusCode, request, SipContentType.XML, content); + } } 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 0edc093..48e5d00 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 @@ -1,23 +1,65 @@ package cn.skcks.docking.gb28181.sip; import cn.skcks.docking.gb28181.sip.generic.SipBuilder; +import cn.skcks.docking.gb28181.sip.request.SipRequestBuilder; +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.*; +import javax.sip.message.Request; +import javax.sip.message.Response; +import java.text.MessageFormat; +import java.util.List; @Slf4j public class SipTest { @Test - public void test(){ + public void test() { + // 发起方 String localIp = "127.0.0.1"; int localPort = 5060; - String localId = "12345678901234567890"; - String localHostAddress = SipBuilder.createHostAddress(localIp,localPort); - SipURI localSipUri = SipBuilder.createSipURI(localId, localHostAddress); + String localId = "00000000000000000001"; + String localHostAddress = SipBuilder.createHostAddress(localIp, localPort); + SipURI localSipUri = SipBuilder.createSipURI(localId, localHostAddress); Address localAddress = SipBuilder.createAddress(localSipUri); + FromHeader fromHeader = SipBuilder.createFromHeader(localAddress, "000001"); log.info("{}", localAddress); + + // 接收方 + String remoteIp = "10.10.10.200"; + int remotePort = 5060; + String remoteId = "00000000000000000002"; + + String remoteHostAddress = SipBuilder.createHostAddress(remoteIp, remotePort); + SipURI remoteSipUri = SipBuilder.createSipURI(remoteId, remoteHostAddress); + Address remoteAddress = SipBuilder.createAddress(remoteSipUri); + ToHeader toHeader = SipBuilder.createToHeader(remoteAddress, null); + log.info("{}", remoteAddress); + + // 目的地 + ContactHeader contactHeader = SipBuilder.createContactHeader(remoteAddress); + + // 公共部分 + String method = Request.REGISTER; + CSeqHeader cSeqHeader = SipBuilder.createCSeqHeader(1, method); + // CallId @ 后面可以是地址也可以为 域 id + CallIdHeader callIdHeader = SipBuilder.createCallIdHeader(MessageFormat.format("{0}@{1}", "123456", localIp)); + List viaHeaders = SipBuilder.createViaHeaders(localIp, localPort, ListeningPoint.UDP, "z9hG4bK" + "0000000001"); + MaxForwardsHeader maxForwardsHeader = SipBuilder.createMaxForwardsHeader(70); + + // 创建请求 + Request request = SipRequestBuilder.createRequest(remoteSipUri, method, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwardsHeader); + request.addHeader(contactHeader); + + log.info("构造请求\n{}", request); + + // 创建响应 + Response response = SipResponseBuilder.createResponse(Response.OK, request); + log.info("构造响应\n{}", response); } }