尚未支持的功能 返回 501 Not Implemented 响应

This commit is contained in:
shikong 2023-09-13 17:17:49 +08:00
parent 2c973673d0
commit 2e8d5fc3f7
3 changed files with 42 additions and 4 deletions

View File

@ -1,8 +1,12 @@
package cn.skcks.docking.gb28181.mocking.core.sip.listener;
import cn.skcks.docking.gb28181.common.json.ResponseStatus;
import cn.skcks.docking.gb28181.core.sip.executor.DefaultSipExecutor;
import cn.skcks.docking.gb28181.core.sip.listener.SipListener;
import cn.skcks.docking.gb28181.core.sip.message.processor.MessageProcessor;
import cn.skcks.docking.gb28181.mocking.core.sip.response.SipResponseBuilder;
import cn.skcks.docking.gb28181.mocking.core.sip.sender.SipSender;
import gov.nist.javax.sip.message.SIPRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
@ -21,6 +25,7 @@ import java.util.concurrent.ConcurrentMap;
@Component
@Slf4j
public class SipListenerImpl implements SipListener {
private final SipSender sender;
private final ConcurrentMap<String, MessageProcessor> requestProcessor = new ConcurrentHashMap<>();
private final ConcurrentMap<String, MessageProcessor> responseProcessor = new ConcurrentHashMap<>();
@ -40,8 +45,14 @@ public class SipListenerImpl implements SipListener {
public void processRequest(RequestEvent requestEvent) {
String method = requestEvent.getRequest().getMethod();
log.debug("传入请求 method => {}", method);
Optional.ofNullable(requestProcessor.get(method)).ifPresent(processor -> {
Optional.ofNullable(requestProcessor.get(method)).ifPresentOrElse(processor -> {
processor.process(requestEvent);
},()->{
SIPRequest request = (SIPRequest)requestEvent.getRequest();
String senderIp = request.getLocalAddress().getHostAddress();
String transport = request.getTopmostViaHeader().getTransport();
Response response = SipResponseBuilder.response(request, Response.NOT_IMPLEMENTED, ResponseStatus.NOT_IMPLEMENTED.getMessage());
sender.sendResponse(senderIp, transport, (provider, ip, port)-> response);
});
}

View File

@ -4,17 +4,19 @@ import cn.skcks.docking.gb28181.core.sip.service.SipService;
import cn.skcks.docking.gb28181.mocking.config.sip.SipConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import javax.sip.ListeningPoint;
import javax.sip.SipException;
import javax.sip.SipProvider;
import javax.sip.message.Request;
import javax.sip.message.Response;
import java.util.List;
import java.util.Objects;
@Slf4j
@RequiredArgsConstructor
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@Component
public class SipSender {
private final SipService sipService;
@ -30,6 +32,28 @@ public class SipSender {
.toList();
}
public void sendResponse(SipProvider sipProvider, SendResponse response) {
log.info("{}", sipProvider);
ListeningPoint[] listeningPoints = sipProvider.getListeningPoints();
if (listeningPoints == null || listeningPoints.length == 0) {
log.error("发送响应失败, 未找到有效的监听地址");
return;
}
ListeningPoint listeningPoint = listeningPoints[0];
String ip = listeningPoint.getIPAddress();
int port = listeningPoint.getPort();
try {
sipProvider.sendResponse(response.build(sipProvider, ip, port));
} catch (SipException e) {
log.error("向{} {}:{} 发送响应失败, 异常: {}", ip, listeningPoint.getPort(), listeningPoint.getTransport(), e.getMessage());
}
}
public void sendResponse(String senderIp,String transport, SendResponse response) {
SipProvider sipProvider = getProvider(transport, senderIp);
sendResponse(sipProvider, response);
}
public void sendRequest(SendRequest request) {
getProviders().parallelStream().forEach(sipProvider -> {
log.info("{}", sipProvider);
@ -52,4 +76,8 @@ public class SipSender {
public interface SendRequest {
Request build(SipProvider provider, String ip, int port);
}
public interface SendResponse {
Response build(SipProvider provider, String ip, int port);
}
}

View File

@ -1,8 +1,8 @@
package cn.skcks.docking.gb28181.mocking.core.sip.service;
import cn.skcks.docking.gb28181.core.sip.listener.SipListener;
import cn.skcks.docking.gb28181.core.sip.message.parser.GbStringMsgParserFactory;
import cn.skcks.docking.gb28181.core.sip.properties.DefaultProperties;
import cn.skcks.docking.gb28181.core.sip.listener.SipListener;
import cn.skcks.docking.gb28181.core.sip.service.SipService;
import cn.skcks.docking.gb28181.mocking.config.sip.SipConfig;
import gov.nist.javax.sip.SipProviderImpl;
@ -13,7 +13,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import javax.sip.*;
import java.util.ArrayList;
import java.util.List;