001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: package gov.nist.siplite.stack;
026:
027: import gov.nist.siplite.header.*;
028: import gov.nist.siplite.address.*;
029: import gov.nist.siplite.header.ViaHeader;
030:
031: import gov.nist.siplite.message.*;
032: import gov.nist.siplite.*;
033: import java.util.*;
034:
035: /**
036: * This is the default router. When the implementation wants to forward
037: * a request and had run out of othe options, then it calls this method
038: * to figure out where to send the request. The default router implements
039: * a simple "default routing algorithm" which just forwards to the configured
040: * proxy address.
041: */
042:
043: public class DefaultRouter implements Router {
044: /** Default route. */
045: protected Hop defaultRoute;
046: /** Current SIP stack context. */
047: protected SipStack sipStack;
048:
049: /** Default constructor. */
050: public DefaultRouter() {
051: }
052:
053: /**
054: * Sets the next hop address.
055: * @param hopString is a string which is interpreted
056: * by us in the following fashion :
057: * host:port/TRANSPORT determines the next hop.
058: */
059: public void setNextHop(String hopString)
060: throws IllegalArgumentException {
061: defaultRoute = new Hop(hopString);
062: defaultRoute.setDefaultRouteFlag();
063: }
064:
065: /**
066: * Return a linked list of addresses corresponding to a requestURI.
067: * This is called for sending out outbound messages for which we do
068: * not directly have the request URI. The implementaion function
069: * is expected to return a linked list of addresses to which the
070: * request is forwarded. The implementation may use this method
071: * to perform location searches etc.
072: *
073: * @param sipRequest is the message to route.
074: * @param isDialog target URI is taken from route list inside of dialog,
075: * else it is taken from request URI
076: * @return enumeration of next hops
077: */
078: public Enumeration getNextHops(Request sipRequest, boolean isDialog)
079: throws IllegalArgumentException {
080: Vector hopList = new Vector();
081:
082: if (defaultRoute != null) {
083: hopList.addElement(defaultRoute);
084: } else {
085: URI requestUri = null;
086: String transport = null;
087: RouteList rl = sipRequest.getRouteHeaders();
088:
089: // When request has no route list,
090: // it's destination URI is same as out of dialog
091: if (rl == null || rl.isEmpty()) {
092: isDialog = false;
093: }
094:
095: Hop hop;
096:
097: // out of dialog - get destination URI from request URI
098: if (!isDialog) {
099: requestUri = sipRequest.getRequestURI();
100: SipURI requestLineUri = (SipURI) sipRequest
101: .getRequestLine().getUri();
102: if (requestLineUri.hasTransport()) {
103: transport = requestLineUri.getTransportParam();
104: } else {
105: transport = sipStack.getDefaultTransport();
106: }
107:
108: hop = createHop(requestUri, transport);
109: hopList.addElement(hop);
110: } else { // inside dialog - get destination URI from first route
111: Enumeration el = rl.getElements();
112: RouteHeader route;
113: while (el.hasMoreElements()) {
114: route = (RouteHeader) el.nextElement();
115: requestUri = route.getAddress().getURI();
116: transport = route.getAddress().getParameter(
117: SIPConstants.GENERAL_TRANSPORT);
118: hop = createHop(requestUri, transport);
119: hopList.addElement(hop);
120: }
121: }
122: }
123: return hopList.elements();
124: }
125:
126: /**
127: * Creates new Hop for given uri and transport
128: *
129: * @param requestUri next hop address
130: * @param transport next hop connection transport type
131: * @return new Hop if requestUri is SipURI
132: * otherwise null
133: */
134: private Hop createHop(URI requestUri, String transport) {
135: Hop hop = null;
136: if (requestUri.isSipURI()) {
137: SipURI sipUri = (SipURI) requestUri;
138: int port = sipUri.getPort();
139: if (port == -1) { // default port
140: port = SIPConstants.DEFAULT_NONTLS_PORT;
141: }
142: hop = new Hop(sipUri.getHost(), port, transport);
143: }
144: return hop;
145: }
146:
147: /**
148: * Gets the default hop.
149: * @return defaultRoute is the default route.
150: */
151: public Hop getOutboundProxy() {
152: return this .defaultRoute;
153: }
154:
155: /**
156: * Sets the outbound proxy.
157: * @param outboundProxy the new proxy location
158: */
159: public void setOutboundProxy(String outboundProxy) {
160: this .defaultRoute = new Hop(outboundProxy);
161: }
162:
163: /**
164: * Sets the SIP stack context.
165: * @param sipStack the new SIP stack
166: */
167: public void setSipStack(SipStack sipStack) {
168: this.sipStack = sipStack;
169: }
170: }
|