Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
An example that illustrates template matching on SIP headers and messages. This uses regular expressions for matching on portions of sip messages. This is useful for situations when say you want to match with certain response classes or request URIs or whatever. You construct a match template that can consist of portions that are exact matches and portions that use regular expressions for matching. You might find this useful for bulding test frameworks.
 package examples.nistgoodies.match;
 class Matcher implements Match {
    Pattern re;
    Matcher (String matchExpr)  {
         = Pattern.compile(matchExpr);
    public boolean match(String toMatch) {
     java.util.regex.Matcher m = .matcher("aaaaab");
     boolean b = m.matches();
     return b;
 public class RegexpMatchTest {
 static final String message1 = "INVITE SIP/3.0\r\n"+
 "From: ;tag=1234\r\n"+
 "Call-ID: 0ha0isnda977644900765@\r\n"+
 "CSeq: 9 INVITE\r\n"+
 "Via: SIP/2.0/UDP\r\n"+
 "Content-Type: application/sdp\r\n"+
 "o=mhandley 29739 7272939 IN IP4\r\n" +
 "c=IN IP4\r\n" +
 "m=video 3227 RTP/AVP 31\r\n" +
 "m=audio 4921 RTP/AVP 12\r\n" +
 "a=rtpmap:31 LPC\r\n";
 static final String message2 = "SIP/2.0 200 OK\r\n"+
 "Via: SIP/2.0/UDP\r\n"+
 "From: \"3ComIII\" <sip:13019768226@>;tag=e13b4296\r\n"+
 "To: \"3ComIII\" <sip:13019768226@>\r\n"+
 "Call-Id: c5ab5808@\r\n"+
 "CSeq: 49455 REGISTER\r\n"+
 "Expires: 1200\r\n"+
 "Contact: <sip:13019768226@>;expires=1199;action=proxy\r\n"+
 "Content-Length: 0\r\n"+
     public static void main(String args[]) throws Exception {
         SIPRequest template = new SIPRequest();
         RequestLine requestLine  = new RequestLine();
         SipFactory sipFactory = null;
         sipFactory = SipFactory.getInstance();
         // AddressFactory addressFactory = sipFactory.createAddressFactory();
         SipUri uri = new SipUri();
         // trap invites on domain for incoming SIP
         // invitations.
          uri.setMatcher(new Matcher("sip:[^.]*"));
          MessageFactory messageFactory = sipFactory.createMessageFactory();
         try {
             SIPRequest sipMessage = (SIPRequestmessageFactory.createRequest();
             ..println("Match returned " +
         } catch (Exception ex) {
         StatusLine statusLine = new StatusLine();
         // matches on all 2XX,1xx and 4xx responses.
         statusLine.setMatcher(new Matcher("SIP/2.0 [1,2,4][0-9][0-9]"));
        SIPResponse responseTemplate = new SIPResponse();
        try {
            SIPResponse sipResponse  = (SIPResponse) ((MessageFactoryImplmessageFactory).createResponse();
            ..println("Match returned " +
        } catch (Exception ex) {
New to GrepCode? Check out our FAQ X