Java tutorial
/* * Copyright (C) 2011 - Jingle Nodes - Yuilop - Neppo * * This file is part of Switji (http://jinglenodes.org) * * Switji is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Switji is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MjSip; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Author(s): * Benhur Langoni (bhlangonijr@gmail.com) * Thiago Camargo (barata7@gmail.com) */ package org.jinglenodes.credit; import org.apache.log4j.Logger; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Namespace; import org.dom4j.QName; import org.jinglenodes.prepare.NodeFormat; import org.jinglenodes.session.CallSession; import org.jinglenodes.session.CallSessionMapper; import org.xmpp.component.AbstractServiceProcessor; import org.xmpp.component.IqRequest; import org.xmpp.packet.IQ; import org.xmpp.packet.JID; import org.xmpp.tinder.JingleIQ; import org.zoolu.sip.message.JIDFactory; import java.util.IllegalFormatException; /** * Created by IntelliJ IDEA. * User: thiago * Date: 3/26/12 * Time: 1:54 PM */ public class CreditServiceProcessor extends AbstractServiceProcessor { private final Logger log = Logger.getLogger(CreditServiceProcessor.class); private final Element requestElement; private final String xmlns; private CallSessionMapper sessionMapper; private String creditService; private NodeFormat nodeFormat; public CreditServiceProcessor(final String elementName, final String xmlns) { this.xmlns = xmlns; this.requestElement = DocumentHelper.createElement(new QName(elementName, new Namespace("", xmlns))); } @Override public IQ createServiceRequest(Object object, String fromNode, String toNode) { if (object instanceof JingleIQ) { final IQ request = new IQ(IQ.Type.set); toNode = nodeFormat.formatNode(toNode, fromNode); final JID to = JIDFactory.getInstance().getJID(null, creditService, null); final JID from = JIDFactory.getInstance().getJID(fromNode, this.getComponentJID().getDomain(), null); final JingleIQ jingleIQ = (JingleIQ) object; request.setTo(to); request.setFrom(from); request.setChildElement(requestElement.createCopy()); final String toBareJid = JIDFactory.getInstance().getJID(toNode, creditService, null).toBareJID(); SessionCredit sessionCredit = new SessionCredit(SessionCredit.RouteType.ip); sessionCredit.setMaxDurationInSeconds(0); sessionCredit.setInitiator(from.toBareJID()); sessionCredit.setResponder(toBareJid); final CallSession session = sessionMapper.getSession(jingleIQ); if (session != null) { session.setSessionCredit(sessionCredit); } final Element e = request.getChildElement(); e.addAttribute("initiator", sessionCredit.getInitiator()); e.addAttribute("responder", sessionCredit.getResponder()); e.addAttribute("sid", jingleIQ.getJingle().getSid()); log.debug("createCreditRequest: " + request.toXML()); return request; } return null; } @Override protected String getRequestId(Object obj) { if (obj instanceof JingleIQ) { final JingleIQ iq = (JingleIQ) obj; return iq.getJingle().getSid(); } return null; } @Override protected void handleResult(IqRequest iq) { if (iq.getOriginalPacket() instanceof JingleIQ) { log.debug("Credit Value Received: " + iq.getResult().toXML()); final CallSession session = sessionMapper.getSession((JingleIQ) iq.getOriginalPacket()); if (session != null) { final SessionCredit sessionCredit = getSessionCredit(iq.getResult(), session.getSessionCredit()); } } } /* * Create the SessionCredit * the information retrieved from the iq */ protected SessionCredit getSessionCredit(final IQ iq, final SessionCredit sessionCredit) { String maxseconds = null; String type = null; String user = null; log.debug("Get Credit Value Received: " + iq.toXML()); final Element e = iq.getChildElement(); maxseconds = e.attributeValue("maxseconds"); type = e.attributeValue("type"); user = e.attributeValue("initiator"); if (maxseconds != null && type != null) { try { final int seconds = Integer.parseInt(maxseconds); sessionCredit.setMaxDurationInSeconds(seconds); final SessionCredit.RouteType rt = SessionCredit.RouteType.valueOf(type); sessionCredit.setRouteType(rt); } catch (IllegalFormatException ife) { log.error("Invalid Credit Value Received: " + iq.toXML(), ife); } catch (IllegalArgumentException ife) { log.error("Invalid Route Type Value Received: " + iq.toXML(), ife); } } else { log.debug("Call Initialized with Default Credits: " + iq.toXML()); } return sessionCredit; } @Override protected void handleError(IqRequest iqRequest) { log.error("Failed to Lock Credits: " + iqRequest.getResult().toXML()); if (iqRequest.getOriginalPacket() instanceof JingleIQ) { final CallSession session = sessionMapper.getSession((JingleIQ) iqRequest.getOriginalPacket()); if (session != null && session.getSessionCredit() != null) { log.warn("Forcing Call Credit for: " + iqRequest.getResult().toXML()); final SessionCredit sessionCredit = new SessionCredit(SessionCredit.RouteType.pstn); session.setSessionCredit(sessionCredit); } } } @Override protected void handleTimeout(IqRequest request) { } @Override public String getNamespace() { return xmlns; } public String getCreditService() { return creditService; } public void setCreditService(String creditService) { this.creditService = creditService; } public CallSessionMapper getSessionMapper() { return sessionMapper; } public void setSessionMapper(CallSessionMapper sessionMapper) { this.sessionMapper = sessionMapper; } public NodeFormat getNodeFormat() { return nodeFormat; } public void setNodeFormat(NodeFormat nodeFormat) { this.nodeFormat = nodeFormat; } }