gpt4 book ai didi

java - 使用 JAIN SIP 的 SIP 注册请求

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:48 24 4
gpt4 key购买 nike

我尝试开发一个sipPhone作为学习结束的项目。现在我仍然坚持如何注册到Asterisk 1.8服务器并进行身份验证。我从here下载了这段代码:

package examples.authorization;

import gov.nist.javax.sip.Utils;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Properties;

import javax.sip.ClientTransaction;
import javax.sip.Dialog;
import javax.sip.DialogTerminatedEvent;
import javax.sip.IOExceptionEvent;
import javax.sip.ListeningPoint;
import javax.sip.RequestEvent;
import javax.sip.ResponseEvent;
import javax.sip.SipFactory;
import javax.sip.SipListener;
import javax.sip.SipProvider;
import javax.sip.SipStack;
import javax.sip.TimeoutEvent;
import javax.sip.TransactionTerminatedEvent;
import javax.sip.address.Address;
import javax.sip.address.AddressFactory;
import javax.sip.header.AuthorizationHeader;
import javax.sip.header.CSeqHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.ContactHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.ToHeader;
import javax.sip.header.ViaHeader;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import javax.sip.message.Response;


public class SipInit implements SipListener {


private String ip;
private SipFactory sipFactory;
private Properties properties;
private SipStack sipStack;
@SuppressWarnings("unused")
private MessageFactory messageFactory;
private HeaderFactory headerFactory;
private AddressFactory addressFactory;
private ListeningPoint listeningPoint;
private int port;
private String protocol;
private SipProvider sipProvider;
private String username;
private Address contactAddress;
@SuppressWarnings("unused")
private ContactHeader contactHeader;
private String server;
private char[] tag;
private Request request;
private String password;
private ClientTransaction inviteTid;
private Dialog dialog;

public void init() {
try {
// Get the local IP address.
this.ip = InetAddress.getLocalHost().getHostAddress();
// Create the SIP factory and set the path name.
this.sipFactory = SipFactory.getInstance();
this.sipFactory.setPathName("gov.nist");
// Create and set the SIP stack properties.
this.properties = new Properties();
this.properties.setProperty("javax.sip.STACK_NAME", "stack");
this.properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");



this.properties.setProperty(
"gov.nist.javax.sip.LOG_MESSAGE_CONTENT", "true");
this.properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
"mss-jsip-debuglog.txt");
this.properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
"mss-jsip-messages.xml");
// Create the SIP stack.
this.sipStack = this.sipFactory.createSipStack(this.properties);
// Create the SIP message factory.
this.messageFactory = this.sipFactory.createMessageFactory();
// Create the SIP header factory.
this.headerFactory = this.sipFactory.createHeaderFactory();
// Create the SIP address factory.
this.addressFactory = this.sipFactory.createAddressFactory();
// Create the SIP listening point and bind it to the local IP
// address, port and protocol.
this.listeningPoint = this.sipStack.createListeningPoint(this.ip,
this.port, this.protocol);
// Create the SIP provider.
this.sipProvider = this.sipStack
.createSipProvider(this.listeningPoint);
// Add our application as a SIP listener.
this.sipProvider.addSipListener(this);
// Create the contact address used for all SIP messages.
this.contactAddress = this.addressFactory.createAddress("sip:" + this.username + "@"
+ this.ip + ";transport=tcp");
// Create the contact header used for all SIP messages.
this.contactHeader = this.headerFactory
.createContactHeader(contactAddress);
// Display the local IP address and port in the text area.
} catch (Exception e) {
e.printStackTrace();
// If an error occurs, display an error message box and exit.
System.exit(-1);
}
}


public void register(Response response) {
Object logger;
try {
int cseq;
cseq++;
ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
ViaHeader viaHeader = this.headerFactory.createViaHeader(this.ip,
this.port, "tcp", null);
viaHeaders.add(viaHeader);
// The "Max-Forwards" header.
MaxForwardsHeader maxForwardsHeader = this.headerFactory
.createMaxForwardsHeader(70);
// The "Call-Id" header.
CallIdHeader callIdHeader = this.sipProvider.getNewCallId();
// The "CSeq" header.
@SuppressWarnings("deprecation")
CSeqHeader cSeqHeader = this.headerFactory.createCSeqHeader(cseq,
"REGISTER");

Address fromAddress = addressFactory.createAddress("sip:"
+ username + '@' + server);

FromHeader fromHeader = this.headerFactory.createFromHeader(
fromAddress, String.valueOf(this.tag));
// The "To" header.
ToHeader toHeader = this.headerFactory.createToHeader(fromAddress,
null);

// this.contactHeader = this.headerFactory
// .createContactHeader(contactAddress);

request = this.messageFactory.createRequest("REGISTER sip:"
+ server + " SIP/2.0\r\n\r\n");
request.addHeader(callIdHeader);
request.addHeader(cSeqHeader);
request.addHeader(fromHeader);
request.addHeader(toHeader);
request.addHeader(maxForwardsHeader);
request.addHeader(viaHeader);
request.addHeader(contactHeader);
if (response != null) {
boolean retry = true;
AuthorizationHeader authHeader = Utils.makeAuthHeader(headerFactory, response,
request, username, password);
request.addHeader(authHeader);
}
inviteTid = sipProvider.getNewClientTransaction(request);
// send the request out.
inviteTid.sendRequest();
this.dialog = inviteTid.getDialog();
// Send the request statelessly through the SIP provider.
// this.sipProvider.sendRequest(request);

// Display the message in the text area.
((Object) logger).debug("Request sent:\n" + request.toString() + "\n\n");
} catch (Exception e) {
// If an error occurred, display the error.
e.printStackTrace();
logger.debug("Request sent failed: " + e.getMessage() + "\n");
}
}

@Override
public void processDialogTerminated(DialogTerminatedEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void processIOException(IOExceptionEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void processRequest(RequestEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void processResponse(ResponseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void processTimeout(TimeoutEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void processTransactionTerminated(TransactionTerminatedEvent arg0) {
// TODO Auto-generated method stub

}


}

但是我遇到了这个错误:

The method makeAuthHeader(HeaderFactory, Response, Request, String, String) is undefined for the type Utils

请帮助我,谢谢。

最佳答案

以下代码片段可能会有所帮助 - (使用 JAIN-Sip 库 - https://github.com/RestComm/jain-sip )

package com.sip.light.client;

import java.text.ParseException;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TooManyListenersException;

import javax.sip.ClientTransaction;
import javax.sip.DialogTerminatedEvent;
import javax.sip.IOExceptionEvent;
import javax.sip.InvalidArgumentException;
import javax.sip.ListeningPoint;
import javax.sip.ObjectInUseException;
import javax.sip.PeerUnavailableException;
import javax.sip.RequestEvent;
import javax.sip.ResponseEvent;
import javax.sip.SipException;
import javax.sip.SipFactory;
import javax.sip.SipProvider;
import javax.sip.TimeoutEvent;
import javax.sip.Transaction;
import javax.sip.TransactionTerminatedEvent;
import javax.sip.TransportNotSupportedException;
import javax.sip.address.AddressFactory;
import javax.sip.header.ContactHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import javax.sip.message.Response;

import gov.nist.javax.sip.DialogTimeoutEvent;
import gov.nist.javax.sip.ListeningPointExt;
import gov.nist.javax.sip.SipListenerExt;
import gov.nist.javax.sip.SipStackExt;
import gov.nist.javax.sip.SipStackImpl;
import gov.nist.javax.sip.clientauthutils.AuthenticationHelper;
import gov.nist.javax.sip.clientauthutils.UserCredentials;
import gov.nist.javax.sip.stack.NioMessageProcessorFactory;
import gov.nist.javax.sip.stack.SIPTransactionStack;

public class SipClientLightSip implements SipListenerExt {
private SipStackExt mSipStack;
private HeaderFactory mHeaderFactory;
private String mPeerHostPort;

private AddressFactory mAddressFactory;
private MessageFactory mMessageFactory;
private ListeningPoint mUdpListeningPoint;
private SipProvider mSipProvider;
private int mUdpPort = 5082;
private String mProtocol = "UDP";
private String mIPAddress ="127.0.0.1"; //
// private String mIPAddress ="10.90.20.68"; //
private UserCredentials mUserCredentials;
private ContactHeader contactHeader;

@Override
public void processRequest(RequestEvent requestEvent) {
System.out.println("RequestEvent "+requestEvent.getServerTransaction().getDialog());
}

@Override
public void processResponse(ResponseEvent responseEvent) {
System.out.println("responseEvent.getResponse()" + responseEvent.getResponse());
if(responseEvent.getResponse().getStatusCode() == Response.UNAUTHORIZED){

AuthenticationHelper authenticationHelper =
mSipStack.getAuthenticationHelper((challengedTransaction, realm) -> mUserCredentials, mHeaderFactory);
try {

authenticationHelper.handleChallenge(responseEvent.getResponse(),responseEvent.getClientTransaction(),mSipProvider,2000).sendRequest();
} catch (SipException e) {
e.printStackTrace();
}
}else if(responseEvent.getResponse().getStatusCode() == Response.OK){
System.out.println("responseEvent.getResponse()" + responseEvent.getResponse());

}
}

@Override
public void processTimeout(TimeoutEvent timeoutEvent) {
Transaction transaction;
if (timeoutEvent.isServerTransaction()) {
transaction = timeoutEvent.getServerTransaction();
} else {
transaction = timeoutEvent.getClientTransaction();
System.out.println(timeoutEvent.getTimeout().getValue());


}
System.out.println("state = " + transaction.getState());
System.out.println("dialog = " + transaction.getDialog());
System.out.println("Transaction Time out");
}

@Override
public void processIOException(IOExceptionEvent exceptionEvent) {
System.out.println("Expcetion occured "+exceptionEvent.getPort());
}

@Override
public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {

if (transactionTerminatedEvent.isServerTransaction())
System.out.println("Transaction terminated event recieved"
+ transactionTerminatedEvent.getServerTransaction());
else {
System.out.println("Transaction terminated "
+ transactionTerminatedEvent.getClientTransaction());

//System.out.println(transactionTerminatedEvent.getClientTransaction().getRequest());

}
}

@Override
public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
System.out.println("processDialogTerminated" +dialogTerminatedEvent);
}


public void init() throws PeerUnavailableException, InvalidArgumentException, TransportNotSupportedException, ObjectInUseException, TooManyListenersException {
SipFactory sipFactory = null;
mSipStack = null;
sipFactory = SipFactory.getInstance();
sipFactory.setPathName("gov.nist");

Properties properties = new Properties();
//properties.setProperty("javax.sip.OUTBOUND_PROXY", "127.0.0.1:5060" + "/"+ "ws");
properties.setProperty("javax.sip.STACK_NAME", "SIP_CLIENT");

properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
properties.setProperty("gov.nist.javax.sip.DEBUG_LOG", "sip_client/out/log/debug.txt");
properties.setProperty("gov.nist.javax.sip.SERVER_LOG", "sip_client/out/log/server.txt");
//properties.setProperty("javax.sip.OUTBOUND_PROXY","127.0.0.1:5082/WS");
properties.setProperty("javax.sip.IP_ADDRESS",mIPAddress);
properties.setProperty("javax.sip.TRANSPORT",mProtocol);
//properties.setProperty("gov.nist.javax.sip.USE_TLS_GATEWAY","true");
properties.setProperty("gov.nist.javax.sip.TCP_POST_PARSING_THREAD_POOL_SIZE", "1");
properties.setProperty("javax.sip.AUTOMATIC_DIALOG_SUPPORT", "off");
properties.setProperty("gov.nist.javax.sip.AUTOMATIC_DIALOG_ERROR_HANDLING","false");
//properties.setProperty("gov.nist.javax.sip.IS_BACK_TO_BACK_USER_AGENT","true");
properties.setProperty("gov.nist.javax.sip.MESSAGE_PROCESSOR_FACTORY", NioMessageProcessorFactory.class.getName());
properties.setProperty("gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS","false");
mSipStack = (SipStackExt) sipFactory.createSipStack(properties);


mHeaderFactory = sipFactory.createHeaderFactory();
mAddressFactory = sipFactory.createAddressFactory();
mMessageFactory = sipFactory.createMessageFactory();
mUdpListeningPoint = mSipStack.createListeningPoint(mIPAddress, mUdpPort, mProtocol);

mSipProvider = mSipStack.createSipProvider(mUdpListeningPoint);
mSipProvider.addSipListener(this);

}

public void sendRegister(UserCredentials userCredentials,String serverIP,int serverPort) throws SipException, ParseException, InvalidArgumentException {

StringBuilder sb = new StringBuilder();

sb.append("REGISTER sip:domain@127.0.0.1:5060;method=GET?Host=127.0.0.1&Location=/&transport="+mProtocol+" SIP/2.0\r\n");
sb.append("CSeq: 1 REGISTER\r\n");
sb.append("To: <sip:piyush@domain:5082>\r\n");
sb.append("From: \"piyush\" <sip:piyush@domain:5082>;tag=o3qshhkifd\r\n");
sb.append("Call-ID: gth3knqa3t3n0bj42osph5\r\n");
sb.append("Max-Forwards: 69\r\n");
sb.append("Via: SIP/2.0/"+mProtocol+" n8020ls4d00s.invalid;branch=z9hG4bK8197727\r\n");
sb.append("Contact: <sip:6q93lp0l@n8020ls4d00s.invalid;transport="+mProtocol+">;+sip.ice;reg-id=1;+sip.instance=\"<urn:uuid:c8f8a859-5f68-48e4-88f4-49abf1ac036f>\";expires=600\r\n");

sb.append("Allow: INVITE,ACK,CANCEL,BYE,UPDATE,MESSAGE,OPTIONS\r\n");
sb.append("Content-Length: 0\r\n");
sb.append("Supported: path,gruu,outbound\r\n");
sb.append("Expires: 600\r\n");


System.out.println("Register request over websocket "+ request);




ClientTransaction ctx = mSipProvider.getNewClientTransaction(request);
ctx.sendRequest();
//timer.schedule(new MyTimerTask(), 1000, 3000);
}

@Override
public void processDialogTimeout(DialogTimeoutEvent arg0) {
// TODO Auto-generated method stub

}



}


/* Output as follows -

Register request over websocket REGISTER sip:domain@127.0.0.1:5060;method=GET?Host=127.0.0.1&Location=/&transport=UDP SIP/2.0
CSeq: 1 REGISTER
To: <sip:piyush@domain:5082>
From: "piyush" <sip:piyush@domain:5082>;tag=o3qshhkifd
Call-ID: gth3knqa3t3n0bj42osph5
Max-Forwards: 69
Via: SIP/2.0/UDP n8020ls4d00s.invalid;branch=z9hG4bK8197727
Contact: <sip:6q93lp0l@n8020ls4d00s.invalid;transport=UDP>;+sip.ice;reg-id=1;+sip.instance="<urn:uuid:c8f8a859-5f68-48e4-88f4-49abf1ac036f>";expires=600
Allow: INVITE,ACK,CANCEL,BYE,UPDATE,MESSAGE,OPTIONS
Supported: path,gruu,outbound
Expires: 600
Content-Length: 0


responseEvent.getResponse()SIP/2.0 401 Unauthorized
CSeq: 1 REGISTER
To: <sip:piyush@domain:5082>;tag=6e9d37d85ef64ab7aee1dfb44a2b1f94
From: "piyush" <sip:piyush@domain:5082>;tag=o3qshhkifd
Call-ID: gth3knqa3t3n0bj42osph5
Via: SIP/2.0/UDP n8020ls4d00s.invalid:5082;branch=z9hG4bK8197727
x-Error-Details: No auth header
WWW-Authenticate: Digest realm="domain",nonce="b97e036da02577c13598f26e4e3d27ec",qop="auth",algorithm=MD5,stale=false,opaque="00000001"
Content-Length: 0


responseEvent.getResponse()SIP/2.0 200 OK
CSeq: 2 REGISTER
To: <sip:piyush@domain:5082>;tag=68067566c18441ccb8a645c812aa23f1
From: "piyush" <sip:piyush@domain:5082>;tag=o3qshhkifd
Call-ID: gth3knqa3t3n0bj42osph5
Via: SIP/2.0/UDP n8020ls4d00s.invalid:5082;branch=z9hG4bK-37-d3e9f8fe24da7121a13553b1b7c0b54d
Contact: <sip:6q93lp0l@n8020ls4d00s.invalid;transport=UDP>;+sip.instance="<urn:uuid:c8f8a859-5f68-48e4-88f4-49abf1ac036f>";expires=599
Expires: 599
Content-Length: 0


responseEvent.getResponse()SIP/2.0 200 OK
CSeq: 2 REGISTER
To: <sip:piyush@domain:5082>;tag=68067566c18441ccb8a645c812aa23f1
From: "piyush" <sip:piyush@domain:5082>;tag=o3qshhkifd
Call-ID: gth3knqa3t3n0bj42osph5
Via: SIP/2.0/UDP n8020ls4d00s.invalid:5082;branch=z9hG4bK-37-d3e9f8fe24da7121a13553b1b7c0b54d
Contact: <sip:6q93lp0l@n8020ls4d00s.invalid;transport=UDP>;+sip.instance="<urn:uuid:c8f8a859-5f68-48e4-88f4-49abf1ac036f>";expires=599
Expires: 599
Content-Length: 0


Transaction terminated gov.nist.javax.sip.stack.SIPClientTransactionImpl@7c805a26
Transaction terminated gov.nist.javax.sip.stack.SIPClientTransactionImpl@5626892b

*/

关于java - 使用 JAIN SIP 的 SIP 注册请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23478466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com