Home | History | Annotate | Download | only in address
      1 /*
      2 * Conditions Of Use
      3 *
      4 * This software was developed by employees of the National Institute of
      5 * Standards and Technology (NIST), an agency of the Federal Government.
      6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
      7 * employees are not subject to copyright protection in the United States
      8 * and are considered to be in the public domain.  As a result, a formal
      9 * license is not needed to use the software.
     10 *
     11 * This software is provided by NIST as a service and is expressly
     12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
     13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
     14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
     15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
     16 * regarding the use of the software or the results thereof, including but
     17 * not limited to the correctness, accuracy, reliability or usefulness of
     18 * the software.
     19 *
     20 * Permission to use this software is contingent upon your acceptance
     21 * of the terms of this agreement
     22 *
     23 * .
     24 *
     25 */
     26 /*******************************************************************************
     27 * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).        *
     28 *******************************************************************************/
     29 package gov.nist.javax.sip.address;
     30 import gov.nist.core.*;
     31 
     32 /**
     33  * Authority part of a URI structure. Section 3.2.2 RFC2396
     34  *
     35  * @version 1.2 $Revision: 1.10 $ $Date: 2009/12/16 14:48:33 $
     36  *
     37  * @author M. Ranganathan   <br/>
     38  *
     39  *
     40  *
     41  */
     42 public class Authority extends NetObject {
     43 
     44     private static final long serialVersionUID = -3570349777347017894L;
     45 
     46     /** hostport field
     47      */
     48     protected HostPort hostPort;
     49 
     50     /** userInfo field
     51      */
     52     protected UserInfo userInfo;
     53 
     54     /**
     55      * Return the host name in encoded form.
     56      * @return encoded string (does the same thing as toString)
     57      */
     58     public String encode() {
     59         return encode(new StringBuffer()).toString();
     60     }
     61 
     62     public StringBuffer encode(StringBuffer buffer) {
     63         if (userInfo != null) {
     64             userInfo.encode(buffer);
     65             buffer.append(AT);
     66             hostPort.encode(buffer);
     67         } else {
     68             hostPort.encode(buffer);
     69         }
     70         return buffer;
     71     }
     72 
     73     /** retruns true if the two Objects are equals , false otherwise.
     74      * @param other Object to test.
     75      * @return boolean
     76      */
     77     @Override
     78     public boolean equals(Object other) {
     79         if (other == null) return false;
     80         if (other.getClass() != getClass()) {
     81             return false;
     82         }
     83         Authority otherAuth = (Authority) other;
     84         if (!this.hostPort.equals(otherAuth.hostPort)) {
     85             return false;
     86         }
     87         if (this.userInfo != null && otherAuth.userInfo != null) {
     88             if (!this.userInfo.equals(otherAuth.userInfo)) {
     89                 return false;
     90             }
     91         }
     92         return true;
     93     }
     94 
     95     /**
     96      * get the hostPort member.
     97      * @return HostPort
     98      */
     99     public HostPort getHostPort() {
    100         return hostPort;
    101     }
    102 
    103     /**
    104      * get the userInfo memnber.
    105      * @return UserInfo
    106      */
    107     public UserInfo getUserInfo() {
    108         return userInfo;
    109     }
    110 
    111     /**
    112          * Get password from the user info.
    113          * @return String
    114          */
    115     public String getPassword() {
    116         if (userInfo == null)
    117             return null;
    118         else
    119             return userInfo.password;
    120     }
    121 
    122     /**
    123      * Get the user name if it exists.
    124      * @return String user or null if not set.
    125      */
    126     public String getUser() {
    127         return userInfo != null ? userInfo.user : null;
    128     }
    129 
    130     /**
    131      * Get the host name.
    132      * @return Host (null if not set)
    133      */
    134     public Host getHost() {
    135         if (hostPort == null)
    136             return null;
    137         else
    138             return hostPort.getHost();
    139     }
    140 
    141     /**
    142      * Get the port.
    143      * @return int port (-1) if port is not set.
    144      */
    145     public int getPort() {
    146         if (hostPort == null)
    147             return -1;
    148         else
    149             return hostPort.getPort();
    150     }
    151 
    152     /** remove the port.
    153      */
    154     public void removePort() {
    155         if (hostPort != null)
    156             hostPort.removePort();
    157     }
    158 
    159     /**
    160      * set the password.
    161      * @param passwd String to set
    162      */
    163     public void setPassword(String passwd) {
    164         if (userInfo == null)
    165             userInfo = new UserInfo();
    166         userInfo.setPassword(passwd);
    167     }
    168 
    169     /**
    170      * Set the user name of the userInfo member.
    171      * @param user String to set
    172      */
    173     public void setUser(String user) {
    174         if (userInfo == null)
    175             userInfo = new UserInfo();
    176         this.userInfo.setUser(user);
    177     }
    178 
    179     /**
    180      * set the host.
    181      * @param host Host to set
    182      */
    183     public void setHost(Host host) {
    184         if (hostPort == null)
    185             hostPort = new HostPort();
    186         hostPort.setHost(host);
    187     }
    188 
    189     /**
    190      * Set the port.
    191      * @param port int to set
    192      */
    193     public void setPort(int port) {
    194         if (hostPort == null)
    195             hostPort = new HostPort();
    196         hostPort.setPort(port);
    197     }
    198 
    199     /**
    200          * Set the hostPort member
    201          * @param h HostPort to set
    202          */
    203     public void setHostPort(HostPort h) {
    204         hostPort = h;
    205     }
    206 
    207     /**
    208          * Set the userInfo member
    209          * @param u UserInfo to set
    210          */
    211     public void setUserInfo(UserInfo u) {
    212         userInfo = u;
    213     }
    214 
    215     /** Remove the user Infor.
    216     *
    217     */
    218     public void removeUserInfo() {
    219         this.userInfo = null;
    220     }
    221 
    222     public Object clone() {
    223         Authority retval = (Authority) super.clone();
    224         if (this.hostPort != null)
    225             retval.hostPort = (HostPort) this.hostPort.clone();
    226         if (this.userInfo != null)
    227             retval.userInfo = (UserInfo) this.userInfo.clone();
    228         return retval;
    229     }
    230 
    231     @Override
    232     public int hashCode() {
    233         if ( this.hostPort == null ) throw new UnsupportedOperationException("Null hostPort cannot compute hashcode");
    234         return this.hostPort.encode().hashCode();
    235     }
    236 }
    237