001: /** An example that illustrates template matching on SIP headers and messages.
002: * This uses regular expressions for matching on portions of sip messages.
003: * This is useful for situations when say you want to match with certain
004: * response classes or request URIs or whatever.
005: * You construct a match template that can consist of portions that are exact
006: * matches and portions that use regular expressions for matching.
007: * You might find this useful for bulding test frameworks.
008: */package examples.nistgoodies.match;
009:
010: import gov.nist.core.Match;
011: import gov.nist.javax.sip.address.SipUri;
012: import gov.nist.javax.sip.header.RequestLine;
013: import gov.nist.javax.sip.header.StatusLine;
014: import gov.nist.javax.sip.message.MessageFactoryImpl;
015: import gov.nist.javax.sip.message.SIPRequest;
016: import gov.nist.javax.sip.message.SIPResponse;
017:
018: import java.util.regex.Pattern;
019:
020: import javax.sip.SipFactory;
021: import javax.sip.message.MessageFactory;
022: import javax.sip.message.Request;
023:
024: class Matcher implements Match {
025: Pattern re;
026:
027: Matcher(String matchExpr) {
028: re = Pattern.compile(matchExpr);
029: }
030:
031: public boolean match(String toMatch) {
032: java.util.regex.Matcher m = re.matcher("aaaaab");
033: boolean b = m.matches();
034: return b;
035: }
036:
037: }
038:
039: public class RegexpMatchTest {
040: static final String message1 = "INVITE sip:joe@blah.com SIP/3.0\r\n"
041: + "To: sip:joe@company.com\r\n"
042: + "From: sip:caller@university.edu ;tag=1234\r\n"
043: + "Call-ID: 0ha0isnda977644900765@10.0.0.1\r\n"
044: + "CSeq: 9 INVITE\r\n"
045: + "Via: SIP/2.0/UDP 135.180.130.133\r\n"
046: + "Content-Type: application/sdp\r\n"
047: + "\r\n"
048: + "v=0\r\n"
049: + "o=mhandley 29739 7272939 IN IP4 126.5.4.3\r\n"
050: + "c=IN IP4 135.180.130.88\r\n"
051: + "m=video 3227 RTP/AVP 31\r\n"
052: + "m=audio 4921 RTP/AVP 12\r\n" + "a=rtpmap:31 LPC\r\n";
053:
054: static final String message2 = "SIP/2.0 200 OK\r\n"
055: + "Via: SIP/2.0/UDP 129.6.55.18:5060\r\n"
056: + "From: \"3ComIII\" <sip:13019768226@129.6.55.78>;tag=e13b4296\r\n"
057: + "To: \"3ComIII\" <sip:13019768226@129.6.55.78>\r\n"
058: + "Call-Id: c5ab5808@129.6.55.18\r\n"
059: + "CSeq: 49455 REGISTER\r\n"
060: + "Expires: 1200\r\n"
061: + "Contact: <sip:13019768226@129.6.55.18>;expires=1199;action=proxy\r\n"
062: + "Content-Length: 0\r\n" + "\r\n";
063:
064: public static void main(String args[]) throws Exception {
065: SIPRequest template = new SIPRequest();
066: RequestLine requestLine = new RequestLine();
067: SipFactory sipFactory = null;
068: sipFactory = SipFactory.getInstance();
069: sipFactory.setPathName("gov.nist");
070: // AddressFactory addressFactory = sipFactory.createAddressFactory();
071: SipUri uri = new SipUri();
072: // trap invites on company.com domain for incoming SIP
073: // invitations.
074: uri.setMatcher(new Matcher("sip:[^.]*company.com"));
075: requestLine.setMethod(Request.INVITE);
076: requestLine.setUri(uri);
077: template.setRequestLine(requestLine);
078: MessageFactory messageFactory = sipFactory
079: .createMessageFactory();
080: try {
081:
082: SIPRequest sipMessage = (SIPRequest) messageFactory
083: .createRequest(message1);
084: System.out.println("Match returned "
085: + sipMessage.match(template));
086:
087: } catch (Exception ex) {
088: ex.printStackTrace();
089: System.exit(0);
090: }
091:
092: StatusLine statusLine = new StatusLine();
093: // matches on all 2XX,1xx and 4xx responses.
094: statusLine.setMatcher(new Matcher("SIP/2.0 [1,2,4][0-9][0-9]"));
095: SIPResponse responseTemplate = new SIPResponse();
096: responseTemplate.setStatusLine(statusLine);
097: try {
098:
099: SIPResponse sipResponse = (SIPResponse) ((MessageFactoryImpl) messageFactory)
100: .createResponse(message2);
101: System.out.println("Match returned "
102: + sipResponse.match(responseTemplate));
103:
104: } catch (Exception ex) {
105: ex.printStackTrace();
106: System.exit(0);
107: }
108:
109: }
110: }
|