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

http://mobicents.googlecode.com/ · Java · 125 lines · 54 code · 22 blank · 49 comment · 13 complexity · cb6443ebfe5b842406a3a83f773b3452 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. /*
  23. * File Name : MgcpMessageParser.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 java.io.IOException;
  39. import java.io.BufferedReader;
  40. import java.io.StringReader;
  41. import java.text.ParseException;
  42. import org.apache.log4j.Logger;
  43. /**
  44. * Provides processing of the MGCP message.
  45. *
  46. * @author Oleg Kulikov
  47. * @author Pavel Mitrenko
  48. */
  49. public class MgcpMessageParser {
  50. private MgcpContentHandler contentHandler;
  51. private static final Logger logger = Logger.getLogger(MgcpMessageParser.class);
  52. /** Creates a new instance of MgcpMessageParser */
  53. public MgcpMessageParser(MgcpContentHandler contentHandler) {
  54. if (contentHandler == null) {
  55. throw new IllegalArgumentException("Content handler cannot be null");
  56. }
  57. this.contentHandler = contentHandler;
  58. }
  59. public void parse(String message) throws IOException, ParseException {
  60. StringReader stringReader = new StringReader(message);
  61. BufferedReader reader = new BufferedReader(stringReader);
  62. String header = reader.readLine();
  63. // if (logger.isDebugEnabled()) {
  64. // logger.debug("Read header: " + header);
  65. // }
  66. contentHandler.header(header);
  67. boolean sdpPresent = false;
  68. String line = null;
  69. while ((line = reader.readLine()) != null) {
  70. line = line.trim();
  71. // if (logger.isDebugEnabled()) {
  72. // logger.debug("Read line: " + line);
  73. // }
  74. sdpPresent = line.length() == 0;
  75. if (sdpPresent) break;
  76. int pos = line.indexOf(':');
  77. if (pos < 0) {
  78. logger.warn("Unrecognized parameter: " + line);
  79. continue;
  80. }
  81. String parmName = line.substring(0, pos).trim();
  82. String parmValue = line.substring(pos + 1).trim();
  83. contentHandler.param(parmName, parmValue);
  84. }
  85. boolean hasMore = false;
  86. String ss = null;
  87. try {
  88. ss = reader.readLine();
  89. } catch (IOException e) {
  90. }
  91. hasMore = ss != null;
  92. if (sdpPresent || hasMore) {
  93. String sdp = ss + "\n";
  94. while ((line = reader.readLine()) != null) {
  95. sdp = sdp + line.trim() + "\r\n";
  96. }
  97. if (logger.isDebugEnabled()) {
  98. logger.debug("Read session description: " + sdp);
  99. }
  100. contentHandler.sessionDescription(sdp);
  101. }
  102. }
  103. }