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.header;
30 
31 /**
32  * Root class from which all SIPHeader objects are subclassed.
33  *
34  * @author M. Ranganathan   <br/>
35  * @version 1.2 $Revision: 1.7 $ $Date: 2009/07/17 18:57:37 $
36  *
37  *
38  */
39 public abstract class SIPHeader
40     extends SIPObject
41     implements SIPHeaderNames, javax.sip.header.Header, HeaderExt {
42 
43     /** name of this header
44      */
45     protected String headerName;
46 
47     /** Value of the header.
48     */
49 
50     /** Constructor
51      * @param hname String to set
52      */
SIPHeader(String hname)53     protected SIPHeader(String hname) {
54         headerName = hname;
55     }
56 
57     /** Default constructor
58      */
SIPHeader()59     public SIPHeader() {
60     }
61 
62     /**
63      * Name of the SIPHeader
64      * @return String
65      */
getHeaderName()66     public String getHeaderName() {
67         return headerName;
68     }
69 
70     /** Alias for getHaderName above.
71     *
72     *@return String headerName
73     *
74     */
getName()75     public String getName() {
76         return this.headerName;
77     }
78 
79     /**
80          * Set the name of the header .
81          * @param hdrname String to set
82          */
setHeaderName(String hdrname)83     public void setHeaderName(String hdrname) {
84         headerName = hdrname;
85     }
86 
87     /** Get the header value (i.e. what follows the name:).
88     * This merely goes through and lops off the portion that follows
89     * the headerName:
90     */
getHeaderValue()91     public String getHeaderValue() {
92         String encodedHdr = null;
93         try {
94             encodedHdr = this.encode();
95         } catch (Exception ex) {
96             return null;
97         }
98         StringBuffer buffer = new StringBuffer(encodedHdr);
99         while (buffer.length() > 0 && buffer.charAt(0) != ':') {
100             buffer.deleteCharAt(0);
101         }
102         if (buffer.length() > 0)
103             buffer.deleteCharAt(0);
104         return buffer.toString().trim();
105     }
106 
107     /** Return false if this is not a header list
108     * (SIPHeaderList overrrides this method).
109     *@return false
110     */
isHeaderList()111     public boolean isHeaderList() {
112         return false;
113     }
114 
115     /** Encode this header into canonical form.
116     */
encode()117     public String encode() {
118         return encode(new StringBuffer()).toString();
119     }
120 
encode(StringBuffer buffer)121     public StringBuffer encode(StringBuffer buffer) {
122         buffer.append(this.headerName).append(COLON).append(SP);
123         this.encodeBody(buffer);
124         buffer.append(NEWLINE);
125         return buffer;
126     }
127 
128     /** Encode the body of this header (the stuff that follows headerName).
129     * A.K.A headerValue.
130     */
encodeBody()131     protected abstract String encodeBody();
132 
133     /** Encode the body of this header in the given buffer.
134      * Default implementation calls encodeBody();
135      */
encodeBody(StringBuffer buffer)136     protected StringBuffer encodeBody(StringBuffer buffer) {
137         return buffer.append(encodeBody());
138     }
139 
140     /** Alias for getHeaderValue.
141      */
getValue()142     public String getValue() {
143         return this.getHeaderValue();
144     }
145 
146     /**
147      * This is a pretty simple hashCode but satisfies requirements.
148      *
149      */
hashCode()150     public int hashCode() {
151         return this.headerName.hashCode();
152     }
153 
toString()154     public final String toString() {
155         return this.encode();
156     }
157 }
158