|  | @@ -11,7 +11,9 @@ import javax.sip.address.Address;
 | 
	
		
			
				|  |  |  import javax.sip.address.SipURI;
 | 
	
		
			
				|  |  |  import javax.sip.header.*;
 | 
	
		
			
				|  |  |  import javax.sip.message.Request;
 | 
	
		
			
				|  |  | +import javax.validation.constraints.NotNull;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Qualifier;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Component;
 | 
	
	
		
			
				|  | @@ -19,6 +21,7 @@ import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  |  import com.genersoft.iot.vmp.conf.SipConfig;
 | 
	
		
			
				|  |  |  import com.genersoft.iot.vmp.gb28181.bean.Device;
 | 
	
		
			
				|  |  |  import com.genersoft.iot.vmp.gb28181.bean.Host;
 | 
	
		
			
				|  |  | +import org.springframework.util.DigestUtils;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * @Description:摄像头命令request创造器 TODO 冗余代码太多待优化
 | 
	
	
		
			
				|  | @@ -168,4 +171,73 @@ public class SIPRequestHeaderProvider {
 | 
	
		
			
				|  |  |  		request.setContent(content, contentTypeHeader);
 | 
	
		
			
				|  |  |  		return request;
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	public Request createRegisterRequest(@NotNull ParentPlatform platform, String fromTag, String viaTag) throws ParseException, InvalidArgumentException, PeerUnavailableException {
 | 
	
		
			
				|  |  | +		Request request = null;
 | 
	
		
			
				|  |  | +		String sipAddress = sipConfig.getSipIp() + ":" + sipConfig.getSipPort();
 | 
	
		
			
				|  |  | +		//请求行
 | 
	
		
			
				|  |  | +		SipURI requestLine = sipFactory.createAddressFactory().createSipURI(platform.getDeviceGBId(),
 | 
	
		
			
				|  |  | +				platform.getServerIP() + ":" + platform.getServerPort());
 | 
	
		
			
				|  |  | +		//via
 | 
	
		
			
				|  |  | +		ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
 | 
	
		
			
				|  |  | +		ViaHeader viaHeader = sipFactory.createHeaderFactory().createViaHeader(platform.getServerIP(), platform.getServerPort(), platform.getTransport(), viaTag);
 | 
	
		
			
				|  |  | +		viaHeader.setRPort();
 | 
	
		
			
				|  |  | +		viaHeaders.add(viaHeader);
 | 
	
		
			
				|  |  | +		//from
 | 
	
		
			
				|  |  | +		SipURI fromSipURI = sipFactory.createAddressFactory().createSipURI(platform.getDeviceGBId(),sipAddress);
 | 
	
		
			
				|  |  | +		Address fromAddress = sipFactory.createAddressFactory().createAddress(fromSipURI);
 | 
	
		
			
				|  |  | +		FromHeader fromHeader = sipFactory.createHeaderFactory().createFromHeader(fromAddress, fromTag);
 | 
	
		
			
				|  |  | +		//to
 | 
	
		
			
				|  |  | +		SipURI toSipURI = sipFactory.createAddressFactory().createSipURI(platform.getDeviceGBId(),sipAddress);
 | 
	
		
			
				|  |  | +		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);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		//ceq
 | 
	
		
			
				|  |  | +		CSeqHeader cSeqHeader = sipFactory.createHeaderFactory().createCSeqHeader(1L, Request.REGISTER);
 | 
	
		
			
				|  |  | +		request = sipFactory.createMessageFactory().createRequest(requestLine, Request.REGISTER, callIdHeader,
 | 
	
		
			
				|  |  | +				cSeqHeader,fromHeader, toHeader, viaHeaders, maxForwards);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		Address concatAddress = sipFactory.createAddressFactory().createAddress(sipFactory.createAddressFactory()
 | 
	
		
			
				|  |  | +				.createSipURI(platform.getDeviceGBId(), sipAddress));
 | 
	
		
			
				|  |  | +		request.addHeader(sipFactory.createHeaderFactory().createContactHeader(concatAddress));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return request;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	public Request createRegisterRequest(@NotNull ParentPlatform parentPlatform, String fromTag, String viaTag,
 | 
	
		
			
				|  |  | +										 String callId, String realm, String nonce, String scheme) throws ParseException, PeerUnavailableException, InvalidArgumentException {
 | 
	
		
			
				|  |  | +		Request registerRequest = createRegisterRequest(parentPlatform, fromTag, viaTag);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		CallIdHeader callIdHeader = (CallIdHeader)registerRequest.getHeader(CallIdHeader.NAME);
 | 
	
		
			
				|  |  | +		callIdHeader.setCallId(callId);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		String uri = "sip:" + parentPlatform.getServerGBId() +
 | 
	
		
			
				|  |  | +				"@" + parentPlatform.getServerIP() +
 | 
	
		
			
				|  |  | +				":" + parentPlatform.getServerPort();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		String HA1 = DigestUtils.md5DigestAsHex((parentPlatform.getDeviceGBId() + ":" + realm + ":" + parentPlatform.getPassword()).getBytes());
 | 
	
		
			
				|  |  | +		String HA2=DigestUtils.md5DigestAsHex((Request.REGISTER + ":" + uri).getBytes());
 | 
	
		
			
				|  |  | +		String RESPONSE = DigestUtils.md5DigestAsHex((HA1 + ":" + nonce + ":" +  HA2).getBytes());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		String authorizationHeaderContent = scheme + " username=\"" + parentPlatform.getDeviceGBId() + "\", " + "realm=\""
 | 
	
		
			
				|  |  | +				+ realm + "\", uri=\"" + uri  + "\", response=\"" + RESPONSE + "\", nonce=\""
 | 
	
		
			
				|  |  | +				+ nonce + "\"";
 | 
	
		
			
				|  |  | +		AuthorizationHeader authorizationHeader = sipFactory.createHeaderFactory().createAuthorizationHeader(authorizationHeaderContent);
 | 
	
		
			
				|  |  | +		registerRequest.addHeader(authorizationHeader);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		return registerRequest;
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  |  }
 |