001: package examples.parser;
002:
003: import gov.nist.javax.sip.header.ims.PAssertedIdentityHeader;
004: import gov.nist.javax.sip.header.ims.PPreferredIdentityHeader;
005:
006: import javax.sdp.SdpFactory;
007: import javax.sdp.SessionDescription;
008: import javax.sip.SipFactory;
009: import javax.sip.address.AddressFactory;
010: import javax.sip.header.ExtensionHeader;
011: import javax.sip.header.HeaderFactory;
012: import javax.sip.message.MessageFactory;
013: import javax.sip.message.Request;
014: import javax.sip.message.Response;
015:
016: /**
017: * This example shows you how you can use the message factory to parse SIP
018: * messages. You dont need to create a sip stack for this example.
019: *
020: * @author M. Ranganathan
021: *
022: */
023: public class Parser {
024:
025: public static void main(String[] args) throws Exception {
026: SipFactory sipFactory = null;
027: HeaderFactory headerFactory;
028: AddressFactory addressFactory;
029: MessageFactory messageFactory;
030:
031: sipFactory = SipFactory.getInstance();
032: sipFactory.setPathName("gov.nist");
033:
034: headerFactory = sipFactory.createHeaderFactory();
035: addressFactory = sipFactory.createAddressFactory();
036: messageFactory = sipFactory.createMessageFactory();
037: // If you get a request from a socket, you can use the jsip api to parse it.
038: String request = "INVITE sip:00001002000022@p25dr;user=TIA-P25-SU SIP/2.0\r\n"
039: + "CSeq: 1 INVITE\r\n"
040: + "From: <sip:0000100200000c@p25dr;user=TIA-P25-SU>;tag=841\r\n"
041: + "To: <sip:00001002000022@p25dr;user=TIA-P25-SU>\r\n"
042: + "Via: SIP/2.0/UDP 02.002.00001.p25dr;branch=z9hG4bKa10f04383e3d8e8dbf3f6d06f6bb6880\r\n"
043: + "Max-Forwards: 70\r\n"
044: + "Route: <sip:TIA-P25-U2UOrig@01.002.00001.p25dr;lr>,<sip:TIA-P25-U2UDest@03.002.00001.p25dr;lr>\r\n"
045: + "Contact: <sip:02.002.00001.p25dr>\r\n"
046: + "Timestamp: 1154567665687\r\n"
047: + "Allow: REGISTER,INVITE,ACK,BYE,CANCEL\r\n"
048: + "Accept: application/sdp ;level=1,application/x-tia-p25-issi\r\n"
049: + "Call-ID: c6a12ddad0ddc1946d9f443c884a7768@127.0.0.1\r\n"
050: + "Content-Type: application/sdp;level=1\r\n"
051: + "P-Asserted-Identity: <sip:x>\r\n"
052: + "P-Preferred-Identity: <sip:x>\r\n"
053: + "Content-Length: 145\r\n\r\n"
054: + "v=0\r\n"
055: + "o=- 30576 0 IN IP4 127.0.0.1\r\n"
056: + "s=TIA-P25-SuToSuCall\r\n"
057: + "t=0 0\r\n"
058: + "c=IN IP4 127.0.0.1\r\n"
059: + "m=audio 12412 RTP/AVP 100\r\n"
060: + "a=rtpmap:100 X-TIA-P25-IMBE/8000\r\n";
061: Request sipRequest = messageFactory.createRequest(request);
062: byte[] contentBytes = sipRequest.getRawContent();
063: String contentString = new String(contentBytes);
064: //SdpFactory sdpFactory = SdpFactory.getInstance();
065: //SessionDescription sd = sdpFactory
066: // .createSessionDescription(contentString);
067:
068: PAssertedIdentityHeader h = (PAssertedIdentityHeader) sipRequest
069: .getHeader(PAssertedIdentityHeader.NAME);
070: System.out.println(h.getClass());
071: System.out.println(h instanceof ExtensionHeader);
072: System.out.println(h instanceof PAssertedIdentityHeader);
073:
074: PPreferredIdentityHeader h2 = (PPreferredIdentityHeader) sipRequest
075: .getHeader(PPreferredIdentityHeader.NAME);
076: System.out.println(h2.getClass());
077: System.out.println(h2 instanceof ExtensionHeader);
078: System.out.println(h2 instanceof PPreferredIdentityHeader);
079:
080: System.out.println("Parsed SIPRequest is :\n"
081: + sipRequest.toString());
082: //System.out.println("Parsed Content is :\n" + sd.toString());
083:
084: // Similarly, if you get a response via a socket, you can use the jsip api to parse it
085: String response = "SIP/2.0 200 OK\r\n"
086: + "CSeq: 1 INVITE\r\n"
087: + "From: <sip:0000100200000c@p25dr;user=TIA-P25-SU>;tag=397\r\n"
088: + "To: <sip:00001002000022@p25dr;user=TIA-P25-SU>;tag=466\r\n"
089: + "Via: SIP/2.0/UDP 01.002.00001.p25dr;branch=z9hG4bK7d2479f1f7d746f315664fb5d0e85d08,"
090: + "SIP/2.0/UDP 02.002.00001.p25dr;branch=z9hG4bKa10f04383e3d8e8dbf3f6d06f6bb6880\r\n"
091: + "Call-ID: c6a12ddad0ddc1946d9f443c884a7768@127.0.0.1\r\n"
092: + "Record-Route: <sip:03.002.00001.p25dr;lr>,<sip:01.002.00001.p25dr;lr>\r\n"
093: + "Contact: <sip:04.002.00001.p25dr;user=TIA-P25-SU>\r\n"
094: + "Timestamp: 1154567665687\r\n"
095: + "Content-Type: application/sdp;level=1\r\n"
096: + "Content-Length: 145\r\n\r\n" + "v=0\r\n"
097: + "o=- 30576 0 in ip4 127.0.0.1\r\n"
098: + "s=tia-p25-sutosucall\r\n" + "c=in ip4 127.0.0.1\r\n"
099: + "t=0 0\r\n" + "m=audio 12230 rtp/avp 100\r\n"
100: + "a=rtpmap:100 x-tia-p25-imbe/8000\r\n";
101:
102: Response sipResponse = messageFactory.createResponse(response);
103: System.out.println("Parsed SIP Response is :\n" + sipResponse);
104: contentBytes = sipResponse.getRawContent();
105: contentString = new String(contentBytes);
106: SdpFactory sdpFactory = SdpFactory.getInstance();
107: SessionDescription sd = sdpFactory
108: .createSessionDescription(contentString);
109: System.out.println("Parsed Content is :\n" + sd.toString());
110:
111: }
112:
113: }
|