简单封装 SipRequestBuilder 与 SipResponseBuilder

This commit is contained in:
shikong 2023-09-23 23:06:03 +08:00
parent 49ea15db92
commit a3d6573bbb
6 changed files with 158 additions and 7 deletions

View File

@ -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";
}

View File

@ -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);

View File

@ -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");
}

View File

@ -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<ViaHeader> via, MaxForwardsHeader maxForwards){
return SipBuilder.getMessageFactory().createRequest(requestURI, method,callId,cSeq,from,to,via,maxForwards);
}
@SneakyThrows
public static <T> Request createRequest(SipURI requestURI, String method, CallIdHeader callId, CSeqHeader cSeq,
FromHeader from, ToHeader to, List<ViaHeader> via, MaxForwardsHeader maxForwards,
ContentTypeHeader contentType, T content) {
return SipBuilder.getMessageFactory().createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards, contentType, content);
}
@SneakyThrows
public static <T> Request createXmlRequest(SipURI requestURI, String method, CallIdHeader callId, CSeqHeader cSeq,
FromHeader from, ToHeader to, List<ViaHeader> via, MaxForwardsHeader maxForwards, T content) {
return SipBuilder.getMessageFactory().createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards, SipContentType.XML, content);
}
@SneakyThrows
public static <T> Request createXmlRequest(SipURI requestURI, String method, CallIdHeader callId, CSeqHeader cSeq,
FromHeader from, ToHeader to, List<ViaHeader> via, MaxForwardsHeader maxForwards, T content, String charset) {
return SipBuilder.getMessageFactory(charset).createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards, SipContentType.XML, content);
}
}

View File

@ -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 <T> 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<ViaHeader> via, MaxForwardsHeader maxForwards){
return SipBuilder.getMessageFactory().createResponse(statusCode,callId,cSeq,from,to,via,maxForwards);
}
@SneakyThrows
public static <T> Response createResponse(int statusCode, Request request, ContentTypeHeader contentType, T content){
return SipBuilder.getMessageFactory().createResponse(statusCode,request,contentType,content);
}
@SneakyThrows
public static <T> Response createXmlResponse(int statusCode, Request request, T content){
return SipBuilder.getMessageFactory().createResponse(statusCode,request, SipContentType.XML,content);
}
@SneakyThrows
public static <T> Response createXmlResponse(int statusCode, Request request, T content, String charset) {
return SipBuilder.getMessageFactory(charset).createResponse(statusCode, request, SipContentType.XML, content);
}
}

View File

@ -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<ViaHeader> 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);
}
}