📄 basereaderutils.java
字号:
/******************************************************************************** Product of NIST/ITL Advanced Networking Technologies Division (ANTD). ********************************************************************************/package gov.nist.examples.bps.reader;import java.util.*;import javax.sip.message.*; import javax.sip.header.*;import javax.sip.address.*;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/*** A few utilities that are used in various places by the stack.* This is used to convert byte arrays to hex strings etc. Generate* tags and branch identifiers and odds and ends.*/public class BaseReaderUtils { public static final String BRANCH_MAGIC_COOKIE = "z9hG4bK"; /** * to hex converter */ private static final char[] toHex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * convert an array of bytes to an hexadecimal string * @return a string * @param b bytes array to convert to a hexadecimal * string */ public static String toHexString(byte b[]) { int pos = 0; char[] c = new char[b.length*2]; for (int i=0; i< b.length; i++) { c[pos++] = toHex[(b[i] >> 4) & 0x0F]; c[pos++] = toHex[b[i] & 0x0f]; } return new String(c); } /** Generate a tag for a FROM header or TO header. Just return a * random 4 digit integer (should be enough to avoid any clashes!) * Tags only need to be unique within a call. * * @return a string that can be used as a tag parameter. */ public static String generateTag() { return new Integer((int)(Math.random() * 10000)).toString(); } /** Generate a cryptographically random identifier that can be used * to generate a branch identifier. * *@return a cryptographically random gloablly unique string that * can be used as a branch identifier. */ public static String generateBranchId() { String b = new Integer((int)(Math.random() * 10000)).toString() + System.currentTimeMillis(); try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte bid[] = messageDigest.digest(b.getBytes()); // cryptographically random string. // prepend with a magic cookie to indicate we // are bis09 compatible. return BRANCH_MAGIC_COOKIE + toHexString(bid); } catch ( NoSuchAlgorithmException e){ return null; } } public static URI getCleanUri(URI uri) { if (uri instanceof SipURI) { SipURI sipURI=(SipURI)uri.clone(); Iterator iterator=sipURI.getParameterNames(); while (iterator!=null && iterator.hasNext()) { String name=(String)iterator.next(); sipURI.removeParameter(name); } return sipURI; } else return uri; } public static String getKey(Message message,String header) { try{ URI uri=null; if (header.equals("From") ) { FromHeader fromHeader=(FromHeader)message.getHeader(FromHeader.NAME); Address fromAddress=fromHeader.getAddress(); uri=fromAddress.getURI(); } else if (header.equals("To") ) { ToHeader toHeader=(ToHeader)message.getHeader(ToHeader.NAME); Address toAddress=toHeader.getAddress(); uri=toAddress.getURI(); } else if (header.equals("Request-URI") ) { uri= ((Request)message).getRequestURI(); } else { return null; } // URI parameters MUST be removed: URI cleanedUri = getCleanUri(uri); String keyresult=cleanedUri.toString(); keyresult=keyresult.toLowerCase(); BaseReaderDebug.println("BaseReaderUtils, getKey(), the key is: " +keyresult); return keyresult; } catch(Exception e) { e.printStackTrace(); return null; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -