添加 GBDateHeader

This commit is contained in:
shikong 2023-09-27 00:57:31 +08:00
parent 74cd2d2048
commit ad72b02371
4 changed files with 51 additions and 3 deletions

View File

@ -19,6 +19,7 @@ import javax.sip.message.Request;
import javax.sip.message.Response;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
@ -156,6 +157,10 @@ public class SipBuilder {
return getHeaderFactory().createExpiresHeader(expires);
}
public static DateHeader createDateHeader(Calendar date){
return getHeaderFactory().createDateHeader(date);
}
@SneakyThrows
public static EventHeader createExpiresHeader(String event){
return getHeaderFactory().createEventHeader(event);

View File

@ -0,0 +1,32 @@
package cn.skcks.docking.gb28181.sip.header;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import gov.nist.javax.sip.header.SIPHeader;
import lombok.*;
import javax.sip.header.Header;
import java.util.Calendar;
@EqualsAndHashCode(callSuper = true)
public class GBDateHeader extends SIPHeader implements Header{
public static final String NAME = "Date";
@Getter
@Setter
private Calendar date;
public GBDateHeader() {
super(NAME);
}
public GBDateHeader(Calendar date) {
super(NAME);
this.date = date;
}
@Override
protected StringBuilder encodeBody(StringBuilder buffer) {
return buffer.append(DateUtil.format(date.getTime(), DatePattern.UTC_SIMPLE_MS_FORMAT));
}
}

View File

@ -1,7 +1,9 @@
package cn.skcks.docking.gb28181.sip.method.register.response;
import cn.hutool.core.date.DateUtil;
import cn.skcks.docking.gb28181.sip.generic.SipBuilder;
import cn.skcks.docking.gb28181.sip.generic.SipResponseBuilder;
import cn.skcks.docking.gb28181.sip.header.GBDateHeader;
import cn.skcks.docking.gb28181.sip.method.register.RegisterBuilder;
import cn.skcks.docking.gb28181.sip.utils.DigestAuthenticationHelper;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
@ -36,14 +38,14 @@ public class RegisterResponseBuilder extends RegisterBuilder {
return SipBuilder.addHeaders(
SipResponseBuilder.createResponse(Response.OK, request),
sipRequest.getContactHeader(),
sipRequest.getExpires());
sipRequest.getExpires(),
new GBDateHeader(DateUtil.calendar()));
}
public Response createAuthorzatioinResponse(Request request, String domain, String password){
SIPRequest sipRequest = (SIPRequest) request;
Authorization authorization = sipRequest.getAuthorization();
if(authorization == null){
String realm = SipUtil.nanoId();
WWWAuthenticateHeader wwwAuthenticateHeader = DigestAuthenticationHelper.generateChallenge(domain);
return SipBuilder.addHeaders(
SipResponseBuilder.createResponse(Response.UNAUTHORIZED, request),
@ -58,6 +60,7 @@ public class RegisterResponseBuilder extends RegisterBuilder {
return SipBuilder.addHeaders(
SipResponseBuilder.createResponse(Response.OK, request),
sipRequest.getContactHeader(),
sipRequest.getExpires());
sipRequest.getExpires(),
new GBDateHeader(DateUtil.calendar()));
}
}

View File

@ -1,7 +1,10 @@
package cn.skcks.docking.gb28181.sip;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.skcks.docking.gb28181.constant.GB28181Constant;
import cn.skcks.docking.gb28181.sip.generic.SipBuilder;
import cn.skcks.docking.gb28181.sip.header.GBDateHeader;
import cn.skcks.docking.gb28181.sip.header.XGBVerHeader;
import cn.skcks.docking.gb28181.sip.header.impl.XGBVerHeaderImpl;
import cn.skcks.docking.gb28181.sip.generic.SipRequestBuilder;
@ -22,6 +25,7 @@ import javax.sip.header.*;
import javax.sip.message.Request;
import javax.sip.message.Response;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.List;
@Slf4j
@ -44,6 +48,7 @@ public class SipTest {
sipStack.deleteSipProvider(sipProvider);
sipStack.deleteListeningPoint(listeningPoint);
}
@Test
public void test() {
SipUtil.setUserAgent("GB28181-Docking-Platform Beta");
@ -87,10 +92,13 @@ public class SipTest {
request.addHeader(contactHeader);
request.addHeader(expiresHeader);
request.addHeader(XGBVerHeaderImpl.GB28181_2016);
request.addHeader(new GBDateHeader(Calendar.getInstance()));
log.info("构造请求\n{}", request);
XGBVerHeaderImpl xgbVerHeader = (XGBVerHeaderImpl) request.getHeader(XGBVerHeader.NAME);
log.info("协议版本 {}", xgbVerHeader);
GBDateHeader gbDateHeader = (GBDateHeader) request.getHeader(GBDateHeader.NAME);
log.info("服务器时间 {}", gbDateHeader);
// 创建响应
Response response = SipResponseBuilder.createResponse(Response.OK, request);