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: package gov.nist.siplite.header;
026:
027: import gov.nist.siplite.parser.*;
028: import gov.nist.siplite.address.*;
029: import gov.nist.core.*;
030:
031: /**
032: * An abstract class for headers that take an address and parameters.
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:
041: public abstract class AddressParametersHeader extends ParametersHeader {
042: /** Saved address. */
043: protected Address address;
044:
045: /**
046: * Gets the Address field.
047: * @return the imbedded Address
048: */
049: public Address getAddress() {
050: return address;
051: }
052:
053: /**
054: * Sets the Address field.
055: * @param address Address to set
056: */
057: public void setAddress(Address address) {
058: this .address = (Address) address;
059: }
060:
061: /**
062: * Constructor given the name of the header.
063: * @param name header to process
064: */
065: protected AddressParametersHeader(String name) {
066: super (name);
067: }
068:
069: /**
070: * Gets the address value.
071: * @return the address value
072: */
073: public Object getValue() {
074: return address;
075: }
076:
077: /**
078: * Sets the header value field.
079: * @param value is the value field to set.
080: * @throws IllegalArgumentException if the value is invalid.
081: */
082: public void setHeaderValue(String value)
083: throws IllegalArgumentException {
084: Header header;
085: StringMsgParser smp = new StringMsgParser();
086: String strNewHeader = getName() + Separators.COLON + value;
087:
088: try {
089: header = smp.parseHeader(strNewHeader);
090:
091: if (header instanceof HeaderList) {
092: header = ((HeaderList) header).getFirst();
093: }
094:
095: setAddress(((AddressParametersHeader) header).getAddress());
096: } catch (ParseException e) {
097: throw new IllegalArgumentException(e.toString());
098: }
099: }
100:
101: /**
102: * Gets the user friendly display name.
103: * @return the display name
104: */
105: public String getDisplayName() {
106: return address.getDisplayName();
107: }
108:
109: /**
110: * Gets the user at host and port name.
111: * @return user@host:port
112: */
113: public String getUserAtHostPort() {
114: return address.getUserAtHostPort();
115: }
116:
117: /**
118: * Gets the host and port name.
119: * @return host:port
120: */
121: public HostPort getHostPort() {
122: return address.getHostPort();
123: }
124:
125: /**
126: * Compares obejct for equivalence.
127: * @param other the object to compare
128: * @return true if matches
129: */
130: public boolean equals(Object other) {
131: if (!other.getClass().equals(this .getClass()))
132: return false;
133: Address otherAddress = ((AddressParametersHeader) other)
134: .getAddress();
135: if (otherAddress == null)
136: return false;
137: if (!otherAddress.equals(address)) {
138: return false;
139: }
140: if (!parameters
141: .equals(((AddressParametersHeader) other).parameters)) {
142: return false;
143: } else
144: return true;
145: }
146:
147: /**
148: * Encode the header content into a String.
149: * @return String
150: */
151: public String encodeBody() {
152: if (address == null) {
153: throw new RuntimeException("No body!");
154: }
155: StringBuffer retval = new StringBuffer();
156: if (address.getAddressType() != Address.NAME_ADDR) {
157: retval.append("<");
158: }
159: retval.append(address.encode());
160: if (address.getAddressType() != Address.NAME_ADDR) {
161: retval.append(">");
162: }
163: retval.append(encodeWithSep());
164: return retval.toString();
165: }
166:
167: /**
168: * Copies the current instance.
169: * @return copy of currrent object
170: */
171: public Object clone() {
172: Exception ex = null;
173: try {
174: AddressParametersHeader retval = (AddressParametersHeader) this
175: .getClass().newInstance();
176: if (this .address != null)
177: retval.address = (Address) this .address.clone();
178: if (this .parameters != null)
179: retval.parameters = (NameValueList) this .parameters
180: .clone();
181: return retval;
182: } catch (InstantiationException ie) {
183: ex = ie;
184: } catch (IllegalAccessException iae) {
185: ex = iae;
186: }
187: if (ex != null) {
188: InternalErrorHandler.handleException(ex);
189: }
190: return null;
191: }
192: }
|