summaryrefslogtreecommitdiffstats
path: root/java/gov/nist/javax/sip/header/ParametersHeader.java
blob: f84ff37fc325a63eeb393944924eca668d19f59f (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
/*
* 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 Advanced Networking Technologies Division  */
/**************************************************************************/

package gov.nist.javax.sip.header;
import gov.nist.core.DuplicateNameValueList;
import gov.nist.core.NameValue;
import gov.nist.core.NameValueList;
import gov.nist.javax.sip.address.GenericURI;

import java.io.Serializable;
import java.text.ParseException;
import java.util.Iterator;

import javax.sip.header.Parameters;

/**
 * Parameters header. Suitable for extension by headers that have parameters.
 *
 * @author M. Ranganathan   <br/>
 *
 *
 * @version 1.2 $Revision: 1.15 $ $Date: 2010/01/12 00:05:27 $
 *
 */
public abstract class ParametersHeader
    extends SIPHeader
    implements javax.sip.header.Parameters, Serializable {
    protected NameValueList parameters;
    
    protected DuplicateNameValueList duplicates;
    
    protected ParametersHeader() {
        this.parameters = new NameValueList();
        this.duplicates = new DuplicateNameValueList();
    }

    protected ParametersHeader(String hdrName) {
        super(hdrName);
        this.parameters = new NameValueList();
        this.duplicates = new DuplicateNameValueList();
    }

    protected ParametersHeader(String hdrName, boolean sync) {
        super(hdrName);
        this.parameters = new NameValueList(sync);
        this.duplicates = new DuplicateNameValueList();
    }

    /**
     * Returns the value of the named parameter, or null if it is not set. A
     * zero-length String indicates flag parameter.
     *
     * @param name name of parameter to retrieve
     * @return the value of specified parameter
     */

    public String getParameter(String name) {
        return this.parameters.getParameter(name);

    }

    /**
     * Return the parameter as an object (dont convert to string).
     *
     * @param name is the name of the parameter to get.
     * @return the object associated with the name.
     */
    public Object getParameterValue(String name) {
        return this.parameters.getValue(name);
    }

    /**
     * Returns an Iterator over the names (Strings) of all parameters present
     * in this ParametersHeader.
     *
     * @return an Iterator over all the parameter names
     */

    public Iterator<String> getParameterNames() {
        return parameters.getNames();
    }

    /** Return true if you have a parameter and false otherwise.
     *
     *@return true if the parameters list is non-empty.
     */

    public boolean hasParameters() {
        return parameters != null && !parameters.isEmpty();
    }

    /**
    * Removes the specified parameter from Parameters of this ParametersHeader.
    * This method returns silently if the parameter is not part of the
    * ParametersHeader.
    *
    * @param name - a String specifying the parameter name
    */

    public void removeParameter(String name) {
        this.parameters.delete(name);
    }

    /**
     * Sets the value of the specified parameter. If the parameter already had
     *
     * a value it will be overwritten. A zero-length String indicates flag
     *
     * parameter.
     *
     *
     *
     * @param name - a String specifying the parameter name
     *
     * @param value - a String specifying the parameter value
     *
     * @throws ParseException which signals that an error has been reached
     *
     * unexpectedly while parsing the parameter name or value.
     *
     */
    public void setParameter(String name, String value) throws ParseException {
        NameValue nv = parameters.getNameValue(name);
        if (nv != null) {
            nv.setValueAsObject(value);
        } else {
            nv = new NameValue(name, value);
            this.parameters.set(nv);
        }
    }
    
    /**
     * Sets the value of the specified parameter. If the parameter already had
     *
     * a value it will be overwritten. A zero-length String indicates flag
     *
     * parameter.
     *
     *
     *
     * @param name - a String specifying the parameter name
     *
     * @param value - a String specifying the parameter value
     *
     * @throws ParseException which signals that an error has been reached
     *
     * unexpectedly while parsing the parameter name or value.
     *
     */
    public void setQuotedParameter(String name, String value)
        throws ParseException {
        NameValue nv = parameters.getNameValue(name);
        if (nv != null) {
            nv.setValueAsObject(value);
            nv.setQuotedValue();
        } else {
            nv = new NameValue(name, value);
            nv.setQuotedValue();
            this.parameters.set(nv);
        }
    }

    /**
     * Sets the value of the specified parameter. If the parameter already had
     *
     * a value it will be overwritten.
     *
     *
     * @param name - a String specifying the parameter name
     *
     * @param value - an int specifying the parameter value
     *
     * @throws ParseException which signals that an error has been reached
     *
     * unexpectedly while parsing the parameter name or value.
     *
     */
    protected void setParameter(String name, int value) {
        Integer val = Integer.valueOf(value);
        this.parameters.set(name,val);

    }

    /**
     * Sets the value of the specified parameter. If the parameter already had
     *
     * a value it will be overwritten.
     *
     *
     * @param name - a String specifying the parameter name
     *
     * @param value - a boolean specifying the parameter value
     *
     * @throws ParseException which signals that an error has been reached
     *
     * unexpectedly while parsing the parameter name or value.
     *
     */
    protected void setParameter(String name, boolean value) {
        Boolean val = Boolean.valueOf(value);
        this.parameters.set(name,val);
    }

    /**
     * Sets the value of the specified parameter. If the parameter already had
     *
     * a value it will be overwritten.
     *
     * @param name - a String specifying the parameter name
     *
     * @param value - a boolean specifying the parameter value
     *
     * @throws ParseException which signals that an error has been reached
     *
     * unexpectedly while parsing the parameter name or value.
     *
     */
    protected void setParameter(String name, float value) {
        Float val = Float.valueOf(value);
        NameValue nv = parameters.getNameValue(name);
        if (nv != null) {
            nv.setValueAsObject(val);
        } else {
            nv = new NameValue(name, val);
            this.parameters.set(nv);
        }
    }

    /**
     * Sets the value of the specified parameter. If the parameter already had
     *
     * a value it will be overwritten. A zero-length String indicates flag
     *
     * parameter.
     *
     *
     *
     * @param name - a String specifying the parameter name
     *
     * @param value - a String specifying the parameter value
     *
     * @throws ParseException which signals that an error has been reached
     *
     * unexpectedly while parsing the parameter name or value.
     *
     */
    protected void setParameter(String name, Object value) {
        this.parameters.set(name,value);
    }

    /**
     * Return true if has a parameter.
     *
     * @param parameterName is the name of the parameter.
     *
     * @return true if the parameter exists and false if not.
     */
    public boolean hasParameter(String parameterName) {
        return this.parameters.hasNameValue(parameterName);
    }

    /**
     *Remove all parameters.
     */
    public void removeParameters() {
        this.parameters = new NameValueList();
    }

    /**
     * get the parameter list.
     * @return parameter list
     */
    public NameValueList getParameters() {
        return parameters;
    }

    /** Set the parameter given a name and value.
     *
     * @param nameValue - the name value of the parameter to set.
     */
    public void setParameter(NameValue nameValue) {
        this.parameters.set(nameValue);
    }

    /**
     * Set the parameter list.
     *
     * @param parameters The name value list to set as the parameter list.
     */
    public void setParameters(NameValueList parameters) {
        this.parameters = parameters;
    }

    /**
     * Get the parameter as an integer value.
     *
     * @param parameterName -- the parameter name to fetch.
     *
     * @return -1 if the parameter is not defined in the header.
     */
    protected int getParameterAsInt(String parameterName) {
        if (this.getParameterValue(parameterName) != null) {
            try {
                if (this.getParameterValue(parameterName) instanceof String) {
                    return Integer.parseInt(this.getParameter(parameterName));
                } else {
                    return ((Integer) getParameterValue(parameterName))
                        .intValue();
                }
            } catch (NumberFormatException ex) {
                return -1;
            }
        } else
            return -1;
    }

    /** Get the parameter as an integer when it is entered as a hex.
     *
     *@param parameterName -- The parameter name to fetch.
     *
     *@return -1 if the parameter is not defined in the header.
     */
    protected int getParameterAsHexInt(String parameterName) {
        if (this.getParameterValue(parameterName) != null) {
            try {
                if (this.getParameterValue(parameterName) instanceof String) {
                    return Integer.parseInt(
                        this.getParameter(parameterName),
                        16);
                } else {
                    return ((Integer) getParameterValue(parameterName))
                        .intValue();
                }
            } catch (NumberFormatException ex) {
                return -1;
            }
        } else
            return -1;
    }

    /** Get the parameter as a float value.
     *
     *@param parameterName -- the parameter name to fetch
     *
     *@return -1 if the parameter is not defined or the parameter as a float.
     */
    protected float getParameterAsFloat(String parameterName) {

        if (this.getParameterValue(parameterName) != null) {
            try {
                if (this.getParameterValue(parameterName) instanceof String) {
                    return Float.parseFloat(this.getParameter(parameterName));
                } else {
                    return ((Float) getParameterValue(parameterName))
                        .floatValue();
                }
            } catch (NumberFormatException ex) {
                return -1;
            }
        } else
            return -1;
    }

    /**
     * Get the parameter as a long value.
     *
     * @param parameterName -- the parameter name to fetch.
     *
     * @return -1 if the parameter is not defined or the parameter as a long.
     */
    protected long getParameterAsLong(String parameterName) {
        if (this.getParameterValue(parameterName) != null) {
            try {
                if (this.getParameterValue(parameterName) instanceof String) {
                    return Long.parseLong(this.getParameter(parameterName));
                } else {
                    return ((Long) getParameterValue(parameterName))
                        .longValue();
                }
            } catch (NumberFormatException ex) {
                return -1;
            }
        } else
            return -1;
    }

    /**
     * Get the parameter value as a URI.
     *
     * @param parameterName -- the parameter name
     *
     * @return value of the parameter as a URI or null if the parameter
     *  not present.
     */
    protected GenericURI getParameterAsURI(String parameterName) {
        Object val = getParameterValue(parameterName);
        if (val instanceof GenericURI)
            return (GenericURI) val;
        else {
            try {
                return new GenericURI((String) val);
            } catch (ParseException ex) {
                //catch ( URISyntaxException ex) {
                return null;
            }
        }
    }

    /**
     * Get the parameter value as a boolean.
     *
     * @param parameterName -- the parameter name
     * @return boolean value of the parameter.
     */
    protected boolean getParameterAsBoolean(String parameterName) {
        Object val = getParameterValue(parameterName);
        if (val == null) {
            return false;
        } else if (val instanceof Boolean) {
            return ((Boolean) val).booleanValue();
        } else if (val instanceof String) {
            return Boolean.valueOf((String) val).booleanValue();
        } else
            return false;
    }

    /**
     * This is for the benifit of the TCK.
     *
     * @return the name value pair for the given parameter name.
     */
    public NameValue getNameValue(String parameterName) {
        return parameters.getNameValue(parameterName);
    }

 
    public Object clone() {
        ParametersHeader retval = (ParametersHeader) super.clone();
        if (this.parameters != null)
            retval.parameters = (NameValueList) this.parameters.clone();
        return retval;
    }

    //-------------------------
    /**
     * Introduced specifically for the P-Charging-Function-Addresses Header and 
     * all other headers that may have multiple header parameters of the same name, but 
     * with multiple possible values.
     * 
     * Example: P-Charging-Function-Addresses: ccf=[5555::b99:c88:d77:e66]; ccf=[5555::a55:b44:c33:d22]; 
     *                                         ecf=[5555::1ff:2ee:3dd:4cc]; ecf=[5555::6aa:7bb:8cc:9dd]
     * @param name of the parameter
     * @param value of the parameter
     */
    public void setMultiParameter(String name, String value)
    {
    	NameValue nv = new NameValue();
    	nv.setName(name);
    	nv.setValue(value);
    	duplicates.set(nv);
    }
    
    /** Set the parameter given a name and value.
    *
    * @param nameValue - the name value of the parameter to set.
    */
   public void setMultiParameter(NameValue nameValue) {
       this.duplicates.set(nameValue);
   }
    
    /**
     * Returns the parameter name
     * @param name
     * @return
     */
    public String getMultiParameter(String name) {
        return this.duplicates.getParameter(name);

    }
    

    public DuplicateNameValueList getMultiParameters() {
        return duplicates;
    }
    
    
    /**
     * Return the parameter as an object (dont convert to string).
     *
     * @param name is the name of the parameter to get.
     * @return the object associated with the name.
     */
    public Object getMultiParameterValue(String name) {
        return this.duplicates.getValue(name);
    }

    /**
     * Returns an Iterator over the names (Strings) of all parameters present
     * in this ParametersHeader.
     *
     * @return an Iterator over all the parameter names
     */

    public Iterator<String> getMultiParameterNames() {
        return duplicates.getNames();
    }

    /** Return true if you have a parameter and false otherwise.
     *
     *@return true if the parameters list is non-empty.
     */

    public boolean hasMultiParameters() {
        return duplicates != null && !duplicates.isEmpty();
    }

    /**
    * Removes the specified parameter from Parameters of this ParametersHeader.
    * This method returns silently if the parameter is not part of the
    * ParametersHeader.
    *
    * @param name - a String specifying the parameter name
    */

    public void removeMultiParameter(String name) {
        this.duplicates.delete(name);
    }
    
    /**
     * Return true if has a parameter.
     *
     * @param parameterName is the name of the parameter.
     *
     * @return true if the parameter exists and false if not.
     */
    public boolean hasMultiParameter(String parameterName) {
        return this.duplicates.hasNameValue(parameterName);
    }

    /**
     *Remove all parameters.
     */
    public void removeMultiParameters() {
        this.duplicates = new DuplicateNameValueList();
    }

    //-------------------------------
    
    @SuppressWarnings("unchecked")
    protected final boolean equalParameters( Parameters other ) {
        if (this==other) return true;

        for ( Iterator i = this.getParameterNames(); i.hasNext();) {
            String pname = (String) i.next();

            String p1 = this.getParameter( pname );
            String p2 = other.getParameter( pname );

            // getting them based on this.getParameterNames. Note that p1 may be null
            // if this is a name-only parameter like rport or lr.
            if (p1 == null ^ p2 == null) return false;
            else if (p1 != null && !p1.equalsIgnoreCase(p2) ) return false;
        }

        // Also compare other's parameters; some duplicate testing here...
        for ( Iterator i = other.getParameterNames(); i.hasNext();) {
            String pname = (String) i.next();

            String p1 = other.getParameter( pname );
            String p2 = this.getParameter( pname );

                // assert( p1 != null );
            // if ( p1 == null ) throw new RuntimeException("Assertion check failed!");
            // if (p2==null) return false;

            // getting them based on this.getParameterNames. Note that p1 may be null
            // if this is a name-only parameter like rport or lr.

            if (p1 == null ^ p2 == null) return false;
            else if (p1 != null && !p1.equalsIgnoreCase(p2) ) return false;
        }

        return true;
    }
    
    
    // ----------- Abstract methods --------------
    protected abstract String encodeBody();

}