summaryrefslogtreecommitdiffstats
path: root/java/gov/nist/javax/sip/address/Authority.java
blob: c277dd1498ed6807e51c4584cb89de8192e8ae72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Conditions Of Use
*
* This software was developed by employees of the National Institute of
* Standards and Technology (NIST), an agency of the Federal Government.
* Pursuant to title 15 Untied States Code Section 105, works of NIST
* employees are not subject to copyright protection in the United States
* and are considered to be in the public domain.  As a result, a formal
* license is not needed to use the software.
*
* This software is provided by NIST as a service and is expressly
* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  NIST does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement
*
* .
*
*/
/*******************************************************************************
* Product of NIST/ITL Advanced Networking Technologies Division (ANTD).        *
*******************************************************************************/
package gov.nist.javax.sip.address;
import gov.nist.core.*;

/**
 * Authority part of a URI structure. Section 3.2.2 RFC2396
 *
 * @version 1.2 $Revision: 1.10 $ $Date: 2009/12/16 14:48:33 $
 *
 * @author M. Ranganathan   <br/>
 *
 *
 *
 */
public class Authority extends NetObject {

    private static final long serialVersionUID = -3570349777347017894L;

    /** hostport field
     */
    protected HostPort hostPort;

    /** userInfo field
     */
    protected UserInfo userInfo;

    /**
     * Return the host name in encoded form.
     * @return encoded string (does the same thing as toString)
     */
    public String encode() {
        return encode(new StringBuffer()).toString();
    }

    public StringBuffer encode(StringBuffer buffer) {
        if (userInfo != null) {
            userInfo.encode(buffer);
            buffer.append(AT);
            hostPort.encode(buffer);
        } else {
            hostPort.encode(buffer);
        }
        return buffer;
    }

    /** retruns true if the two Objects are equals , false otherwise.
     * @param other Object to test.
     * @return boolean
     */
    @Override
    public boolean equals(Object other) {
        if (other == null) return false;
        if (other.getClass() != getClass()) {
            return false;
        }
        Authority otherAuth = (Authority) other;
        if (!this.hostPort.equals(otherAuth.hostPort)) {
            return false;
        }
        if (this.userInfo != null && otherAuth.userInfo != null) {
            if (!this.userInfo.equals(otherAuth.userInfo)) {
                return false;
            }
        }
        return true;
    }

    /**
     * get the hostPort member.
     * @return HostPort
     */
    public HostPort getHostPort() {
        return hostPort;
    }

    /**
     * get the userInfo memnber.
     * @return UserInfo
     */
    public UserInfo getUserInfo() {
        return userInfo;
    }

    /**
         * Get password from the user info.
         * @return String
         */
    public String getPassword() {
        if (userInfo == null)
            return null;
        else
            return userInfo.password;
    }

    /**
     * Get the user name if it exists.
     * @return String user or null if not set.
     */
    public String getUser() {
        return userInfo != null ? userInfo.user : null;
    }

    /**
     * Get the host name.
     * @return Host (null if not set)
     */
    public Host getHost() {
        if (hostPort == null)
            return null;
        else
            return hostPort.getHost();
    }

    /**
     * Get the port.
     * @return int port (-1) if port is not set.
     */
    public int getPort() {
        if (hostPort == null)
            return -1;
        else
            return hostPort.getPort();
    }

    /** remove the port.
     */
    public void removePort() {
        if (hostPort != null)
            hostPort.removePort();
    }

    /**
     * set the password.
     * @param passwd String to set
     */
    public void setPassword(String passwd) {
        if (userInfo == null)
            userInfo = new UserInfo();
        userInfo.setPassword(passwd);
    }

    /**
     * Set the user name of the userInfo member.
     * @param user String to set
     */
    public void setUser(String user) {
        if (userInfo == null)
            userInfo = new UserInfo();
        this.userInfo.setUser(user);
    }

    /**
     * set the host.
     * @param host Host to set
     */
    public void setHost(Host host) {
        if (hostPort == null)
            hostPort = new HostPort();
        hostPort.setHost(host);
    }

    /**
     * Set the port.
     * @param port int to set
     */
    public void setPort(int port) {
        if (hostPort == null)
            hostPort = new HostPort();
        hostPort.setPort(port);
    }

    /**
         * Set the hostPort member
         * @param h HostPort to set
         */
    public void setHostPort(HostPort h) {
        hostPort = h;
    }

    /**
         * Set the userInfo member
         * @param u UserInfo to set
         */
    public void setUserInfo(UserInfo u) {
        userInfo = u;
    }

    /** Remove the user Infor.
    *
    */
    public void removeUserInfo() {
        this.userInfo = null;
    }

    public Object clone() {
        Authority retval = (Authority) super.clone();
        if (this.hostPort != null)
            retval.hostPort = (HostPort) this.hostPort.clone();
        if (this.userInfo != null)
            retval.userInfo = (UserInfo) this.userInfo.clone();
        return retval;
    }
    
    @Override
    public int hashCode() {
        if ( this.hostPort == null ) throw new UnsupportedOperationException("Null hostPort cannot compute hashcode");
        return this.hostPort.encode().hashCode();
    }
}