001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.sip;
028:
029: import javax.microedition.io.Connector;
030: import com.sun.midp.i3test.TestCase;
031: import gov.nist.microedition.sip.SipServerConnectionImpl;
032:
033: import java.io.OutputStream;
034:
035: /**
036: * Tests for SipRefreshHelper class.
037: * sendRegister() example was taken from JSR180 spec.
038: */
039: public class TestSipRefreshHelper extends TestCase implements
040: SipClientConnectionListener, SipServerConnectionListener,
041: SipRefreshListener {
042:
043: /** Refresh identifier */
044: int refreshID = 0;
045:
046: /** Refresh status */
047: int refreshStatus = 0;
048:
049: /** Refresh helper */
050: SipRefreshHelper refHelper = null;
051:
052: /**
053: * Sends 'REGISTER' request.
054: */
055: public void sendRegister() {
056: SipClientConnection scc = null;
057: SipConnectionNotifier scn = null;
058:
059: try {
060: // Open SIP server connection and listen to port 5060
061: scn = (SipConnectionNotifier) Connector.open("sip:5060");
062: scn.setListener(this );
063:
064: // Initialize connection to the registrar host.com
065: scc = (SipClientConnection) Connector.open("sip:localhost");
066: scc.setListener(this );
067:
068: // Initialize request and set From, To and Contact headers
069: scc.initRequest("REGISTER", null);
070: scc.setHeader("From", "sip:sippy.user@localhost");
071: scc.setHeader("To", "sip:sippy.user@localhost");
072: scc.setHeader("Contact",
073: "<sip:UserB@192.168.200.201>;expires=3600");
074: scc.addHeader("Contact",
075: "<mailto:UserB@biloxi.com>;expires=4294967295");
076:
077: refreshID = scc.enableRefresh(this );
078: assertTrue("Invalid refreshID!", refreshID != 0);
079:
080: // System.out.println(">>> id = " + refreshID);
081:
082: scc.send();
083:
084: refHelper = SipRefreshHelper.getInstance();
085: assertNotNull("refHelper is null!", refHelper);
086:
087: // -----------------------------
088: // do something else for a while
089: // ------------------------------
090: synchronized (this ) {
091: try {
092: wait(1200);
093: } catch (Exception e) {
094: System.out.println(">>> Exception!!! " + e);
095: }
096: }
097:
098: // System.out.println(">>> (1) status = " + refreshStatus);
099:
100: // check that refresh was successful
101: assertEquals("Invalid refresh status (1)!", 200,
102: refreshStatus);
103:
104: // update REGISTER, with new "mailto:" Contact and no content
105: String c[] = { "<mailto:UserB@company.com>" };
106: refHelper.update(refreshID, c, null, 0, 6000);
107:
108: // -----------------------------
109: // do something else for a while
110: // ------------------------------
111: synchronized (this ) {
112: try {
113: wait(1200);
114: } catch (Exception e) {
115: System.out.println(">>> Exception!!! " + e);
116: }
117: }
118:
119: // System.out.println(">>> (2) status = " + refreshStatus);
120:
121: // check that refresh is still ok
122: assertEquals("Invalid refresh status (2)!", 200,
123: refreshStatus);
124:
125: // stop REGISTER refresh altogether
126: refHelper.stop(refreshID);
127: } catch (Exception ex) { // handle Exceptions
128: ex.printStackTrace();
129: fail("Exception '" + ex + "' was thrown.");
130: }
131: }
132:
133: /**
134: * Accept a new connection request and process the request from client.
135: * Send "OK" to client
136: *
137: * This method is declared in SipServerConnectionListener
138: * The code was copied from TestSipOriginatingInvite
139: *
140: * @param scnLocal Local SIP connection notifier
141: */
142: public void notifyRequest(SipConnectionNotifier scnLocal) {
143: SipServerConnection ssc = null;
144:
145: // System.out.println(">>> Request received!");
146:
147: try {
148: // block and wait for incoming request.
149: // SipServerConnection is establised and returned
150: // when new request is received.
151: ssc = scnLocal.acceptAndOpen();
152: assertNotNull("ssc is null", ssc);
153:
154: assertEquals("State should be REQUEST_RECEIVED",
155: SipServerConnectionImpl.REQUEST_RECEIVED,
156: ((SipServerConnectionImpl) ssc).getState());
157:
158: if (ssc.getMethod().equals("REGISTER")) {
159: /*
160: * RFC 3261, p. 66:
161: *
162: * The registrar returns a 200 (OK) response. The response MUST
163: * contain Contact header field values enumerating all current
164: * bindings. Each Contact value MUST feature an "expires"
165: * parameter indicating its expiration interval chosen by the
166: * registrar. The response SHOULD include a Date header field.
167: */
168:
169: int i, index;
170: String[] contacts = ssc.getHeaders("Contact");
171: assertNotNull("contact is null", contacts);
172: assertTrue("Invalid Contact header length: "
173: + contacts.length, contacts.length > 0);
174:
175: String[] newContacts = new String[contacts.length];
176: assertNotNull("newContact is null", newContacts);
177:
178: for (i = 0; i < contacts.length; i++) {
179: index = contacts[i].indexOf(';');
180:
181: // System.out.println(">>> " + contacts[i]);
182:
183: if (index == -1) {
184: newContacts[i] = contacts[i];
185: } else {
186: newContacts[i] = contacts[i]
187: .substring(0, index);
188: }
189:
190: newContacts[i] += ";expires=1";
191: }
192:
193: // write message body to respond back to client
194: ssc.initResponse(200);
195:
196: assertEquals("State should be INITIALIZED",
197: SipServerConnectionImpl.INITIALIZED,
198: ((SipServerConnectionImpl) ssc).getState());
199:
200: // remove the default Contact header
201: ssc.removeHeader("Contact");
202:
203: // DEBUG
204: /*
205: contacts = ssc.getHeaders("Contact");
206: for (i = 0; i < contacts.length; i++) {
207: System.out.println(">>> " + contacts[i]);
208: }
209: */
210: //
211: ssc.send();
212: }
213: } catch (SipException sipex) {
214: sipex.printStackTrace();
215: fail("Unexpected SipException: " + sipex);
216: } catch (Exception e) {
217: e.printStackTrace();
218: fail("Unexpected Exception: " + e);
219: }
220: }
221:
222: /**
223: * Notifier
224: * @param scc client connection
225: */
226: public void notifyResponse(SipClientConnection scc) {
227: try {
228: // retrieve the response received
229: scc.receive(0);
230:
231: // System.out.println(">>> Response received: " +
232: // scc.getStatusCode());
233:
234: if (scc.getStatusCode() == 200) {
235: // handle 200 OK response
236: } else {
237: // handle possible error responses
238: fail("Response code: " + scc.getStatusCode());
239: }
240: } catch (Exception ex) {
241: // handle Exceptions
242: fail("Exception '" + ex + "' was thrown.");
243: }
244: }
245:
246: /**
247: * Refresh callback.
248: * @param ID refresh ID
249: * @param statusCode refresh status
250: * @param reasonPhrase
251: */
252: public void refreshEvent(int ID, int statusCode, String reasonPhrase) {
253: // System.out.println(">>> Refresh event, code " + statusCode);
254:
255: refreshStatus = statusCode;
256:
257: if (statusCode == 0) {
258: // stopped refresh
259: // fail("stopped refresh!");
260: } else if (statusCode == 200) {
261: // successful refresh
262: } else {
263: // failed request
264: fail("failed request, status: " + statusCode);
265: }
266: }
267:
268: /**
269: * Run the tests.
270: */
271: public void runTests() {
272: declare("SipRefreshHelper test: sendRegister()");
273: sendRegister();
274: }
275: }
|