public boolean register(SipURI requestUri, String user, String password, String contact,
      int expiry, long timeout) {
    initErrorInfo();

    try {
      AddressFactory addr_factory = parent.getAddressFactory();
      HeaderFactory hdr_factory = parent.getHeaderFactory();

      if (requestUri == null) {
        requestUri = addr_factory.createSipURI(null, ((SipURI) (myAddress.getURI())).getHost());
        requestUri.setPort(((SipURI) (myAddress.getURI())).getPort());
        if (((SipURI) (myAddress.getURI())).getTransportParam() != null) {
          requestUri.setTransportParam(((SipURI) (myAddress.getURI())).getTransportParam());
        }
      }

      String method = Request.REGISTER;

      ToHeader to_header = hdr_factory.createToHeader(myAddress, null);
      FromHeader from_header = hdr_factory.createFromHeader(myAddress, generateNewTag());

      CallIdHeader callid_header = hdr_factory.createCallIdHeader(myRegistrationId);

      cseq = hdr_factory.createCSeqHeader(cseq == null ? 1 : (cseq.getSeqNumber() + 1), method);

      MaxForwardsHeader max_forwards = hdr_factory.createMaxForwardsHeader(MAX_FORWARDS_DEFAULT);

      if (contact != null) {
        URI uri = addr_factory.createURI(contact);
        if (uri.isSipURI() == false) {
          setReturnCode(INVALID_ARGUMENT);
          setErrorMessage("URI " + contact + " is not a Sip URI");
          return false;
        }

        Address contact_address = addr_factory.createAddress(uri);
        ContactHeader hdr = hdr_factory.createContactHeader(contact_address);
        hdr.setExpires(expiry);

        synchronized (contactLock) {
          contactInfo = new SipContact();
          contactInfo.setContactHeader(hdr);
        }
      }

      List<ViaHeader> via_headers = getViaHeaders();

      Request msg = parent.getMessageFactory().createRequest(requestUri, method, callid_header,
          cseq, from_header, to_header, via_headers, max_forwards);

      msg.addHeader(contactInfo.getContactHeader()); // use
      // setHeader()?

      if (expiry > 0) {
        ExpiresHeader expires = hdr_factory.createExpiresHeader(expiry);
        msg.setExpires(expires);
      }

      // include any auth information for this User Agent's registration
      // if any exists

      Map<String, AuthorizationHeader> auth_list =
          getAuthorizations().get(myRegistrationId);
      if (auth_list != null) {
        List<AuthorizationHeader> auth_headers =
            new ArrayList<>(auth_list.values());
        Iterator<AuthorizationHeader> i = auth_headers.iterator();
        while (i.hasNext()) {
          AuthorizationHeader auth = i.next();
          msg.addHeader(auth);
        }
      } else {
        // create the auth list entry for this phone's registrations
        enableAuthorization(myRegistrationId);
      }

      // send the REGISTRATION request and get the response
      Response response = sendRegistrationMessage(msg, user, password, timeout);
      if (response == null) {
        return false;
      }

      // update our contact info with that of the server response -
      // server may have reset our contact expiry

      ListIterator<?> contacts = response.getHeaders(ContactHeader.NAME);
      if (contacts != null) {
        while (contacts.hasNext()) {
          // TODO - at some point save ALL the contact headers and
          // provide a getter for the list of SipContact objects
          // (gobalContactList).
          // dispose() and unregister() can use the list of contact
          // headers.
          // for now just save this agent's info

          ContactHeader hdr = (ContactHeader) contacts.next();
          if (hdr.getAddress().getURI().toString().equals(contactInfo.getURI()) == true) {
            contactInfo.setContactHeader(hdr);
            break;
          }
        }
      }

      return true;
    } catch (Exception ex) {
      setReturnCode(EXCEPTION_ENCOUNTERED);
      setException(ex);
      setErrorMessage("Exception: " + ex.getClass().getName() + ": " + ex.getMessage());
      return false;
    }
  }
