public static void basicExample001 ( ) throws IOException { final String rawMessage = new StringBuilder ( "BYE sip:bob@127.0.0.1:5060 SIP/2.0\r\n" ) . append ( "Via: SIP/2.0/UDP 127.0.1.1:5061;branch=z9hG4bK-28976-1-7\r\n" ) . append ( "From: alice <sip:alice@127.0.1.1:5061>;tag=28976SIPpTag001\r\n" ) . append ( "To: bob <sip:bob@127.0.0.1:5060>;tag=28972SIPpTag011\r\n" ) . append ( "Call-ID: 1-28976@127.0.1.1\r\n" ) . append ( "CSeq: 2 BYE\r\n" ) . append ( "Contact: sip:alice@127.0.1.1:5061\r\n" ) . append ( "Max-Forwards: 70\r\n" ) . append ( "Subject: Example BYE Message\r\n" ) . append ( "Content-Length: 0\r\n" ) . append ( "\r\n" ) . toString ( ) ; // Every object in SIP Lib has a frame-method, which // will attempt to frame the raw content into that object. // This is true for SIP messages, SIP header, SIP URIs // etc etc. All frame-methods are overloaded and accept // Strings, Buffers and byte-arrays. final SipMessage msg = SipMessage . frame ( rawMessage ) ; // Once the message has successfully been parsed you // can access headers etc within the SIP message. final FromHeader from = msg . getFromHeader ( ) ; // All headers that typically are needed for any application, and // in particular for SIP stacks, have explicit methods and returns // explicit objects. You may still use the generic getHeader but then // you will get a generic SIP header back. final ContactHeader contact = msg . getContactHeader ( ) ; // Instead of having to do SipMessage.getMethod().equals("BYE") etc // the SIP message has many convenience methods for making the code // more readable, less error prone and less boiler place to write. if ( msg . isBye ( ) ) { System . out . println ( "Yay, this was a BYE message" ) ; } if ( msg . isRequest ( ) ) { System . out . println ( "Yay, this was SIP request" ) ; } }
This is the most basic example showing how you can parse a SIP message based off of a String . In this example we are the ones creating that raw message ourselves but typically you would read this off of the network or perhaps from a file if you are building a tool of some sort .