io.pkts.examples.siplib.SipLibIntroExample Maven / Gradle / Ivy
package io.pkts.examples.siplib;
import io.pkts.packet.sip.SipMessage;
import io.pkts.packet.sip.SipRequest;
import io.pkts.packet.sip.header.ContactHeader;
import io.pkts.packet.sip.header.RecordRouteHeader;
import io.pkts.packet.sip.header.SipHeader;
import io.pkts.packet.sip.header.ViaHeader;
/**
* @author [email protected]
*/
public class SipLibIntroExample {
public static void introExample001() throws Exception {
// (1)
final String rawMessage = new StringBuilder("BYE sip:[email protected]:5060 SIP/2.0\r\n")
.append("Via: SIP/2.0/UDP 192.168.0.100:5060;rport;branch=z9hG4bK-28976-1-7\r\n")
.append("From: alice ;tag=28976SIPpTag001\r\n")
.append("To: bob ;tag=28972SIPpTag011\r\n")
.append("Call-ID: [email protected]\r\n")
.append("CSeq: 2 BYE\r\n")
.append("Contact: sip:[email protected]\r\n")
.append("Max-Forwards: 70\r\n")
.append("Drop-Me: Drop this header on proxy\r\n")
.append("Subject: Example BYE Message\r\n")
.append("Content-Length: 0\r\n")
.append("\r\n").toString();
// (2)
final SipMessage bye = SipMessage.frame(rawMessage).toRequest();
// (3)
final SipMessage proxyBye = bye.copy()
.onRequestURI(uri -> uri.copy().withHost("sipstack.io").build())
.withHeader(SipHeader.create("X-SIP-Lib-Version", "2.x"))
.onHeader(header -> {
if (header.isSubjectHeader()) {
// change
return header.copy().withValue("Example of Proxied BYE Message").build();
} else if (header.is("Drop-Me")) {
// drop
return null;
}
// keep
return header;
})
.withTopMostRecordRouteHeader(RecordRouteHeader.withHost("62.63.64.65").withTransportTCP().build())
.withTopMostViaHeader(ViaHeader.withHost("62.63.64.65").withBranch().withTransportTCP().build())
.onViaHeader((index, via) -> via.withReceived("54.55.56.57"))
.build();
System.out.println(bye);
System.out.println(proxyBye);
}
public static void main(final String ... args) throws Exception {
SipLibIntroExample.introExample001();
}
}