📄 sipuri.java
字号:
public int getPortNumber() { return port; } /** * hasTransportParam * @return bool */ public boolean hasTransportParam() { return hastransparam; } /** * setTransportParam * @param t */ public void setTransportParam(int t) { if ( t == NoTransportParam ) { hastransparam = false; } else { hastransparam = true; transparam = t; } } /** * hasUserParam * @return bool */ public boolean hasUserParam() { return hasuserparam; } /** * setUserParam * @param u */ public void setUserParam(int u) { if ( u == NoUserParam ) { hasuserparam = false; } else { hasuserparam = true; userparam = u; } } public int getUserParam() { return userparam;} /** * hasMethodParam * @return bool */ public boolean hasMethodParam() { return hasmethodparam; } /** * setMethodParam * @param m */ public void setMethodParam(int m) { if ( m == Sip.BadMethod ) { hasmethodparam = false; } else { hasmethodparam = true; meth = m; } } /** * hasTtlParam * @return bool */ public boolean hasTtlParam() { return hasttlparam; } /** * setTtl * @param t */ public void setTtl(int t) { ttl = t; } public int getTtlParam() { return ttl;} /** * hasMaddrParam * @return bool */ public boolean hasMaddrParam() { return hasmaddrparam; } /** * getMaddrParam * @return String */ public String getMaddrParam() { return maddrhostname; } /** * setMaddrParam * @param newmaddr */ public void setMaddrParam(String newmaddr) { if( newmaddr != null ) { maddrhostname = newmaddr; hasmaddrparam = true; } else { hasmaddrparam = false; maddrhostname = null; } } /** * hasTag * @return bool */ public boolean hasTag() { return hastag; } /** * equals */ public boolean equals(SipUri uri) { if ( !isValid() || !uri.isValid() ) return false; if ( (username.equals(uri.getUsername()) ) && ( password.equals(uri.getPassword() ) ) && ( hostname.equals(uri.getHostname() ) ) && ( port == uri.getPortNumber() ) ) { return true; } return false; } // class variables private String protocol = "sip"; private String fullname; private boolean hasuserinfo; private String username; private boolean haspassword; private String password; private int port; private boolean isvalid; private boolean hastransparam; private int transparam; private boolean hasuserparam; private int userparam; private boolean hasmethodparam; private int meth; private boolean hasttlparam; private int ttl; private boolean hasmaddrparam; private String maddrhostname; private boolean hastag; private boolean hasport; private String tag; private String hostname; /** * clear */ private void clear() { isvalid = true; hasuserinfo = false; haspassword = false; port = getSipPort(); hastransparam = false; hasuserparam = false; hasmethodparam = false; hasttlparam = false; ttl = 0; hasmaddrparam = false; hastag = false; hasport = false; // items in equals() that could cause problems username = ""; hostname = ""; password = ""; } /** * parseUri * @param parseinput */ protected void parseUri(String parseinput) { String inputline; String userinfo; String hostport; String parameters; isvalid = false; inputline = parseinput.trim(); // Squeeze out blah<uri>blah if( inputline.indexOf( '<' ) >= 0 ) { // Validity check (possibly bogus) if( inputline.indexOf( '>' ) == -1 ) { return; } // Parse out the squeezed uri String beforeangle = inputline.substring( 0, inputline.indexOf( '>' ) ); parseUri( beforeangle.substring( beforeangle.indexOf( '<' ) + 1 )); // Parse the fullname, if any setFullname( inputline.substring( 0, inputline.indexOf( '<' ) ) ); // Parse the parameters, if any parseParameters( inputline.substring( inputline.indexOf( '>' ) + 1 ) ); return; } // Take out the protocol prefix if it's there if( inputline.toLowerCase().startsWith( "sip:" ) || inputline.toLowerCase().startsWith( "im:" ) ) { int colonPos = inputline.indexOf( ':' ); protocol = inputline.substring( 0, colonPos ); inputline = inputline.substring( colonPos + 1 ); } // If we have user info if( inputline.indexOf( '@' ) >= 0 ) { userinfo = inputline.substring( 0, inputline.indexOf( '@' ) ); if( userinfo.indexOf( ':' ) >= 0 ) { setUsername( userinfo.substring( 0, userinfo.indexOf( ':' ) ) ); setPassword( userinfo.substring( userinfo.indexOf( ':' ) + 1 ) ); } else { setUsername( userinfo ); } inputline = inputline.substring(inputline.indexOf( '@' ) + 1 ); } // If we have any parameters if( inputline.indexOf( ';' ) >= 0 ) { hostport = inputline.substring( 0, inputline.indexOf( ';' ) ).trim(); inputline = inputline.substring( inputline.indexOf( ';' ) ); } else if( inputline.indexOf( '?' ) >= 0 ) { hostport = inputline.substring( 0, inputline.indexOf( '?' ) ).trim(); inputline = inputline.substring( inputline.indexOf( '?' ) + 1); } else { hostport = inputline; inputline = ""; // *** LOOK AT *** } if( hostport.indexOf( ':' ) >= 0 ) { setHostname( hostport.substring( 0, hostport.indexOf( ':' ) ) ); try { String sPortNum = hostport.substring( hostport.indexOf( ':' ) + 1 ); int portNum = Integer.parseInt(sPortNum); setPortNumber( portNum ); hasport = true; } catch (NumberFormatException nfe) { System.err.println("Couldn't parse port number, setting to standard value"); setPortNumber( getSipPort() ); } } else { setHostname( hostport ); setPortNumber( getSipPort() ); } if( inputline.indexOf( '?' ) >= 0 ) { parameters = inputline.substring( 0, inputline.indexOf( '?' ) ); inputline = inputline.substring( inputline.indexOf( '?' ) + 1 ); } else { parameters = inputline; inputline = null; } // Parse the parameters at the end parseParameters( parameters ); // If we make it this far isvalid = true; } /** * parseParameters * @param parseinput */ private void parseParameters(String parseinput) { String curparm; String parmname; String parmvalue; String parameters; // NOTE: Calling this function will never change validity of the URI parameters = parseinput.trim(); while( parameters.startsWith(";") ) { curparm = parameters.substring( 1 ).trim(); int location = parameters.indexOf(";", 1); if ( location > 0 ) parameters = parameters.substring( 1, location ); else parameters = parameters.substring( 1 ); parmname = curparm.substring( 0, curparm.indexOf( '=' ) ).toLowerCase(); parmvalue = curparm.substring( curparm.indexOf( '=' ) + 1 ).trim(); // Check if the URI has a tag if( parmname.equals( "tag" ) ) { hastag = true; tag = parmvalue; } // maddr param if( parmname.equals( "maddr" ) ) { hasmaddrparam = true; maddrhostname = parmvalue.toLowerCase(); } // ttl param else if( parmname.equals( "ttl" ) ) { hasttlparam = true; try { ttl = Integer.parseInt(parmvalue); } catch (NumberFormatException nfe) { System.err.println("Error formatting ttl, set to default value"); ttl = DEFAULT_TTL; } } // transport param else if( parmname.equals( "transport" ) ) { if( parmvalue.toLowerCase() == "udp" ) { transparam = UDP; hastransparam = true; } else if( parmvalue.toLowerCase() == "tcp" ) { transparam = TCP; hastransparam = true; } } } } /** * Test method for SipUri */ public static void main(String[] args) { for (int i = 0; i < args.length; i++) { SipUri sipUri = new SipUri(args[i]); // see what we got System.out.println("Fullname is " + sipUri.getFullname()); System.out.println("Username is " + sipUri.getUsername()); System.out.println("Hostname is " + sipUri.getHostname()); System.out.println("Port is " + sipUri.getPortNumber()); if ( sipUri.hasTag() ) System.out.println("Tag is " + sipUri.getTag()); if ( sipUri.hasMaddrParam() ) System.out.println("Multicast address is " + sipUri.getMaddrParam()); if ( sipUri.hasTtlParam() ) System.out.println("TTL is " + sipUri.getTtlParam()); if ( sipUri.hasUserParam() ) System.out.println("User parameter is " + SipUri.getUserParamString(sipUri.getUserParam())); if ( sipUri.hasTransportParam() ) System.out.println("Transport parameter is " + SipUri.getTransportParamString(sipUri.getUserParam())); if ( sipUri.hasPassword() ) System.out.println("Password is " + sipUri.getPassword()); System.out.println("I would write this uri as:"); System.out.println(sipUri.uri()); } System.exit(0); } // class variables // UserParam static final int Phone = 0; static final int IP = 1; static final int NoUserParam = 2; // TransportParam public static final int UDP = 0; public static final int TCP = 1; public static final int NoTransportParam = 2; static final int DEFAULT_TTL = 15;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -