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

http://mobicents.googlecode.com/ · Java · 215 lines · 149 code · 40 blank · 26 comment · 27 complexity · d4877b32edcc563bb222a11f88855b2b MD5 · raw file

  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.NotificationRequest;
  26. import jain.protocol.ip.mgcp.message.NotificationRequestResponse;
  27. import jain.protocol.ip.mgcp.message.parms.DigitMap;
  28. import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
  29. import jain.protocol.ip.mgcp.message.parms.NotifiedEntity;
  30. import jain.protocol.ip.mgcp.message.parms.RequestIdentifier;
  31. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  32. import java.io.IOException;
  33. import java.net.InetAddress;
  34. import java.text.ParseException;
  35. import org.apache.log4j.Logger;
  36. import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
  37. import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
  38. import org.mobicents.protocols.mgcp.parser.Utils;
  39. /**
  40. * @author Oleg Kulikov
  41. * @author Amit Bhayani
  42. *
  43. */
  44. public class NotificationRequestHandler extends TransactionHandler {
  45. private static final Logger logger = Logger.getLogger(NotificationRequestHandler.class);
  46. private NotificationRequest command;
  47. private NotificationRequestResponse response;
  48. public NotificationRequestHandler(JainMgcpStackImpl stack) {
  49. super(stack);
  50. }
  51. public NotificationRequestHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
  52. super(stack, address, port);
  53. }
  54. @Override
  55. public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
  56. Utils utils = utilsFactory.allocate();
  57. MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
  58. try {
  59. parser.parse(message);
  60. } catch (Exception e) {
  61. throw new ParseException(e.getMessage(), -1);
  62. } finally {
  63. utilsFactory.deallocate(utils);
  64. }
  65. NotifiedEntity notifiedEntity = command.getNotifiedEntity();
  66. if (command.getNotifiedEntity() != null) {
  67. this.stack.provider.setNotifiedEntity(notifiedEntity);
  68. }
  69. return command;
  70. }
  71. @Override
  72. public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
  73. Utils utils = utilsFactory.allocate();
  74. MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
  75. try {
  76. parser.parse(message);
  77. } catch (IOException e) {
  78. logger.error("Decode RQNT Response failed", e);
  79. } finally {
  80. utilsFactory.deallocate(utils);
  81. }
  82. return response;
  83. }
  84. @Override
  85. public String encode(JainMgcpCommandEvent event) {
  86. Utils utils = utilsFactory.allocate();
  87. NotificationRequest req = (NotificationRequest) event;
  88. StringBuffer buffer = new StringBuffer();
  89. buffer.append("RQNT ").append(event.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
  90. req.getEndpointIdentifier()).append(SINGLE_CHAR_SPACE).append(MGCP_VERSION).append(NEW_LINE);
  91. if (req.getNotifiedEntity() != null) {
  92. buffer.append("N:").append(req.getNotifiedEntity()).append(NEW_LINE);
  93. }
  94. buffer.append("X:").append(req.getRequestIdentifier()).append(NEW_LINE);
  95. if (req.getDigitMap() != null) {
  96. buffer.append("D:").append(req.getDigitMap()).append(NEW_LINE);
  97. }
  98. if (req.getSignalRequests() != null) {
  99. buffer.append("S:").append(utils.encodeEventNames(req.getSignalRequests())).append(NEW_LINE);
  100. }
  101. if (req.getRequestedEvents() != null) {
  102. buffer.append("R:").append(utils.encodeRequestedEvents(req.getRequestedEvents())).append(NEW_LINE);
  103. }
  104. if (req.getDetectEvents() != null) {
  105. buffer.append("T:").append(utils.encodeEventNames(req.getDetectEvents())).append(NEW_LINE);
  106. }
  107. utilsFactory.deallocate(utils);
  108. return buffer.toString();
  109. }
  110. @Override
  111. public String encode(JainMgcpResponseEvent event) {
  112. NotificationRequestResponse response = (NotificationRequestResponse) event;
  113. ReturnCode returnCode = response.getReturnCode();
  114. String encodedEvent = (new StringBuffer().append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(
  115. response.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(returnCode.getComment())
  116. .append(NEW_LINE)).toString();
  117. return encodedEvent;
  118. }
  119. private class CommandContentHandle implements MgcpContentHandler {
  120. private Utils utils = null;
  121. public CommandContentHandle(Utils utils) {
  122. this.utils = utils;
  123. }
  124. public void header(String header) throws ParseException {
  125. command = new NotificationRequest(source != null ? source : stack, endpoint, new RequestIdentifier("0"));
  126. command.setTransactionHandle(remoteTID);
  127. }
  128. public void param(String name, String value) throws ParseException {
  129. if (name.equalsIgnoreCase("N")) {
  130. command.setNotifiedEntity(utils.decodeNotifiedEntity(value, true));
  131. } else if (name.equalsIgnoreCase("X")) {
  132. command.setRequestIdentifier(new RequestIdentifier(value));
  133. } else if (name.equalsIgnoreCase("R")) {
  134. command.setRequestedEvents(utils.decodeRequestedEventList(value));
  135. } else if (name.equalsIgnoreCase("S")) {
  136. command.setSignalRequests(utils.decodeEventNames(value));
  137. } else if (name.equalsIgnoreCase("T")) {
  138. command.setDetectEvents(utils.decodeEventNames(value));
  139. } else if (name.equalsIgnoreCase("D")) {
  140. command.setDigitMap(new DigitMap(value));
  141. }
  142. }
  143. public void sessionDescription(String sd) throws ParseException {
  144. throw new UnsupportedOperationException("Not supported yet.");
  145. }
  146. }
  147. private class ResponseContentHandle implements MgcpContentHandler {
  148. private Utils utils;
  149. public ResponseContentHandle(Utils utils) {
  150. this.utils = utils;
  151. }
  152. public void header(String header) throws ParseException {
  153. String[] tokens = utils.splitStringBySpace(header);
  154. int tid = Integer.parseInt(tokens[1]);
  155. response = new NotificationRequestResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
  156. .parseInt(tokens[0])));
  157. response.setTransactionHandle(tid);
  158. }
  159. public void param(String name, String value) throws ParseException {
  160. throw new UnsupportedOperationException("Not supported yet.");
  161. }
  162. public void sessionDescription(String sd) throws ParseException {
  163. throw new UnsupportedOperationException("Not supported yet.");
  164. }
  165. }
  166. @Override
  167. public JainMgcpResponseEvent getProvisionalResponse() {
  168. NotificationRequestResponse provisionalresponse = null;
  169. if (!sent) {
  170. provisionalresponse = new NotificationRequestResponse(source != null ? source : stack,
  171. ReturnCode.Transaction_Being_Executed);
  172. }
  173. return provisionalresponse;
  174. }
  175. }