1 /*
2 * jSDP: A Java implementation of SDP protocol Copyright (C) 2007 Claudio Di
3 * Vita
4 */
5 package net.sourceforge.jsdp.util;
6
7 import java.io.Serializable;
8 import java.net.Inet4Address;
9 import java.net.InetAddress;
10 import java.net.UnknownHostException;
11 import java.util.regex.Pattern;
12
13 import net.sourceforge.jsdp.SDPException;
14
15 /**
16 * A generic SDP network address. There are three network address types:
17 * <ul>
18 * <li><b>IP4</b>: the standard IP address</li>
19 * <li><b>IP6</b>: the newer version of IP, still not used</li>
20 * <li><b>FQDN</b>: Fully Qualified Domain Name, that is a DNS host name</li>
21 * </ul>
22 *
23 * @since 0.1.0
24 *
25 * @version 1.0
26 *
27 * @author <a href="mailto:cdivita@users.sourceforge.net">Claudio Di Vita</a>
28 */
29 public class Address implements Cloneable, Serializable {
30
31 /** The class Stream Unique Identifier, SUID */
32 private static final long serialVersionUID = -3442566371424669052L;
33
34 /**
35 * FQDN format:
36 * <ul>
37 * <li>fqdn: <i><label></i> ("." <i><label></i>)</li>
38 * <li>label: <code>([a-zA-Z]+(([\\w-]+)*[\\w]+)*)+</code>
39 * </ul>
40 */
41 private static final Pattern fqdnPattern = Pattern.compile("([a-zA-Z]+(([\\w-]+)*[\\w]+)*)+(\\.[a-zA-Z]+(([\\w-]+)*[\\w]+)*)*");
42
43 /** The Internet network type */
44 public static final String IN = "IN";
45
46 /** The FDQN address type */
47 private static final String FQDN = "FQDN";
48
49 /** The IP4 address type */
50 public static final String IP4 = "IP4";
51
52 /** The IP6 address type */
53 public static final String IP6 = "IP6";
54
55 /** The network address */
56 protected String address;
57
58 /** The address type */
59 protected String addressType;
60
61 /**
62 * Creates a new <tt>Address</tt>.
63 *
64 */
65 protected Address() {
66
67 super();
68
69 try {
70 setAddress(InetAddress.getLocalHost().getHostAddress());
71 }
72 catch (Exception someException) {
73 /* Do nothing */
74 }
75 }
76
77 /**
78 * Creates a new <tt>Address</tt>.
79 *
80 * @param address the address value
81 *
82 * @throws SDPException if the address is not valid
83 */
84 public Address(final String address) throws SDPException {
85
86 super();
87
88 if (address == null) {
89 throw new SDPException("Invalid host name");
90 }
91
92 setAddress(address);
93 }
94
95 /**
96 * Indicate if an address is a Fully Qualified Domain Name (and not an IP4
97 * or IP6 address)
98 *
99 * @param address the address to check
100 *
101 * @return <tt>true</tt> if the address is a Fully Qualified Domain Name,
102 * <tt>false</tt> otherwise
103 */
104 public static boolean isFQDN(final String address) {
105
106 return fqdnPattern.matcher(address).matches();
107 }
108
109 /**
110 * Returns a clone of this address.
111 *
112 * @return a clone of this address
113 */
114 public Object clone() {
115
116 Address host = new Address();
117
118 host.addressType = this.addressType;
119 host.address = new String(this.address);
120
121 return host;
122 }
123
124 /**
125 * Compare for equality of hosts. Host names are compared by textual
126 * equality. No dns lookup is performed.
127 *
128 * @param object object to compare
129 *
130 * @return <tt>true</tt> if two address are equals, <tt>false</tt>
131 * otherwise
132 */
133 public boolean equals(final Object object) {
134
135 if (!(object instanceof Address)) {
136 return false;
137 }
138
139 Address otherHost = (Address) object;
140 return otherHost.address.equalsIgnoreCase(this.address);
141 }
142
143 /**
144 * Returns the address value.
145 *
146 * @return the address value
147 */
148 public String getAddress() {
149
150 return address;
151 }
152
153 /**
154 * Returns the address type. Possible values are:
155 * <ul>
156 * <li><b>IP4</b>: the standard IP address</li>
157 * <li><b>IP6</b>: the newer version of IP, still not used</li>
158 * <li><b>FQDN</b>: Fully Qualified Domain Name, that is a DNS host name</li>
159 * </ul>
160 *
161 * @return the address type
162 */
163 public String getAddressType() {
164
165 return addressType;
166 }
167
168 /**
169 * Indicate if the address is a Fully Qualified Domain Name (and not an IP4
170 * or IP6 address).
171 *
172 * @return <tt>true</tt> if the address is a Fully Qualified Domain Name,
173 * <tt>false</tt> otherwise
174 */
175 public boolean isFQDN() {
176
177 return addressType.compareTo(FQDN) == 0;
178 }
179
180 /**
181 * Indicates if the address is an IP address (and not a Fully Qualified
182 * Domain name).
183 *
184 * @return <tt>true</tt> if is an IP address, <tt>false</tt> otherwise
185 */
186 public boolean isIPAddress() {
187
188 return addressType.compareTo(FQDN) != 0;
189 }
190
191 /**
192 * Sets the address value
193 *
194 * @param address the address value to set
195 *
196 * @throws SDPException if the address is not valid
197 */
198 public void setAddress(final String address) throws SDPException {
199
200 if (isFQDN(address)) {
201 this.address = address;
202 /*
203 * Because at this time all internet host has an IP4 address, if the
204 * host name is a FQDN, the address type is considered to be IP4
205 */
206 this.addressType = IP4;
207 }
208 else {
209 try {
210 InetAddress ip = InetAddress.getByName(address);
211 this.addressType = (ip instanceof Inet4Address) ? IP4 : IP6;
212 /*
213 * if (ip instanceof Inet4Address) { this.addressType = IP4; }
214 * else { this.addressType = IP6; }
215 */
216 this.address = address;
217 }
218 catch (UnknownHostException invalidIP) {
219 throw new SDPException("Not IP4 or IP6 address: " + address);
220 }
221 }
222 }
223
224 /**
225 * Returns a string representation of the address.
226 *
227 * @return The string representation of the address
228 */
229 public String toString() {
230
231 return addressType + " " + address;
232 }
233 }