|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.SipConfig; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; | 
|---|
|  |  |  | import gov.nist.javax.sip.message.MessageFactoryImpl; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Qualifier; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | import org.springframework.util.DigestUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SipFactory sipFactory; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | @Qualifier(value="tcpSipProvider") | 
|---|
|  |  |  | private SipProvider tcpSipProvider; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | @Qualifier(value="udpSipProvider") | 
|---|
|  |  |  | private SipProvider udpSipProvider; | 
|---|
|  |  |  | //   @Autowired | 
|---|
|  |  |  | //   @Qualifier(value="tcpSipProvider") | 
|---|
|  |  |  | //   private SipProvider tcpSipProvider; | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //   @Autowired | 
|---|
|  |  |  | //   @Qualifier(value="udpSipProvider") | 
|---|
|  |  |  | //   private SipProvider udpSipProvider; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Request createKeetpaliveMessageRequest(ParentPlatform parentPlatform, String content, String viaTag, String fromTag, String toTag) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | public Request createKeetpaliveMessageRequest(ParentPlatform parentPlatform, String content, String viaTag, String fromTag, String toTag, CallIdHeader callIdHeader) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | Request request = null; | 
|---|
|  |  |  | // sipuri | 
|---|
|  |  |  | SipURI requestURI = sipFactory.createAddressFactory().createSipURI(parentPlatform.getServerGBId(), parentPlatform.getServerIP() + ":" + parentPlatform.getServerPort()); | 
|---|
|  |  |  | 
|---|
|  |  |  | SipURI toSipURI = sipFactory.createAddressFactory().createSipURI(parentPlatform.getServerGBId(), parentPlatform.getServerIP() + ":" + parentPlatform.getServerPort() ); | 
|---|
|  |  |  | Address toAddress = sipFactory.createAddressFactory().createAddress(toSipURI); | 
|---|
|  |  |  | ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress, toTag); | 
|---|
|  |  |  | // callid | 
|---|
|  |  |  | CallIdHeader callIdHeader = parentPlatform.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId() | 
|---|
|  |  |  | : udpSipProvider.getNewCallId(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // Forwards | 
|---|
|  |  |  | MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70); | 
|---|
|  |  |  | // ceq | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Request createRegisterRequest(@NotNull ParentPlatform platform, long CSeq, String fromTag, String viaTag) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | public Request createRegisterRequest(@NotNull ParentPlatform platform, long CSeq, String fromTag, String viaTag, CallIdHeader callIdHeader) throws ParseException, InvalidArgumentException, PeerUnavailableException { | 
|---|
|  |  |  | Request request = null; | 
|---|
|  |  |  | String sipAddress = sipConfig.getSipIp() + ":" + sipConfig.getSipPort(); | 
|---|
|  |  |  | //请求行 | 
|---|
|  |  |  | 
|---|
|  |  |  | Address toAddress = sipFactory.createAddressFactory().createAddress(toSipURI); | 
|---|
|  |  |  | ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress,null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //callid | 
|---|
|  |  |  | CallIdHeader callIdHeader = null; | 
|---|
|  |  |  | if(platform.getTransport().equals("TCP")) { | 
|---|
|  |  |  | callIdHeader = tcpSipProvider.getNewCallId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(platform.getTransport().equals("UDP")) { | 
|---|
|  |  |  | callIdHeader = udpSipProvider.getNewCallId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //Forwards | 
|---|
|  |  |  | MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Request createRegisterRequest(@NotNull ParentPlatform parentPlatform, String fromTag, String viaTag, | 
|---|
|  |  |  | String callId, WWWAuthenticateHeader www ) throws ParseException, PeerUnavailableException, InvalidArgumentException { | 
|---|
|  |  |  | Request registerRequest = createRegisterRequest(parentPlatform, 2L, fromTag, viaTag); | 
|---|
|  |  |  | String callId, WWWAuthenticateHeader www , CallIdHeader callIdHeader) throws ParseException, PeerUnavailableException, InvalidArgumentException { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Request registerRequest = createRegisterRequest(parentPlatform, 2L, fromTag, viaTag, callIdHeader); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String realm = www.getRealm(); | 
|---|
|  |  |  | String nonce = www.getNonce(); | 
|---|
|  |  |  | 
|---|
|  |  |  | // qop 保护质量 包含auth(默认的)和auth-int(增加了报文完整性检测)两种策略 | 
|---|
|  |  |  | String qop = www.getQop(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | CallIdHeader callIdHeader = (CallIdHeader)registerRequest.getHeader(CallIdHeader.NAME); | 
|---|
|  |  |  | callIdHeader.setCallId(callId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | SipURI requestURI = sipFactory.createAddressFactory().createSipURI(parentPlatform.getServerGBId(), parentPlatform.getServerIP() + ":" + parentPlatform.getServerPort()); | 
|---|
|  |  |  | String cNonce = null; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Request createMessageRequest(ParentPlatform parentPlatform, String content, String fromTag) throws PeerUnavailableException, ParseException, InvalidArgumentException { | 
|---|
|  |  |  | public Request createMessageRequest(ParentPlatform parentPlatform, String content, String fromTag, CallIdHeader callIdHeader) throws PeerUnavailableException, ParseException, InvalidArgumentException { | 
|---|
|  |  |  | Request request = null; | 
|---|
|  |  |  | // sipuri | 
|---|
|  |  |  | SipURI requestURI = sipFactory.createAddressFactory().createSipURI(parentPlatform.getServerGBId(), parentPlatform.getServerIP()+ ":" + parentPlatform.getServerPort()); | 
|---|
|  |  |  | 
|---|
|  |  |  | SipURI toSipURI = sipFactory.createAddressFactory().createSipURI(parentPlatform.getDeviceGBId(), parentPlatform.getServerGBDomain()); | 
|---|
|  |  |  | Address toAddress = sipFactory.createAddressFactory().createAddress(toSipURI); | 
|---|
|  |  |  | ToHeader toHeader = sipFactory.createHeaderFactory().createToHeader(toAddress, null); | 
|---|
|  |  |  | // callid | 
|---|
|  |  |  | CallIdHeader callIdHeader = parentPlatform.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId() | 
|---|
|  |  |  | : udpSipProvider.getNewCallId(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // Forwards | 
|---|
|  |  |  | MaxForwardsHeader maxForwards = sipFactory.createHeaderFactory().createMaxForwardsHeader(70); | 
|---|
|  |  |  | // ceq | 
|---|
|  |  |  | CSeqHeader cSeqHeader = sipFactory.createHeaderFactory().createCSeqHeader(1L, Request.MESSAGE); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | request = sipFactory.createMessageFactory().createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, | 
|---|
|  |  |  | MessageFactoryImpl messageFactory = (MessageFactoryImpl) sipFactory.createMessageFactory(); | 
|---|
|  |  |  | // 设置编码, 防止中文乱码 | 
|---|
|  |  |  | messageFactory.setDefaultContentEncodingCharset("gb2312"); | 
|---|
|  |  |  | request = messageFactory.createRequest(requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, | 
|---|
|  |  |  | toHeader, viaHeaders, maxForwards); | 
|---|
|  |  |  | ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("APPLICATION", "MANSCDP+xml"); | 
|---|
|  |  |  | request.setContent(content, contentTypeHeader); | 
|---|