PageRenderTime 58ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/protocols/jain-mgcp/stack/src/main/java/org/mobicents/protocols/mgcp/parser/Utils.java

http://mobicents.googlecode.com/
Java | 1852 lines | 1374 code | 248 blank | 230 comment | 396 complexity | 66af7af2990b7756932d4b0e8a0c8f1e MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. /*
  23. * File Name : Utils.java
  24. *
  25. * The JAIN MGCP API implementaion.
  26. *
  27. * The source code contained in this file is in in the public domain.
  28. * It can be used in any project or product without prior permission,
  29. * license or royalty payments. There is NO WARRANTY OF ANY KIND,
  30. * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION,
  31. * THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  32. * AND DATA ACCURACY. We do not warrant or make any representations
  33. * regarding the use of the software or the results thereof, including
  34. * but not limited to the correctness, accuracy, reliability or
  35. * usefulness of the software.
  36. */
  37. package org.mobicents.protocols.mgcp.parser;
  38. import jain.protocol.ip.mgcp.message.parms.Bandwidth;
  39. import jain.protocol.ip.mgcp.message.parms.BearerInformation;
  40. import jain.protocol.ip.mgcp.message.parms.CapabilityValue;
  41. import jain.protocol.ip.mgcp.message.parms.CompressionAlgorithm;
  42. import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
  43. import jain.protocol.ip.mgcp.message.parms.ConnectionMode;
  44. import jain.protocol.ip.mgcp.message.parms.ConnectionParm;
  45. import jain.protocol.ip.mgcp.message.parms.DigitMap;
  46. import jain.protocol.ip.mgcp.message.parms.EchoCancellation;
  47. import jain.protocol.ip.mgcp.message.parms.EmbeddedRequest;
  48. import jain.protocol.ip.mgcp.message.parms.EncryptionMethod;
  49. import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
  50. import jain.protocol.ip.mgcp.message.parms.EventName;
  51. import jain.protocol.ip.mgcp.message.parms.ExtendedConnectionParm;
  52. import jain.protocol.ip.mgcp.message.parms.GainControl;
  53. import jain.protocol.ip.mgcp.message.parms.InfoCode;
  54. import jain.protocol.ip.mgcp.message.parms.LocalOptVal;
  55. import jain.protocol.ip.mgcp.message.parms.LocalOptionExtension;
  56. import jain.protocol.ip.mgcp.message.parms.LocalOptionValue;
  57. import jain.protocol.ip.mgcp.message.parms.NotificationRequestParms;
  58. import jain.protocol.ip.mgcp.message.parms.NotifiedEntity;
  59. import jain.protocol.ip.mgcp.message.parms.PacketizationPeriod;
  60. import jain.protocol.ip.mgcp.message.parms.ReasonCode;
  61. import jain.protocol.ip.mgcp.message.parms.RegularConnectionParm;
  62. import jain.protocol.ip.mgcp.message.parms.RequestedAction;
  63. import jain.protocol.ip.mgcp.message.parms.RequestedEvent;
  64. import jain.protocol.ip.mgcp.message.parms.ResourceReservation;
  65. import jain.protocol.ip.mgcp.message.parms.RestartMethod;
  66. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  67. import jain.protocol.ip.mgcp.message.parms.SilenceSuppression;
  68. import jain.protocol.ip.mgcp.message.parms.SupportedModes;
  69. import jain.protocol.ip.mgcp.message.parms.SupportedPackages;
  70. import jain.protocol.ip.mgcp.message.parms.TypeOfNetwork;
  71. import jain.protocol.ip.mgcp.message.parms.TypeOfService;
  72. import jain.protocol.ip.mgcp.pkg.MgcpEvent;
  73. import jain.protocol.ip.mgcp.pkg.PackageName;
  74. import java.text.ParseException;
  75. import java.util.ArrayList;
  76. import java.util.List;
  77. import java.util.regex.Matcher;
  78. import java.util.regex.Pattern;
  79. import org.apache.log4j.Logger;
  80. import org.mobicents.protocols.mgcp.jain.pkg.AUMgcpEvent;
  81. import org.mobicents.protocols.mgcp.jain.pkg.AUPackage;
  82. /**
  83. *
  84. * @author Oleg Kulikov
  85. * @author Pavel Mitrenko
  86. * @author Amit Bhayani
  87. */
  88. public class Utils {
  89. private static final Logger logger = Logger.getLogger(Utils.class);
  90. private static final char AMPERSAND = '@';
  91. private static final String REGEX_EMBEDDED_COMMAND = "\\(E\\(.*\\)\\)";
  92. private static final Pattern pattern = Pattern.compile(REGEX_EMBEDDED_COMMAND);
  93. private static final Pattern commaPattern = Pattern.compile(",");
  94. private static final Pattern equalsPattern = Pattern.compile("=");
  95. private static final Pattern colonPattern = Pattern.compile(":");
  96. private static final Pattern semiColonPattern = Pattern.compile(";");
  97. private static final Pattern dashPattern = Pattern.compile("-");
  98. PackageName au = AUPackage.AU;
  99. MgcpEvent rfc2897pa = AUMgcpEvent.aupa;
  100. StringBuilder decdReqEveStrBuilder = new StringBuilder();
  101. List<String> list = new ArrayList<String>();
  102. List<String> decdEventName = new ArrayList<String>();
  103. List<String> decdReqtEventList = new ArrayList<String>();
  104. /** Creates a new instance of Utils */
  105. public Utils() {
  106. }
  107. public String[] splitStringBySpace(String value) {
  108. // return spacePattern.split(value, 0);
  109. try {
  110. String[] token = null;
  111. this.split(' ', value, decdEventName);
  112. token = new String[decdEventName.size()];
  113. decdEventName.toArray(token);
  114. return token;
  115. } finally {
  116. decdEventName.clear();
  117. }
  118. }
  119. /**
  120. * Create ConnectionMode object from given text value.
  121. *
  122. * @param mode
  123. * the text value of the connection mode.
  124. * @return the ConnectionMode object.
  125. */
  126. public ConnectionMode decodeConnectionMode(String mode) {
  127. if (mode.equals("sendrecv")) {
  128. return ConnectionMode.SendRecv;
  129. } else if (mode.equalsIgnoreCase("sendonly")) {
  130. return ConnectionMode.SendOnly;
  131. } else if (mode.equalsIgnoreCase("recvonly")) {
  132. return ConnectionMode.RecvOnly;
  133. } else if (mode.equalsIgnoreCase("confrnce")) {
  134. return ConnectionMode.Confrnce;
  135. } else if (mode.equalsIgnoreCase("conttest")) {
  136. return ConnectionMode.Conttest;
  137. } else if (mode.equalsIgnoreCase("data")) {
  138. return ConnectionMode.Data;
  139. } else if (mode.equalsIgnoreCase("loopback")) {
  140. return ConnectionMode.Loopback;
  141. } else if (mode.equalsIgnoreCase("netwloop")) {
  142. return ConnectionMode.Netwloop;
  143. } else if (mode.equalsIgnoreCase("netwtest")) {
  144. return ConnectionMode.Netwtest;
  145. }
  146. return ConnectionMode.Inactive;
  147. }
  148. public String encodeBearerInformation(BearerInformation bearerInformation) {
  149. StringBuffer s = new StringBuffer("e:");
  150. if (BearerInformation.ENC_METHOD_A_LAW == bearerInformation.getEncodingMethod()) {
  151. s.append("A");
  152. } else if (BearerInformation.ENC_METHOD_MU_LAW == bearerInformation.getEncodingMethod()) {
  153. s.append("mu");
  154. }
  155. return s.toString();
  156. }
  157. /**
  158. * Create BearerInformation object from given text.
  159. *
  160. * @param text
  161. * the text value of the object.
  162. * @return BearerInformation object.
  163. */
  164. public BearerInformation decodeBearerInformation(String text) throws ParseException {
  165. // BearerInformation =BearerAttribute 0*(","0*(WSP)BearerAttribute)
  166. // BearerAttribute =("e"":"BearerEncoding)
  167. // (BearerExtensionName [":"BearerExtensionValue ])
  168. // BearerExtensionName =PackageLCOExtensionName
  169. // BearerExtensionValue =LocalOptionExtensionValue
  170. // BearerEncoding ="A"/"mu"
  171. text = text.toLowerCase();
  172. if (!text.startsWith("e:")) {
  173. throw new ParseException("Bearer extensions not supported", 0);
  174. }
  175. text = text.substring(text.indexOf(":") + 1).trim();
  176. if (text.equals("a")) {
  177. return BearerInformation.EncMethod_A_Law;
  178. } else if (text.equals("mu")) {
  179. return BearerInformation.EncMethod_mu_Law;
  180. } else {
  181. throw new ParseException("Unknown value for BearerInformation: " + text, 0);
  182. }
  183. }
  184. /**
  185. * Create local connection options from given text.
  186. *
  187. * @param text
  188. * the text value.
  189. * @return array of LocalOptionValue objects.
  190. */
  191. public LocalOptionValue[] decodeLocalOptionValueList(String text) throws ParseException {
  192. // LocalConnectionOptions =LocalOptionValue 0*(WSP)
  193. // 0*(","0*(WSP)LocalOptionValue 0*(WSP))
  194. String[] tokens = commaPattern.split(text, 0);
  195. LocalOptionValue[] options = new LocalOptionValue[tokens.length];
  196. for (int i = 0; i < tokens.length; i++) {
  197. options[i] = decodeLocalOptionValue(tokens[i]);
  198. }
  199. return options;
  200. }
  201. /**
  202. * Create local connection object from given text.
  203. *
  204. * @param text
  205. * the text value of the LocalOptionValue object.
  206. * @return LocalOption object.
  207. */
  208. public LocalOptionValue decodeLocalOptionValue(String text) throws ParseException {
  209. // LocalOptionValue =("p"":"packetizationPeriod)
  210. // /("a"":"compressionAlgorithm)
  211. // /("b"":"bandwidth)
  212. // /("e"":"echoCancellation)
  213. // /("gc"":"gainControl)
  214. // /("s"":"silenceSuppression)
  215. // /("t"":"typeOfService)
  216. // /("r"":"resourceReservation)
  217. // /("k"":"encryptiondata)
  218. // /("nt"":"(typeOfNetwork /
  219. // supportedTypeOfNetwork))
  220. // /(LocalOptionExtensionName
  221. // [":"LocalOptionExtensionValue ])
  222. int pos = text.indexOf(':');
  223. if (pos < 0) {
  224. throw new ParseException("Could not parse local connection option: " + text, 0);
  225. }
  226. String name = text.substring(0, pos).trim();
  227. String value = text.substring(pos + 1).trim();
  228. if (name.equalsIgnoreCase("a")) {
  229. return decodeCompressionAlgorithm(value);
  230. } else if (name.equalsIgnoreCase("p")) {
  231. return decodePacketizationPeriod(value);
  232. } else if (name.equalsIgnoreCase("b")) {
  233. return decodeBandwidth(value);
  234. } else if (name.equalsIgnoreCase("e")) {
  235. return decodeEchoCancellation(value);
  236. } else if (name.equalsIgnoreCase("gc")) {
  237. return decodeGainControl(value);
  238. } else if (name.equalsIgnoreCase("s")) {
  239. return decodeSilenceSuppression(value);
  240. } else if (name.equalsIgnoreCase("t")) {
  241. return decodeTypeOfService(value);
  242. } else if (name.equalsIgnoreCase("r")) {
  243. return decodeResourceReservation(value);
  244. } else if (name.equalsIgnoreCase("k")) {
  245. return decodeEncryptionMethod(value);
  246. } else if (name.equalsIgnoreCase("nt")) {
  247. return decodeTypeOfNetwork(value);
  248. } else {
  249. return new LocalOptionExtension(name, value);
  250. }
  251. }
  252. public String encodeLocalOptionVale(LocalOptionValue localOptionValue) {
  253. StringBuffer s = new StringBuffer("");
  254. switch (localOptionValue.getLocalOptionValueType()) {
  255. case LocalOptionValue.BANDWIDTH:
  256. Bandwidth b = (Bandwidth) localOptionValue;
  257. s.append("b:").append(encodeBandwidth(b));
  258. break;
  259. case LocalOptionValue.COMPRESSION_ALGORITHM:
  260. CompressionAlgorithm compressionAlgorithm = (CompressionAlgorithm) localOptionValue;
  261. s.append("a:").append(encodeCompressionAlgorithm(compressionAlgorithm));
  262. break;
  263. case LocalOptionValue.ECHO_CANCELLATION:
  264. EchoCancellation echoCancellation = (EchoCancellation) localOptionValue;
  265. s.append("e:").append(encodeEchoCancellation(echoCancellation));
  266. break;
  267. case LocalOptionValue.ENCRYPTION_METHOD:
  268. EncryptionMethod encryptionMethod = (EncryptionMethod) localOptionValue;
  269. s.append("k:").append(encodeEncryptionMethod(encryptionMethod));
  270. break;
  271. case LocalOptionValue.GAIN_CONTROL:
  272. GainControl gainControl = (GainControl) localOptionValue;
  273. s.append("gc:").append(encodeGainControl(gainControl));
  274. break;
  275. case LocalOptionValue.LOCAL_OPTION_EXTENSION:
  276. LocalOptionExtension localOptionExtension = (LocalOptionExtension) localOptionValue;
  277. s.append(localOptionExtension.getLocalOptionExtensionName()).append(":").append(
  278. localOptionExtension.getLocalOptionExtensionValue());
  279. break;
  280. case LocalOptionValue.PACKETIZATION_PERIOD:
  281. PacketizationPeriod packetizationPeriod = (PacketizationPeriod) localOptionValue;
  282. s.append("p:").append(encodePacketizationPeriod(packetizationPeriod));
  283. break;
  284. case LocalOptionValue.RESOURCE_RESERVATION:
  285. s.append("r:");
  286. ResourceReservation resourceReservation = (ResourceReservation) localOptionValue;
  287. s.append(encodeResourceReservation(resourceReservation));
  288. break;
  289. case LocalOptionValue.SILENCE_SUPPRESSION:
  290. s.append("s:");
  291. SilenceSuppression silenceSuppression = (SilenceSuppression) localOptionValue;
  292. s.append(encodeSilenceSuppression(silenceSuppression));
  293. break;
  294. case LocalOptionValue.TYPE_OF_NETWORK:
  295. s.append("nt:");
  296. TypeOfNetwork typeOfNetwork = (TypeOfNetwork) localOptionValue;
  297. s.append(encodeTypeOfNetwork(typeOfNetwork));
  298. break;
  299. case LocalOptionValue.TYPE_OF_SERVICE:
  300. TypeOfService typeOfService = (TypeOfService) localOptionValue;
  301. s.append("t:").append(encodeTypeOfService(typeOfService));
  302. break;
  303. default:
  304. logger.error("LocalOptionValue " + localOptionValue + " not identified");
  305. break;
  306. }
  307. return s.toString();
  308. }
  309. /**
  310. * Create CompressionAlgorithm object from given text.
  311. *
  312. * @param text
  313. * the text value of the compression algoritm.
  314. * @return CompressionAlgorithm object.
  315. */
  316. public CompressionAlgorithm decodeCompressionAlgorithm(String value) throws ParseException {
  317. // compressionAlgorithm =algorithmName 0*(";"algorithmName)
  318. return new CompressionAlgorithm(semiColonPattern.split(value, 0));
  319. }
  320. public String encodeCompressionAlgorithm(CompressionAlgorithm compressionAlgorithm) {
  321. StringBuffer s = new StringBuffer("");
  322. String[] names = compressionAlgorithm.getCompressionAlgorithmNames();
  323. boolean first = true;
  324. for (int i = 0; i < names.length; i++) {
  325. if (first) {
  326. first = false;
  327. } else {
  328. s.append(";");
  329. }
  330. s.append(names[i]);
  331. }
  332. return s.toString();
  333. }
  334. /**
  335. * Create PacketizationPeriod object from given text.
  336. *
  337. * @param text
  338. * the text view of the PacketizationPeriod object.
  339. * @return PacketizationPeriod object.
  340. */
  341. public PacketizationPeriod decodePacketizationPeriod(String value) throws ParseException {
  342. // packetizationPeriod =1*4(DIGIT)["-"1*4(DIGIT)]
  343. int pos = value.indexOf('-');
  344. if (pos < 0) {
  345. try {
  346. return new PacketizationPeriod(Integer.parseInt(value));
  347. } catch (Exception e) {
  348. throw new ParseException("Invalid packetization period:" + value, 0);
  349. }
  350. } else {
  351. String low = value.substring(0, pos).trim();
  352. String hight = value.substring(pos + 1).trim();
  353. try {
  354. return new PacketizationPeriod(Integer.parseInt(low), Integer.parseInt(hight));
  355. } catch (Exception e) {
  356. throw new ParseException("Invalid packetization period:" + value, 0);
  357. }
  358. }
  359. }
  360. public String encodePacketizationPeriod(PacketizationPeriod packetizationPeriod) {
  361. StringBuffer s = new StringBuffer("");
  362. if (packetizationPeriod.getPacketizationPeriodLowerBound() != packetizationPeriod
  363. .getPacketizationPeriodUpperBound()) {
  364. s.append(packetizationPeriod.getPacketizationPeriodLowerBound()).append("-").append(
  365. packetizationPeriod.getPacketizationPeriodUpperBound());
  366. } else {
  367. s.append(packetizationPeriod.getPacketizationPeriodLowerBound());
  368. }
  369. return s.toString();
  370. }
  371. /**
  372. * Create Bandwidth object from given text.
  373. *
  374. * @param text
  375. * the text view of the Bandwidth object.
  376. * @return Bandwidth object.
  377. */
  378. public Bandwidth decodeBandwidth(String value) throws ParseException {
  379. // bandwidth =1*4(DIGIT)["-"1*4(DIGIT)]
  380. int pos = value.indexOf('-');
  381. if (pos < 0) {
  382. try {
  383. return new Bandwidth(Integer.parseInt(value));
  384. } catch (Exception e) {
  385. throw new ParseException("Invalid packetization period:" + value, 0);
  386. }
  387. } else {
  388. String low = value.substring(0, pos).trim();
  389. String hight = value.substring(pos + 1).trim();
  390. try {
  391. return new Bandwidth(Integer.parseInt(low), Integer.parseInt(hight));
  392. } catch (Exception e) {
  393. throw new ParseException("Invalid packetization period:" + value, 0);
  394. }
  395. }
  396. }
  397. public String encodeBandwidth(Bandwidth bandwidth) {
  398. StringBuffer s = new StringBuffer("");
  399. if (bandwidth.getBandwidthLowerBound() != bandwidth.getBandwidthUpperBound()) {
  400. s.append(bandwidth.getBandwidthLowerBound()).append("-").append(bandwidth.getBandwidthUpperBound());
  401. } else {
  402. s.append(bandwidth.getBandwidthLowerBound());
  403. }
  404. return s.toString();
  405. }
  406. /**
  407. * Decode EchoCancellation object from given text.
  408. *
  409. * @param text
  410. * the text value of the EchoCancellation.
  411. * @return EchoCancellation object.
  412. */
  413. public EchoCancellation decodeEchoCancellation(String value) throws ParseException {
  414. // echoCancellation ="on"/"off"
  415. if (value.equalsIgnoreCase("on")) {
  416. return EchoCancellation.EchoCancellationOn;
  417. } else if (value.equalsIgnoreCase("of")) {
  418. return EchoCancellation.EchoCancellationOff;
  419. } else {
  420. throw new ParseException("Invalid value for EchoCancellation :" + value, 0);
  421. }
  422. }
  423. public String encodeEchoCancellation(EchoCancellation echoCancellation) {
  424. StringBuffer s = new StringBuffer("");
  425. if (echoCancellation.getEchoCancellation()) {
  426. s.append("on");
  427. } else {
  428. s.append("off");
  429. }
  430. return s.toString();
  431. }
  432. /**
  433. * Decode GainControl object from given text.
  434. *
  435. * @param text
  436. * the text value of the GainControl.
  437. * @return GainControl object.
  438. */
  439. public GainControl decodeGainControl(String value) throws ParseException {
  440. // gainControl ="auto"/["-"] 1**4(DIGIT)
  441. if (value.equalsIgnoreCase("auto")) {
  442. return new GainControl();
  443. } else {
  444. try {
  445. return new GainControl(Integer.parseInt(value));
  446. } catch (Exception e) {
  447. throw new ParseException("Invalid value for EchoCancellation :" + value, 0);
  448. }
  449. }
  450. }
  451. public String encodeGainControl(GainControl gainControl) {
  452. StringBuffer s = new StringBuffer("");
  453. if (gainControl.getGainControl() == 0) {
  454. s.append("auto");
  455. } else {
  456. s.append(gainControl.getGainControl());
  457. }
  458. return s.toString();
  459. }
  460. /**
  461. * Decode SilenceSuppression object from given text.
  462. *
  463. * @param text
  464. * the text value of the SilenceSuppression.
  465. * @return SilenceSuppression object.
  466. */
  467. public SilenceSuppression decodeSilenceSuppression(String value) throws ParseException {
  468. // silenceSuppression ="on"/"off"
  469. if (value.equalsIgnoreCase("on")) {
  470. return SilenceSuppression.SilenceSuppressionOn;
  471. } else if (value.equalsIgnoreCase("off")) {
  472. return SilenceSuppression.SilenceSuppressionOff;
  473. } else {
  474. throw new ParseException("Invalid value for SilenceSuppression :" + value, 0);
  475. }
  476. }
  477. public String encodeSilenceSuppression(SilenceSuppression silenceSuppression) {
  478. StringBuffer s = new StringBuffer("");
  479. if (silenceSuppression.getSilenceSuppression()) {
  480. s.append("on");
  481. } else {
  482. s.append("off");
  483. }
  484. return s.toString();
  485. }
  486. /**
  487. * Decode TypeOfService object from given text.
  488. *
  489. * @param text
  490. * the text value of the TypeOfService.
  491. * @return TypeOfService object.
  492. */
  493. public TypeOfService decodeTypeOfService(String value) throws ParseException {
  494. // typeOfService =1*2(HEXDIG);1 hex only for capabilities
  495. try {
  496. return new TypeOfService((byte) Integer.parseInt(value));
  497. } catch (Exception e) {
  498. throw new ParseException("Invalid value for TypeOfService :" + value, 0);
  499. }
  500. }
  501. public String encodeTypeOfService(TypeOfService typeOfService) {
  502. String s = Integer.toString(typeOfService.getTypeOfService(), 16).toUpperCase();
  503. return s;
  504. }
  505. /**
  506. * Decode ResourceReservation object from given text.
  507. *
  508. * @param text
  509. * the text value of the ResourceReservation.
  510. * @return ResourceReservation object.
  511. */
  512. public ResourceReservation decodeResourceReservation(String value) throws ParseException {
  513. // resourceReservation ="g"/"cl"/"be"
  514. if (value.equalsIgnoreCase("g")) {
  515. return ResourceReservation.Guaranteed;
  516. } else if (value.equalsIgnoreCase("cl")) {
  517. return ResourceReservation.ControlledLoad;
  518. } else if (value.equalsIgnoreCase("be")) {
  519. return ResourceReservation.BestEffort;
  520. } else {
  521. throw new ParseException("Invalid value for EchoCancellation :" + value, 0);
  522. }
  523. }
  524. public String encodeResourceReservation(ResourceReservation resourceReservation) {
  525. StringBuffer s = new StringBuffer("");
  526. switch (resourceReservation.getResourceReservation()) {
  527. case ResourceReservation.BEST_EFFORT:
  528. s.append("be");
  529. break;
  530. case ResourceReservation.CONTROLLED_LOAD:
  531. s.append("cl");
  532. break;
  533. case ResourceReservation.GUARANTEED:
  534. s.append("g");
  535. break;
  536. }
  537. return s.toString();
  538. }
  539. /**
  540. * Decode EncryptionMethod object from given text.
  541. *
  542. * @param text
  543. * the text value of the EncryptionMethod.
  544. * @return EncryptionMethod object.
  545. */
  546. public EncryptionMethod decodeEncryptionMethod(String value) throws ParseException {
  547. // encryptiondata =("clear"":"encryptionKey )
  548. // /("base64"":"encodedEncryptionKey )
  549. // /("uri"":"URItoObtainKey )
  550. // /("prompt");defined in SDP,not usable in MGCP!
  551. int pos = value.indexOf(':');
  552. if (pos < 0) {
  553. throw new ParseException("Invalid value for EncryptionData: " + value, 0);
  554. }
  555. String method = value.substring(0, pos).trim();
  556. String key = value.substring(pos + 1).trim();
  557. if (method.equalsIgnoreCase("clear")) {
  558. return new EncryptionMethod(EncryptionMethod.CLEAR, key);
  559. } else if (method.equalsIgnoreCase("base64")) {
  560. return new EncryptionMethod(EncryptionMethod.BASE64, key);
  561. } else if (method.equalsIgnoreCase("uri")) {
  562. return new EncryptionMethod(EncryptionMethod.URI, key);
  563. } else {
  564. throw new ParseException("Invalid value for EncryptionData: " + method, 0);
  565. }
  566. }
  567. public String encodeEncryptionMethod(EncryptionMethod encryptionMethod) {
  568. StringBuffer s = new StringBuffer("");
  569. switch (encryptionMethod.getEncryptionMethod()) {
  570. case EncryptionMethod.BASE64:
  571. s.append("base64:").append(encryptionMethod.getEncryptionKey());
  572. break;
  573. case EncryptionMethod.CLEAR:
  574. s.append("clear:").append(encryptionMethod.getEncryptionKey());
  575. break;
  576. case EncryptionMethod.URI:
  577. s.append("uri:").append(encryptionMethod.getEncryptionKey());
  578. break;
  579. }
  580. return s.toString();
  581. }
  582. /**
  583. * Decode TypeOfNetwork object from given text.
  584. *
  585. * @param text
  586. * the text value of the TypeOfNetwork.
  587. * @return TypeOfNetwork object.
  588. */
  589. public TypeOfNetwork decodeTypeOfNetwork(String value) throws ParseException {
  590. // typeOfNetwork ="IN"/"ATM"/"LOCAL"/OtherTypeOfNetwork
  591. if (value.equalsIgnoreCase("in")) {
  592. return TypeOfNetwork.In;
  593. } else if (value.equalsIgnoreCase("atm")) {
  594. return TypeOfNetwork.Atm;
  595. } else if (value.equalsIgnoreCase("local")) {
  596. return TypeOfNetwork.Local;
  597. } else {
  598. throw new ParseException("Invalid value for TypeOfNetwork :" + value, 0);
  599. }
  600. }
  601. public String encodeTypeOfNetwork(TypeOfNetwork typeOfNetwork) {
  602. StringBuffer s = new StringBuffer("");
  603. switch (typeOfNetwork.getTypeOfNetwork()) {
  604. case TypeOfNetwork.IN:
  605. s.append("in");
  606. break;
  607. case TypeOfNetwork.ATM:
  608. s.append("atm");
  609. break;
  610. case TypeOfNetwork.LOCAL:
  611. s.append("local");
  612. break;
  613. }
  614. return s.toString();
  615. }
  616. public RestartMethod decodeRestartMethod(String value) throws ParseException {
  617. // RestartMethod = "graceful" / "forced" / "restart" / "disconnected"
  618. // / "cancel-graceful" / extensionRestartMethod
  619. // extensionRestartMethod = PackageExtensionRM
  620. // PackageExtensionRM = packageName "/" 1*32(ALPHA / DIGIT / HYPHEN)
  621. if (value.equalsIgnoreCase("graceful")) {
  622. return RestartMethod.Graceful;
  623. } else if (value.equalsIgnoreCase("forced")) {
  624. return RestartMethod.Forced;
  625. } else if (value.equalsIgnoreCase("restart")) {
  626. return RestartMethod.Restart;
  627. } else if (value.equalsIgnoreCase("disconnected")) {
  628. return RestartMethod.Disconnected;
  629. } else if (value.equalsIgnoreCase("cancel-grateful")) {
  630. return RestartMethod.CancelGraceful;
  631. } else {
  632. // TODO: Add support for extension restart method.
  633. throw new ParseException("Extension restarts not (yet) supported:" + value, 0);
  634. }
  635. }
  636. public RequestedEvent[] decodeRequestedEventList(String value) throws ParseException {
  637. // RequestedEvents =requestedEvent 0*(","0*(WSP)requestedEvent)
  638. // String[] tokens = commaPattern.split(value, 0);
  639. this.split(',', value, this.decdReqtEventList);
  640. try {
  641. int size = decdReqtEventList.size();
  642. RequestedEvent[] events = new RequestedEvent[size];
  643. for (int i = 0; i < size; i++) {
  644. events[i] = decodeRequestedEvent(decdReqtEventList.get(i));
  645. }
  646. return events;
  647. } finally {
  648. this.decdReqtEventList.clear();
  649. }
  650. }
  651. public RequestedEvent decodeRequestedEvent(String value) throws ParseException {
  652. // requestedEvent =(eventName ["("requestedActions ")"])
  653. // /(eventName "("requestedActions ")" "("eventParameters ")")
  654. // Replace all space, tabs
  655. // value = value.replaceAll("\\s", "");
  656. // value = spacePattern.matcher(value).replaceAll("");
  657. char[] valueChar = value.toCharArray();
  658. for (char c : valueChar) {
  659. if (c != ' ') {
  660. decdReqEveStrBuilder.append(c);
  661. }
  662. }
  663. value = decdReqEveStrBuilder.toString();
  664. decdReqEveStrBuilder.delete(0, valueChar.length);
  665. int pos1 = value.indexOf('(');
  666. // int pos2 = value.indexOf(')', pos1);
  667. // int pos3 = value.indexOf('(', pos2);
  668. // int pos4 = value.indexOf(')', pos3);
  669. if (pos1 == -1) { // no actions, no parameters
  670. // TODO: RFC3435 : 3.2.2.16 :When no action is specified, the
  671. // default action is to notify the event. This should be taken care
  672. // by MGW or N action should be added here by default?
  673. return new RequestedEvent(decodeEventName(value, null), null);
  674. }
  675. String evtName = value.substring(0, pos1);
  676. value = value.substring(pos1, value.length());
  677. Matcher matcher = pattern.matcher(value);
  678. if ((matcher.find())) {
  679. // This is Embedded Notification Request
  680. // TODO : We assumed that Embedded Notification Request E will not
  681. // be
  682. // combined with any other Action N, A, D, S, I or K
  683. String embeddedNotificatonRequest = value.substring(3, value.length() - 2);
  684. EmbeddedRequest embeddedRequest = decodeEmbeddedRequest(embeddedNotificatonRequest);
  685. RequestedAction[] actions = new RequestedAction[] { new RequestedAction(embeddedRequest) };
  686. return new RequestedEvent(decodeEventName(evtName, null), actions);
  687. }
  688. int pos2 = value.indexOf(')');
  689. int pos3 = value.indexOf('(', pos2);
  690. if (pos3 == -1) {
  691. value = value.substring(1, pos2);
  692. return new RequestedEvent(decodeEventName(evtName, null), decodeRequestedActions(value));
  693. }
  694. int pos4 = value.indexOf(')', pos3);
  695. String parms = value.substring(pos3 + 1, pos4);
  696. value = value.substring(1, pos2);
  697. return new RequestedEvent(decodeEventName(evtName, parms), decodeRequestedActions(value));
  698. }
  699. private void split(char splitChar, String value, List<String> arryList) {
  700. char[] charValue = value.toCharArray();
  701. int count = 0;
  702. for (char c : charValue) {
  703. count++;
  704. if (c == splitChar) {
  705. arryList.add(decdReqEveStrBuilder.toString());
  706. decdReqEveStrBuilder.delete(0, count);
  707. count = 0;
  708. } else {
  709. decdReqEveStrBuilder.append(c);
  710. }
  711. }
  712. arryList.add(decdReqEveStrBuilder.toString());
  713. decdReqEveStrBuilder.delete(0, count);
  714. }
  715. public EventName decodeEventName(String value, String param) throws ParseException {
  716. // eventName =[(packageName /"*")"/"]
  717. // (eventId /"all"/eventRange
  718. // /"*"/"#");for DTMF
  719. // ["@"(ConnectionId /"$"/"*")]
  720. this.split('/', value, this.decdEventName);
  721. // String tokens[] = forwardSlashPattern.split(value, 0);
  722. try {
  723. int size = decdEventName.size();
  724. if (size == 1) {
  725. return new EventName(PackageName.AllPackages, MgcpEvent.factory(decdEventName.get(0)).withParm(param));
  726. } else if (size == 2) {
  727. int pos = decdEventName.get(1).indexOf(AMPERSAND);
  728. if (pos > 0) {
  729. String cid = (decdEventName.get(1).substring(pos + 1)).trim();
  730. ConnectionIdentifier connectionIdentifier = null;
  731. if ((ConnectionIdentifier.AnyConnection).toString().equals(cid)) {
  732. connectionIdentifier = ConnectionIdentifier.AnyConnection;
  733. } else if ((ConnectionIdentifier.AllConnections).toString().equals(cid)) {
  734. connectionIdentifier = ConnectionIdentifier.AllConnections;
  735. } else {
  736. connectionIdentifier = new ConnectionIdentifier(cid);
  737. }
  738. return new EventName(PackageName.factory(decdEventName.get(0)), MgcpEvent.factory(
  739. (decdEventName.get(1)).substring(0, pos)).withParm(param), connectionIdentifier);
  740. } else {
  741. return new EventName(PackageName.factory(decdEventName.get(0)), MgcpEvent.factory(
  742. decdEventName.get(1)).withParm(param));
  743. }
  744. } else if (size == 3) {
  745. int pos = decdEventName.get(2).indexOf(AMPERSAND);
  746. if (pos < 0) {
  747. throw new ParseException("Invalid token " + decdEventName.get(2), 0);
  748. }
  749. String cid = (decdEventName.get(1)).substring(pos + 1);
  750. return new EventName(PackageName.factory(decdEventName.get(0)), MgcpEvent.factory(
  751. (decdEventName.get(1)).substring(0, pos)).withParm(param), new ConnectionIdentifier(cid));
  752. } else {
  753. throw new ParseException("Unexpected event name " + value, 0);
  754. }
  755. } finally {
  756. decdEventName.clear();
  757. }
  758. }
  759. public String encodeInfoCodeList(InfoCode[] infoCodes) {
  760. StringBuffer msg = new StringBuffer("");
  761. boolean first = true;
  762. for (int i = 0; i < infoCodes.length; i++) {
  763. if (first) {
  764. first = false;
  765. } else {
  766. msg.append(',');
  767. }
  768. InfoCode infoCode = infoCodes[i];
  769. switch (infoCode.getInfoCode()) {
  770. case (InfoCode.BEARER_INFORMATION):
  771. msg.append("B");
  772. break;
  773. case (InfoCode.CALL_IDENTIFIER):
  774. msg.append("C");
  775. break;
  776. case (InfoCode.CONNECTION_IDENTIFIER):
  777. msg.append("I");
  778. break;
  779. case (InfoCode.NOTIFIED_ENTITY):
  780. msg.append("N");
  781. break;
  782. case (InfoCode.REQUEST_IDENTIFIER):
  783. msg.append("X");
  784. break;
  785. case (InfoCode.LOCAL_CONNECTION_OPTIONS):
  786. msg.append("L");
  787. break;
  788. case (InfoCode.CONNECTION_MODE):
  789. msg.append("M");
  790. break;
  791. case (InfoCode.REQUESTED_EVENTS):
  792. msg.append("R");
  793. break;
  794. case (InfoCode.SIGNAL_REQUESTS):
  795. msg.append("S");
  796. break;
  797. case (InfoCode.DIGIT_MAP):
  798. msg.append("D");
  799. break;
  800. case (InfoCode.OBSERVED_EVENTS):
  801. msg.append("O");
  802. break;
  803. case (InfoCode.CONNECTION_PARAMETERS):
  804. msg.append("P");
  805. break;
  806. case (InfoCode.REASON_CODE):
  807. msg.append("E");
  808. break;
  809. case (InfoCode.SPECIFIC_ENDPOINT_ID):
  810. msg.append("Z");
  811. break;
  812. case (InfoCode.QUARANTINE_HANDLING):
  813. msg.append("Q");
  814. break;
  815. case (InfoCode.DETECT_EVENTS):
  816. msg.append("T");
  817. break;
  818. case (InfoCode.REMOTE_CONNECTION_DESCRIPTOR):
  819. msg.append("RC");
  820. break;
  821. case (InfoCode.LOCAL_CONNECTION_DESCRIPTOR):
  822. msg.append("LC");
  823. break;
  824. case (InfoCode.CAPABILITIES):
  825. msg.append("A");
  826. break;
  827. case (InfoCode.EVENT_STATES):
  828. msg.append("ES");
  829. break;
  830. case (InfoCode.RESTART_METHOD):
  831. msg.append("RM");
  832. break;
  833. case (InfoCode.RESTART_DELAY):
  834. msg.append("RD");
  835. break;
  836. default:
  837. logger.error("The InfoCode " + infoCode + " is not supported");
  838. break;
  839. }
  840. }
  841. return msg.toString();
  842. }
  843. public InfoCode[] decodeInfoCodeList(String value) throws ParseException {
  844. String tokens[] = commaPattern.split(value.trim(), 0);
  845. InfoCode[] infoCodes = new InfoCode[tokens.length];
  846. for (int i = 0; i < tokens.length; i++) {
  847. infoCodes[i] = decodeInfoCode(tokens[i]);
  848. }
  849. return infoCodes;
  850. }
  851. private InfoCode decodeInfoCode(String value) throws ParseException {
  852. value = value.trim();
  853. if (value.equalsIgnoreCase("B")) {
  854. return InfoCode.BearerInformation;
  855. }
  856. else if (value.equalsIgnoreCase("C")) {
  857. return InfoCode.CallIdentifier;
  858. }
  859. else if (value.equalsIgnoreCase("I")) {
  860. return InfoCode.ConnectionIdentifier;
  861. }
  862. else if (value.equalsIgnoreCase("N")) {
  863. return InfoCode.NotifiedEntity;
  864. }
  865. else if (value.equalsIgnoreCase("X")) {
  866. return InfoCode.RequestIdentifier;
  867. }
  868. else if (value.equalsIgnoreCase("L")) {
  869. return InfoCode.LocalConnectionOptions;
  870. }
  871. else if (value.equalsIgnoreCase("M")) {
  872. return InfoCode.ConnectionMode;
  873. }
  874. else if (value.equalsIgnoreCase("R")) {
  875. return InfoCode.RequestedEvents;
  876. }
  877. else if (value.equalsIgnoreCase("S")) {
  878. return InfoCode.SignalRequests;
  879. }
  880. else if (value.equalsIgnoreCase("D")) {
  881. return InfoCode.DigitMap;
  882. }
  883. else if (value.equalsIgnoreCase("O")) {
  884. return InfoCode.ObservedEvents;
  885. }
  886. else if (value.equalsIgnoreCase("P")) {
  887. return InfoCode.ConnectionParameters;
  888. }
  889. else if (value.equalsIgnoreCase("E")) {
  890. return InfoCode.ReasonCode;
  891. }
  892. else if (value.equalsIgnoreCase("Z")) {
  893. return InfoCode.SpecificEndpointID;
  894. }
  895. else if (value.equalsIgnoreCase("Q")) {
  896. return InfoCode.QuarantineHandling;
  897. }
  898. else if (value.equalsIgnoreCase("T")) {
  899. return InfoCode.DetectEvents;
  900. }
  901. else if (value.equalsIgnoreCase("RC")) {
  902. return InfoCode.RemoteConnectionDescriptor;
  903. }
  904. else if (value.equalsIgnoreCase("LC")) {
  905. return InfoCode.LocalConnectionDescriptor;
  906. }
  907. else if (value.equalsIgnoreCase("A")) {
  908. return InfoCode.Capabilities;
  909. }
  910. else if (value.equalsIgnoreCase("ES")) {
  911. return InfoCode.EventStates;
  912. }
  913. else if (value.equalsIgnoreCase("RM")) {
  914. return InfoCode.RestartMethod;
  915. }
  916. else if (value.equalsIgnoreCase("RD")) {
  917. return InfoCode.RestartDelay;
  918. }
  919. else {
  920. throw new ParseException("Extension action not suported", 0);
  921. }
  922. }
  923. public CapabilityValue[] decodeCapabilityList(String value) throws ParseException {
  924. String tokens[] = commaPattern.split(value.trim(), 0);
  925. CapabilityValue[] capabilityValues = new CapabilityValue[tokens.length];
  926. for (int i = 0; i < tokens.length; i++) {
  927. capabilityValues[i] = decodeCapability(tokens[i]);
  928. }
  929. return capabilityValues;
  930. }
  931. public String encodeCapabilityList(CapabilityValue[] c) {
  932. StringBuffer s = new StringBuffer("");
  933. boolean first = true;
  934. for (int i = 0; i < c.length; i++) {
  935. if (first) {
  936. first = false;
  937. } else {
  938. s.append(",");
  939. }
  940. s.append(encodeCapability(c[i]));
  941. }
  942. return s.toString();
  943. }
  944. public String encodeCapability(CapabilityValue c) {
  945. StringBuffer s = new StringBuffer("");
  946. switch (c.getCapabilityValueType()) {
  947. case CapabilityValue.LOCAL_OPTION_VALUE:
  948. LocalOptVal localOptVal = (LocalOptVal) c;
  949. LocalOptionValue localOptionValue = localOptVal.getLocalOptionValue();
  950. s.append(encodeLocalOptionVale(localOptionValue));
  951. break;
  952. case CapabilityValue.SUPPORTED_PACKAGES:
  953. s.append("v:");
  954. SupportedPackages supportedPackages = (SupportedPackages) c;
  955. PackageName[] packageNameList = supportedPackages.getSupportedPackageNames();
  956. s.append(encodePackageNameList(packageNameList));
  957. break;
  958. case CapabilityValue.SUPPORTED_MODES:
  959. s.append("m:");
  960. SupportedModes supportedModes = (SupportedModes) c;
  961. ConnectionMode[] connectionModeList = supportedModes.getSupportedModes();
  962. s.append(encodeConnectionModeList(connectionModeList));
  963. break;
  964. }
  965. return s.toString();
  966. }
  967. public String encodeConnectionModeList(ConnectionMode[] connectionModeList) {
  968. StringBuffer s = new StringBuffer("");
  969. boolean first = true;
  970. for (int i = 0; i < connectionModeList.length; i++) {
  971. if (first) {
  972. first = false;
  973. } else {
  974. s.append(";");
  975. }
  976. s.append(connectionModeList[i].toString());
  977. }
  978. return s.toString();
  979. }
  980. public String encodePackageNameList(PackageName[] packageNameList) {
  981. StringBuffer s = new StringBuffer("");
  982. boolean first = true;
  983. for (int i = 0; i < packageNameList.length; i++) {
  984. if (first) {
  985. first = false;
  986. } else {
  987. s.append(";");
  988. }
  989. s.append(packageNameList[i].toString());
  990. }
  991. return s.toString();
  992. }
  993. public PackageName[] decodePackageNameList(String value) {
  994. String[] packages = semiColonPattern.split(value, 0);
  995. PackageName[] supportedPackageNames = new PackageName[packages.length];
  996. for (int i = 0; i < packages.length; i++) {
  997. PackageName p = PackageName.factory(packages[i]);
  998. supportedPackageNames[i] = p;
  999. }
  1000. return supportedPackageNames;
  1001. }
  1002. public CapabilityValue decodeCapability(String value) throws ParseException {
  1003. CapabilityValue capabilityValue = null;
  1004. int pos = value.indexOf(':');
  1005. if (pos < 0) {
  1006. throw new ParseException("Invalid value for EncryptionData: " + value, 0);
  1007. }
  1008. String key = value.substring(0, pos).trim();
  1009. String capability = value.substring(pos + 1).trim();
  1010. if ("a".equals(key)) {
  1011. String[] codecs = semiColonPattern.split(capability, 0);
  1012. CompressionAlgorithm compressionAlgorithm = new CompressionAlgorithm(codecs);
  1013. capabilityValue = new LocalOptVal(compressionAlgorithm);
  1014. } else if ("b".equals(key)) {
  1015. String[] bandwidthRange = dashPattern.split(capability, 0);
  1016. int lower = Integer.parseInt(bandwidthRange[0]);
  1017. int upper = lower;
  1018. if (bandwidthRange.length == 2) {
  1019. upper = Integer.parseInt(bandwidthRange[1]);
  1020. }
  1021. Bandwidth bandwidth = new Bandwidth(lower, upper);
  1022. capabilityValue = new LocalOptVal(bandwidth);
  1023. } else if ("p".equals(key)) {
  1024. String[] packetizationRange = dashPattern.split(capability, 0);
  1025. int lower = Integer.parseInt(packetizationRange[0]);
  1026. int upper = lower;
  1027. if (packetizationRange.length == 2) {
  1028. upper = Integer.parseInt(packetizationRange[1]);
  1029. }
  1030. PacketizationPeriod packetizationPeriod = new PacketizationPeriod(lower, upper);
  1031. capabilityValue = new LocalOptVal(packetizationPeriod);
  1032. } else if ("e".equals(key)) {
  1033. if ("on".equals(capability)) {
  1034. capabilityValue = new LocalOptVal(EchoCancellation.EchoCancellationOn);
  1035. } else {
  1036. capabilityValue = new LocalOptVal(EchoCancellation.EchoCancellationOff);
  1037. }
  1038. } else if ("s".equals(key)) {
  1039. if ("on".equals(capability)) {
  1040. capabilityValue = new LocalOptVal(SilenceSuppression.SilenceSuppressionOn);
  1041. } else {
  1042. capabilityValue = new LocalOptVal(SilenceSuppression.SilenceSuppressionOff);
  1043. }
  1044. } else if ("gc".equals(key)) {
  1045. int gainControl = 0;
  1046. try {
  1047. gainControl = Integer.parseInt(capability);
  1048. } catch (NumberFormatException ne) {
  1049. // Ignore
  1050. }
  1051. capabilityValue = new LocalOptVal(new GainControl(gainControl));
  1052. } else if ("t".equals(key)) {
  1053. byte typeOfService = 0;
  1054. try {
  1055. typeOfService = Byte.parseByte(capability);
  1056. } catch (NumberFormatException ne) {
  1057. // Ignore
  1058. }
  1059. capabilityValue = new LocalOptVal(new TypeOfService(typeOfService));
  1060. } else if ("r".equals(key)) {
  1061. if ("g".equals(capability)) {
  1062. capabilityValue = new LocalOptVal(ResourceReservation.Guaranteed);
  1063. } else if ("cl".equals(capability)) {
  1064. capabilityValue = new LocalOptVal(ResourceReservation.ControlledLoad);
  1065. } else if ("be".equals(capability)) {
  1066. capabilityValue = new LocalOptVal(ResourceReservation.BestEffort);
  1067. }
  1068. } else if ("k".equals(key)) {
  1069. EncryptionMethod encryptionMethod = decodeEncryptionMethod(capability);
  1070. capabilityValue = new LocalOptVal(encryptionMethod);
  1071. } else if ("nt".equals(key)) {
  1072. TypeOfNetwork typeOfNetwork = decodeTypeOfNetwork(capability);
  1073. capabilityValue = new LocalOptVal(typeOfNetwork);
  1074. } else if ("v".equals(key)) {
  1075. PackageName[] supportedPackageNames = decodePackageNameList(capability);
  1076. capabilityValue = new SupportedPackages(supportedPackageNames);
  1077. } else if ("m".equals(key)) {
  1078. String[] modes = semiColonPattern.split(capability, 0);
  1079. ConnectionMode[] supportedConnectionModes = new ConnectionMode[modes.length];
  1080. for (int i = 0; i < modes.length; i++) {
  1081. ConnectionMode c = decodeConnectionMode(modes[i]);
  1082. supportedConnectionModes[i] = c;
  1083. }
  1084. capabilityValue = new SupportedModes(supportedConnectionModes);
  1085. }
  1086. return capabilityValue;
  1087. }
  1088. public RequestedAction[] decodeRequestedActions(String value) throws ParseException {
  1089. // requestedActions =requestedAction 0*(","0*(WSP)requestedAction)
  1090. String tokens[] = commaPattern.split(value, 0);
  1091. RequestedAction[] actions = new RequestedAction[tokens.length];
  1092. for (int i = 0; i < tokens.length; i++) {
  1093. actions[i] = decodeRequestedAction(tokens[i]);
  1094. }
  1095. return actions;
  1096. }
  1097. public RequestedAction decodeRequestedAction(String value) throws ParseException {
  1098. // requestedAction ="N"/"A"/"D"/"S"/"I"/"K"
  1099. // /"E""("EmbeddedRequest ")"
  1100. // /ExtensionAction
  1101. value = value.trim();
  1102. if (value.equalsIgnoreCase("N")) {
  1103. return RequestedAction.NotifyImmediately;
  1104. } else if (value.equalsIgnoreCase("A")) {
  1105. return RequestedAction.Accumulate;
  1106. } else if (value.equalsIgnoreCase("S")) {
  1107. return RequestedAction.Swap;
  1108. } else if (value.equalsIgnoreCase("I")) {
  1109. return RequestedAction.Ignore;
  1110. } else if (value.equalsIgnoreCase("K")) {
  1111. return RequestedAction.KeepSignalsActive;
  1112. } else if (value.equalsIgnoreCase("D")) {
  1113. return RequestedAction.TreatAccordingToDigitMap;
  1114. } else if (value.equalsIgnoreCase("E")) {
  1115. return new RequestedAction(decodeEmbeddedRequest(value));
  1116. } else if (value.equalsIgnoreCase("X")) {
  1117. // X is RequestIdentifier and we have already taken care of this.
  1118. // Ignore here
  1119. return RequestedAction.Ignore;
  1120. } else {
  1121. throw new ParseException("Extension action not suported", 0);
  1122. }
  1123. }
  1124. private String getEvent(String s) {
  1125. char[] c = s.toCharArray();
  1126. String command = "";
  1127. int start = 0;
  1128. int end = 0;
  1129. int paraenthisis = 0;
  1130. boolean first = true;
  1131. for (int i = 0; i < c.length; i++) {
  1132. if (c[i] == '(') {
  1133. paraenthisis++;
  1134. if (first) {
  1135. first = false;
  1136. start = i;
  1137. command = String.valueOf(c[i - 1]);
  1138. }
  1139. } else if (c[i] == ')') {
  1140. paraenthisis--;
  1141. }
  1142. if (!first && paraenthisis == 0) {
  1143. end = i + 1;
  1144. break;
  1145. // command ends
  1146. }
  1147. }
  1148. // System.out.println("Command = " + command + " Start = " + start + "
  1149. // end = " + end);
  1150. return s.substring(0, end);
  1151. }
  1152. public String encodeEmbeddedRequest(EmbeddedRequest embeddedRequest) {
  1153. StringBuffer s = new StringBuffer("");
  1154. boolean first = true;
  1155. RequestedEvent[] requestedEventList = embeddedRequest.getEmbeddedRequestList();
  1156. if (requestedEventList != null) {
  1157. if (first) {
  1158. first = false;
  1159. } else {
  1160. s.append(",");
  1161. }
  1162. s.append("R(").append(encodeRequestedEvents(requestedEventList)).append(")");
  1163. }
  1164. EventName[] eventNameList = embeddedRequest.getEmbeddedSignalRequest();
  1165. if (eventNameList != null) {
  1166. if (first) {
  1167. first = false;
  1168. } else {
  1169. s.append(",");
  1170. }
  1171. s.append("S(").append(encodeEventNames(eventNameList)).append(")");
  1172. }
  1173. DigitMap digitMap = embeddedRequest.getEmbeddedDigitMap();
  1174. if (digitMap != null) {
  1175. if (first) {
  1176. first = false;
  1177. } else {
  1178. s.append(",");
  1179. }
  1180. s.append("D(").append(digitMap.toString()).append(")");
  1181. }
  1182. return s.toString();
  1183. }
  1184. public EmbeddedRequest decodeEmbeddedRequest(String value) throws ParseException {
  1185. // EmbeddedRequest =("R""("EmbeddedRequestList ")"
  1186. // [","0*(WSP)"S""("EmbeddedSignalRequest ")"]
  1187. // [","0*(WSP)"D""("EmbeddedDigitMap ")"])
  1188. // /("S""("EmbeddedSignalRequest ")"
  1189. // [","0*(WSP)"D""("EmbeddedDigitMap ")"])
  1190. // /("D""("EmbeddedDigitMap ")")
  1191. RequestedEvent[] requestedEvents = null;
  1192. EventName[] signalEvents = null;
  1193. DigitMap digitMap = null;
  1194. while (value.length() > 0) {
  1195. String temp = this.getEvent(value);
  1196. value = value.substring(temp.length(), value.length());
  1197. if (temp.startsWith(",")) {
  1198. temp = temp.substring(1, temp.length());
  1199. }
  1200. if (temp.startsWith("R")) {
  1201. temp = temp.substring(2, temp.length() - 1);
  1202. requestedEvents = decodeRequestedEventList(temp);
  1203. } else if (temp.startsWith("S")) {
  1204. temp = temp.substring(2, temp.length() - 1);
  1205. signalEvents = decodeEventNames(temp);
  1206. } else if (temp.startsWith("D")) {
  1207. temp = temp.substring(2, temp.length() - 1);
  1208. digitMap = new DigitMap(temp);
  1209. }
  1210. }
  1211. return new EmbeddedRequest(requestedEvents, signalEvents, digitMap);
  1212. }
  1213. public EventName[] decodeEventNames(String value) throws ParseException {
  1214. String tokens[] = commaPattern.split(value, 0);
  1215. EventName[] events = new EventName[tokens.length];
  1216. for (int i = 0; i < tokens.length; i++) {
  1217. String name = null;
  1218. String parm = null;
  1219. int pos = tokens[i].indexOf('(');
  1220. if (pos > 0) {
  1221. name = tokens[i].substring(0, pos);
  1222. parm = tokens[i].substring(pos + 1, tokens[i].length() - 1);
  1223. } else {
  1224. name = tokens[i];
  1225. }
  1226. events[i] = decodeEventName(name, parm);
  1227. }
  1228. return events;
  1229. }
  1230. public String encodeEventNames(EventName[] events) {
  1231. StringBuffer s = new StringBuffer("");
  1232. boolean first = true;
  1233. for (EventName e : events) {
  1234. if (first) {
  1235. first = false;
  1236. } else {
  1237. s.append(',');
  1238. }
  1239. s.append(encodeEventName(e));
  1240. }
  1241. return s.toString();
  1242. }
  1243. public String encodeEventName(EventName e) {
  1244. StringBuffer s = new StringBuffer("");
  1245. s.append(e.getPackageName().toString()).append('/').append(e.getEventIdentifier().getName());
  1246. if (e.getConnectionIdentifier() != null) {
  1247. s.append(AMPERSAND).append(e.getConnectionIdentifier().toString());
  1248. }
  1249. if (e.getEventIdentifier().getParms() != null) {
  1250. s.append('(').append(e.getEventIdentifier().getParms()).append(')');
  1251. }
  1252. return s.toString();
  1253. }
  1254. /**
  1255. * Creates EndpointIdentifier object from givent endpont's name.
  1256. *
  1257. * @param name
  1258. * the name of the given endpoint.
  1259. * @return EdnpointIdentifier object.
  1260. */
  1261. public EndpointIdentifier decodeEndpointIdentifier(String name) {
  1262. try {
  1263. this.split(AMPERSAND, name, list);
  1264. String[] s = new String[list.size()];
  1265. list.toArray(s);
  1266. return new EndpointIdentifier(s[0], s[1]);
  1267. } finally {
  1268. list.clear();
  1269. }
  1270. }
  1271. public String encodeEndpointIdentifier(EndpointIdentifier e) {
  1272. String s = e.getLocalEndpointName();
  1273. if (e.getDomainName() != null) {
  1274. s += "@" + e.getDomainName();
  1275. }
  1276. return s;
  1277. }
  1278. public EndpointIdentifier[] decodeEndpointIdentifiers(String name) {
  1279. String[] s = commaPattern.split(name, 0);
  1280. EndpointIdentifier[] endpointIdentifiers = new EndpointIdentifier[s.length];
  1281. for (int i = 0; i < s.length; i++) {
  1282. endpointIdentifiers[i] = decodeEndpointIdentifier(s[i]);
  1283. }
  1284. return endpointIdentifiers;
  1285. }
  1286. public String encodeEndpointIdentifiers(EndpointIdentifier[] endpointIdentifierList) {
  1287. StringBuffer msg = new StringBuffer("");
  1288. boolean first = true;
  1289. for (int i = 0; i < endpointIdentifierList.length; i++) {
  1290. if (first) {
  1291. first = false;
  1292. } else {
  1293. msg.append(",");
  1294. }
  1295. msg.append(encodeEndpointIdentifier(endpointIdentifierList[i]));
  1296. }
  1297. return msg.toString();
  1298. }
  1299. public ReturnCode decodeReturnCode(int code) throws ParseException {
  1300. switch (code) {
  1301. case ReturnCode.CAS_SIGNALING_PROTOCOL_ERROR:
  1302. return ReturnCode.CAS_Signaling_Protocol_Error;
  1303. case ReturnCode.CONNECTION_WAS_DELETED:
  1304. return ReturnCode.Connection_Was_Deleted;
  1305. case ReturnCode.ENDPOINT_HAS_NO_DIGIT_MAP:
  1306. return ReturnCode.Endpoint_Has_No_Digit_Map;
  1307. case ReturnCode.ENDPOINT_INSUFFICIENT_RESOURCES:
  1308. return ReturnCode.Endpoint_Insufficient_Resources;
  1309. case ReturnCode.ENDPOINT_IS_RESTARTING:
  1310. return ReturnCode.Endpoint_Is_Restarting;
  1311. case ReturnCode.ENDPOINT_NOT_READY:
  1312. return ReturnCode.Endpoint_Not_Ready;
  1313. case ReturnCode.ENDPOINT_REDIRECTED:
  1314. return ReturnCode.Endpoint_Redirected;
  1315. case ReturnCode.ENDPOINT_UNKNOWN:
  1316. return ReturnCode.Endpoint_Unknown;
  1317. case ReturnCode.GATEWAY_CANNOT_DETECT_REQUESTED_EVENT:
  1318. return ReturnCode.Gateway_Cannot_Detect_Requested_Event;
  1319. case ReturnCode.GATEWAY_CANNOT_GENERATE_REQUESTED_SIGNAL:
  1320. return ReturnCode.Gateway_Cannot_Generate_Requested_Signal;
  1321. case ReturnCode.GATEWAY_CANNOT_SEND_SPECIFIED_ANNOUNCEMENT:
  1322. return ReturnCode.Gateway_Cannot_Send_Specified_Announcement;
  1323. case ReturnCode.INCOMPATIBLE_PROTOCOL_VERSION:
  1324. return ReturnCode.Incompatible_Protocol_Version;
  1325. case ReturnCode.INCORRECT_CONNECTION_ID:
  1326. return ReturnCode.Incorrect_Connection_ID;
  1327. case ReturnCode.INSUFFICIENT_BANDWIDTH:
  1328. return ReturnCode.Insufficient_Bandwidth;
  1329. case ReturnCode.INSUFFICIENT_BANDWIDTH_NOW:
  1330. return ReturnCode.Insufficient_Bandwidth_Now;
  1331. case ReturnCode.INSUFFICIENT_RESOURCES_NOW:
  1332. return ReturnCode.Insufficient_Resources_Now;
  1333. case ReturnCode.INTERNAL_HARDWARE_FAILURE:
  1334. return ReturnCode.Internal_Hardware_Failure;
  1335. case ReturnCode.INTERNAL_INCONSISTENCY_IN_LOCALCONNECTIONOPTIONS:
  1336. return ReturnCode.Internal_Inconsistency_In_LocalConnectionOptions;
  1337. case ReturnCode.MISSING_REMOTECONNECTIONDESCRIPTOR:
  1338. return ReturnCode.Missing_RemoteConnectionDescriptor;
  1339. case ReturnCode.NO_SUCH_EVENT_OR_SIGNAL:
  1340. return ReturnCode.No_Such_Event_Or_Signal;
  1341. case ReturnCode.PHONE_OFF_HOOK:
  1342. return ReturnCode.Phone_Off_Hook;
  1343. case ReturnCode.PHONE_ON_HOOK:
  1344. return ReturnCode.Phone_On_Hook;
  1345. case ReturnCode.PROTOCOL_ERROR:
  1346. return ReturnCode.Protocol_Error;
  1347. case ReturnCode.TRANSACTION_BEING_EXECUTED:
  1348. return ReturnCode.Transaction_Being_Executed;
  1349. case ReturnCode.TRANSACTION_EXECUTED_NORMALLY:
  1350. return ReturnCode.Transaction_Executed_Normally;
  1351. case ReturnCode.TRANSIENT_ERROR:
  1352. return ReturnCode.Transient_Error;
  1353. case ReturnCode.TRUNK_GROUP_FAILURE:
  1354. return ReturnCode.Trunk_Group_Failure;
  1355. case ReturnCode.UNKNOWN_CALL_ID:
  1356. return ReturnCode.Unknown_Call_ID;
  1357. case ReturnCode.UNKNOWN_EXTENSION_IN_LOCALCONNECTIONOPTIONS:
  1358. return ReturnCode.Unknown_Extension_In_LocalConnectionOptions;
  1359. case ReturnCode.UNKNOWN_OR_ILLEGAL_COMBINATION_OF_ACTIONS:
  1360. return ReturnCode.Unknown_Or_Illegal_Combination_Of_Actions;
  1361. case ReturnCode.UNRECOGNIZED_EXTENSION:
  1362. return ReturnCode.Unrecognized_Extension;
  1363. case ReturnCode.UNSUPPORTED_OR_INVALID_MODE:
  1364. return ReturnCode.Unsupported_Or_Invalid_Mode;
  1365. case ReturnCode.UNSUPPORTED_OR_UNKNOWN_PACKAGE:
  1366. return ReturnCode.Unsupported_Or_Unknown_Package;
  1367. default:
  1368. // TODO: 0xx should be treated as response acknowledgement.
  1369. if ((code > 99) && (code < 200))
  1370. return (ReturnCode.Transaction_Being_Executed);
  1371. else if ((code > 199) && code < 300)
  1372. return (ReturnCode.Transaction_Executed_Normally);
  1373. else if ((code > 299) && code < 400)
  1374. return (ReturnCode.Endpoint_Redirected);
  1375. else if ((code > 399) && (code < 500))
  1376. return (ReturnCode.Transient_Error);
  1377. else if ((code > 499) && (code < 1000))
  1378. return (ReturnCode.Protocol_Error);
  1379. else
  1380. throw new ParseException("unknown response code: " + code, 0);
  1381. }
  1382. }
  1383. public String encodeLocalOptionValueList(LocalOptionValue[] options) {
  1384. StringBuffer msg = new StringBuffer("");
  1385. boolean first = true;
  1386. for (int i = 0; i < options.length; i++) {
  1387. if (first) {
  1388. first = false;
  1389. } else {
  1390. msg.append(",");
  1391. }
  1392. String s = options[i].toString();
  1393. if (s.indexOf("\n") != -1) {
  1394. s = s.substring(0, s.indexOf("\n"));
  1395. }
  1396. msg.append(s);
  1397. }
  1398. return msg.toString();
  1399. }
  1400. public String encodeNotificationRequestParms(NotificationRequestParms parms) {
  1401. StringBuffer msg = new StringBuffer("X:").append(parms.getRequestIdentifier()).append("\n");
  1402. if (parms.getSignalRequests() != null) {
  1403. msg.append("S:").append(encodeEventNames(parms.getSignalRequests())).append("\n");
  1404. }
  1405. if (parms.getRequestedEvents() != null) {
  1406. msg.append("R:").append(encodeRequestedEvents(parms.getRequestedEvents())).append("\n");
  1407. }
  1408. if (parms.getDetectEvents() != null) {
  1409. msg.append("T:").append(encodeEventNames(parms.getDetectEvents())).append("\n");
  1410. }
  1411. if (parms.getDigitMap() != null) {
  1412. msg.append("D:").append(parms.getDigitMap()).append("\n");
  1413. }
  1414. return msg.toString();
  1415. }
  1416. public String encodeRequestedEvent(RequestedEvent evt) {
  1417. StringBuffer s = new StringBuffer((evt.getEventName().getPackageName()).toString()).append("/").append(
  1418. evt.getEventName().getEventIdentifier().getName());
  1419. if (evt.getEventName().getConnectionIdentifier() != null) {
  1420. s.append(AMPERSAND).append(evt.getEventName().getConnectionIdentifier().toString());
  1421. }
  1422. String parms = evt.getEventName().getEventIdentifier().getParms();
  1423. RequestedAction[] actions = evt.getRequestedActions();
  1424. if (actions != null) {
  1425. String ac = encodeRequestedActions(actions);
  1426. s.append(" (").append(ac).append(")");
  1427. }
  1428. if (parms != null) {
  1429. s.append(" (").append(parms).append(")");
  1430. }
  1431. return s.toString();
  1432. }
  1433. public String encodeRequestedEvents(RequestedEvent[] evts) {
  1434. StringBuffer s = new StringBuffer("");
  1435. for (int i = 0; i < evts.length; i++) {
  1436. s.append(encodeRequestedEvent(evts[i]));
  1437. if (i != evts.length - 1) {
  1438. s.append(',');
  1439. }
  1440. }
  1441. return s.toString();
  1442. }
  1443. public String encodeRequestedActions(RequestedAction[] actions) {
  1444. StringBuffer s = new StringBuffer("");
  1445. String d = "";
  1446. for (int i = 0; i < actions.length; i++) {
  1447. d = i == 0 ? "" : ",";
  1448. s.append(d).append(encodeRequestedAction(actions[i]));
  1449. }
  1450. return s.toString();
  1451. }
  1452. public String encodeRequestedAction(RequestedAction action) {
  1453. StringBuffer s = new StringBuffer("");
  1454. if (RequestedAction.EMBEDDED_NOTIFICATION_REQUEST != action.getRequestedAction()) {
  1455. return action.toString();
  1456. } else {
  1457. EmbeddedRequest embeddedRequest = action.getEmbeddedRequest();
  1458. s.append("E(").append(encodeEmbeddedRequest(embeddedRequest)).append(")");
  1459. }
  1460. return s.toString();
  1461. }
  1462. public String encodeConnectionParm(ConnectionParm parm) {
  1463. int type = parm.getConnectionParmType();
  1464. if (type == RegularConnectionParm.JITTER) {
  1465. return "JI=" + parm.getConnectionParmValue();
  1466. } else if (type == RegularConnectionParm.LATENCY) {
  1467. return "LA=" + parm.getConnectionParmValue();
  1468. } else if (type == RegularConnectionParm.OCTETS_RECEIVED) {
  1469. return "OR=" + parm.getConnectionParmValue();
  1470. } else if (type == RegularConnectionParm.OCTETS_SENT) {
  1471. return "OS=" + parm.getConnectionParmValue();
  1472. } else if (type == RegularConnectionParm.PACKETS_LOST) {
  1473. return "PL=" + parm.getConnectionParmValue();
  1474. } else if (type == RegularConnectionParm.PACKETS_RECEIVED) {
  1475. return "PR=" + parm.getConnectionParmValue();
  1476. } else if (type == RegularConnectionParm.PACKETS_SENT) {
  1477. return "PS=" + parm.getConnectionParmValue();
  1478. } else {
  1479. return ((ExtendedConnectionParm) parm).getConnectionParmExtensionName() + "="
  1480. + parm.getConnectionParmValue();
  1481. }
  1482. }
  1483. public ConnectionParm decodeConnectionParm(String parm) {
  1484. String[] tokens = equalsPattern.split(parm, 0);
  1485. String name = tokens[0].trim();
  1486. String value = tokens[1].trim();
  1487. if (name.equalsIgnoreCase("JI")) {
  1488. return new RegularConnectionParm(RegularConnectionParm.JITTER, Integer.parseInt(value));
  1489. } else if (name.equalsIgnoreCase("LA")) {
  1490. return new RegularConnectionParm(RegularConnectionParm.LATENCY, Integer.parseInt(value));
  1491. } else if (name.equalsIgnoreCase("OR")) {
  1492. return new RegularConnectionParm(RegularConnectionParm.OCTETS_RECEIVED, Integer.parseInt(value));
  1493. } else if (name.equalsIgnoreCase("OS")) {
  1494. return new RegularConnectionParm(RegularConnectionParm.OCTETS_SENT, Integer.parseInt(value));
  1495. } else if (name.equalsIgnoreCase("PL")) {
  1496. return new RegularConnectionParm(RegularConnectionParm.PACKETS_LOST, Integer.parseInt(value));
  1497. } else if (name.equalsIgnoreCase("PR")) {
  1498. return new RegularConnectionParm(RegularConnectionParm.PACKETS_RECEIVED, Integer.parseInt(value));
  1499. } else if (name.equalsIgnoreCase("PS")) {
  1500. return new RegularConnectionParm(RegularConnectionParm.PACKETS_SENT, Integer.parseInt(value));
  1501. } else {
  1502. return new ExtendedConnectionParm(name, Integer.parseInt(value));
  1503. }
  1504. }
  1505. public String encodeConnectionParms(ConnectionParm[] parms) {
  1506. StringBuffer s = new StringBuffer("");
  1507. for (int i = 0; i < parms.length - 1; i++) {
  1508. s.append(encodeConnectionParm(parms[i])).append(",");
  1509. }
  1510. s.append(encodeConnectionParm(parms[parms.length - 1]));
  1511. return s.toString();
  1512. }
  1513. public ConnectionParm[] decodeConnectionParms(String value) {
  1514. String tokens[] = commaPattern.split(value, 0);
  1515. ConnectionParm[] parms = new ConnectionParm[tokens.length];
  1516. for (int i = 0; i < tokens.length; i++) {
  1517. parms[i] = decodeConnectionParm(tokens[i].trim());
  1518. }
  1519. return parms;
  1520. }
  1521. public ReasonCode decodeReasonCode(String value) {
  1522. String[] tokens = this.splitStringBySpace(value);
  1523. int code = Integer.parseInt(tokens[0]);
  1524. ReasonCode reasonCode = null;
  1525. switch (code) {
  1526. case ReasonCode.ENDPOINT_MALFUNCTIONING:
  1527. reasonCode = ReasonCode.Endpoint_Malfunctioning;
  1528. break;
  1529. case ReasonCode.ENDPOINT_OUT_OF_SERVICE:
  1530. reasonCode = ReasonCode.Endpoint_Out_Of_Service;
  1531. break;
  1532. case ReasonCode.ENDPOINT_STATE_IS_NOMINAL:
  1533. reasonCode = ReasonCode.Endpoint_State_Is_Nominal;
  1534. break;
  1535. case ReasonCode.LOSS_OF_LOWER_LAYER_CONNECTIVITY:
  1536. reasonCode = ReasonCode.Loss_Of_Lower_Layer_Connectivity;
  1537. break;
  1538. }
  1539. return reasonCode;
  1540. }
  1541. public NotifiedEntity decodeNotifiedEntity(String value, boolean toMGW) throws ParseException {
  1542. NotifiedEntity notifiedEntity = null;
  1543. String localName = null;
  1544. String domainName = null;
  1545. int port = 0;
  1546. String domainAndPort[] = null;
  1547. try {
  1548. String tokens[] = null;
  1549. try {
  1550. this.split(AMPERSAND, value, list);
  1551. tokens = new String[list.size()];
  1552. list.toArray(tokens);
  1553. } finally {
  1554. list.clear();
  1555. }
  1556. if (tokens.length == 2) {
  1557. localName = tokens[0];
  1558. domainAndPort = colonPattern.split(tokens[1], 0);
  1559. domainName = domainAndPort[0];
  1560. } else {
  1561. domainAndPort = colonPattern.split(tokens[0], 0);
  1562. domainName = domainAndPort[0];
  1563. return new NotifiedEntity(domainName);
  1564. }
  1565. if (domainAndPort.length == 2) {
  1566. port = Integer.parseInt(domainAndPort[1]);
  1567. } else if (toMGW) {
  1568. // See 3.5 of RFC3435. "by the Call Agents, to the default
  1569. // MGCP port for gateways, 2427."
  1570. port = 2427;
  1571. } else {
  1572. port = 2727;
  1573. }
  1574. notifiedEntity = new NotifiedEntity(localName, domainName, port);
  1575. } catch (Exception ex) {
  1576. throw new ParseException("unable to parse the " + value + " Message = " + ex.getMessage(), 0);
  1577. }
  1578. return notifiedEntity;
  1579. }
  1580. public String encodeNotifiedEntity(NotifiedEntity n) {
  1581. StringBuffer s = new StringBuffer("");
  1582. if (n.getLocalName() != null) {
  1583. s.append(n.getLocalName()).append("@");
  1584. }
  1585. if (n.getDomainName() != null) {
  1586. s.append(n.getDomainName());
  1587. }
  1588. if (n.getPortNumber() > 0) {
  1589. s.append(":").append(n.getPortNumber());
  1590. }
  1591. return s.toString();
  1592. }
  1593. public static void main(String args[]) {
  1594. Utils u = new Utils();
  1595. String text = "blllablaa@whatever.com";
  1596. EndpointIdentifier edId = null;
  1597. long currentTime = System.currentTimeMillis();
  1598. for (int i = 0; i < 10000; i++) {
  1599. edId = u.decodeEndpointIdentifier(text);
  1600. }
  1601. long opeTime = System.currentTimeMillis() - currentTime;
  1602. System.out.println("took " + opeTime);
  1603. }
  1604. }