summaryrefslogtreecommitdiff
path: root/java/gov/nist/javax/sip/header/SIPHeaderList.java
blob: f2483038467c3fee554dd3699138980a99ebc0fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
* Conditions Of Use
*
* This software was developed by employees of the National Institute of
* Standards and Technology (NIST), an agency of the Federal Government.
* Pursuant to title 15 Untied States Code Section 105, works of NIST
* employees are not subject to copyright protection in the United States
* and are considered to be in the public domain.  As a result, a formal
* license is not needed to use the software.
*
* This software is provided by NIST as a service and is expressly
* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  NIST does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement
*
* .
*
*/
/*******************************************************************************
 * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).       *
 ******************************************************************************/
package gov.nist.javax.sip.header;

import gov.nist.core.GenericObject;
import gov.nist.core.Separators;
import gov.nist.javax.sip.header.ims.PrivacyHeader;

import javax.sip.header.Header;
import java.lang.reflect.Constructor;
import java.util.*;

/**
 *
 * This is the root class for all lists of SIP headers. It imbeds a
 * SIPObjectList object and extends SIPHeader Lists of ContactSIPObjects etc.
 * derive from this class. This supports homogeneous lists (all elements in the
 * list are of the same class). We use this for building type homogeneous lists
 * of SIPObjects that appear in SIPHeaders
 *
 * @version 1.2 $Revision: 1.15 $ $Date: 2005/10/09 18:47:53
 */
public abstract class SIPHeaderList<HDR extends SIPHeader> extends SIPHeader implements java.util.List<HDR>, Header {

    private static boolean prettyEncode = false;
    /**
     * hlist field.
     */
    protected List<HDR> hlist;

    private Class<HDR> myClass;

    public String getName() {
        return this.headerName;
    }


    private SIPHeaderList() {
        hlist = new LinkedList<HDR>();
    }

    /**
     * Constructor
     *
     * @param objclass
     *            Class to set
     * @param hname
     *            String to set
     */
    protected SIPHeaderList(Class<HDR> objclass, String hname) {
        this();
        this.headerName = hname;
        this.myClass =  objclass;
    }

    /**
     * Concatenate the list of stuff that we are keeping around and also the
     * text corresponding to these structures (that we parsed).
     *
     * @param objectToAdd
     */
    public boolean add(HDR objectToAdd) {
        hlist.add((HDR)objectToAdd);
        return true;
    }

    /**
     * Concatenate the list of stuff that we are keeping around and also the
     * text corresponding to these structures (that we parsed).
     *
     * @param obj
     *            Genericobject to set
     */
    public void addFirst(HDR obj) {
        hlist.add(0,(HDR) obj);
    }

    /**
     * Add to this list.
     *
     * @param sipheader
     *            SIPHeader to add.
     * @param top
     *            is true if we want to add to the top of the list.
     */
    public void add(HDR sipheader, boolean top) {
        if (top)
            this.addFirst(sipheader);
        else
            this.add(sipheader);
    }

    /**
     * Concatenate two compatible lists. This appends or prepends the new list
     * to the end of this list.
     *
     * @param other
     *            SIPHeaderList to set
     * @param topFlag
     *            flag which indicates which end to concatenate
     *            the lists.
     * @throws IllegalArgumentException
     *             if the two lists are not compatible
     */
    public void concatenate(SIPHeaderList<HDR> other, boolean topFlag)
            throws IllegalArgumentException {
        if (!topFlag) {
            this.addAll(other);
        } else {
            // add given items to the top end of the list.
            this.addAll(0, other);
        }

    }



    /**
     * Encode a list of sip headers. Headers are returned in cannonical form.
     *
     * @return String encoded string representation of this list of headers.
     *         (Contains string append of each encoded header).
     */
    public String encode() {
        return encode(new StringBuffer()).toString();
    }

    public StringBuffer encode(StringBuffer buffer) {
        if (hlist.isEmpty()) {
            buffer.append(headerName).append(':').append(Separators.NEWLINE);
        }
        else {
            // The following headers do not have comma separated forms for
            // multiple headers. Thus, they must be encoded separately.
            if (this.headerName.equals(SIPHeaderNames.WWW_AUTHENTICATE)
                    || this.headerName.equals(SIPHeaderNames.PROXY_AUTHENTICATE)
                    || this.headerName.equals(SIPHeaderNames.AUTHORIZATION)
                    || this.headerName.equals(SIPHeaderNames.PROXY_AUTHORIZATION)
                    || (prettyEncode &&
                            (this.headerName.equals(SIPHeaderNames.VIA) || this.headerName.equals(SIPHeaderNames.ROUTE) || this.headerName.equals(SIPHeaderNames.RECORD_ROUTE))) // Less confusing to read
                    || this.getClass().equals( ExtensionHeaderList.class) ) {
                ListIterator<HDR> li = hlist.listIterator();
                while (li.hasNext()) {
                    HDR sipheader = (HDR) li.next();
                    sipheader.encode(buffer);
                }
            } else {
                // These can be concatenated together in an comma separated
                // list.
                buffer.append(headerName).append(Separators.COLON).append(Separators.SP);
                this.encodeBody(buffer);
                buffer.append(Separators.NEWLINE);
            }
        }
        return buffer;
    }

    /**
     * Return a list of encoded strings (one for each sipheader).
     *
     * @return LinkedList containing encoded strings in this header list. an
     *         empty list is returned if this header list contains no sip
     *         headers.
     */

    public List<String> getHeadersAsEncodedStrings() {
        List<String> retval = new LinkedList<String>();

        ListIterator<HDR> li = hlist.listIterator();
        while (li.hasNext()) {
            Header sipheader = li.next();
            retval.add(sipheader.toString());

        }

        return retval;

    }

    /**
     * Get the first element of this list.
     *
     * @return SIPHeader first element of the list.
     */
    public HDR getFirst() {
        if (hlist == null || hlist.isEmpty())
            return null;
        else
            return  hlist.get(0);
    }

    /**
     * Get the last element of this list.
     *
     * @return SIPHeader last element of the list.
     */
    public HDR getLast() {
        if (hlist == null || hlist.isEmpty())
            return null;
        return  hlist.get(hlist.size() - 1);
    }

    /**
     * Get the class for the headers of this list.
     *
     * @return Class of header supported by this list.
     */
    public Class<HDR> getMyClass() {
        return  this.myClass;
    }

    /**
     * Empty check
     *
     * @return boolean true if list is empty
     */
    public boolean isEmpty() {
        return hlist.isEmpty();
    }

    /**
     * Get an initialized iterator for my imbedded list
     *
     * @return the generated ListIterator
     */
    public ListIterator<HDR> listIterator() {

        return hlist.listIterator(0);
    }

    /**
     * Get the imbedded linked list.
     *
     * @return the imedded linked list of SIP headers.
     */
    public List<HDR> getHeaderList() {
        return this.hlist;
    }

    /**
     * Get the list iterator for a given position.
     *
     * @param position
     *            position for the list iterator to return
     * @return the generated list iterator
     */
    public ListIterator<HDR> listIterator(int position) {
        return hlist.listIterator(position);
    }

    /**
     * Remove the first element of this list.
     */
    public HDR removeFirst() {
        if (hlist.size() != 0) {
            return hlist.remove(0);
        }
        return null;
    }

    /**
     * Remove the last element of this list.
     */
    public HDR removeLast() {
        if (hlist.size() != 0) {
            return hlist.remove(hlist.size() - 1);
        }
        return null;
    }

    /**
     * Remove a sip header from this list of sip headers.
     *
     * @param obj
     *            SIPHeader to remove
     * @return boolean
     */
    public boolean remove(HDR obj) {
        if (hlist.size() == 0)
            return false;
        else
            return hlist.remove(obj);
    }

    /**
     * Set the root class for all objects inserted into my list (for assertion
     * check)
     *
     * @param cl
     *            class to set
     */
    protected void setMyClass(Class<HDR> cl) {
        this.myClass = cl;
    }

