001: package test.unit.gov.nist.javax.sip.stack;
002:
003: import java.util.ArrayList;
004: import java.util.Properties;
005: import java.util.Timer;
006: import java.util.TimerTask;
007:
008: import javax.sip.ClientTransaction;
009: import javax.sip.DialogTerminatedEvent;
010: import javax.sip.IOExceptionEvent;
011: import javax.sip.ListeningPoint;
012: import javax.sip.RequestEvent;
013: import javax.sip.ResponseEvent;
014: import javax.sip.ServerTransaction;
015: import javax.sip.SipFactory;
016: import javax.sip.SipListener;
017: import javax.sip.SipProvider;
018: import javax.sip.SipStack;
019: import javax.sip.TimeoutEvent;
020: import javax.sip.TransactionTerminatedEvent;
021: import javax.sip.address.Address;
022: import javax.sip.address.AddressFactory;
023: import javax.sip.address.SipURI;
024: import javax.sip.header.CSeqHeader;
025: import javax.sip.header.CallIdHeader;
026: import javax.sip.header.ContactHeader;
027: import javax.sip.header.EventHeader;
028: import javax.sip.header.FromHeader;
029: import javax.sip.header.HeaderFactory;
030: import javax.sip.header.MaxForwardsHeader;
031: import javax.sip.header.RouteHeader;
032: import javax.sip.header.SubscriptionStateHeader;
033: import javax.sip.header.ToHeader;
034: import javax.sip.header.ViaHeader;
035: import javax.sip.message.MessageFactory;
036: import javax.sip.message.Request;
037: import javax.sip.message.Response;
038:
039: import junit.framework.TestCase;
040:
041: public class DeliverUnsolicitedNotifyTest extends TestCase implements
042: SipListener {
043:
044: private boolean notifySeen = false;
045:
046: private boolean notifyResponseSeen = false;
047:
048: private MessageFactory messageFactory;
049:
050: private SipProvider sipProvider;
051:
052: private AddressFactory addressFactory;
053:
054: private HeaderFactory headerFactory;
055:
056: private ListeningPoint listeningPoint;
057:
058: private int port;
059:
060: private String transport;
061:
062: private SipStack sipStack;
063:
064: private static Timer timer = new Timer();
065:
066: public void processDialogTerminated(
067: DialogTerminatedEvent dialogTerminatedEvent) {
068: // TODO Auto-generated method stub
069:
070: }
071:
072: public void processIOException(IOExceptionEvent exceptionEvent) {
073: // TODO Auto-generated method stub
074:
075: }
076:
077: public void processRequest(RequestEvent requestEvent) {
078: try {
079: if (requestEvent.getRequest().getMethod().equals(
080: Request.NOTIFY)) {
081: this .notifySeen = true;
082: }
083: Response response = this .messageFactory.createResponse(
084: Response.OK, requestEvent.getRequest());
085: ServerTransaction st = requestEvent.getServerTransaction();
086: if (st == null) {
087: st = sipProvider.getNewServerTransaction(requestEvent
088: .getRequest());
089: }
090: st.sendResponse(response);
091:
092: } catch (Exception ex) {
093: ex.printStackTrace();
094: fail("Unexpected exception ");
095: }
096: }
097:
098: public void processResponse(ResponseEvent responseEvent) {
099: this .notifyResponseSeen = true;
100:
101: }
102:
103: public void processTimeout(TimeoutEvent timeoutEvent) {
104: // TODO Auto-generated method stub
105:
106: }
107:
108: public void processTransactionTerminated(
109: TransactionTerminatedEvent transactionTerminatedEvent) {
110: // TODO Auto-generated method stub
111:
112: }
113:
114: public void setUp() {
115: SipFactory sipFactory = SipFactory.getInstance();
116: sipFactory.resetFactory();
117: sipFactory.setPathName("gov.nist");
118: Properties properties = new Properties();
119: properties.setProperty("javax.sip.STACK_NAME", "mystack");
120:
121: // The following properties are specific to nist-sip
122: // and are not necessarily part of any other jain-sip
123: // implementation.
124:
125: properties.setProperty("gov.nist.javax.sip.THREAD_POOL_SIZE",
126: "1");
127:
128: properties
129: .setProperty(
130: "gov.nist.javax.sip.DELIVER_UNSOLICITED_NOTIFY",
131: "true");
132:
133: try {
134: this .port = 6050;
135: this .transport = "udp";
136: this .sipStack = sipFactory.createSipStack(properties);
137: this .listeningPoint = sipStack.createListeningPoint(
138: "127.0.0.1", port, transport);
139: sipProvider = sipStack.createSipProvider(listeningPoint);
140: this .addressFactory = sipFactory.createAddressFactory();
141: this .headerFactory = sipFactory.createHeaderFactory();
142: // Create the request.
143: this .messageFactory = sipFactory.createMessageFactory();
144: sipProvider.addSipListener(this );
145:
146: timer.schedule(new TimerTask() {
147:
148: public void run() {
149: if (!notifySeen || !notifyResponseSeen) {
150: fail("Did not see expected event");
151: }
152: sipStack.stop();
153:
154: }
155:
156: }, 4000);
157:
158: } catch (Exception e) {
159: e.printStackTrace();
160: fail("Could not create sip stack");
161: }
162:
163: }
164:
165: public void testSendRequest() {
166: try {
167: String fromName = "BigGuy";
168: String fromSipAddress = "here.com";
169: String fromDisplayName = "The Master Blaster";
170:
171: String toSipAddress = "there.com";
172: String toUser = "LittleGuy";
173: String toDisplayName = "The Little Blister";
174:
175: // create >From Header
176: SipURI fromAddress = addressFactory.createSipURI(fromName,
177: fromSipAddress);
178:
179: Address fromNameAddress = addressFactory
180: .createAddress(fromAddress);
181: fromNameAddress.setDisplayName(fromDisplayName);
182: FromHeader fromHeader = headerFactory.createFromHeader(
183: fromNameAddress, "12345");
184:
185: // create To Header
186: SipURI toAddress = addressFactory.createSipURI(toUser,
187: toSipAddress);
188: Address toNameAddress = addressFactory
189: .createAddress(toAddress);
190: toNameAddress.setDisplayName(toDisplayName);
191: ToHeader toHeader = headerFactory.createToHeader(
192: toNameAddress, null);
193:
194: // create Request URI
195: SipURI requestURI = addressFactory.createSipURI(toUser,
196: toSipAddress);
197:
198: // Create ViaHeaders
199:
200: ArrayList viaHeaders = new ArrayList();
201:
202: ViaHeader viaHeader = headerFactory.createViaHeader(
203: "127.0.0.1", port, transport, null);
204:
205: // add via headers
206: viaHeaders.add(viaHeader);
207:
208: // Create a new CallId header
209: CallIdHeader callIdHeader = sipProvider.getNewCallId();
210: // JvB: Make sure that the implementation matches the messagefactory
211: callIdHeader = headerFactory
212: .createCallIdHeader(callIdHeader.getCallId());
213:
214: // Create a new Cseq header
215: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L,
216: Request.NOTIFY);
217:
218: // Create a new MaxForwardsHeader
219: MaxForwardsHeader maxForwards = headerFactory
220: .createMaxForwardsHeader(70);
221:
222: Request request = messageFactory.createRequest(requestURI,
223: Request.NOTIFY, callIdHeader, cSeqHeader,
224: fromHeader, toHeader, viaHeaders, maxForwards);
225: // Create contact headers
226: String host = listeningPoint.getIPAddress();
227:
228: SipURI contactUrl = addressFactory.createSipURI(fromName,
229: host);
230: contactUrl.setPort(listeningPoint.getPort());
231:
232: // Create the contact name address.
233: SipURI contactURI = addressFactory.createSipURI(fromName,
234: host);
235: contactURI.setTransportParam(transport);
236: contactURI.setPort(sipProvider.getListeningPoint(transport)
237: .getPort());
238:
239: Address contactAddress = addressFactory
240: .createAddress(contactURI);
241:
242: // Add the contact address.
243: contactAddress.setDisplayName(fromName);
244:
245: ContactHeader contactHeader = headerFactory
246: .createContactHeader(contactAddress);
247: request.addHeader(contactHeader);
248:
249: // JvB: To test forked SUBSCRIBEs, send it via the Forker
250: // Note: BIG Gotcha: Need to do this before creating the
251: // ClientTransaction!
252:
253: RouteHeader route = headerFactory
254: .createRouteHeader(addressFactory
255: .createAddress("<sip:127.0.0.1:" + port
256: + ";transport=" + transport
257: + ";lr>"));
258: request.addHeader(route);
259: // JvB end added
260:
261: // Create an event header for the subscription.
262: EventHeader eventHeader = headerFactory
263: .createEventHeader("foo");
264: eventHeader.setEventId("foo");
265: request.addHeader(eventHeader);
266:
267: SubscriptionStateHeader ssh = headerFactory
268: .createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE);
269: // Create the client transaction.
270: request.addHeader(ssh);
271: ClientTransaction ct = sipProvider
272: .getNewClientTransaction(request);
273:
274: ct.sendRequest();
275: Thread.sleep(10000);
276: } catch (Exception ex) {
277: ex.printStackTrace();
278: fail("Unexpected exception sending request");
279: }
280: }
281:
282: }
|