PageRenderTime 23ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

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