添加 SipUtil UserAgent 以及可配置

SipBuilder 默认携带 SipUtil UserAgent 头
This commit is contained in:
shikong 2023-09-26 02:26:07 +08:00
parent e9fb357b95
commit d810236d62
6 changed files with 37 additions and 8 deletions

View File

@ -3,6 +3,7 @@ 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 cn.skcks.docking.gb28181.sip.utils.SipUtil;
import gov.nist.javax.sip.message.MessageFactoryImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@ -25,10 +26,13 @@ public class SipBuilder {
return SipFactory.getInstance();
}
public static UserAgentHeader userAgentHeader = SipUtil.getUserAgentHeader();
@SneakyThrows
public static MessageFactory getMessageFactory(){
MessageFactoryImpl messageFactory = (MessageFactoryImpl)getSipFactory().createMessageFactory();
messageFactory.setDefaultContentEncodingCharset(DEFAULT_CHARSET);
messageFactory.setDefaultUserAgentHeader(userAgentHeader);
log.debug("将使用 {} 编码 sip 消息", DEFAULT_CHARSET);
return messageFactory;
}

View File

@ -2,6 +2,7 @@ package cn.skcks.docking.gb28181.sip.request;
import cn.skcks.docking.gb28181.sip.generic.SipBuilder;
import cn.skcks.docking.gb28181.sip.generic.SipContentType;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
import lombok.SneakyThrows;
import javax.sip.address.SipURI;

View File

@ -2,6 +2,7 @@ package cn.skcks.docking.gb28181.sip.response;
import cn.skcks.docking.gb28181.sip.generic.SipBuilder;
import cn.skcks.docking.gb28181.sip.generic.SipContentType;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
import lombok.SneakyThrows;
import javax.sip.header.*;
@ -10,9 +11,10 @@ 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);
public static Response createResponse(int statusCode, Request request){
return SipBuilder.getMessageFactory().createResponse(statusCode, request);
}
@SneakyThrows

View File

@ -11,13 +11,11 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import javax.sip.PeerUnavailableException;
import javax.sip.SipFactory;
import javax.sip.header.FromHeader;
import javax.sip.header.Header;
import javax.sip.header.UserAgentHeader;
import javax.sip.message.Request;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
@ -26,6 +24,8 @@ import java.util.List;
public class SipUtil {
@Setter
public static String UserAgent = "GB28181-Docking-Platform";
@Setter
public static String UserAgentVersion = "0.0.1";
private static final char[] DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
public static String nanoId(){
@ -59,11 +59,12 @@ public class SipUtil {
return "z9hG4bK" + RandomStringUtils.randomNumeric(10);
}
public static UserAgentHeader createUserAgentHeader() throws PeerUnavailableException, ParseException {
List<String> agentParam = new ArrayList<>();
agentParam.add("GB28181-Docking-Platform");
@SneakyThrows
public static UserAgentHeader getUserAgentHeader(){
List<String> agentParam = new ArrayList<>(3);
agentParam.add(UserAgent);
agentParam.add(" ");
agentParam.add(StringUtils.replace(UserAgent,"-SNAPSHOT",""));
agentParam.add(StringUtils.replace(UserAgentVersion,"-SNAPSHOT",""));
return SipFactory.getInstance().createHeaderFactory().createUserAgentHeader(agentParam);
}

View File

@ -6,6 +6,7 @@ import cn.skcks.docking.gb28181.sip.header.XGBVerHeader;
import cn.skcks.docking.gb28181.sip.header.impl.XGBVerHeaderImpl;
import cn.skcks.docking.gb28181.sip.request.SipRequestBuilder;
import cn.skcks.docking.gb28181.sip.response.SipResponseBuilder;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@ -21,6 +22,8 @@ import java.util.List;
public class SipTest {
@Test
public void test() {
SipUtil.setUserAgent("GB28181-Docking-Platform Beta");
SipUtil.setUserAgentVersion("0.1.0");
// 发起方
String localIp = "127.0.0.1";
int localPort = 5060;

View File

@ -0,0 +1,18 @@
package cn.skcks.docking.gb28181.sip.process;
import cn.skcks.docking.gb28181.sip.generic.SipBuilder;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import javax.sip.header.UserAgentHeader;
@Slf4j
public class RequestTest {
@Test
void test(){
SipUtil.setUserAgentVersion("0.1.0");
UserAgentHeader userAgentHeader = SipBuilder.userAgentHeader;
log.info("\n{}",userAgentHeader);
}
}