001: /**
002: * $RCSfile$
003: * $Revision: $
004: * $Date: $
005: *
006: * Copyright (C) 2007 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.openfire.sip.tester.stack;
011:
012: import org.jivesoftware.openfire.sip.tester.Log;
013:
014: import javax.sip.SipStack;
015: import javax.sip.address.Hop;
016: import javax.sip.address.Router;
017: import javax.sip.address.SipURI;
018: import javax.sip.address.URI;
019: import javax.sip.header.RouteHeader;
020: import javax.sip.message.Request;
021: import java.util.LinkedList;
022: import java.util.ListIterator;
023:
024: /**
025: * <p/>
026: * Title: SIP COMMUNICATOR
027: * </p>
028: * <p/>
029: * Description:JAIN-SIP Audio/Video phone application
030: * </p>
031: * <p/>
032: * Copyright: Copyright (c) 2003
033: * </p>
034: * <p/>
035: * Organisation: LSIIT laboratory (http://lsiit.u-strasbg.fr)
036: * </p>
037: * <p/>
038: * Network Research Team (http://www-r2.u-strasbg.fr))
039: * </p>
040: * <p/>
041: * Louis Pasteur University - Strasbourg - France
042: * </p>
043: *
044: * @author Emil Ivov (http://www.emcho.com)
045: * @version 1.1
046: */
047: public class SipCommRouter implements Router {
048: protected SipStack myStack;
049:
050: protected SipCommHop outboundProxy = null;
051:
052: protected Router stackRouter = null;
053:
054: public SipCommRouter(SipStack sipStack, String outboundProxy) {
055: outboundProxy = SIPConfig.getOutboundProxy();
056: this .myStack = sipStack;
057: if (outboundProxy != null && outboundProxy.length() > 0) {
058: this .outboundProxy = new SipCommHop(outboundProxy);
059: }
060: }
061:
062: /**
063: * Return the default address to forward the request to. The list is
064: * organized in the following priority.
065: * <p/>
066: * If the outboung proxy has been specified, then it is used to construct
067: * the first element of the list.
068: * <p/>
069: * If the requestURI refers directly to a host, the host and port
070: * information are extracted from it and made the next hop on the list.
071: *
072: * @param sipRequest is the sip request to route.
073: */
074: public ListIterator getNextHops(Request sipRequest) {
075:
076: URI requestURI = sipRequest.getRequestURI();
077: if (requestURI == null) {
078: throw new IllegalArgumentException(
079: "Bad message: Null requestURI");
080: }
081: LinkedList hops = new LinkedList();
082: if (outboundProxy != null) {
083: hops.add(outboundProxy);
084: }
085: ListIterator routes = sipRequest.getHeaders(RouteHeader.NAME);
086: if (routes != null && routes.hasNext()) {
087: while (routes.hasNext()) {
088: RouteHeader route = (RouteHeader) routes.next();
089: SipURI uri = (SipURI) route.getAddress().getURI();
090: int port = uri.getPort();
091: port = (port == -1) ? 5060 : port;
092: String host = uri.getHost();
093: Log.debug("getNextHops", host);
094: String transport = uri.getTransportParam();
095: if (transport == null) {
096: transport = "udp";
097: }
098: Hop hop = new SipCommHop(host + ':' + port + '/'
099: + transport);
100: hops.add(hop);
101: }
102: } else if (requestURI instanceof SipURI
103: && ((SipURI) requestURI).getMAddrParam() != null) {
104: SipURI sipURI = ((SipURI) requestURI);
105: String maddr = sipURI.getMAddrParam();
106: String transport = sipURI.getTransportParam();
107: if (transport == null) {
108: transport = "udp";
109: }
110: int port = 5060;
111: Hop hop = new SipCommHop(maddr, port, transport);
112: hops.add(hop);
113: } else if (requestURI instanceof SipURI) {
114: SipURI sipURI = ((SipURI) requestURI);
115: int port = sipURI.getPort();
116: if (port == -1) {
117: port = 5060;
118: }
119: String host = sipURI.getHost();
120: String transport = sipURI.getTransportParam();
121: if (transport == null) {
122: transport = "UDP";
123: }
124: Hop hop = new SipCommHop(host + ":" + port + "/"
125: + transport);
126: hops.add(hop);
127: } else {
128: throw new IllegalArgumentException("Malformed requestURI");
129: }
130: return (hops.size() == 0) ? null : hops.listIterator();
131: }
132:
133: /**
134: * @return Returns the outboundProxy.
135: */
136: public Hop getOutboundProxy() {
137: return this .outboundProxy;
138: }
139:
140: protected void setOutboundProxy(String proxy) {
141: if (SIPConfig.getOutboundProxy() != null
142: && SIPConfig.getOutboundProxy().length() > 0) {
143: this .outboundProxy = new SipCommHop(proxy);
144: }
145: }
146:
147: }
|