All Downloads are FREE. Search and download functionalities are using the official Maven repository.

examples.parser.Parser Maven / Gradle / Ivy

package examples.parser;

import gov.nist.javax.sip.header.ims.PAssertedIdentityHeader;
import gov.nist.javax.sip.header.ims.PPreferredIdentityHeader;

import javax.sdp.Connection;
import javax.sdp.SdpFactory;
import javax.sdp.SessionDescription;
import javax.sdp.SessionName;
import javax.sip.SipFactory;
import javax.sip.address.AddressFactory;
import javax.sip.header.ExtensionHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import javax.sip.message.Response;

/**
 * This example shows you how you can use the message factory to parse SIP
 * messages. You dont need to create a sip stack for this example.
 * 
 * @author M. Ranganathan
 * 
 */
public class Parser {

	public static void main(String[] args) throws Exception {
		SipFactory sipFactory = null;
		HeaderFactory headerFactory;
		AddressFactory addressFactory;
		MessageFactory messageFactory;

		sipFactory = SipFactory.getInstance();
		sipFactory.setPathName("gov.nist");

		headerFactory = sipFactory.createHeaderFactory();
		addressFactory = sipFactory.createAddressFactory();
		messageFactory = sipFactory.createMessageFactory();
		// If you get a request from a socket, you can use the jsip api to parse it.
		String request = "INVITE sip:00001002000022@p25dr;user=TIA-P25-SU SIP/2.0\r\n"
				+ "CSeq: 1 INVITE\r\n"
				+ "From: ;tag=841\r\n"
				+ "To: \r\n"
				+ "Via: SIP/2.0/UDP 02.002.00001.p25dr;branch=z9hG4bKa10f04383e3d8e8dbf3f6d06f6bb6880\r\n"
				+ "Max-Forwards: 70\r\n"
				+ "Route: ,\r\n"
				+ "Contact: \r\n"
				+ "Timestamp: 1154567665687\r\n"
				+ "Allow: REGISTER,INVITE,ACK,BYE,CANCEL\r\n"
				+ "Accept: application/sdp ;level=1,application/x-tia-p25-issi\r\n"
				+ "Call-ID: [email protected]\r\n"
				+ "Content-Type: application/sdp;level=1\r\n"
				+ "P-Asserted-Identity: \r\n"
				+ "P-Preferred-Identity: \r\n"
				+ "Content-Length: 145\r\n\r\n"
				+ "v=0\r\n"
				+ "o=- 30576 0 IN IP4 127.0.0.1\r\n"
				+ "s=TIA-P25-SuToSuCall\r\n"
				+ "t=0 0\r\n"
				+ "c=IN IP4 127.0.0.1\r\n"
				+ "m=audio 12412 RTP/AVP 100\r\n"
				+ "a=rtpmap:100 X-TIA-P25-IMBE/8000\r\n";
		Request sipRequest = messageFactory.createRequest(request);
		byte[] contentBytes = sipRequest.getRawContent();
		String contentString = new String(contentBytes);
		//SdpFactory sdpFactory = SdpFactory.getInstance();
		//SessionDescription sd = sdpFactory
		//		.createSessionDescription(contentString);
		
		PAssertedIdentityHeader h = (PAssertedIdentityHeader)sipRequest.getHeader(PAssertedIdentityHeader.NAME);
		System.out.println( h.getClass() );
		System.out.println( h instanceof ExtensionHeader );
		System.out.println( h instanceof PAssertedIdentityHeader );
		
		PPreferredIdentityHeader h2 = (PPreferredIdentityHeader) sipRequest.getHeader(PPreferredIdentityHeader.NAME);
		System.out.println( h2.getClass() );
		System.out.println( h2 instanceof ExtensionHeader );
		System.out.println( h2 instanceof PPreferredIdentityHeader );

			
		System.out.println("Parsed SIPRequest is :\n" + sipRequest.toString());
		//System.out.println("Parsed Content is :\n" + sd.toString());
		
		// Similarly, if you get a response via a socket, you can use the jsip api to parse it
		String response = "SIP/2.0 200 OK\r\n"+
		"CSeq: 1 INVITE\r\n"+
		"From: ;tag=397\r\n"+
		"To: ;tag=466\r\n"+
		"Via: SIP/2.0/UDP 01.002.00001.p25dr;branch=z9hG4bK7d2479f1f7d746f315664fb5d0e85d08," +
		"SIP/2.0/UDP 02.002.00001.p25dr;branch=z9hG4bKa10f04383e3d8e8dbf3f6d06f6bb6880\r\n"+ 
		"Call-ID: [email protected]\r\n" + 
		"Record-Route: ,\r\n"+
		"Contact: \r\n"+
		"Timestamp: 1154567665687\r\n"+
		"Content-Type: application/sdp;level=1\r\n"+
		"Content-Length: 145\r\n\r\n"+
		"v=0\r\n"+
		"o=- 30576 0 in ip4 127.0.0.1\r\n"+
		"s=tia-p25-sutosucall\r\n"+
		"c=in ip4 127.0.0.1\r\n"+
		"t=0 0\r\n"+
		"m=audio 12230 rtp/avp 100\r\n"+
		"a=rtpmap:100 x-tia-p25-imbe/8000\r\n";
		
		Response sipResponse = messageFactory.createResponse(response);
		System.out.println("Parsed SIP Response is :\n" + sipResponse);
		contentBytes = sipResponse.getRawContent();
		contentString = new String(contentBytes);
		SdpFactory sdpFactory = SdpFactory.getInstance();
		SessionDescription sd = sdpFactory.createSessionDescription(contentString);
		System.out.println("Parsed Content is :\n" + sd.toString());

		Connection connection =sdpFactory.createConnection("127.0.0.1");
		System.out.println("connection address type =" + connection.getAddressType() + "=>" + "IP4".equals(connection.getAddressType()));
		SessionName sessionName = sdpFactory.createSessionName(" ");
		System.out.println("session name value =" + sessionName.getValue() + "=>" + " ".equals(sessionName.getValue()));
		SessionDescription sessionDescription = sdpFactory.createSessionDescription();
		sessionDescription.setSessionName(sessionName);
		sessionDescription.setConnection(connection);
		System.out.println("connection address type =" + sessionDescription.getConnection().getAddressType() + "=>" + "IP4".equals(sessionDescription.getConnection().getAddressType()));
		System.out.println("session name value =" + sessionDescription.getSessionName().getValue() + "=>" + " ".equals(sessionDescription.getSessionName().getValue()));
		
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy