PageRenderTime 36ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/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/*
 24 * File Name     : MgcpMessageParser.java
 25 *
 26 * The JAIN MGCP API implementaion.
 27 *
 28 * The source code contained in this file is in in the public domain.
 29 * It can be used in any project or product without prior permission,
 30 * license or royalty payments. There is  NO WARRANTY OF ANY KIND,
 31 * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION,
 32 * THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
 33 * AND DATA ACCURACY.  We do not warrant or make any representations
 34 * regarding the use of the software or the  results thereof, including
 35 * but not limited to the correctness, accuracy, reliability or
 36 * usefulness of the software.
 37 */
 38
 39package org.mobicents.protocols.mgcp.parser;
 40
 41import java.io.IOException;
 42import java.io.BufferedReader;
 43import java.io.StringReader;
 44
 45import java.text.ParseException;
 46import org.apache.log4j.Logger;
 47
 48
 49/**
 50 * Provides processing of the MGCP message.
 51 *
 52 * @author Oleg Kulikov
 53 * @author Pavel Mitrenko
 54 */
 55public class MgcpMessageParser {
 56  
 57    private MgcpContentHandler contentHandler;
 58    private static final Logger logger = Logger.getLogger(MgcpMessageParser.class);
 59    
 60    /** Creates a new instance of MgcpMessageParser */
 61    public MgcpMessageParser(MgcpContentHandler contentHandler) {
 62        if (contentHandler == null) {
 63            throw new IllegalArgumentException("Content handler cannot be null");
 64        }
 65        this.contentHandler = contentHandler;
 66    }
 67    
 68    public void parse(String message) throws IOException, ParseException {
 69        StringReader stringReader = new StringReader(message);
 70        BufferedReader reader = new BufferedReader(stringReader);
 71        
 72        String header = reader.readLine();
 73        
 74//        if (logger.isDebugEnabled()) {
 75//            logger.debug("Read header: " + header);
 76//        }
 77        contentHandler.header(header);
 78
 79        boolean sdpPresent = false;
 80        String line = null;
 81        
 82        while ((line = reader.readLine()) != null) {
 83            line = line.trim();
 84//            if (logger.isDebugEnabled()) {
 85//                logger.debug("Read line: " + line);
 86//            }
 87            
 88            sdpPresent = line.length() == 0;
 89            if (sdpPresent) break;
 90            
 91            int pos = line.indexOf(':');
 92            
 93            if (pos < 0) {
 94                logger.warn("Unrecognized parameter: " + line);
 95                continue;
 96            }
 97            
 98            
 99            String parmName = line.substring(0, pos).trim();
100            String parmValue = line.substring(pos + 1).trim();
101            
102            contentHandler.param(parmName, parmValue);
103        }
104        
105        boolean hasMore = false;
106        String ss = null;
107        try {
108            ss = reader.readLine();
109        } catch (IOException e) {
110            
111        }
112        hasMore = ss != null;
113        
114        if (sdpPresent || hasMore) {
115            String sdp = ss + "\n";
116            while ((line = reader.readLine()) != null) {
117                sdp = sdp + line.trim() + "\r\n";
118            }
119            if (logger.isDebugEnabled()) {
120                logger.debug("Read session description: " + sdp);
121            }
122            contentHandler.sessionDescription(sdp);
123        }
124    }
125}