1 /** 2 * Copyright 2013 Florian Schmaus 3 * 4 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.jivesoftware.smack.util.dns; 17 18 public class HostAddress { 19 private String fqdn; 20 private int port; 21 private Exception exception; 22 23 /** 24 * Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222 25 * 26 * @param fqdn Fully qualified domain name. 27 * @throws IllegalArgumentException If the fqdn is null. 28 */ 29 public HostAddress(String fqdn) { 30 if (fqdn == null) 31 throw new IllegalArgumentException("FQDN is null"); 32 if (fqdn.charAt(fqdn.length() - 1) == '.') { 33 this.fqdn = fqdn.substring(0, fqdn.length() - 1); 34 } 35 else { 36 this.fqdn = fqdn; 37 } 38 // Set port to the default port for XMPP client communication 39 this.port = 5222; 40 } 41 42 /** 43 * Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222 44 * 45 * @param fqdn Fully qualified domain name. 46 * @param port The port to connect on. 47 * @throws IllegalArgumentException If the fqdn is null or port is out of valid range (0 - 65535). 48 */ 49 public HostAddress(String fqdn, int port) { 50 this(fqdn); 51 if (port < 0 || port > 65535) 52 throw new IllegalArgumentException( 53 "DNS SRV records weight must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port); 54 55 this.port = port; 56 } 57 58 public String getFQDN() { 59 return fqdn; 60 } 61 62 public int getPort() { 63 return port; 64 } 65 66 public void setException(Exception e) { 67 this.exception = e; 68 } 69 70 @Override 71 public String toString() { 72 return fqdn + ":" + port; 73 } 74 75 @Override 76 public boolean equals(Object o) { 77 if (this == o) { 78 return true; 79 } 80 if (!(o instanceof HostAddress)) { 81 return false; 82 } 83 84 final HostAddress address = (HostAddress) o; 85 86 if (!fqdn.equals(address.fqdn)) { 87 return false; 88 } 89 return port == address.port; 90 } 91 92 @Override 93 public int hashCode() { 94 int result = 1; 95 result = 37 * result + fqdn.hashCode(); 96 return result * 37 + port; 97 } 98 99 public String getErrorMessage() { 100 String error; 101 if (exception == null) { 102 error = "No error logged"; 103 } 104 else { 105 error = exception.getMessage(); 106 } 107 return toString() + " Exception: " + error; 108 } 109 } 110