001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027: package gov.nist.siplite.address;
028:
029: import gov.nist.core.*;
030:
031: /**
032: * Authority part of a URI structure. Section 3.2.2 RFC2396
033: *
034: * @version JAIN-SIP-1.1
035: *
036: *
037: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
038: *
039: */
040: public class Authority extends GenericObject {
041:
042: /**
043: * Host and port field.
044: */
045: protected HostPort hostPort;
046:
047: /**
048: * User information field.
049: */
050: protected UserInfo userInfo;
051:
052: /**
053: * Returns the host name in encoded form.
054: * @return encoded string (does the same thing as toString)
055: */
056: public String encode() {
057: StringBuffer retval = new StringBuffer();
058:
059: if (userInfo != null) {
060: String user = userInfo.encode();
061: if (user != null) {
062: retval.append(user).append(Separators.AT);
063: }
064: }
065:
066: if (hostPort != null) {
067: retval.append(hostPort.encode());
068: }
069:
070: return retval.toString();
071: }
072:
073: /**
074: * Returns true if the two Objects are equals , false otherwise.
075: * @param other Object to test.
076: * @return true if objects match
077: */
078: public boolean equals(Object other) {
079: if (!other.getClass().getName().equals(
080: this .getClass().getName())) {
081: return false;
082: }
083: Authority otherAuth = (Authority) other;
084: if (!this .hostPort.equals(otherAuth.hostPort)) {
085: return false;
086: }
087: if (this .userInfo != null && otherAuth.userInfo != null) {
088: if (!this .userInfo.equals(otherAuth.userInfo)) {
089: return false;
090: }
091: }
092: return true;
093: }
094:
095: /**
096: * Gets the host and port member.
097: * @return the host and port
098: */
099: public HostPort getHostPort() {
100: return hostPort;
101: }
102:
103: /**
104: * Gets the user information memnber.
105: * @return the user information
106: */
107: public UserInfo getUserInfo() {
108: return userInfo;
109: }
110:
111: /**
112: * Gets the password from the user informatio.
113: * @return the password
114: */
115: public String getPassword() {
116: if (userInfo == null) {
117: return null;
118: } else {
119: return userInfo.getPassword();
120: }
121: }
122:
123: /**
124: * Gets the user name if it exists.
125: * @return user or null if not set.
126: */
127: public String getUser() {
128: return (userInfo != null) ? userInfo.getUser() : null;
129: }
130:
131: /**
132: * Gets the host name.
133: * @return Host (null if not set)
134: */
135: public Host getHost() {
136: if (hostPort == null)
137: return null;
138: else
139: return hostPort.getHost();
140: }
141:
142: /**
143: * Gets the port.
144: * @return port (-1) if port is not set.
145: */
146: public int getPort() {
147: if (hostPort == null)
148: return -1;
149: else
150: return hostPort.getPort();
151: }
152:
153: /**
154: * Removes the port.
155: */
156: public void removePort() {
157: if (hostPort != null) {
158: hostPort.removePort();
159: }
160: }
161:
162: /**
163: * Sets the password.
164: * @param passwd String to set
165: * @throws IllegalArgumentException if password contains invalid
166: * characters
167: */
168: public void setPassword(String passwd)
169: throws IllegalArgumentException {
170: if (userInfo == null)
171: userInfo = new UserInfo();
172: userInfo.setPassword(passwd);
173: }
174:
175: /**
176: * Sets the user name of the user information member.
177: * @param user String to set
178: * @throws IllegalArgumentException if user name contains invalid
179: * characters
180: */
181: public void setUser(String user) throws IllegalArgumentException {
182: if (userInfo == null) {
183: userInfo = new UserInfo();
184: }
185:
186: userInfo.setUser(user);
187: }
188:
189: /**
190: * Sets the host.
191: * @param host Host to set
192: */
193: public void setHost(Host host) {
194: if (hostPort == null) {
195: hostPort = new HostPort();
196: }
197:
198: hostPort.setHost(host);
199: }
200:
201: /**
202: * Sets the port.
203: * @param port int to set
204: */
205: public void setPort(int port) {
206: if (hostPort == null) {
207: hostPort = new HostPort();
208: }
209: hostPort.setPort(port);
210: }
211:
212: /**
213: * Sets the host and port member.
214: * @param h HostPort to set
215: */
216: public void setHostPort(HostPort h) {
217: hostPort = h;
218: }
219:
220: /**
221: * Sets the user information member.
222: * @param u UserInfo to set
223: */
224: public void setUserInfo(UserInfo u) {
225: userInfo = u;
226: }
227:
228: /**
229: * Removes the user information.
230: */
231: public void removeUserInfo() {
232: userInfo = null;
233: }
234:
235: /**
236: * Copies the object contents
237: * @return the copy of this object
238: */
239: public Object clone() {
240: Authority retval = new Authority();
241:
242: try {
243: retval.setUser(getUser());
244: retval.setPassword(getPassword());
245: } catch (IllegalArgumentException e) {
246: // intentionally ignored
247: // it shoild be impossible to get here due to getUser()
248: // and getPassword() return verified values
249: }
250: retval.setHostPort(getHostPort());
251:
252: return retval;
253: }
254:
255: }
|