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: /*
026: */
027: package gov.nist.core;
028:
029: import gov.nist.siplite.parser.Lexer;
030:
031: /**
032: * Stores hostname.
033: *@version JAIN-SIP-1.1
034: *
035: *
036: *<a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
037: *
038: *
039: *
040: */
041: public class Host {
042: /** Type of host is a textual host name. */
043: protected static final int HOSTNAME = 1;
044: /** Type of the host is a numeric IPV4 address. */
045: protected static final int IPV4ADDRESS = 2;
046: /** Type of the host is a numeric IPV6 address. */
047: protected static final int IPV6ADDRESS = 3;
048:
049: /** Host name field. */
050: protected String hostname;
051:
052: /** Address field. */
053: protected int addressType;
054:
055: /** Default constructor. */
056: public Host() {
057: addressType = HOSTNAME;
058: }
059:
060: /**
061: * Constructor given host name or IP address.
062: * This method checks the type of and IP address
063: * to determine if it is an IpV4 or IPv6 address.
064: *
065: * @param newHostName host name to be stored.
066: * @throws IllegalArgumentException in case of invalid host name
067: */
068: public Host(String newHostName) throws IllegalArgumentException {
069: setHostname(newHostName);
070: }
071:
072: /**
073: * Return the host name in encoded form.
074: * @return the host name saved when the instance was
075: * created
076: */
077: public String encode() {
078: if (addressType == IPV6ADDRESS && '[' != hostname.charAt(0))
079: return "[" + hostname + "]";
080: return hostname;
081: }
082:
083: /**
084: * Compares for equality of hosts.
085: * Host names are compared by textual equality. No dns lookup
086: * is performed.
087: * @param obj Object to set
088: * @return boolean
089: */
090: public boolean equals(Object obj) {
091: if (!this .getClass().equals(obj.getClass())) {
092: return false;
093: }
094: Host otherHost = (Host) obj;
095: return otherHost.hostname.equals(hostname);
096:
097: }
098:
099: /**
100: * Gets the HostName field.
101: * @return the host name saved when the instance was created
102: */
103: public String getHostname() {
104: return hostname;
105: }
106:
107: /**
108: * Gets the Address field.
109: * @return the host name saved when the instance was created
110: */
111: public String getAddress() {
112: return hostname;
113: }
114:
115: /**
116: * Sets the hostname member.
117: * @param h host name to set
118: * @throws IllegalArgumentException in case of invalid host name
119: */
120: public void setHostname(String h) throws IllegalArgumentException {
121: if (h == null) {
122: throw new IllegalArgumentException("Null address");
123: }
124: h = h.trim().toLowerCase();
125:
126: // IPv4 has stronger restriction than hostname
127: if (Lexer.isValidIpv4Address(h)) {
128: addressType = IPV4ADDRESS;
129: } else if (Lexer.isValidHostname(h)) {
130: addressType = HOSTNAME;
131: } else {
132: String addr = h;
133: // IPv6 reference?
134: if (h.charAt(0) == '[' && h.charAt(h.length() - 1) == ']') {
135: addr = h.substring(1, h.length() - 1);
136: }
137: if (!Lexer.isValidIpv6Address(addr)) {
138: throw new IllegalArgumentException("Illegal hostname "
139: + addr);
140: }
141: }
142: hostname = h;
143: }
144:
145: /**
146: * Sets the address member.
147: * @param address address to set
148: * @throws IllegalArgumentException in case of invalid host name
149: */
150: public void setAddress(String address)
151: throws IllegalArgumentException {
152: setHostname(address);
153: }
154:
155: /**
156: * Returns true if the address is a DNS host name
157: * (and not an IPV4 address).
158: * @return true if the hostname is a DNS name
159: */
160: public boolean isHostname() {
161: return addressType == HOSTNAME;
162: }
163:
164: /**
165: * Returns true if the address is a DNS host name
166: * (and not an IPV4 address).
167: * @return true if the hostname is host address.
168: */
169: public boolean isIPAddress() {
170: return addressType != HOSTNAME;
171: }
172:
173: /**
174: * Makes a copy of the current instance.
175: * @return copy of current object
176: */
177: public Object clone() {
178: Host retval = new Host();
179: retval.addressType = this .addressType;
180: retval.hostname = new String(this.hostname);
181: return retval;
182: }
183: }
|