1 package gov.nist.javax.sip.parser.extensions; 2 3 import gov.nist.javax.sip.header.*; 4 import gov.nist.javax.sip.header.extensions.*; 5 import gov.nist.javax.sip.parser.*; 6 7 import java.text.ParseException; 8 import javax.sip.*; 9 10 /** 11 * Parser for SIP Session Expires Header. 12 * 13 * 14 */ 15 public class SessionExpiresParser extends ParametersParser { 16 17 /** 18 * protected constructor. 19 * @param text is the text of the header to parse 20 */ SessionExpiresParser(String text)21 public SessionExpiresParser(String text) { 22 super(text); 23 } 24 25 /** 26 * constructor. 27 * @param lexer is the lexer passed in from the enclosing parser. 28 */ SessionExpiresParser(Lexer lexer)29 protected SessionExpiresParser(Lexer lexer) { 30 super(lexer); 31 } 32 33 /** 34 * Parse the header. 35 */ parse()36 public SIPHeader parse() throws ParseException { 37 SessionExpires se = new SessionExpires(); 38 if (debug) 39 dbg_enter("parse"); 40 try { 41 headerName(TokenTypes.SESSIONEXPIRES_TO); 42 43 String nextId = lexer.getNextId(); 44 45 try { 46 int delta = Integer.parseInt(nextId); 47 se.setExpires(delta); 48 } catch (NumberFormatException ex) { 49 throw createParseException("bad integer format"); 50 } catch (InvalidArgumentException ex) { 51 throw createParseException(ex.getMessage()); 52 } 53 // May have parameters... 54 this.lexer.SPorHT(); 55 super.parse(se); 56 return se; 57 58 } finally { 59 if (debug) 60 dbg_leave("parse"); 61 } 62 63 } 64 main(String args[])65 public static void main(String args[]) throws ParseException { 66 String to[] = 67 { "Session-Expires: 30\n", 68 "Session-Expires: 45;refresher=uac\n", 69 }; 70 71 for (int i = 0; i < to.length; i++) { 72 SessionExpiresParser tp = new SessionExpiresParser(to[i]); 73 SessionExpires t = (SessionExpires) tp.parse(); 74 System.out.println("encoded = " + t.encode()); 75 System.out.println("\ntime=" + t.getExpires() ); 76 if ( t.getParameter("refresher") != null) 77 System.out.println("refresher=" + t.getParameter("refresher") ); 78 79 } 80 } 81 82 83 } 84 85