SIPVIAHeader.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package bssoft.stack.sip;
  2. import java.util.StringTokenizer;
  3. public class SIPVIAHeader {
  4. public String header = null;
  5. public String headerValue = null;
  6. public String branch = null;
  7. public String received = null;
  8. public int rport = -1;
  9. public String ip = null;
  10. public int port = 0;
  11. public String tag = null;
  12. public boolean rportService = false;
  13. public boolean flag = false;
  14. public SIPVIAHeader(String message) {
  15. if (message != null && message.length() > 0) {
  16. this.rport = 0;
  17. this.received = null;
  18. int iS = message.indexOf("Via: ");
  19. int iE = 0;
  20. if (iS < 0) {
  21. iS = message.indexOf(String.valueOf(SIPStack.SIP_LINE_END) + "v: ");
  22. if (iS >= 0)
  23. iS += 2;
  24. }
  25. if (iS >= 0) {
  26. iE = message.indexOf(SIPStack.SIP_LINE_END, iS);
  27. if (iE > 0) {
  28. this.header = message.substring(iS, iE);
  29. if (this.header.startsWith("Via: ")) {
  30. this.headerValue = this.header.substring(5);
  31. } else if (this.header.startsWith("v: ")) {
  32. this.headerValue = this.header.substring(3);
  33. }
  34. }
  35. }
  36. if (this.headerValue != null && this.headerValue.length() > 0) {
  37. StringTokenizer st = new StringTokenizer(this.headerValue, ";", true);
  38. int tokenCount = 0;
  39. String token = null;
  40. while (st.hasMoreTokens()) {
  41. token = st.nextToken().trim();
  42. if (token != null && token.length() > 0 && token.compareTo(";") != 0) {
  43. tokenCount++;
  44. if (tokenCount == 1 && token.startsWith("SIP/")) {
  45. StringTokenizer tokenArray = new StringTokenizer(token, " ", true);
  46. int fieldCount = 0;
  47. while (tokenArray.hasMoreTokens()) {
  48. String field = tokenArray.nextToken().trim();
  49. if (field != null && field.length() > 0 && field.compareTo(" ") != 0) {
  50. fieldCount++;
  51. if (fieldCount == 2) {
  52. iS = field.indexOf(":");
  53. if (iS > 0) {
  54. this.port = Integer.parseInt(field.substring(iS + 1));
  55. this.ip = field.substring(0, iS);
  56. continue;
  57. }
  58. this.port = 5060;
  59. this.ip = field;
  60. }
  61. }
  62. }
  63. continue;
  64. }
  65. if (tokenCount != 1 && token.startsWith("branch=")) {
  66. this.branch = token.substring(7);
  67. continue;
  68. }
  69. if (tokenCount != 1 && token.compareTo("rport") == 0) {
  70. this.rportService = true;
  71. continue;
  72. }
  73. if (tokenCount != 1 && token.startsWith("rport=")) {
  74. this.rportService = true;
  75. this.rport = Integer.parseInt(token.substring(6).trim());
  76. continue;
  77. }
  78. if (tokenCount != 1 && token.startsWith("received=")) {
  79. this.rportService = true;
  80. this.received = token.substring(9).trim();
  81. }
  82. }
  83. }
  84. this.flag = true;
  85. }
  86. }
  87. }
  88. public String getReceived() {
  89. return this.received;
  90. }
  91. public int getRport() {
  92. return this.rport;
  93. }
  94. public String getAddress() {
  95. return String.valueOf(this.ip) + ":" + this.port;
  96. }
  97. public String getIp() {
  98. return this.ip;
  99. }
  100. public int getPort() {
  101. return this.port;
  102. }
  103. public String getBranch() {
  104. return this.branch;
  105. }
  106. }