⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sipregister.java

📁 Java SIP
💻 JAVA
字号:
/* * This file was derived from libdissipate, an open source SIP library. The original file  * was modified on 1/23/2001.  Please see * http://www.div8.net/dissipate for more information. * * Copyright (c) 2000 Billy Biggs <bbiggs@div8.net> * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. *  * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public * License for more details. *  * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB.  If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * *//** * Class representing a SIP registration server and a users status with it. *  * @author S.R.Jones * @version 1 *//* * This code has been generated using C2J++ * C2J++ is based on Chris Laffra's C2J (laffra@watson.ibm.com) * Read general disclaimer distributed with C2J++ before using this code * For information about C2J++, send mail to Ilya_Tilevich@ibi.com */package org.mitre.jsip;import org.mitre.jsip.event.*;public class SipRegister extends Object{    /**     * SipRegister     * @param user     * @param serveruri     */    public SipRegister(SipUser user, SipUri serveruri)    {	rstate = NotConnected;	regop = NoRegOpPending;	authtype = BasicAuthenticationRequired;		regcall = new SipCall( user, null, SipCall.RegisterCall );	regcall.setSubject( "Register Call" );		setServerUri( serveruri );    }        /**     * getRegisterState     * @return RegisterState     */    public int getRegisterState()    { return rstate; }        /**     * getAuthenticationType     * @return AuthType     */    public int getAuthenticationType()    { return authtype; }    /**     * Sets the authentication required as detailed in a response message     */    public void setAuthentication( SipMessage respMessage ) 	throws RegisterException    {	regop = NoRegOpPending;	localStatusUpdated( respMessage );    }		    /**     * getServerUri     * @return SipUri     */    public SipUri getServerUri()    { return regserver; }        /**     * setServerUri     * @param newregserver     */    public void setServerUri(SipUri newregserver)    {	regserver = newregserver;    }    /**     * Request registration using already provided username and password     */    public void requestRegister() {	System.out.println("***** Trying to register again as " + username );	requestRegister( username, password );    }    /**     * Requests registration from the proxy.     */    public void requestRegister( String username, String password )    {	// keep these if we need them again	System.err.println("Register: " + username + " with " + password);	this.username = username;  	this.password = password; 		if ( regop == NoRegOpPending ) {	    regop = RegRequest;	    	    if ( rstate == AuthenticationRequired ) {		System.out.println( "SipRegister: Authentication required" );				if ( authtype == DigestAuthenticationRequired ) {		    authresponse = Sip.getDigestResponse( username, password, "REGISTER",  regserver.uri(), authstr );		} else if ( authtype == BasicAuthenticationRequired ) {		    authresponse = Sip.getBasicResponse( username, password );		} else if ( authtype == ProxyDigestAuthenticationRequired ) {		    proxyauthresponse = Sip.getDigestResponse( username, password, "REGISTER", regserver.uri(), proxyauthstr );		} else if ( authtype == ProxyBasicAuthenticationRequired ) {		    proxyauthresponse = Sip.getBasicResponse( username, password );		}	    }	    	    System.out.println( "SipRegister: Auth is " + authresponse );	    System.out.println( "SipRegister: Proxy auth is " + proxyauthresponse );	    	    	    rstate = TryingServer;	    curtrans = regcall.newRegister( regserver, false, authresponse, proxyauthresponse, null, null );	    	}	//   localStatusUpdated();    }    /**     * requestRegister     * @param dummy_     */    public void requestRegister(SipUri dummy_)    {	localStatusUpdated();    }    /**     * requestClearRegistration     */    public void requestClearRegistration()    {	if ( regop == NoRegOpPending ) {	    rstate = Disconnecting;	    regop = RegClear;	    	    curtrans = regcall.newRegister( regserver, true );	    //	    connect( curtrans, SIGNAL( statusUpdated() ), this, SLOT( localStatusUpdated() ) );	}	//statusUpdated();	localStatusUpdated();    }    /**     * getRegisterCall     * @return SipCall *     */    public SipCall  getRegisterCall()    { return regcall; }    /**     * getAuthRealm     * @return String     */    public String getAuthRealm()    {	String realm;		if( authstr.indexOf( "realm=\"" ) >= 0 ) {	    realm = authstr.substring( authstr.indexOf( "realm=\"" ) + 7 );	    realm = realm.substring( 0, realm.indexOf( '\"' ) );	    return realm;	}		return null;    }        /**     * getProxyAuthRealm     * @return String     */    public String getProxyAuthRealm()    {	String realm;		if( proxyauthstr.indexOf( "realm=\"" ) >= 0 ) {	    realm = proxyauthstr.substring( authstr.indexOf( "realm=\"" ) + 7 );	    System.out.println( "SipRegister: Realm is " + realm );	    realm = realm.substring( 0, realm.indexOf( '\"' ) );	    return realm;	}		return null;    }        /**     * statusUpdated     */    //private void statusUpdated() {    //	SipCall.statusUpdated();    //}    /**     * Update the register status depending on the response message received     */    private void localStatusUpdated( SipMessage resp ) throws RegisterException {	String authstrtemp;	// switch on the response code	switch ( resp.getStatus().getCode() ) {	case ( 200 ):	    switch ( regop ) {	    case RegRequest:		rstate = Connected;		break;	    case RegClear:		rstate = NotConnected;		break;	    default:		break;	    }	    break;	case ( 401 ):	    System.out.println("previous rstate is " + rstate );	    if ( rstate == AuthenticationRequired ) {		// we tried and failed! Let's throw an exception		throw new RegisterException( "Unauthorized username/password" );	    }	    rstate = AuthenticationRequired;	    	    authstr = resp.getHeaderData( SipHeader.WWW_Authenticate );	    	    authstrtemp = authstr.toLowerCase();	    	    if( authstrtemp.indexOf( "digest" ) >= 0 ) {		authtype = DigestAuthenticationRequired;	    } else {		authtype = BasicAuthenticationRequired;	    }	    break;	case ( 407 ):	    if ( rstate == AuthenticationRequired ) {		// we tried and failed again		throw new RegisterException( "Unauthorized Proxy username/password" );	    }	    rstate = AuthenticationRequired;	    	    proxyauthstr = curtrans.getFinalProxyAuthString();	    System.out.println( "SipRegister: Proxy auth string: " + proxyauthstr );	    	    authstrtemp = proxyauthstr.toLowerCase();	    	    if( authstrtemp.indexOf( "digest" ) >= 0 ) {		authtype = ProxyDigestAuthenticationRequired;	    } else {		authtype = ProxyBasicAuthenticationRequired;	    }	    break;	default:	    switch ( regop ) {	    case RegRequest:		rstate = NotConnected;		break;	    case RegClear:		rstate = Connected;		break;	    default:		break;	    }	}    }    /**     * localStatusUpdated     */    private void localStatusUpdated()    {	System.out.println("REGCALLLISTENER in lSU: status is " + 			   curtrans.getStatus().getCode());	String authstrtemp;		SipStatus status = curtrans.getStatus();	if( ( status != null ) && ( status.getCode() >= 200 ) ) { // final response	    	    if( curtrans.getStatus().getCode() == 200 ) {				switch ( regop ) {		case RegRequest:		    rstate = Connected;		    break;		case RegClear:		    rstate = NotConnected;		    break;		default:		    break;		}			    } else if( curtrans.getStatus().getCode() == 401 ) {				rstate = AuthenticationRequired;				authstr = curtrans.getFinalWWWAuthString();				authstrtemp = authstr.toLowerCase();				if( authstrtemp.indexOf( "digest" ) >= 0 ) {		    authtype = DigestAuthenticationRequired;		} else {		    authtype = BasicAuthenticationRequired;		}			    } else if( curtrans.getStatus().getCode() == 407 ) {				rstate = AuthenticationRequired;				proxyauthstr = curtrans.getFinalProxyAuthString();		System.out.println( "SipRegister: Proxy auth string: " + proxyauthstr );				authstrtemp = proxyauthstr.toLowerCase();				if( authstrtemp.indexOf( "digest" ) >= 0 ) {		    authtype = ProxyDigestAuthenticationRequired;		} else {		    authtype = ProxyBasicAuthenticationRequired;		}			    } else {				switch ( regop ) {		case RegRequest:		    rstate = NotConnected;		    break;		case RegClear:		    rstate = Connected;		    break;		default:		    break;		}			    }	}		regop = NoRegOpPending;	//	statusUpdated();    }     // class variables    static final int NotConnected = 0;    static final int TryingServer = 1;    static final int AuthenticationRequired = 2;    static final int Connected = 3;    static final int Disconnecting = 4;    static final int BasicAuthenticationRequired = 0;    static final int DigestAuthenticationRequired = 1;    static final int ProxyBasicAuthenticationRequired = 2;    static final int ProxyDigestAuthenticationRequired = 3;    static final int NoRegOpPending = 0;    static final int RegRequest = 1;    static final int RegClear = 2;    private SipCall   regcall;    private SipTransaction   curtrans;    private int regop;    private String username;    private String password;    private CallListener regCallListener;    int rstate;    int authtype;    SipUri regserver;    String authstr;    String proxyauthstr;    String authresponse;    String proxyauthresponse;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -