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