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.header;
028:
029: import gov.nist.core.*;
030: import java.util.*;
031:
032: /**
033: * This is a list for the headers.
034: *
035: *
036: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
037: *
038: */
039: public class HeaderList extends Header {
040: /** SIP header list. */
041: protected Vector sipHeaderVector;
042:
043: /**
044: * Copies the current instance.
045: * @return copy of the current instance
046: */
047: public Object clone() {
048: try {
049: HeaderList retval = (HeaderList) this .getClass()
050: .newInstance();
051: if (this .headerName != null)
052: retval.headerName = new String(this .headerName);
053: if (this .headerValue != null)
054: retval.headerValue = new String(this .headerValue);
055: retval.sipHeaderVector = new Vector();
056: for (int i = 0; i < sipHeaderVector.size(); i++) {
057: Header siphdr = (Header) sipHeaderVector.elementAt(i);
058: Header newHdr = (Header) siphdr.clone();
059: retval.sipHeaderVector.addElement(newHdr);
060: }
061: return (Object) retval;
062: } catch (Exception ex) {
063: ex.printStackTrace();
064: System.out.print("Problem with clone method");
065: System.exit(0);
066: return null;
067: }
068: }
069:
070: /**
071: * Default constructor
072: */
073: public HeaderList() {
074: sipHeaderVector = new Vector();
075: }
076:
077: /**
078: * Concatenates two compatible lists. This appends or prepends the new list
079: * to the end of this list.
080: * @param other HeaderList to set
081: * @param top boolean to set
082: */
083: public void concatenate(HeaderList other, boolean top) {
084: if (other != null) {
085: if (top) {
086: for (int i = 0; i < size(); i++) {
087: Header sipHeader = (Header) elementAt(i);
088: other.add(sipHeader);
089: }
090: } else {
091: for (int i = 0; i < other.size(); i++) {
092: Header sipHeader = (Header) other.elementAt(i);
093: add(sipHeader);
094: }
095: }
096: }
097: }
098:
099: /**
100: * Constructor with initial header.
101: * @param sipHeaderName to set
102: */
103: public HeaderList(String sipHeaderName) {
104: sipHeaderVector = new Vector();
105: this .headerName = sipHeaderName;
106: }
107:
108: /**
109: * Adds a new element.
110: * @param sipHeader to add
111: */
112: public void add(Object sipHeader) throws IllegalArgumentException {
113: if (headerName != null) {
114: String expandedName1 = NameMap.expandHeaderName(headerName);
115: String expandedName2 = NameMap
116: .expandHeaderName(((Header) sipHeader)
117: .getHeaderName());
118:
119: if (!expandedName1.equalsIgnoreCase(expandedName2)) {
120: throw new IllegalArgumentException("bad type");
121: }
122: }
123:
124: if (sipHeader != null)
125: sipHeaderVector.addElement(sipHeader);
126: }
127:
128: /**
129: * Adds a new element on the top of the list.
130: * @param sipHeader to add
131: */
132: public void addFirst(Object sipHeader) {
133: if (sipHeader != null) {
134: Vector vec = new Vector();
135: vec.addElement(sipHeader);
136: for (int i = 0; i < sipHeaderVector.size(); i++) {
137: vec.addElement(sipHeaderVector.elementAt(i));
138: }
139: sipHeaderVector = vec;
140: }
141: }
142:
143: /**
144: * Returns true if this is empty.
145: * @return true if no headers in the list
146: */
147: public boolean isEmpty() {
148: return sipHeaderVector.isEmpty();
149: }
150:
151: /**
152: * Returns the list size.
153: * @return size
154: */
155: public int size() {
156: return sipHeaderVector.size();
157: }
158:
159: /**
160: * Returns the element at the position i.
161: * @return Object
162: * @param i index of the requested element
163: */
164: public Object elementAt(int i) {
165: return sipHeaderVector.elementAt(i);
166: }
167:
168: /**
169: * Removes the specified element.
170: * @param element entry to delete
171: */
172: public void removeElement(Object element) {
173: sipHeaderVector.removeElement(element);
174: }
175:
176: /**
177: * Removes the first element of the list.
178: */
179: public void removeFirst() {
180: if (sipHeaderVector.size() == 0)
181: return;
182: else
183: sipHeaderVector.removeElementAt(0);
184:
185: }
186:
187: /**
188: * Removes the lastentry in the header list.
189: */
190: public void removeLast() {
191: if (sipHeaderVector.size() != 0) {
192: sipHeaderVector.removeElementAt(sipHeaderVector.size() - 1);
193: }
194: }
195:
196: /**
197: * Returns a vector of encoded strings (one for each sipheader).
198: * @return Vector containing encoded strings in this header list.
199: * an empty vector is returned if this header list contains no
200: * sip headers.
201: */
202: public Vector getHeadersAsEncodedStrings() {
203: Vector retval = new Vector();
204:
205: for (int i = 0; i < size(); i++) {
206: Header sipheader = (Header) elementAt(i);
207: retval.addElement(sipheader.encode());
208: }
209: return retval;
210:
211: }
212:
213: /**
214: * Returns an enumeration of the imbedded vector.
215: * @return an Enumeration of the elements of the vector.
216: */
217: public Enumeration getElements() {
218: return this .sipHeaderVector.elements();
219: }
220:
221: /**
222: * Gets the first element of the vector.
223: * @return the first element of the vector.
224: */
225: public Header getFirst() {
226: if (sipHeaderVector.size() == 0)
227: return null;
228: return (Header) this .sipHeaderVector.elementAt(0);
229: }
230:
231: /**
232: * Gets the first element of the vector.
233: * @return the first element of the vector.
234: */
235: public Object first() {
236: if (sipHeaderVector.size() == 0)
237: return null;
238: return this .sipHeaderVector.elementAt(0);
239: }
240:
241: /**
242: * Gets the last element of the vector.
243: * @return the last element of the vector.
244: */
245: public Object last() {
246: if (sipHeaderVector.size() == 0)
247: return null;
248: else
249: return (Header) this .sipHeaderVector
250: .elementAt(sipHeaderVector.size() - 1);
251:
252: }
253:
254: /**
255: * Gets the value of the header list.
256: * @return a vector of the header list contents
257: */
258: public Object getValue() {
259: Vector retval = new Vector();
260: for (int i = 0; i < size(); i++) {
261: Header sipheader = (Header) elementAt(i);
262: retval.addElement(sipheader);
263: }
264: return retval;
265: }
266:
267: /**
268: * Gets the parameters of the header list.
269: *
270: * @return always returns null
271: */
272: public NameValueList getParameters() {
273: return null;
274: }
275:
276: /**
277: * Encodes the contents as a string.
278: * @return encoded string of object contents
279: */
280: public String encode() {
281: if (sipHeaderVector.isEmpty() || headerName == null)
282: return "";
283:
284: StringBuffer encoding = new StringBuffer();
285:
286: // The following headers do not have comma separated forms for
287: // multiple headers. Thus, they must be encoded separately.
288: if (this .headerName.equals(WWW_AUTHENTICATE)
289: || this .headerName.equals(PROXY_AUTHENTICATE)
290: || this .headerName.equals(AUTHORIZATION)
291: || this .headerName.equals(PROXY_AUTHORIZATION)) {
292:
293: for (int i = 0; i < sipHeaderVector.size(); i++) {
294: Header sipheader = (Header) sipHeaderVector
295: .elementAt(i);
296: encoding.append(sipheader.encode());
297: }
298:
299: return encoding.toString();
300: } else {
301: // These can be concatenated together in an comma separated
302: // list.
303: return headerName + Separators.COLON + Separators.SP
304: + this .encodeBody() + Separators.NEWLINE;
305: }
306: }
307:
308: /**
309: * Encodes body as a string.
310: * @return encoded string of body contents
311: */
312: protected String encodeBody() {
313: StringBuffer sbuf = new StringBuffer();
314: for (int i = 0; i < sipHeaderVector.size(); i++) {
315: Header sipHeader = (Header) sipHeaderVector.elementAt(i);
316: sbuf.append(sipHeader.encodeBody());
317: if (i + 1 < sipHeaderVector.size())
318: sbuf.append(",");
319: }
320: return sbuf.toString();
321: }
322:
323: /**
324: * Gets the header list.
325: * @return the header list
326: */
327: public Vector getHeaders() {
328: return this.sipHeaderVector;
329: }
330: }
|