To.java revision 600c7a4bbc7348167293eac928192e695b4ad5ba
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 *******************************************************************************/
29package gov.nist.javax.sip.header;
30
31import gov.nist.core.HostPort;
32import gov.nist.javax.sip.address.AddressImpl;
33import gov.nist.javax.sip.parser.Parser;
34
35import javax.sip.header.ToHeader;
36import java.text.ParseException;
37
38/**
39 * To SIP Header.
40 *
41 * @version 1.2 $Revision: 1.11 $ $Date: 2009/07/17 18:57:39 $
42 *
43 * @author M. Ranganathan <br/>
44 * @author Olivier Deruelle <br/>
45 *
46 *
47 *
48 */
49
50public final class To extends AddressParametersHeader implements
51        javax.sip.header.ToHeader {
52
53    /**
54     * Comment for <code>serialVersionUID</code>
55     */
56    private static final long serialVersionUID = -4057413800584586316L;
57
58    /**
59     * default Constructor.
60     */
61    public To() {
62        super(TO,true);
63    }
64
65    /**
66     * Generate a TO header from a FROM header
67     */
68    public To(From from) {
69        super(TO);
70        setAddress(from.address);
71        setParameters(from.parameters);
72    }
73
74    /**
75     * Encode the header into a String.
76     *
77     * @since 1.0
78     * @return String
79     */
80    public String encode() {
81        return headerName + COLON + SP + encodeBody() + NEWLINE;
82    }
83
84    /**
85     * Encode the header content into a String.
86     *
87     * @return String
88     */
89    protected String encodeBody() {
90        return encodeBody(new StringBuffer()).toString();
91    }
92
93    protected StringBuffer encodeBody(StringBuffer buffer) {
94        if (address != null) {
95            if (address.getAddressType() == AddressImpl.ADDRESS_SPEC) {
96                buffer.append(LESS_THAN);
97            }
98            address.encode(buffer);
99            if (address.getAddressType() == AddressImpl.ADDRESS_SPEC) {
100                buffer.append(GREATER_THAN);
101            }
102
103            if (!parameters.isEmpty()) {
104                buffer.append(SEMICOLON);
105                parameters.encode(buffer);
106            }
107        }
108        return buffer;
109    }
110
111    /**
112     * Conveniance accessor function to get the hostPort field from the address.
113     * Warning -- this assumes that the embedded URI is a SipURL.
114     *
115     * @return hostport field
116     */
117    public HostPort getHostPort() {
118        if (address == null)
119            return null;
120        return address.getHostPort();
121    }
122
123    /**
124     * Get the display name from the address.
125     *
126     * @return Display name
127     */
128    public String getDisplayName() {
129        if (address == null)
130            return null;
131        return address.getDisplayName();
132    }
133
134    /**
135     * Get the tag parameter from the address parm list.
136     *
137     * @return tag field
138     */
139    public String getTag() {
140        if (parameters == null)
141            return null;
142        return getParameter(ParameterNames.TAG);
143
144    }
145
146    /**
147     * Boolean function
148     *
149     * @return true if the Tag exist
150     */
151    public boolean hasTag() {
152        if (parameters == null)
153            return false;
154        return hasParameter(ParameterNames.TAG);
155
156    }
157
158    /**
159     * remove Tag member
160     */
161    public void removeTag() {
162            if (parameters != null)
163                parameters.delete(ParameterNames.TAG);
164
165    }
166
167    /**
168     * Set the tag member. This should remain empty for the initial request in
169     * a dialog.
170     *
171     * @param t - tag String to set.
172     */
173    public void setTag(String t) throws ParseException {
174        // JvB: check that it is a valid token
175        Parser.checkToken(t);
176        this.setParameter(ParameterNames.TAG, t);
177    }
178
179    /**
180     * Get the user@host port string.
181     */
182    public String getUserAtHostPort() {
183        if (address == null)
184            return null;
185        return address.getUserAtHostPort();
186    }
187
188    public boolean equals(Object other) {
189        return (other instanceof ToHeader) && super.equals(other);
190    }
191}
192