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 aae4139..df263c2 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 @@ -4,6 +4,7 @@ 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.Setter; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -14,6 +15,8 @@ import javax.sip.address.AddressFactory; import javax.sip.address.SipURI; import javax.sip.header.*; import javax.sip.message.MessageFactory; +import javax.sip.message.Request; +import javax.sip.message.Response; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; @@ -21,7 +24,11 @@ import java.util.List; @Slf4j public class SipBuilder { + @Setter public static String DEFAULT_CHARSET = StandardCharsets.UTF_8.name(); + @Setter + public static XGBVerHeader GB_VERSION = XGBVerHeaderImpl.GB28181_2016; + public static SipFactory getSipFactory(){ return SipFactory.getInstance(); } @@ -158,4 +165,18 @@ public class SipBuilder { public static XGBVerHeader createXGBVerHeader(int m,int n){ return new XGBVerHeaderImpl(m,n); } + + public static Response addHeaders(Response response,Header ...headers){ + for (Header header : headers) { + response.addHeader(header); + } + return response; + } + + public static Request addHeaders(Request request, Header ...headers){ + for (Header header : headers) { + request.addHeader(header); + } + return request; + } } diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipRequestBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipRequestBuilder.java index 41b9c78..4409c3e 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipRequestBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipRequestBuilder.java @@ -1,8 +1,5 @@ package cn.skcks.docking.gb28181.sip.generic; -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; @@ -10,29 +7,39 @@ import javax.sip.header.*; import javax.sip.message.Request; import java.util.List; +import static cn.skcks.docking.gb28181.sip.generic.SipBuilder.GB_VERSION; + 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); + FromHeader from, ToHeader to, List via, MaxForwardsHeader maxForwards) { + return SipBuilder.addHeaders( + SipBuilder.getMessageFactory().createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards), + GB_VERSION); } @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); + return SipBuilder.addHeaders( + SipBuilder.getMessageFactory().createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards, contentType, content), + GB_VERSION); } @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); + FromHeader from, ToHeader to, List via, MaxForwardsHeader maxForwards, T content) { + return SipBuilder.addHeaders( + SipBuilder.getMessageFactory().createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards, SipContentType.XML, content), + GB_VERSION); } @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); + return SipBuilder.addHeaders( + SipBuilder.getMessageFactory(charset).createRequest(requestURI, method, callId, cSeq, from, to, via, maxForwards, SipContentType.XML, content), + GB_VERSION); } } diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipResponseBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipResponseBuilder.java index 1074dcb..b8aa09f 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipResponseBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipResponseBuilder.java @@ -7,31 +7,45 @@ import javax.sip.message.Request; import javax.sip.message.Response; import java.util.List; +import static cn.skcks.docking.gb28181.sip.generic.SipBuilder.GB_VERSION; + public class SipResponseBuilder { @SneakyThrows - public static Response createResponse(int statusCode, Request request){ - return SipBuilder.getMessageFactory().createResponse(statusCode, request); + public static Response createResponse(int statusCode, Request request) { + return SipBuilder.addHeaders( + SipBuilder.getMessageFactory().createResponse(statusCode, request), + GB_VERSION); } @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); + FromHeader from, ToHeader to, List via, MaxForwardsHeader maxForwards) { + return SipBuilder.addHeaders( + SipBuilder.getMessageFactory().createResponse(statusCode, callId, cSeq, from, to, via, maxForwards), + GB_VERSION); } @SneakyThrows - public static Response createResponse(int statusCode, Request request, ContentTypeHeader contentType, T content){ - return SipBuilder.getMessageFactory().createResponse(statusCode,request,contentType,content); + public static Response createResponse(int statusCode, Request request, ContentTypeHeader contentType, T content) { + return SipBuilder.addHeaders( + SipBuilder.getMessageFactory().createResponse(statusCode, request, contentType, content), + GB_VERSION + ); } @SneakyThrows - public static Response createXmlResponse(int statusCode, Request request, T content){ - return SipBuilder.getMessageFactory().createResponse(statusCode,request, SipContentType.XML,content); + public static Response createXmlResponse(int statusCode, Request request, T content) { + return SipBuilder.addHeaders( + SipBuilder.getMessageFactory().createResponse(statusCode, request, SipContentType.XML, content), + GB_VERSION + ); } @SneakyThrows public static Response createXmlResponse(int statusCode, Request request, T content, String charset) { - return SipBuilder.getMessageFactory(charset).createResponse(statusCode, request, SipContentType.XML, content); + return SipBuilder.addHeaders( + SipBuilder.getMessageFactory(charset).createResponse(statusCode, request, SipContentType.XML, content), + GB_VERSION); } }