001/** 002 * 003 * Copyright © 2013-2014 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.util.dns; 018 019import org.jivesoftware.smack.SmackException.ConnectionException; 020import org.jivesoftware.smack.util.Objects; 021 022public class HostAddress { 023 private final String fqdn; 024 private final int port; 025 private Exception exception; 026 027 /** 028 * Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222 029 * 030 * @param fqdn Fully qualified domain name. 031 * @throws IllegalArgumentException If the fqdn is null. 032 */ 033 public HostAddress(String fqdn) { 034 // Set port to the default port for XMPP client communication 035 this(fqdn, 5222); 036 } 037 038 /** 039 * Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222 040 * 041 * @param fqdn Fully qualified domain name. 042 * @param port The port to connect on. 043 * @throws IllegalArgumentException If the fqdn is null or port is out of valid range (0 - 65535). 044 */ 045 public HostAddress(String fqdn, int port) { 046 Objects.requireNonNull(fqdn, "FQDN is null"); 047 if (port < 0 || port > 65535) 048 throw new IllegalArgumentException( 049 "Port must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port); 050 if (fqdn.charAt(fqdn.length() - 1) == '.') { 051 this.fqdn = fqdn.substring(0, fqdn.length() - 1); 052 } 053 else { 054 this.fqdn = fqdn; 055 } 056 this.port = port; 057 } 058 059 public String getFQDN() { 060 return fqdn; 061 } 062 063 public int getPort() { 064 return port; 065 } 066 067 public void setException(Exception e) { 068 this.exception = e; 069 } 070 071 /** 072 * Retrieve the Exception that caused a connection failure to this HostAddress. Every 073 * HostAddress found in {@link ConnectionException} will have an Exception set, 074 * which can be retrieved with this method. 075 * 076 * @return the Exception causing this HostAddress to fail 077 */ 078 public Exception getException() { 079 return this.exception; 080 } 081 082 @Override 083 public String toString() { 084 return fqdn + ":" + port; 085 } 086 087 @Override 088 public boolean equals(Object o) { 089 if (this == o) { 090 return true; 091 } 092 if (!(o instanceof HostAddress)) { 093 return false; 094 } 095 096 final HostAddress address = (HostAddress) o; 097 098 if (!fqdn.equals(address.fqdn)) { 099 return false; 100 } 101 return port == address.port; 102 } 103 104 @Override 105 public int hashCode() { 106 int result = 1; 107 result = 37 * result + fqdn.hashCode(); 108 return result * 37 + port; 109 } 110 111 public String getErrorMessage() { 112 if (exception == null) { 113 return "No error logged"; 114 } 115 return "'" + toString() + "' failed because " + exception.toString(); 116 } 117}