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 java.util.StringTokenizer;
015:
016: /**
017: * Routing algorithms return a list of hops to which the request is routed.
018: *
019: * @author M. Ranganathan <mranga@nist.gov> <br/> <a href=" {@docRoot}
020: * /uncopyright.html">This code is in the public domain.</a> IPv6
021: * Support added by Emil Ivov (emil_ivov (at) yahoo (dot) com)<br/>
022: * Network Research Team (http://www-r2.u-strasbg.fr))<br/> Louis
023: * Pasteur University - Strasbourg - France<br/>
024: * @version JAIN-SIP-1.1
025: */
026: public class SipCommHop implements javax.sip.address.Hop {
027:
028: protected String host;
029: protected int port;
030: protected String transport;
031: protected boolean explicitRoute; // this is generated from a ROUTE
032: protected boolean defaultRoute; // This is generated from the proxy addr
033: protected boolean uriRoute; // This is extracted from the requestURI.
034:
035: /**
036: * Debugging println.
037: */
038: public String toString() {
039: return host + ":" + port + "/" + transport;
040: }
041:
042: /**
043: * Create new hop given host, port and transport.
044: *
045: * @param hostName hostname
046: * @param portNumber port
047: * @param trans transport
048: */
049: public SipCommHop(String hostName, int portNumber, String trans) {
050: host = hostName;
051: port = portNumber;
052: transport = trans;
053: }
054:
055: /**
056: * Creates new Hop
057: *
058: * @param hop is a hop string in the form of host:port/Transport
059: * @throws IllegalArgumentException exception if string is not properly formatted or null.
060: */
061: public SipCommHop(String hop) throws IllegalArgumentException {
062: if (hop == null) {
063: Log.debug("SipCommHop", "Null arg: " + hop);
064: throw new IllegalArgumentException("Null arg!");
065: }
066: StringTokenizer stringTokenizer = new StringTokenizer(hop + "/");
067: String hostPort = stringTokenizer.nextToken("/").trim();
068: transport = stringTokenizer.nextToken().trim();
069:
070: if (transport == null) {
071: transport = "UDP";
072: } else if (transport == "") {
073: transport = "UDP";
074: }
075: if (transport.compareToIgnoreCase("UDP") != 0
076: && transport.compareToIgnoreCase("TCP") != 0) {
077: throw new IllegalArgumentException(hop);
078: }
079: String portString = null;
080: // IPv6 hostport
081: if (hostPort.charAt(0) == '[') {
082: int rightSqBrackIndex = hostPort.indexOf(']');
083: if (rightSqBrackIndex == -1) {
084: throw new IllegalArgumentException(
085: "Bad IPv6 reference spec");
086: }
087: host = hostPort.substring(0, rightSqBrackIndex + 1);
088: int portColon = hostPort.indexOf(':', rightSqBrackIndex);
089: if (portColon != -1) {
090: try {
091: portString = hostPort.substring(portColon + 1)
092: .trim();
093: } catch (IndexOutOfBoundsException exc) {
094: // Do nothing - handled later
095: }
096: }
097: }
098: // IPv6 address and no port
099: else if (hostPort.indexOf(':') != hostPort.lastIndexOf(":")) {
100: host = '[' + hostPort + ']';
101: } else { // no square brackets and a single or zero colons => IPv4
102: // hostPort
103: int portColon = hostPort.indexOf(':');
104: if (portColon == -1) {
105: host = hostPort;
106: } else {
107: host = hostPort.substring(0, portColon).trim();
108: try {
109: portString = hostPort.substring(portColon + 1)
110: .trim();
111: } catch (IndexOutOfBoundsException exc) {
112: // Do nothing - handled later
113: }
114: }
115: }
116: if (host == null || host.equals("")) {
117: throw new IllegalArgumentException("no host!");
118: }
119: if (portString == null || portString.equals("")) {
120: port = 5060;
121: } else {
122: try {
123: port = Integer.parseInt(portString);
124: } catch (NumberFormatException ex) {
125: throw new IllegalArgumentException("Bad port spec");
126: }
127: }
128: }
129:
130: /**
131: * Retruns the host string.
132: *
133: * @return host String
134: * @uml.property name="host"
135: */
136: public String getHost() {
137: return host;
138: }
139:
140: /**
141: * Returns the port.
142: *
143: * @return port integer.
144: * @uml.property name="port"
145: */
146: public int getPort() {
147: return port;
148: }
149:
150: /**
151: * returns the transport string.
152: *
153: * @uml.property name="transport"
154: */
155: public String getTransport() {
156: return transport;
157: }
158:
159: /**
160: * Return true if this is an explicit route (ie. extrcted from a ROUTE
161: * Header)
162: *
163: * @uml.property name="explicitRoute"
164: */
165: public boolean isExplicitRoute() {
166: return explicitRoute;
167: }
168:
169: /**
170: * Return true if this is a default route (ie. next hop proxy address)
171: *
172: * @uml.property name="defaultRoute"
173: */
174: public boolean isDefaultRoute() {
175: return defaultRoute;
176: }
177:
178: /**
179: * Return true if this is uriRoute
180: */
181: public boolean isURIRoute() {
182: return uriRoute;
183: }
184:
185: /**
186: * Set the URIRoute flag.
187: */
188: public void setURIRouteFlag() {
189: uriRoute = true;
190: }
191:
192: /**
193: * Set the defaultRouteFlag.
194: */
195: public void setDefaultRouteFlag() {
196: defaultRoute = true;
197: }
198:
199: /**
200: * Set the explicitRoute flag.
201: */
202: public void setExplicitRouteFlag() {
203: explicitRoute = true;
204: }
205: }
|