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*/
26package gov.nist.javax.sip.parser;
27
28import gov.nist.javax.sip.header.*;
29import gov.nist.core.*;
30import java.text.ParseException;
31import javax.sip.*;
32
33/**
34 * Parser for RetryAfter header.
35 *
36 * @version 1.2
37 *
38 * @author Olivier Deruelle
39 * @author M. Ranganathan
40 *
41 *
42 * @version 1.2 $Revision: 1.10 $ $Date: 2009/11/04 17:23:00 $
43 */
44public class RetryAfterParser extends HeaderParser {
45
46    /**
47     * Creates a new instance of RetryAfterParser
48     * @param retryAfter the header to parse
49     */
50    public RetryAfterParser(String retryAfter) {
51        super(retryAfter);
52    }
53
54    /**
55     * Constructor
56     * @param lexer the lexer to use to parse the header
57     */
58    protected RetryAfterParser(Lexer lexer) {
59        super(lexer);
60    }
61
62    /**
63     * parse the String message
64     * @return SIPHeader (RetryAfter object)
65     * @throws SIPParseException if the message does not respect the spec.
66     */
67    public SIPHeader parse() throws ParseException {
68
69        if (debug)
70            dbg_enter("RetryAfterParser.parse");
71
72        RetryAfter retryAfter = new RetryAfter();
73        try {
74            headerName(TokenTypes.RETRY_AFTER);
75
76            // mandatory delatseconds:
77            String value = lexer.number();
78            try {
79                int ds = Integer.parseInt(value);
80                retryAfter.setRetryAfter(ds);
81            } catch (NumberFormatException ex) {
82                throw createParseException(ex.getMessage());
83            } catch (InvalidArgumentException ex) {
84                throw createParseException(ex.getMessage());
85            }
86
87            this.lexer.SPorHT();
88            if (lexer.lookAhead(0) == '(') {
89                String comment = this.lexer.comment();
90                retryAfter.setComment(comment);
91            }
92            this.lexer.SPorHT();
93
94            while (lexer.lookAhead(0) == ';') {
95                this.lexer.match(';');
96                this.lexer.SPorHT();
97                lexer.match(TokenTypes.ID);
98                Token token = lexer.getNextToken();
99                value = token.getTokenValue();
100                if (value.equalsIgnoreCase("duration")) {
101                    this.lexer.match('=');
102                    this.lexer.SPorHT();
103                    value = lexer.number();
104                    try {
105                        int duration = Integer.parseInt(value);
106                        retryAfter.setDuration(duration);
107                    } catch (NumberFormatException ex) {
108                        throw createParseException(ex.getMessage());
109                    } catch (InvalidArgumentException ex) {
110                        throw createParseException(ex.getMessage());
111                    }
112                } else {
113                    this.lexer.SPorHT();
114                    this.lexer.match('=');
115                    this.lexer.SPorHT();
116                    lexer.match(TokenTypes.ID);
117                    Token secondToken = lexer.getNextToken();
118                    String secondValue = secondToken.getTokenValue();
119                    retryAfter.setParameter(value, secondValue);
120                }
121                this.lexer.SPorHT();
122            }
123        } finally {
124            if (debug)
125                dbg_leave("RetryAfterParser.parse");
126        }
127
128        return retryAfter;
129    }
130
131}
132