    /**
     * convert to a string representation (for printing).
     *
     * @param indentation
     *            int to set
     * @return String string representation of object (for printing).
     */
    public String debugDump(int indentation) {
        stringRepresentation = "";
        String indent = new Indentation(indentation).getIndentation();

        String className = this.getClass().getName();
        sprint(indent + className);
        sprint(indent + "{");

        for (Iterator<HDR> it = hlist.iterator(); it.hasNext();) {
            HDR sipHeader = (HDR) it.next();
            sprint(indent + sipHeader.debugDump());
        }
        sprint(indent + "}");
        return stringRepresentation;
    }

    /**
     * convert to a string representation
     *
     * @return String
     */
    public String debugDump() {
        return debugDump(0);
    }

    /**
     * Array conversion.
     *
     * @return SIPHeader []
     */
    public Object[] toArray() {
        return hlist.toArray();

    }

    /**
     * index of an element.
     *
     * @return index of the given element (-1) if element does not exist.
     */
    public int indexOf(GenericObject gobj) {
        return hlist.indexOf(gobj);
    }

    /**
     * insert at a location.
     *
     * @param index
     *            location where to add the sipHeader.
     * @param sipHeader
     *            SIPHeader structure to add.
     */

    public void add(int index, HDR  sipHeader)
            throws IndexOutOfBoundsException {
        hlist.add(index, sipHeader);
    }

    /**
     * Equality comparison operator.
     *
     * @param other
     *            the other object to compare with. true is returned iff the
     *            classes match and list of headers herein is equal to the list
     *            of headers in the target (order of the headers is not
     *            important).
     */
    @SuppressWarnings("unchecked")
    public boolean equals(Object other) {

        if (other == this)
            return true;

        if (other instanceof SIPHeaderList) {
            SIPHeaderList<SIPHeader> that = (SIPHeaderList<SIPHeader>) other;
            if (this.hlist == that.hlist)
                return true;
            else if (this.hlist == null)
                return that.hlist == null || that.hlist.size() == 0;
            return this.hlist.equals(that.hlist);
        }
        return false;
    }

    /**
     * Template match against a template. null field in template indicates wild
     * card match.
     */
    public boolean match(SIPHeaderList<?> template) {
        if (template == null)
            return true;
        if (!this.getClass().equals(template.getClass()))
            return false;
        SIPHeaderList<SIPHeader> that = (SIPHeaderList<SIPHeader>) template;
        if (this.hlist == that.hlist)
            return true;
        else if (this.hlist == null)
            return false;
        else {

            for (Iterator<SIPHeader> it = that.hlist.iterator(); it.hasNext();) {
                SIPHeader sipHeader = (SIPHeader) it.next();

                boolean found = false;
                for (Iterator<HDR> it1 = this.hlist.iterator(); it1.hasNext()
                        && !found;) {
                    SIPHeader sipHeader1 = (SIPHeader) it1.next();
                    found = sipHeader1.match(sipHeader);
                }
                if (!found)
                    return false;
            }
            return true;
        }
    }


    /**
     * make a clone of this header list.
     *
     * @return clone of this Header.
     */
    public Object clone() {
        try {
            Class<?> clazz = this.getClass();

            Constructor<?> cons = clazz.getConstructor((Class[])null);
            SIPHeaderList<HDR> retval = (SIPHeaderList<HDR>) cons.newInstance((Object[])null);
            retval.headerName = this.headerName;
            retval.myClass  = this.myClass;
            return retval.clonehlist(this.hlist);
        } catch (Exception ex) {
            throw new RuntimeException("Could not clone!", ex);
        }
    }

    protected final SIPHeaderList<HDR> clonehlist(List<HDR> hlistToClone) {
        if (hlistToClone != null) {
            for (Iterator<HDR> it = (Iterator<HDR>) hlistToClone.iterator(); it.hasNext();) {
                Header h = it.next();
                this.hlist.add((HDR)h.clone());
            }
        }
        return this;
    }

    /**
     * Get the number of headers in the list.
     */
    public int size() {
        return hlist.size();
    }

