Appendix 2

Appendix 2 contains an example program for managing a SUBSCRIBE dialog. This program shows how to establish and terminate a SUBSCRIBE dialog. The default profile is used as the registration context in the SUBSCRIBE request. It means that the ‘From’ header does not have to be set for the SUBSCRIBE request and SipConnectionNotifier object does not have to be set as second argument to the SipClientConnection.initRequest() operation.

Code of the client side:

public class SubscribeClientSide
  {
  private Form iForm = null;
  SubscribeClientSide(Form aForm)
    {
    iForm = aForm;
    }
  public void createSubscription()
    {
    try{
      //Default profile is used in this example so own registration
      //is not needed.
      SipConnectionNotifier scn = (SipConnectionNotifier) 
        Connector.open( "sip:*;type=\"application/test\"" );
      SipClientConnection subscribeObj = null;
      subscribeObj = (SipClientConnection)
        Connector.open("sip:UserB@myServer:5070");
      subscribeObj.initRequest("SUBSCRIBE",null);
      subscribeObj.setHeader("Accept-Contact",
                             "*;type=\"application/test\"");
      subscribeObj.setHeader("Event","presence");
      subscribeObj.send();
      boolean flag = subscribeObj.receive(5000);
      if(false == flag)
        throw new RuntimeException("Response not arrived");
      if(subscribeObj.getStatusCode()!= 200)
        throw new RuntimeException("status code " 
                                   + subscribeObj.getStatusCode());
      SipDialog sd = subscribeObj.getDialog();
      SipServerConnection notifyObj = scn.acceptAndOpen();
      String method = notifyObj.getMethod();
      if(!method.equals("NOTIFY"))
        throw new RuntimeException("Unexpected request arrived: " 
                                   + method);
      notifyObj.initResponse(200);
      notifyObj.send();
      notifyObj.close();
      SipClientConnection unSubscribeObj = null;
      //Below is created un-subscribe request.
      unSubscribeObj = sd.getNewClientConnection("SUBSCRIBE");
      subscribeObj.close();
      unSubscribeObj.setHeader( "Accept-Contact",
                                "*;type=\"application/test\"" );
     unSubscribeObj.setHeader( "Expires", "0");
     unSubscribeObj.send();
     flag = unSubscribeObj.receive(5000);
     if(false == flag)
       throw new RuntimeException("Response not arrived");
     if(unSubscribeObj.getStatusCode()!= 200)
       throw new RuntimeException("status code " 
                                  + unSubscribeObj.getStatusCode());
     notifyObj = scn.acceptAndOpen();
     method = notifyObj.getMethod();
     if(!method.equals("NOTIFY"))
       throw new RuntimeException("Unexpected request arrived: " 
                                  + method);
      notifyObj.initResponse(200);
      notifyObj.send();
      notifyObj.close();
      unSubscribeObj.close();
      scn.close();
      iForm.append("Subscribe managed ok!!!");
    }
    catch(Exception e)
      {
      iForm.append("Exception occurred: " + e);
      }
    }
  }

Code of the server side:

public class SubscribeServerSide
   {
  private Form iForm = null;
SubscribeServerSide(Form aForm)
    {
    iForm = aForm;
    }
  public void manageSubscribe()
    {
    try{
      SipConnectionNotifier scn = (SipConnectionNotifier) 
         Connector.open( "sip:*;type=\"application/test\"" );
       //Waiting SUBSCRIBE request.
       SipServerConnection subscribeSrvObj = scn.acceptAndOpen();
       String method = subscribeSrvObj.getMethod();
       if(!method.equals("SUBSCRIBE"))
          throw new RuntimeException("Unexpected request arrived #1: " + method);
       subscribeSrvObj.initResponse(200);
       subscribeSrvObj.setHeader( "Expires", "1200" );
       subscribeSrvObj.send();
       SipDialog sd = subscribeSrvObj.getDialog();
       subscribeSrvObj.close();
      //NOTIFY is sent immediately after 200 OK to SUBSCRIBE has been sent. 
      SipClientConnection notifyObj = null;
      notifyObj = sd.getNewClientConnection( "NOTIFY" );
      notifyObj.setHeader( "Accept-Contact", "*;type=\"application/test\"" );
      notifyObj.setHeader( "Subscription-State", "active");
      notifyObj.setHeader( "Expires", "1200" );
      notifyObj.send();
      boolean flag = notifyObj.receive(5000);
      if(false == flag)
         throw new RuntimeException("Response not arrived");
      if(notifyObj.getStatusCode()!= 200)
         throw new RuntimeException("status code " + notifyObj.getStatusCode());
      notifyObj.close();
      //Waiting un-SUBSCRIBE request.
      SipServerConnection unSubscribeSrvObj = scn.acceptAndOpen();
      if(false == sd.isSameDialog(unSubscribeSrvObj))
         throw new RuntimeException("Arrived request doesn't belong this dialog");
      method = unSubscribeSrvObj.getMethod();
      if(!method.equals("SUBSCRIBE"))
         throw new RuntimeException("Unexpected request arrived #2: " + method);
      unSubscribeSrvObj.initResponse(200);
      unSubscribeSrvObj.send();
      sd = unSubscribeSrvObj.getDialog();
      unSubscribeSrvObj.close();
      notifyObj = sd.getNewClientConnection( "NOTIFY" );
      notifyObj.setHeader( "Accept-Contact", "*;type=\"application/test\"" );
      notifyObj.setHeader( "Subscription-State", "terminated");
      notifyObj.setHeader( "Expires", "0" );
      notifyObj.send();
      flag = notifyObj.receive(5000);
      if(false == flag)
         throw new RuntimeException("Response not arrived");
      if(notifyObj.getStatusCode()!= 200)
         throw new RuntimeException("status code " + notifyObj.getStatusCode());
      notifyObj.close();
      scn.close();
      sd = null;
      iForm.append("Subscribe managed ok!!!");
    }
    catch(Exception e)
      {
      iForm.append("Exception occurred: " + e);
      }
  }
}