PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 405 lines | 276 code | 48 blank | 81 comment | 78 complexity | 656a02b05bd11afe0061579bffdfbc25 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. package org.mobicents.protocols.mgcp.stack;
  23. import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
  24. import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
  25. import jain.protocol.ip.mgcp.message.AuditEndpoint;
  26. import jain.protocol.ip.mgcp.message.AuditEndpointResponse;
  27. import jain.protocol.ip.mgcp.message.parms.BearerInformation;
  28. import jain.protocol.ip.mgcp.message.parms.CapabilityValue;
  29. import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
  30. import jain.protocol.ip.mgcp.message.parms.DigitMap;
  31. import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
  32. import jain.protocol.ip.mgcp.message.parms.EventName;
  33. import jain.protocol.ip.mgcp.message.parms.InfoCode;
  34. import jain.protocol.ip.mgcp.message.parms.NotifiedEntity;
  35. import jain.protocol.ip.mgcp.message.parms.ReasonCode;
  36. import jain.protocol.ip.mgcp.message.parms.RequestIdentifier;
  37. import jain.protocol.ip.mgcp.message.parms.RequestedEvent;
  38. import jain.protocol.ip.mgcp.message.parms.RestartMethod;
  39. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  40. import java.io.IOException;
  41. import java.net.InetAddress;
  42. import java.text.ParseException;
  43. import java.util.ArrayList;
  44. import java.util.Collections;
  45. import org.apache.log4j.Logger;
  46. import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
  47. import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
  48. import org.mobicents.protocols.mgcp.parser.Utils;
  49. /**
  50. *
  51. * @author amit bhayani
  52. *
  53. */
  54. public class AuditEndpointHandler extends TransactionHandler {
  55. private static final Logger logger = Logger.getLogger(AuditEndpointHandler.class);
  56. private AuditEndpoint command;
  57. private AuditEndpointResponse response;
  58. public AuditEndpointHandler(JainMgcpStackImpl stack) {
  59. super(stack);
  60. }
  61. public AuditEndpointHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
  62. super(stack, address, port);
  63. }
  64. @Override
  65. public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
  66. Utils utils = utilsFactory.allocate();
  67. MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
  68. try {
  69. parser.parse(message);
  70. } catch (Exception e) {
  71. throw new ParseException(e.getMessage(), -1);
  72. } finally {
  73. utilsFactory.deallocate(utils);
  74. }
  75. return command;
  76. }
  77. @Override
  78. public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
  79. Utils utils = utilsFactory.allocate();
  80. MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
  81. try {
  82. parser.parse(message);
  83. } catch (IOException e) {
  84. logger.error("Decoding of AUEP Response failed", e);
  85. } finally {
  86. utilsFactory.deallocate(utils);
  87. }
  88. return response;
  89. }
  90. @Override
  91. public String encode(JainMgcpCommandEvent event) {
  92. Utils utils = utilsFactory.allocate();
  93. // encode message header
  94. AuditEndpoint evt = (AuditEndpoint) event;
  95. StringBuffer s = new StringBuffer();
  96. s.append("AUEP ").append(evt.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
  97. evt.getEndpointIdentifier()).append(MGCP_VERSION).append(NEW_LINE);
  98. // encode mandatory parameters
  99. InfoCode[] requestedInfos = evt.getRequestedInfo();
  100. if (requestedInfos != null) {
  101. s.append("F: ").append(utils.encodeInfoCodeList(requestedInfos));
  102. }
  103. utilsFactory.deallocate(utils);
  104. // return msg;
  105. return s.toString();
  106. }
  107. @Override
  108. public String encode(JainMgcpResponseEvent event) {
  109. Utils utils = utilsFactory.allocate();
  110. AuditEndpointResponse response = (AuditEndpointResponse) event;
  111. ReturnCode returnCode = response.getReturnCode();
  112. StringBuffer s = new StringBuffer();
  113. s.append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(response.getTransactionHandle()).append(
  114. SINGLE_CHAR_SPACE).append(returnCode.getComment()).append(NEW_LINE);
  115. if (response.getCapabilities() != null) {
  116. // TODO How to insert a new line with A : for different set of
  117. // compression Algo?
  118. s.append("A: ").append(utils.encodeCapabilityList(response.getCapabilities())).append(NEW_LINE);
  119. }
  120. if (response.getBearerInformation() != null) {
  121. s.append("B: ").append(utils.encodeBearerInformation(response.getBearerInformation())).append(NEW_LINE);
  122. }
  123. ConnectionIdentifier[] connectionIdentifiers = response.getConnectionIdentifiers();
  124. if (connectionIdentifiers != null) {
  125. s.append("I: ");
  126. // msg += "I:";
  127. boolean first = true;
  128. for (int i = 0; i < connectionIdentifiers.length; i++) {
  129. if (first) {
  130. first = false;
  131. } else {
  132. s.append(",");
  133. }
  134. s.append(connectionIdentifiers[i].toString());
  135. }
  136. s.append(NEW_LINE);
  137. }
  138. if (response.getNotifiedEntity() != null) {
  139. s.append("N: ").append(utils.encodeNotifiedEntity(response.getNotifiedEntity())).append(NEW_LINE);
  140. }
  141. if (response.getRequestIdentifier() != null) {
  142. s.append("X: ").append(response.getRequestIdentifier()).append(NEW_LINE);
  143. }
  144. RequestedEvent[] r = response.getRequestedEvents();
  145. if (r != null) {
  146. s.append("R: ").append(utils.encodeRequestedEvents(r)).append(NEW_LINE);
  147. }
  148. EventName[] sEvet = response.getSignalRequests();
  149. if (sEvet != null) {
  150. s.append("S: ").append(utils.encodeEventNames(sEvet)).append(NEW_LINE);
  151. }
  152. if (response.getDigitMap() != null) {
  153. s.append("D: ").append(response.getDigitMap()).append(NEW_LINE);
  154. }
  155. EventName[] o = response.getObservedEvents();
  156. if (o != null) {
  157. s.append("O: ").append(utils.encodeEventNames(o)).append(NEW_LINE);
  158. }
  159. if (response.getReasonCode() != null) {
  160. s.append("E: ").append(response.getReasonCode()).append(NEW_LINE);
  161. }
  162. EventName[] t = response.getDetectEvents();
  163. if (t != null) {
  164. s.append("T: ").append(utils.encodeEventNames(t)).append(NEW_LINE);
  165. }
  166. EventName[] es = response.getEventStates();
  167. if (es != null) {
  168. s.append("ES: ").append(utils.encodeEventNames(es)).append(NEW_LINE);
  169. }
  170. if (response.getRestartMethod() != null) {
  171. s.append("RM: ").append(response.getRestartMethod()).append(NEW_LINE);
  172. }
  173. if (response.getRestartDelay() > 0) {
  174. s.append("RD: ").append(response.getRestartDelay()).append(NEW_LINE);
  175. }
  176. EndpointIdentifier[] z = response.getEndpointIdentifierList();
  177. if (z != null) {
  178. s.append("Z: ").append(utils.encodeEndpointIdentifiers(z)).append(NEW_LINE);
  179. }
  180. utilsFactory.deallocate(utils);
  181. return s.toString();
  182. }
  183. @Override
  184. public JainMgcpResponseEvent getProvisionalResponse() {
  185. AuditEndpointResponse provisionalResponse = null;
  186. if (!sent) {
  187. provisionalResponse = new AuditEndpointResponse(source != null ? source : stack,
  188. ReturnCode.Transaction_Being_Executed);
  189. provisionalResponse.setTransactionHandle(remoteTID);
  190. }
  191. return provisionalResponse;
  192. }
  193. private class CommandContentHandle implements MgcpContentHandler {
  194. Utils utils = null;
  195. public CommandContentHandle(Utils utils) {
  196. this.utils = utils;
  197. }
  198. /**
  199. * Receive notification of the header of a message. Parser will call
  200. * this method to report about header reading.
  201. *
  202. * @param header
  203. * the header from the message.
  204. */
  205. public void header(String header) throws ParseException {
  206. command = new AuditEndpoint(source != null ? source : stack, endpoint);
  207. command.setTransactionHandle(remoteTID);
  208. }
  209. /**
  210. * Receive notification of the parameter of a message. Parser will call
  211. * this method to report about parameter reading.
  212. *
  213. * @param name
  214. * the name of the parameter
  215. * @param value
  216. * the value of the parameter.
  217. */
  218. public void param(String name, String value) throws ParseException {
  219. if (name.equalsIgnoreCase("F")) {
  220. command.setRequestedInfo(utils.decodeInfoCodeList(value));
  221. } else {
  222. logger.error("Unknown code " + name);
  223. }
  224. }
  225. /**
  226. * Receive notification of the session description. Parser will call
  227. * this method to report about session descriptor reading.
  228. *
  229. * @param sd
  230. * the session description from message.
  231. */
  232. public void sessionDescription(String sd) throws ParseException {
  233. throw new ParseException("SessionDescription shouldn't have been included in AUEP command", 0);
  234. }
  235. }
  236. private class ResponseContentHandle implements MgcpContentHandler {
  237. Utils utils = null;
  238. public ResponseContentHandle(Utils utils) {
  239. this.utils = utils;
  240. }
  241. /**
  242. * Receive notification of the header of a message. Parser will call
  243. * this method to report about header reading.
  244. *
  245. * @param header
  246. * the header from the message.
  247. */
  248. public void header(String header) throws ParseException {
  249. String[] tokens = utils.splitStringBySpace(header);
  250. int tid = Integer.parseInt(tokens[1]);
  251. response = new AuditEndpointResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
  252. .parseInt(tokens[0])));
  253. response.setTransactionHandle(tid);
  254. }
  255. /**
  256. * Receive notification of the parameter of a message. Parser will call
  257. * this method to report about parameter reading.
  258. *
  259. * @param name
  260. * the name of the paremeter
  261. * @param value
  262. * the value of the parameter.
  263. */
  264. public void param(String name, String value) throws ParseException {
  265. if (name.equals("Z")) {
  266. EndpointIdentifier[] endpointIdentifierList = utils.decodeEndpointIdentifiers(value);
  267. response.setEndpointIdentifierList(endpointIdentifierList);
  268. }
  269. if (name.equalsIgnoreCase("B")) {
  270. BearerInformation b = utils.decodeBearerInformation(value);
  271. response.setBearerInformation(b);
  272. } else if (name.equalsIgnoreCase("I")) {
  273. ConnectionIdentifier[] is = response.getConnectionIdentifiers();
  274. if (is == null) {
  275. ConnectionIdentifier i = new ConnectionIdentifier(value);
  276. response.setConnectionIdentifiers(new ConnectionIdentifier[] { i });
  277. } else {
  278. ArrayList<ConnectionIdentifier> arrayList = new ArrayList<ConnectionIdentifier>();
  279. Collections.addAll(arrayList, is);
  280. arrayList.add(new ConnectionIdentifier(value));
  281. ConnectionIdentifier[] temp = new ConnectionIdentifier[arrayList.size()];
  282. response.setConnectionIdentifiers(arrayList.toArray(temp));
  283. }
  284. } else if (name.equalsIgnoreCase("N")) {
  285. NotifiedEntity n = utils.decodeNotifiedEntity(value, true);
  286. response.setNotifiedEntity(n);
  287. } else if (name.equalsIgnoreCase("X")) {
  288. RequestIdentifier r = new RequestIdentifier(value);
  289. response.setRequestIdentifier(r);
  290. } else if (name.equalsIgnoreCase("R")) {
  291. RequestedEvent[] r = utils.decodeRequestedEventList(value);
  292. response.setRequestedEvents(r);
  293. } else if (name.equalsIgnoreCase("S")) {
  294. EventName[] s = utils.decodeEventNames(value);
  295. response.setSignalRequests(s);
  296. } else if (name.equalsIgnoreCase("D")) {
  297. DigitMap d = new DigitMap(value);
  298. response.setDigitMap(d);
  299. } else if (name.equalsIgnoreCase("O")) {
  300. EventName[] o = utils.decodeEventNames(value);
  301. response.setObservedEvents(o);
  302. } else if (name.equalsIgnoreCase("E")) {
  303. ReasonCode e = utils.decodeReasonCode(value);
  304. response.setReasonCode(e);
  305. } else if (name.equalsIgnoreCase("Q")) {
  306. // response.set
  307. } else if (name.equalsIgnoreCase("T")) {
  308. EventName[] t = utils.decodeEventNames(value);
  309. response.setDetectEvents(t);
  310. } else if (name.equalsIgnoreCase("A")) {
  311. CapabilityValue[] capabilities = response.getCapabilities();
  312. if (capabilities == null) {
  313. response.setCapabilities(utils.decodeCapabilityList(value));
  314. } else {
  315. CapabilityValue[] newCapability = utils.decodeCapabilityList(value);
  316. int size = capabilities.length + newCapability.length;
  317. CapabilityValue[] temp = new CapabilityValue[size];
  318. int count = 0;
  319. for (int i = 0; i < capabilities.length; i++) {
  320. temp[count] = capabilities[i];
  321. count++;
  322. }
  323. for (int j = 0; j < newCapability.length; j++) {
  324. temp[count] = newCapability[j];
  325. count++;
  326. }
  327. response.setCapabilities(temp);
  328. }
  329. } else if (name.equalsIgnoreCase("ES")) {
  330. EventName[] es = utils.decodeEventNames(value);
  331. response.setEventStates(es);
  332. } else if (name.equalsIgnoreCase("RM")) {
  333. RestartMethod rm = utils.decodeRestartMethod(value);
  334. response.setRestartMethod(rm);
  335. } else if (name.equalsIgnoreCase("RD")) {
  336. int restartDelay = 0;
  337. try {
  338. restartDelay = Integer.parseInt(value);
  339. } catch (NumberFormatException nfe) {
  340. logger.error("RD throws error " + value, nfe);
  341. }
  342. response.setRestartDelay(restartDelay);
  343. }
  344. }
  345. /**
  346. * Receive notification of the session description. Parser will call
  347. * this method to report about session descriptor reading.
  348. *
  349. * @param sd
  350. * the session description from message.
  351. */
  352. public void sessionDescription(String sd) throws ParseException {
  353. // response.setLocalConnectionDescriptor(new
  354. // ConnectionDescriptor(sd));
  355. }
  356. }
  357. }