    /**
     * Return true if this is a header list (overrides the base class method
     * which returns false).
     *
     * @return true
     */
    public boolean isHeaderList() {
        return true;
    }

    /**
     * Encode the body of this header (the stuff that follows headerName). A.K.A
     * headerValue. This will not give a reasonable result for WWW-Authenticate,
     * Authorization, Proxy-Authenticate and Proxy-Authorization and hence this
     * is protected.
     */
    protected String encodeBody() {
        return encodeBody(new StringBuffer()).toString();
    }

    protected StringBuffer encodeBody(StringBuffer buffer) {
        ListIterator<HDR> iterator = this.listIterator();
        while (true) {
            SIPHeader sipHeader = (SIPHeader) iterator.next();
            if ( sipHeader == this ) throw new RuntimeException ("Unexpected circularity in SipHeaderList");
            sipHeader.encodeBody(buffer);
            // if (body.equals("")) System.out.println("BODY == ");
            if (iterator.hasNext()) {
                if (!this.headerName.equals(PrivacyHeader.NAME))
                    buffer.append(Separators.COMMA);
                else
                    buffer.append(Separators.SEMICOLON);
                continue;
            } else
                break;

        }
        return buffer;
    }

    public boolean addAll(Collection<? extends HDR> collection) {
        return this.hlist.addAll(collection);
    }

    public boolean addAll(int index, Collection<? extends HDR> collection) {
        return this.hlist.addAll(index, collection);

    }

    public boolean containsAll(Collection<?> collection) {
        return this.hlist.containsAll(collection);
    }




    public void clear() {
        this.hlist.clear();
    }

    public boolean contains(Object header) {
        return this.hlist.contains(header);
    }


    /**
     * Get the object at the specified location.
     *
     * @param index --
     *            location from which to get the object.
     *
     */
    public HDR get(int index) {
        return  this.hlist.get(index);
    }

    /**
     * Return the index of a given object.
     *
     * @param obj --
     *            object whose index to compute.
     */
    public int indexOf(Object obj) {
        return this.hlist.indexOf(obj);
    }

    /**
     * Return the iterator to the imbedded list.
     *
     * @return iterator to the imbedded list.
     *
     */

    public java.util.Iterator<HDR> iterator() {
        return this.hlist.listIterator();
    }

    /**
     * Get the last index of the given object.
     *
     * @param obj --
     *            object whose index to find.
     */
    public int lastIndexOf(Object obj) {

        return this.hlist.lastIndexOf(obj);
    }

    /**
     * Remove the given object.
     *
     * @param obj --
     *            object to remove.
     *
     */

    public boolean remove(Object obj) {

        return this.hlist.remove(obj);
    }

    /**
     * Remove the object at a given index.
     *
     * @param index --
     *            index at which to remove the object
     */

    public HDR remove(int index) {
        return this.hlist.remove(index);
    }

    /**
     * Remove all the elements.
     * @see List#removeAll(java.util.Collection)
     */
    public boolean removeAll(java.util.Collection<?> collection) {
        return this.hlist.removeAll(collection);
    }


    /**
     * @see List#retainAll(java.util.Collection)
     * @param collection
     */
    public boolean retainAll(java.util.Collection<?> collection) {
        return this.hlist.retainAll(collection);
    }

    /**
     * Get a sublist of the list.
     *
     * @see List#subList(int, int)
     */
    public java.util.List<HDR> subList(int index1, int index2) {
        return this.hlist.subList(index1, index2);

    }

    /**
     * @see Object#hashCode()
     * @return -- the computed hashcode.
     */
    public int hashCode() {

        return this.headerName.hashCode();
    }

    /**
     * Set a SIPHeader at a particular position in the list.
     *
     * @see List#set(int, java.lang.Object)
     */
    public HDR set(int position, HDR sipHeader) {

        return hlist.set(position, sipHeader);

    }


    public static void setPrettyEncode(boolean flag) {
        prettyEncode = flag;
    }


    public <T> T[] toArray(T[] array) {
        return this.hlist.toArray(array);
    }


}