public boolean register ( SipURI requestUri , String user , String password , String contact , int expiry , long timeout ) { initErrorInfo ( ) ; try { AddressFactory addr_factory = parent . getAddressFactory ( ) ; HeaderFactory hdr_factory = parent . getHeaderFactory ( ) ; if ( requestUri == null ) { requestUri = addr_factory . createSipURI ( null , ( ( SipURI ) ( myAddress . getURI ( ) ) ) . getHost ( ) ) ; requestUri . setPort ( ( ( SipURI ) ( myAddress . getURI ( ) ) ) . getPort ( ) ) ; if ( ( ( SipURI ) ( myAddress . getURI ( ) ) ) . getTransportParam ( ) != null ) { requestUri . setTransportParam ( ( ( SipURI ) ( myAddress . getURI ( ) ) ) . getTransportParam ( ) ) ; } } String method = Request . REGISTER ; ToHeader to_header = hdr_factory . createToHeader ( myAddress , null ) ; FromHeader from_header = hdr_factory . createFromHeader ( myAddress , generateNewTag ( ) ) ; CallIdHeader callid_header = hdr_factory . createCallIdHeader ( myRegistrationId ) ; cseq = hdr_factory . createCSeqHeader ( cseq == null ? 1 : ( cseq . getSeqNumber ( ) + 1 ) , method ) ; MaxForwardsHeader max_forwards = hdr_factory . createMaxForwardsHeader ( MAX_FORWARDS_DEFAULT ) ; if ( contact != null ) { URI uri = addr_factory . createURI ( contact ) ; if ( uri . isSipURI ( ) == false ) { setReturnCode ( INVALID_ARGUMENT ) ; setErrorMessage ( "URI " + contact + " is not a Sip URI" ) ; return false ; } Address contact_address = addr_factory . createAddress ( uri ) ; ContactHeader hdr = hdr_factory . createContactHeader ( contact_address ) ; hdr . setExpires ( expiry ) ; synchronized ( contactLock ) { contactInfo = new SipContact ( ) ; contactInfo . setContactHeader ( hdr ) ; } } List < ViaHeader > via_headers = getViaHeaders ( ) ; Request msg = parent . getMessageFactory ( ) . createRequest ( requestUri , method , callid_header , cseq , from_header , to_header , via_headers , max_forwards ) ; msg . addHeader ( contactInfo . getContactHeader ( ) ) ; if ( expiry > 0 ) { ExpiresHeader expires = hdr_factory . createExpiresHeader ( expiry ) ; msg . setExpires ( expires ) ; } Map < String , AuthorizationHeader > auth_list = getAuthorizations ( ) . get ( myRegistrationId ) ; if ( auth_list != null ) { List < AuthorizationHeader > auth_headers = new ArrayList < > ( auth_list . values ( ) ) ; Iterator < AuthorizationHeader > i = auth_headers . iterator ( ) ; while ( i . hasNext ( ) ) { AuthorizationHeader auth = i . next ( ) ; msg . addHeader ( auth ) ; } } else { enableAuthorization ( myRegistrationId ) ; } Response response = sendRegistrationMessage ( msg , user , password , timeout ) ; if ( response == null ) { return false ; } ListIterator < ? > contacts = response . getHeaders ( ContactHeader . NAME ) ; if ( contacts != null ) { while ( contacts . hasNext ( ) ) { ContactHeader hdr = ( ContactHeader ) contacts . next ( ) ; if ( hdr . getAddress ( ) . getURI ( ) . toString ( ) . equals ( contactInfo . getURI ( ) ) == true ) { contactInfo . setContactHeader ( hdr ) ; break ; } } } return true ; } catch ( Exception ex ) { setReturnCode ( EXCEPTION_ENCOUNTERED ) ; setException ( ex ) ; setErrorMessage ( "Exception: " + ex . getClass ( ) . getName ( ) + ": " + ex . getMessage ( ) ) ; return false ; } }
This method is the same as the register(String user, String password, String contact, int
expiry, long timeout) method except for the Request-URI used in the outgoing REGISTER method.
If the requestUri parameter passed into this method is null, the Request-URI for the REGISTER
is derived from this SipPhone's URI, or address of record (for example, if this SipPhone's
address of record is "sip:amit@cafesip.org", the REGISTER Request-URI will be sip:cafesip.org).
Otherwise, the requestUri passed in is used for the REGISTER Request-URI.